Skip to content

Commit

Permalink
v1.5.0 release.
Browse files Browse the repository at this point in the history
Merge branch 'develop' -- See `CHANGELOG.md` for details.
  • Loading branch information
silentbicycle committed Feb 15, 2021
2 parents 7f89ba4 + fbbf981 commit 11a6af1
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ example_no_runner
example_random
example_shuffle
example_trunc
example_cpp
*.o
45 changes: 45 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,50 @@
# greatest Changes By Release

## v1.5.0 - 2021-02-15

### API Changes

Changed default: `GREATEST_USE_LONGJMP` now defaults to 0. This eliminates
a warning about variables that can possibly become stale/corrupt in the
presence of `longjmp`. Since `GREATEST_FAIL_WITH_LONGJMP` isn't frequently
used, it should probably be opt-in.

Added `greatest_set_exact_name_match()` / `-e` flag, which changes the
name-based filtering from substring to exact match. Note that filtering
on an exact suite name will not skip tests run outside of any suite.

Added `GREATEST_ASSERT_NEQ` and `GREATEST_ASSERT_NEQm`. (Thanks @tekknolagi.)

Added `GREATEST_ASSERT_GT`, `GREATEST_ASSERT_GTE`, `GREATEST_ASSERT_LT`,
and `GREATEST_ASSERT_LTE`, along with their custom message (`m`)
variants.


### Bug Fixes

Makefile: Fix targets so all files are rebuilt when `greatest.h` or the
`Makefile` are modified, but without potentially breaking the build due
to including `greatest.h` as a linker argument to `example_trunc` (which
could happen with clang). (Thanks @vemakereporter, @theosotr.)

Calls to `GREATEST_RUN_TEST` from inside another test are now ignored.

Other flags starting with `--` besides `--help` (print help) and `--`
(ignore rest of ARGV) now produce an "Unknown argument" message;
previously they were unintentionally handled like `--`.


### Other Improvements

Added built `example_cpp` executable to `.gitignore`.

Expanded on the role of `RUN_TEST`, `RUN_TEST1`, `RUN_TESTp`,
`PASS`, `SKIP`, and `FAIL` in the README.

Addressed a `-Wimplicit-fallthrough` warning when building with clang
using `-Weverything`.


## v1.4.2 - 2019-03-24

### API Changes
Expand Down
16 changes: 9 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ PROGRAMS_CPP= example_cpp
# Uncomment to demo c99 parametric testing.
#CFLAGS += -std=c99

# Uncomment to disable setjmp()/longjmp().
#CFLAGS += -DGREATEST_USE_LONGJMP=0
# Uncomment to enable setjmp()/longjmp().
#CFLAGS += -DGREATEST_USE_LONGJMP=1

# Uncomment to disable clock() / time.h.
#CFLAGS += -DGREATEST_USE_TIME=0
Expand All @@ -32,19 +32,21 @@ example: example.o example_suite.o
example_no_suite: example_no_suite.o
example_no_runner: example_no_runner.o
example_shuffle: example_shuffle.o
example_trunc: example_trunc.o

example_cpp: example_cpp.cpp
*.o: greatest.h Makefile

example_cpp: example_cpp.cpp greatest.h Makefile
${CXX} -o $@ example_cpp.cpp ${CPPFLAGS} ${LDFLAGS}

%.o: %.c
${CC} -c -o $@ ${CFLAGS} $<

%.o: %.cpp
${CXX} -c -o $@ ${CPPFLAGS} $<

%: %.o
${CC} -o $@ ${LDFLAGS} $^

*.o: Makefile
*.o: greatest.h

clean:
rm -f ${PROGRAMS_C} ${PROGRAMS_CPP} *.o *.core

63 changes: 58 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
A testing system for C, contained in 1 header file.


## Key Features
## Key Features and Project Goals

- **Small, Portable, Lightweight**

Expand Down Expand Up @@ -114,22 +114,42 @@ Total: 1 test (47 ticks, 0.000 sec), 3 assertions
Pass: 1, fail: 0, skip: 0.
```

Tests are run with `RUN_TEST(test_name)`, which can be called directly
from the test runner's `main` function or grouped into suites (which are
run with `RUN_SUITE(suite_name)`). (Calls to `RUN_TEST` from inside
another test are ignored.)

Test cases can be run with arguments: `RUN_TEST1(test_name, arg)` passes
a single argument, and if C99 features are supported, then
`RUN_TESTp(test_name, ...)` uses `__VA_ARGS__` to run a test case with
one or mare arguments. `greatest_set_test_suffix` sets a name suffix, so
output from the test runner can include info about arguments.

Test cases should call assertions and then end with `PASS()`, `SKIP()`,
`FAIL()`, or their custom message variants (e.g. `SKIPm("TODO");`).
If there are any test failures, the test runner will return 1,
otherwise it will return 0. (Skips do not cause the test runner to
report failure.)

`PASS()`, `SKIP()`, `FAIL()`, and their custom message variants are
macros that updating internal bookkeeping and then returning and enum
value, such as `GREATEST_TEST_RES_FAIL`. They all `return` from the
current test case function.

`PASS()`/`PASSm("msg")` prints as a dot when verbosity is zero, or
the test name and custom message (if any) with verbosity >= 1.

`FAIL()`/`FAILm("msg")` always prints "FAIL test_name: msg file:line".

`SKIP()`/`SKIPm("msg")` prints as an 's' when verbosity is zero, or
the test name and custom message (if any) with verbosity >= 1.
`SKIP()`/`SKIPm("msg")` prints as an 's' when verbosity is zero, or the
test name and custom message (if any) with verbosity >= 1. Because skips
are not treated as a failure by the test runner, they can be used to
skip test cases that aren't relevant in a particular build or
environment, a way to temporarily disable broken tests, or as a sort of
todo list for tests and functionality under active development.

Tests and suites are just functions, so normal C scoping rules apply.
For example, a test or suite named "main" will have a name collision.
For example, a test or suite named `main` will have a name collision.

(For more examples, look at `example.c` and `example_suite.c`.)

Expand Down Expand Up @@ -159,6 +179,11 @@ also contain "slow":

The string matching includes optional test name suffixes.

The `greatest_set_exact_name_match()` function and corresponding `-e`
command line runner flag can be used to only run tests and/or suites
whose names exactly match the name filter(s). Note: exact-match suite
filtering by name will not skip tests that are run outside of any suite.


## Available Assertions

Expand Down Expand Up @@ -187,6 +212,31 @@ differ, use `ASSERT_EQ_FMT`. To compare with custom equality test
and print functions, use `ASSERT_EQUAL_T` instead.


### `ASSERT_NEQ(EXPECTED, ACTUAL)`

Assert that `EXPECTED != ACTUAL`.


### `ASSERT_GT(EXPECTED, ACTUAL)`

Assert that `EXPECTED > ACTUAL`.


### `ASSERT_GTE(EXPECTED, ACTUAL)`

Assert that `EXPECTED >= ACTUAL`.


### `ASSERT_LT(EXPECTED, ACTUAL)`

Assert that `EXPECTED < ACTUAL`.


### `ASSERT_LTE(EXPECTED, ACTUAL)`

Assert that `EXPECTED <= ACTUAL`.


### `ASSERT_EQ_FMT(EXPECTED, ACTUAL, FORMAT)`

Assert that `EXPECTED == ACTUAL`. If they are not equal, print their
Expand Down Expand Up @@ -350,14 +400,15 @@ The function should have a return type of `enum greatest_test_res`.

Test runners build with the following command line options:

Usage: (test_runner) [-hlfav] [-s SUITE] [-t TEST] [-x EXCLUDE]
Usage: (test_runner) [-hlfave] [-s SUITE] [-t TEST] [-x EXCLUDE]
-h, --help print this Help
-l List suites and tests, then exit (dry run)
-f Stop runner after first failure
-a Abort on first failure (implies -f)
-v Verbose output
-s SUITE only run suite w/ name containing substring SUITE
-t TEST only run test w/ name containing substring TEST
-e only run exact name match for -s or -t
-x EXCLUDE exclude tests containing string substring EXCLUDE

Any arguments after `--` will be ignored.
Expand All @@ -383,9 +434,11 @@ The command line flags above have corresponding functions:
- `greatest_stop_at_first_fail()`
- `greatest_abort_on_fail()`
- `greatest_list_only()`
- `greatest_set_exact_name_match()`
- `greatest_set_suite_filter(const char *filter)`
- `greatest_set_test_filter(const char *filter)`
- `greatest_set_test_exclude(const char *filter)`
- `greatest_get_verbosity()`
- `greatest_set_verbosity(unsigned int verbosity)`


Expand Down
70 changes: 70 additions & 0 deletions example.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ TEST expect_equal(void) {
PASS();
}

TEST expect_not_equal(void) {
int i = 9;
ASSERT_NEQ(10, i);
PASS();
}

TEST expect_str_equal(void) {
const char *foo1 = "foo1";
ASSERT_STR_EQ("foo2", foo1);
Expand Down Expand Up @@ -255,6 +261,60 @@ TEST extra_slow_test(void) {
PASS();
}

TEST nested_RUN_TEST(void) {
printf("This nested RUN_TEST call should not trigger an infinite loop...\n");
RUN_TEST(nested_RUN_TEST);
PASS();
}

TEST eq_pass_and_fail(void) {
const int x = 1, y = 2;
ASSERT_EQ(x, x);
ASSERT_EQm("y == y", y, y);
ASSERT_EQ(x, y);
PASS();
}

TEST neq_pass_and_fail(void) {
const int x = 1, y = 2;
ASSERT_NEQm("x != y", x, y);
ASSERT_NEQ(x, x);
PASS();
}

TEST gt_pass_and_fail(void) {
const int x = 1, y = 2;
ASSERT_GTm("y > x", y, x);
ASSERT_GT(x, x);
PASS();
}

TEST gte_pass_and_fail(void) {
const int x = 1, y = 2, z = 3;;
ASSERT_GTE(z, y);
ASSERT_GTE(y, x);
ASSERT_GTE(z, x);
ASSERT_GTEm("y >= y", y, y);
ASSERT_GTE(y, z);
PASS();
}

TEST lt_pass_and_fail(void) {
const int x = 1, y = 2;
ASSERT_LTm("x < y", x, y);
ASSERT_LT(x, x);
PASS();
}

TEST lte_pass_and_fail(void) {
const int x = 1, y = 2, z = 3;;
ASSERT_LTE(y, z);
ASSERT_LTEm("x <= y", x, y);
ASSERT_LTE(x, x);
ASSERT_LTE(z, x);
PASS();
}

static void trace_setup(void *arg) {
printf("-- in setup callback\n");
teardown_was_called = 0;
Expand All @@ -279,6 +339,7 @@ SUITE(suite) {
printf("\nThis should fail:\n");
RUN_TEST(expect_str_equal);
printf("\nThis should pass:\n");
RUN_TEST(expect_not_equal);
RUN_TEST(expect_strn_equal);
printf("\nThis should fail:\n");
RUN_TEST(expect_boxed_int_equal);
Expand Down Expand Up @@ -359,6 +420,15 @@ SUITE(suite) {
RUN_TEST(expect_enum_equal_only_evaluates_args_once);

RUN_TEST(extra_slow_test);
RUN_TEST(nested_RUN_TEST);

printf("\nThese next several tests should also fail:\n");
RUN_TEST(eq_pass_and_fail);
RUN_TEST(neq_pass_and_fail);
RUN_TEST(gt_pass_and_fail);
RUN_TEST(gte_pass_and_fail);
RUN_TEST(lt_pass_and_fail);
RUN_TEST(lte_pass_and_fail);
}

TEST standalone_test(void) {
Expand Down
3 changes: 0 additions & 3 deletions example_trunc.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/* Make the buffer size VERY small, to check truncation */
#define GREATEST_TESTNAME_BUF_SIZE 8

/* Disable longjmp, since it isn't used here. */
#define GREATEST_USE_LONGJMP 0

#include "greatest.h"

TEST t(void) {
Expand Down
Loading

0 comments on commit 11a6af1

Please sign in to comment.