From a4d903dc17069b64563768bd680197cef6b23acd Mon Sep 17 00:00:00 2001 From: Matthew Zito Date: Wed, 10 Apr 2024 01:32:55 -0700 Subject: [PATCH] feat: add new is comparison API --- .clang-format | 12 +++++------- clib.json | 2 +- include/libtap.h | 12 ++++++++++++ t/main.c | 45 +++++++++++++++++++++++++++++++++------------ 4 files changed, 51 insertions(+), 20 deletions(-) diff --git a/.clang-format b/.clang-format index 5931e4a..d8cfdf8 100644 --- a/.clang-format +++ b/.clang-format @@ -72,8 +72,6 @@ BreakAfterAttributes: Never BreakArrays: false BreakBeforeBinaryOperators: All BreakBeforeBraces: Custom -BraceWrapping: - AfterFunction: true BreakBeforeConceptDeclarations: Always BreakBeforeInlineASMColon: OnlyMultiline BreakBeforeTernaryOperators: true @@ -81,7 +79,7 @@ BreakConstructorInitializers: BeforeColon BreakInheritanceList: BeforeColon BreakStringLiterals: true ColumnLimit: 80 -CommentPragmas: "^ IWYU pragma:" +CommentPragmas: '^ IWYU pragma:' CompactNamespaces: false ConstructorInitializerIndentWidth: 4 ContinuationIndentWidth: 2 @@ -117,7 +115,7 @@ IncludeCategories: SortPriority: 0 CaseSensitive: false IncludeIsMainRegex: ([-_](test|unittest))?$ -IncludeIsMainSourceRegex: "" +IncludeIsMainSourceRegex: '' IndentAccessModifiers: false IndentCaseBlocks: false IndentCaseLabels: true @@ -141,8 +139,8 @@ KeepEmptyLinesAtTheStartOfBlocks: false LambdaBodyIndentation: Signature Language: Cpp LineEnding: DeriveLF -MacroBlockBegin: "" -MacroBlockEnd: "" +MacroBlockBegin: '' +MacroBlockEnd: '' MaxEmptyLinesToKeep: 1 NamespaceIndentation: None PPIndentWidth: -1 @@ -169,7 +167,7 @@ RawStringFormats: - CPP - c++ - C++ - CanonicalDelimiter: "" + CanonicalDelimiter: '' BasedOnStyle: google - Language: TextProto Delimiters: diff --git a/clib.json b/clib.json index 66b7e68..70583dc 100644 --- a/clib.json +++ b/clib.json @@ -1,6 +1,6 @@ { "name": "libtap", - "version": "0.0.2", + "version": "0.0.3", "author": "Matthew Zito", "repo": "exbotanical/libtap", "license": "MIT", diff --git a/include/libtap.h b/include/libtap.h index f56b1dd..370f9c7 100644 --- a/include/libtap.h +++ b/include/libtap.h @@ -40,6 +40,18 @@ unsigned int bail_out(const char* fmt, ...); #define ok(test, ...) \ __ok(test ? 1 : 0, __func__, __FILE__, __LINE__, s_fmt(__VA_ARGS__)) +#define is(actual, expected, ...) \ + __ok( \ + !(actual == expected ? 0 \ + : !actual ? -1 \ + : !expected ? 1 \ + : strcmp(actual, expected)), \ + __func__, \ + __FILE__, \ + __LINE__, \ + s_fmt(__VA_ARGS__) \ + ); + #define skip_start(cond, num_skips, ...) \ do { \ if (cond) { \ diff --git a/t/main.c b/t/main.c index 96505a7..5938354 100644 --- a/t/main.c +++ b/t/main.c @@ -16,21 +16,18 @@ typedef struct { } test_util; static void -will_fail () -{ +will_fail () { test_util *util; util->a + util->b; } static void -will_succeed () -{ +will_succeed () { 10 + 10; } int -main () -{ +main () { pa_setup(); unsigned int is_skip = 1; @@ -41,7 +38,7 @@ main () assert(ok(1 == 11, "result of %d == %d", 1, 11) == 0); assert( pa_match_stdout("not ok 1 - result of 1 == 11\n" - "# \tFailed test (t/main.c:main at line 41)\n") + "# \tFailed test (t/main.c:main at line 38)\n") == 1 ); @@ -71,7 +68,7 @@ main () assert(ok(1 == 19, "the second todo test") == 0); assert( pa_match_stdout("not ok 8 - the second todo test # TODO is todo man\n" - "# \tFailed (TODO)test (t/main.c:main at line 71)\n") + "# \tFailed (TODO)test (t/main.c:main at line 68)\n") == 1 ); @@ -97,19 +94,43 @@ main () assert( pa_match_stdout("not ok 10 - dies\n" - "# \tFailed test (t/main.c:main at line 90)\n") + "# \tFailed test (t/main.c:main at line 87)\n") == 1 ); - assert(exit_status() == 3); + char *x = s_fmt("x"); + char *y = s_fmt("y"); + char *a = s_fmt("a"); + char *a2 = s_fmt("a"); + char *b = s_fmt("b"); + char *n = NULL; + is(a, b, "not equal"); + assert( + pa_match_stdout("not ok 11 - not equal\n" + "# \tFailed test (t/main.c:main at line 108)\n") + == 1 + ); + + is(a, a2, "equal"); + assert(pa_match_stdout("ok 12 - equal\n") == 1); + + is(n, NULL, "both null"); + assert(pa_match_stdout("ok 13 - both null\n") == 1); + + assert(exit_status() == 6); cleanup(); assert( - pa_match_stdout("# Planned 7 tests but ran 10\n" - "# Failed 3 tests of 10\n") + pa_match_stdout("# Planned 7 tests but ran 13\n" + "# Failed 4 tests of 13\n") == 1 ); pa_teardown(); + free(x); + free(y); + free(a); + free(a2); + free(b); }