Last active
April 29, 2016 20:21
-
-
Save CrockAgile/14e5502de89ab8de9fee9782256538c1 to your computer and use it in GitHub Desktop.
Cgreen Introduction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
git clone https://github.com/cgreen-devs/cgreen.git | |
cd cgreen | |
make -j4 | |
make test | |
make install | |
# hacky fix since default install to lib64 for some reason | |
# open to suggestions of better solution | |
# append "/usr/local/lib64" to ld config | |
sudo vim /etc/ld.so.conf | |
sudo ldconfig |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cgreen/cgreen.h> | |
// What is the subject under test? SUT | |
// Only one per test file | |
Describe(ncom_message); | |
// Unfortunately required even if unused | |
BeforeEach(ncom_message) | |
{} | |
AfterEach(ncom_message) | |
{} | |
Ensure(ncom_message, is_correct_size) | |
{ | |
ncom_message test_message; | |
assert_that( | |
sizeof(test_message), | |
is_equal_to(NCOM_MSG_SIZE) ); | |
} | |
// repeat for other tests |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cgreen/cgreen.h> // general unit testing | |
#include <cgreen/mocks.h> // mocking functionality | |
#include "oxts_rt_driver.h" | |
// Declares "oxts_rt_configure_socket" which uses "psync_socket_bind". | |
// Mocks and stubs help to isolate our unit tests from | |
// other functions, objects, and the system. | |
// "psync_socket_bind" is not what we are testing, | |
// so instead we will create a replacement mock function. | |
int psync_socket_bind( | |
ps_socket * const sock ) | |
{ | |
// "mock" is a special function that returns values | |
// based on expectations provided in the tests below | |
return (int)mock(sock); | |
} | |
// global test object recycled for each test | |
ps_socket test_socket; | |
Describe(oxts_socket); | |
BeforeEach(oxts_socket) | |
{ | |
memset( &test_socket, 0, sizeof(test_socket) ); | |
} | |
AfterEach(oxts_socket) | |
{} | |
Ensure(oxts_socket, configuration_calls_bind) | |
{ | |
// expect that psync_socket_bind will be called once (no more) | |
// with the socket provided and will return DTC_NONE | |
expect( psync_socket_bind, | |
when(sock, is_equal_to( &test_socket )), | |
will_return(DTC_NONE) ); | |
assert_that( | |
oxts_rt_configure_socket( &test_socket, "10.10.10.10", 1000 ), | |
is_equal_to(DTC_NONE) ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# same as all makefiles | |
# tests can be built as classic objects and linked | |
# but Cgreen provides its own convenient "cgreen-runner" | |
# which will find and run all Cgreen tests in a .so | |
TEST_RUNNER := test/test-oxts-rt-driver.so | |
TEST_SRCS := $(wildcard test/*.c) | |
test: $(TEST_RUNNER) | |
cgreen-runner --colours $< | |
# test files are linked before libraries, which allows a mock | |
# version of any function in the library to be substituted | |
$(TEST_RUNNER): $(TEST_SRCS) $(TARGET) | |
$(CC) $(CCFLAGS) $(INCLUDE) -shared -o $@ $^ $(LIBS) -lcgreen |
This needs to be added to 4-Makefile:
# moc_target shared library file
MOC_TARGET := tests/libps_neobotix_usboard_moc_driver.so
# moc-target
$(MOC_TARGET): $(OBJS)
$(CC) -shared $(CCFLAGS) -o $@ $^
Then revise the TEST_RUNNER target to depend on MOC_TARGET instead of TARGET
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For those of use with lenovo laptops the path /usr/local/lib/x86_64-linux-gnu/ needs to be added to /etc/ld.so.conf rather than /usr/local/lib64
Also I needed to run updatedb after install