From 616ff505f666d4d162e5d2d8c0238f8af6f79c40 Mon Sep 17 00:00:00 2001 From: Danny van Kooten Date: Thu, 12 Mar 2020 14:19:24 +0100 Subject: add first template tests --- tests/test.h | 3 ++- tests/test_template.c | 28 +++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/test.h b/tests/test.h index 05c35b9..d0c0e0a 100644 --- a/tests/test.h +++ b/tests/test.h @@ -6,11 +6,12 @@ #define START_TESTS int main() { #define END_TESTS } #define TEST(name) strcpy(current_test, #name); +#define assert_str(actual, expected) _assert(strcmp(actual, expected) == 0, __FILE__, __LINE__, "invalid string: expected %s, got %s", expected, actual) +#define assert(assertion, format, ...) _assert(assertion, __FILE__, __LINE__, format, ##__VA_ARGS__) /* used to store the running test name */ char current_test[256] = {'\0'}; -#define assert(assertion, format, ...) _assert(assertion, __FILE__, __LINE__, format, ##__VA_ARGS__) static void _assert(int assertion, const char filename[64], const int line, char *format, ...) { if (assertion) diff --git a/tests/test_template.c b/tests/test_template.c index 62ac77c..88d1ab4 100644 --- a/tests/test_template.c +++ b/tests/test_template.c @@ -4,7 +4,33 @@ START_TESTS TEST(text_only) { - + char *input = "Hello world."; + char *output = template(input, NULL); + assert_str(output, "Hello world."); + free(output); } +TEST(text_with_var) { + char *input = "Hello {{name}}."; + struct hashmap *ctx = hashmap_new(); + hashmap_insert(ctx, "name", "world"); + char *output = template(input, ctx); + assert_str(output, "Hello world."); + hashmap_free(ctx); + free(output); +} + +TEST(text_multiline) { + char *input = "Hello {{name}}.\nL2"; + struct hashmap *ctx = hashmap_new(); + hashmap_insert(ctx, "name", "world"); + char *output = template(input, ctx); + assert_str(output, "Hello world.\nL2"); + hashmap_free(ctx); + free(output); +} + + + + END_TESTS \ No newline at end of file -- cgit v1.2.3