Skip to content

Commit

Permalink
Add traversal tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericbonnet committed Mar 6, 2020
1 parent a986ffa commit 6a3d0f9
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 7 deletions.
9 changes: 5 additions & 4 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ target_sources(test_picotest
hooks.c
testAssertions.c
testAssertionHooks.c
testFilters.c
testLoggers.c
testCases.c
testCaseHooks.c
testSuites.c
testSuiteHooks.c
testFixtures.c
testFixtureHooks.c
testSuites.c
testSuiteHooks.c
testFilters.c
testTraversal.c
testLoggers.c
)
target_link_libraries(test_picotest
PRIVATE
Expand Down
6 changes: 3 additions & 3 deletions tests/mainSuite.inc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <picotest.h>

PICOTEST_SUITE(mainSuite, testAssertions, testAssertionHooks, testFilters,
testLoggers, testCases, testCaseHooks, testSuites,
testSuiteHooks, testFixtures, testFixtureHooks);
PICOTEST_SUITE(mainSuite, testAssertions, testAssertionHooks, testCases,
testCaseHooks, testFixtures, testFixtureHooks, testSuites,
testSuiteHooks, testFilters, testTraversal, testLoggers);
99 changes: 99 additions & 0 deletions tests/testTraversal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include <stdio.h>

#include <picotest.h>

/*
* Test utilities
*/

#define MAX_TESTS 6
static struct {
int nbTests;
int nbSuites;
int nbCases;
int testCalled;
struct {
const char *name;
int nb;
} tests[MAX_TESTS];
} state;
void init() {
memset(&state, 0, sizeof(state));
}
PicoTestTraverseProc traverse;
static void traverse(const char *name, int nb) {
int i = state.nbTests++;
if (nb) {
state.nbSuites++;
} else {
state.nbCases++;
}
if (i < MAX_TESTS) {
state.tests[i].name = name;
state.tests[i].nb = nb;
}
}

PICOTEST_SUITE(doTraverseSuite, doTraverseCase1, doTraverseSubsuite,
doTraverseCase2);
PICOTEST_SUITE(doTraverseSubsuite, doTraverseSubcase1, doTraverseSubcase2)

PICOTEST_CASE(doTraverseCase1) {
state.testCalled++;
}
PICOTEST_CASE(doTraverseCase2) {
state.testCalled++;
}
PICOTEST_CASE(doTraverseSubcase1) {
state.testCalled++;
}
PICOTEST_CASE(doTraverseSubcase2) {
state.testCalled++;
}

/*
* Test suite for traversal
*/
#include "hooks.h"

PICOTEST_SUITE(testTraversal, testTraverseShouldNotRunTests,
testTraverseShouldTraverseTestsDepthFirst,
testTraverseShouldGetTestInfo);

PICOTEST_CASE(testTraverseShouldNotRunTests) {
init();
PICOTEST_TRAVERSE(doTraverseSuite, traverse);
PICOTEST_ASSERT(state.testCalled == 0);
}
PICOTEST_CASE(testTraverseShouldTraverseTestsDepthFirst) {
init();
PICOTEST_TRAVERSE(doTraverseSuite, traverse);
PICOTEST_ASSERT(state.nbTests == 6);

PICOTEST_VERIFY(strcmp(state.tests[0].name, "doTraverseSuite") == 0);
PICOTEST_VERIFY(strcmp(state.tests[1].name, "doTraverseCase1") == 0);
PICOTEST_VERIFY(strcmp(state.tests[2].name, "doTraverseSubsuite") == 0);
PICOTEST_VERIFY(strcmp(state.tests[3].name, "doTraverseSubcase1") == 0);
PICOTEST_VERIFY(strcmp(state.tests[4].name, "doTraverseSubcase2") == 0);
PICOTEST_VERIFY(strcmp(state.tests[5].name, "doTraverseCase2") == 0);
}
PICOTEST_CASE(testTraverseShouldGetTestInfo) {
init();
PICOTEST_TRAVERSE(doTraverseSuite, traverse);
PICOTEST_ASSERT(state.nbTests == 6);
PICOTEST_VERIFY(state.nbSuites == 2);
PICOTEST_VERIFY(state.nbCases == 4);

PICOTEST_VERIFY(strcmp(state.tests[0].name, "doTraverseSuite") == 0);
PICOTEST_VERIFY(state.tests[0].nb == 3);
PICOTEST_VERIFY(strcmp(state.tests[1].name, "doTraverseCase1") == 0);
PICOTEST_VERIFY(state.tests[1].nb == 0);
PICOTEST_VERIFY(strcmp(state.tests[2].name, "doTraverseSubsuite") == 0);
PICOTEST_VERIFY(state.tests[2].nb == 2);
PICOTEST_VERIFY(strcmp(state.tests[3].name, "doTraverseSubcase1") == 0);
PICOTEST_VERIFY(state.tests[3].nb == 0);
PICOTEST_VERIFY(strcmp(state.tests[4].name, "doTraverseSubcase2") == 0);
PICOTEST_VERIFY(state.tests[4].nb == 0);
PICOTEST_VERIFY(strcmp(state.tests[5].name, "doTraverseCase2") == 0);
PICOTEST_VERIFY(state.tests[5].nb == 0);
}

0 comments on commit 6a3d0f9

Please sign in to comment.