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 --- src/hashmap.c | 10 ++++++---- src/hashmap.h | 3 ++- src/hyde.c | 5 ++++- src/template.c | 27 ++++++++++++++------------- src/template.h | 2 +- tests/test.h | 3 ++- tests/test_template.c | 28 +++++++++++++++++++++++++++- 7 files changed, 56 insertions(+), 22 deletions(-) diff --git a/src/hashmap.c b/src/hashmap.c index 3d9edd0..b56b364 100644 --- a/src/hashmap.c +++ b/src/hashmap.c @@ -3,6 +3,8 @@ #include #include "hashmap.h" +#define HASH(v) (v[0] - 'a') % HASHMAP_CAP + struct node { char *key; void *value; @@ -12,7 +14,7 @@ struct node { struct hashmap *hashmap_new() { struct hashmap *hm = malloc(sizeof *hm); if (!hm) err(EXIT_FAILURE, "out of memory"); - for (int i=0; i < 26; i++) { + for (int i=0; i < HASHMAP_CAP; i++) { hm->buckets[i] = NULL; } @@ -20,7 +22,7 @@ struct hashmap *hashmap_new() { } void hashmap_insert(struct hashmap *hm, char *key, void *value) { - int pos = (key[0] - 'a') % 26; + int pos = HASH(key); struct node *head = hm->buckets[pos]; struct node *node = head; @@ -40,7 +42,7 @@ void hashmap_insert(struct hashmap *hm, char *key, void *value) { } void *hashmap_get(struct hashmap *hm, char *key) { - int pos = (key[0] - 'a') % 26; + int pos = HASH(key); struct node *node = hm->buckets[pos]; while (node) { if (strcmp(node->key, key) == 0) { @@ -61,7 +63,7 @@ void hashmap_free(struct hashmap *hm) { struct node *node; struct node *next; - for (int i=0; i < 26; i++) { + for (int i=0; i < HASHMAP_CAP; i++) { node = hm->buckets[i]; while (node) { next = node->next; diff --git a/src/hashmap.h b/src/hashmap.h index a37d3d6..93297af 100644 --- a/src/hashmap.h +++ b/src/hashmap.h @@ -1,6 +1,7 @@ +#define HASHMAP_CAP 26 struct hashmap { - struct node *buckets[26]; + struct node *buckets[HASHMAP_CAP]; }; void hashmap_insert(struct hashmap *hm, char *key, void *value); diff --git a/src/hyde.c b/src/hyde.c index 172f15d..bab2513 100644 --- a/src/hyde.c +++ b/src/hyde.c @@ -1,4 +1,5 @@ #include "template.h" +#include #include int main() { @@ -21,7 +22,9 @@ int main() { }; hashmap_insert(ctx, "posts", &posts); - template(input, ctx); + char *output = template(input, ctx); + printf("Output: %s\n", output); hashmap_free(ctx); free(input); + free(output); } \ No newline at end of file diff --git a/src/template.c b/src/template.c index 5d400b9..1a3c049 100644 --- a/src/template.c +++ b/src/template.c @@ -89,24 +89,25 @@ mpc_parser_t *parser_init() { return Template; } -void template(char *tmpl, struct hashmap *ctx) { +char * template(char *tmpl, struct hashmap *ctx) { mpc_parser_t *parser = parser_init(); mpc_result_t r; - if (mpc_parse("input", tmpl, parser, &r)) { - mpc_ast_print(r.output); - - // FIXME: Allocate precisely - char *output = malloc(strlen(tmpl) * 2); - output[0] = '\0'; - eval(output, r.output, ctx); - printf("Template: \n%s", output); - - mpc_ast_delete(r.output); - free(output); - } else { + if (!mpc_parse("input", tmpl, parser, &r)) { mpc_err_print(r.error); mpc_err_delete(r.error); + return NULL; } + + mpc_ast_print(r.output); + + // FIXME: Allocate precisely + char *output = malloc(strlen(tmpl) * 2); + output[0] = '\0'; + + eval(output, r.output, ctx); + mpc_ast_delete(r.output); + + return output; } diff --git a/src/template.h b/src/template.h index 896ef71..9174ad8 100644 --- a/src/template.h +++ b/src/template.h @@ -1,7 +1,7 @@ #include "hashmap.h" char *read_file(char *filename); -void template(char *tmpl, struct hashmap *ctx); +char *template(char *tmpl, struct hashmap *ctx); struct post { char title[64]; 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