Trying to use a library
Dependencies: mbed-rtos mbed BlinkLib
Fork of NerfUS by
test/GoogleMockTest.cpp@3:aaf84424abb1, 2017-02-04 (annotated)
- Committer:
- Maxime Dupuis
- Date:
- Sat Feb 04 22:48:36 2017 -0500
- Revision:
- 3:aaf84424abb1
Integrate GoogleTest and GoogleMock
How to build and run the tests:
- mkdir build
- cd build
- cmake ..
- make
- ./run_all_tests
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Maxime Dupuis |
3:aaf84424abb1 | 1 | #include "gmock/gmock.h" |
Maxime Dupuis |
3:aaf84424abb1 | 2 | #include "gtest/gtest.h" |
Maxime Dupuis |
3:aaf84424abb1 | 3 | |
Maxime Dupuis |
3:aaf84424abb1 | 4 | class Adder |
Maxime Dupuis |
3:aaf84424abb1 | 5 | { |
Maxime Dupuis |
3:aaf84424abb1 | 6 | public: |
Maxime Dupuis |
3:aaf84424abb1 | 7 | virtual ~Adder() {} |
Maxime Dupuis |
3:aaf84424abb1 | 8 | virtual int add(int a, int b) |
Maxime Dupuis |
3:aaf84424abb1 | 9 | { |
Maxime Dupuis |
3:aaf84424abb1 | 10 | return a + b; |
Maxime Dupuis |
3:aaf84424abb1 | 11 | } |
Maxime Dupuis |
3:aaf84424abb1 | 12 | }; |
Maxime Dupuis |
3:aaf84424abb1 | 13 | |
Maxime Dupuis |
3:aaf84424abb1 | 14 | class MockAdder : public Adder |
Maxime Dupuis |
3:aaf84424abb1 | 15 | { |
Maxime Dupuis |
3:aaf84424abb1 | 16 | public: |
Maxime Dupuis |
3:aaf84424abb1 | 17 | MOCK_METHOD2(add, int(int a, int b)); |
Maxime Dupuis |
3:aaf84424abb1 | 18 | }; |
Maxime Dupuis |
3:aaf84424abb1 | 19 | |
Maxime Dupuis |
3:aaf84424abb1 | 20 | TEST(GoogleMockTest, ExpectCalled) |
Maxime Dupuis |
3:aaf84424abb1 | 21 | { |
Maxime Dupuis |
3:aaf84424abb1 | 22 | MockAdder adder; |
Maxime Dupuis |
3:aaf84424abb1 | 23 | |
Maxime Dupuis |
3:aaf84424abb1 | 24 | EXPECT_CALL(adder, add(1, 2)); |
Maxime Dupuis |
3:aaf84424abb1 | 25 | |
Maxime Dupuis |
3:aaf84424abb1 | 26 | adder.add(1, 2); |
Maxime Dupuis |
3:aaf84424abb1 | 27 | } |
Maxime Dupuis |
3:aaf84424abb1 | 28 | |
Maxime Dupuis |
3:aaf84424abb1 | 29 | TEST(GoogleMockTest, DefineReturnedValue) |
Maxime Dupuis |
3:aaf84424abb1 | 30 | { |
Maxime Dupuis |
3:aaf84424abb1 | 31 | using ::testing::Return; |
Maxime Dupuis |
3:aaf84424abb1 | 32 | MockAdder adder; |
Maxime Dupuis |
3:aaf84424abb1 | 33 | |
Maxime Dupuis |
3:aaf84424abb1 | 34 | EXPECT_CALL(adder, add(1, 2)) |
Maxime Dupuis |
3:aaf84424abb1 | 35 | .WillOnce(Return(42)); |
Maxime Dupuis |
3:aaf84424abb1 | 36 | |
Maxime Dupuis |
3:aaf84424abb1 | 37 | ASSERT_EQ(42, adder.add(1, 2)); |
Maxime Dupuis |
3:aaf84424abb1 | 38 | } |