Skip to content

Instantly share code, notes, and snippets.

@mrk21
Last active December 20, 2015 16:59
Show Gist options
  • Save mrk21/6165243 to your computer and use it in GitHub Desktop.
Save mrk21/6165243 to your computer and use it in GitHub Desktop.
googlemock(http://opencv.jp/googlemockdocs/) を Bandit(http://banditcpp.org/) で使えるようにした。 ※ライブラリにした → bandit_with_gmock(https://github.com/mrk21/bandit_with_gmock)
#include <bandit/bandit.h>
#include <gmock/gmock.h>
namespace bandit_with_gmock {
class listener_adapter: public testing::EmptyTestEventListener, public bandit::listener {
protected:
bandit::listener * base;
bool is_failed;
public:
listener_adapter(bandit::listener * base) : base(base), is_failed(false) {}
virtual void test_run_starting(void) {
this->base->test_run_starting();
}
virtual void test_run_complete(void) {
this->base->test_run_complete();
}
virtual void context_starting(const char * desc) {
this->base->context_starting(desc);
}
virtual void context_ended(const char * desc) {
this->base->context_ended(desc);
}
virtual void test_run_error(const char * desc, const bandit::test_run_error & error) {
this->base->test_run_error(desc, error);
}
virtual void it_starting(const char * desc) {
this->is_failed = false;
this->base->it_starting(desc);
}
virtual void it_succeeded(const char * desc) {
this->base->it_succeeded(desc);
}
virtual void it_failed(const char * desc, const bandit::assertion_exception & ex) {
this->base->it_failed(desc, ex);
}
virtual void it_unknown_error(const char * desc) {
this->base->it_unknown_error(desc);
}
virtual void it_skip(const char * desc) {
return this->base->it_skip(desc);
}
virtual bool did_we_pass() const {
return this->base->did_we_pass();
}
virtual void OnTestPartResult(const testing::TestPartResult & result) {
if (!result.failed() || this->is_failed) return;
this->is_failed = true;
throw bandit::assertion_exception(result.summary(), result.file_name(), result.line_number());
}
};
int run(int argc, char * argv[]) {
bandit::options opt(argc, argv);
bandit::failure_formatter_ptr formatter(bandit::create_formatter(opt));
bandit::detail::colorizer colorizer(!opt.no_color());
bandit::listener_ptr reporter(bandit::create_reporter(opt, formatter.get(), colorizer));
listener_adapter * listener = new listener_adapter(reporter.get());
bandit::registered_listener(listener);
testing::InitGoogleMock(&argc, argv);
auto & gtest_listeners = testing::UnitTest::GetInstance()->listeners();
delete gtest_listeners.Release(gtest_listeners.default_result_printer());
gtest_listeners.Append(listener);
return bandit::run(opt, bandit::detail::specs(), bandit::context_stack(), *listener);
}
}
class hoge {
public:
void func(void){
this->func1();
this->func2();
}
virtual void func1(void) = 0;
virtual void func2(void) = 0;
};
class hoge_mock: public hoge {
public:
MOCK_METHOD0(func1, void(void));
MOCK_METHOD0(func2, void(void));
};
go_bandit([]{
using namespace bandit;
describe("hoge", []{
// F
// There were failures!
//
// hoge foo:
// googlemock_for_bandit.cpp:114: Mock function called more times than expected - returning directly.
// Function call: func1()
// Expected: to be never called
// Actual: called once - over-saturated and active
it("foo", [&]{
hoge_mock mock;
EXPECT_CALL(mock, func1()).Times(0);
EXPECT_CALL(mock, func2()).Times(1);
mock.func();
});
});
});
int main(int argc, char * argv[]) {
return bandit_with_gmock::run(argc, argv);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment