Andrew Jeffery | d30d757 | 2023-04-18 14:51:51 +0930 | [diff] [blame] | 1 | |
| 2 | #include <assert.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <stdint.h> |
| 5 | #include <stdio.h> |
| 6 | |
| 7 | #ifndef SYSCONFDIR |
| 8 | // Bypass compilation error due to -DSYSCONFDIR not provided |
| 9 | #define SYSCONFDIR |
| 10 | #endif |
| 11 | |
| 12 | #include "config.c" |
| 13 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame^] | 14 | static void execute_test(const char *input, const char *key, |
| 15 | const char *expected) |
Andrew Jeffery | d30d757 | 2023-04-18 14:51:51 +0930 | [diff] [blame] | 16 | { |
| 17 | struct config *ctx; |
| 18 | const char *found; |
| 19 | char *buf; |
| 20 | |
| 21 | ctx = calloc(1, sizeof(*ctx)); |
| 22 | buf = strdup(input); |
| 23 | config_parse(ctx, buf); |
| 24 | free(buf); |
| 25 | found = config_get_value(ctx, key); |
| 26 | if (!expected) |
| 27 | assert(!found); |
| 28 | if (expected) { |
| 29 | assert(found); |
| 30 | assert(!strcmp(expected, found)); |
| 31 | } |
| 32 | config_fini(ctx); |
| 33 | } |
| 34 | |
| 35 | static void test_config_parse_basic(void) |
| 36 | { |
| 37 | execute_test("tty = ttyS0", "tty", "ttyS0"); |
| 38 | } |
| 39 | |
| 40 | static void test_config_parse_no_key(void) |
| 41 | { |
| 42 | execute_test("= ttyS0", "tty", NULL); |
| 43 | } |
| 44 | |
| 45 | static void test_config_parse_no_value(void) |
| 46 | { |
| 47 | execute_test("tty =", "tty", NULL); |
| 48 | } |
| 49 | |
| 50 | static void test_config_parse_no_operator(void) |
| 51 | { |
| 52 | execute_test("tty ttyS0", "tty", NULL); |
| 53 | } |
| 54 | |
| 55 | static void test_config_parse_no_spaces(void) |
| 56 | { |
| 57 | execute_test("tty=ttyS0", "tty", "ttyS0"); |
| 58 | } |
| 59 | |
| 60 | static void test_config_parse_empty(void) |
| 61 | { |
| 62 | execute_test("", "tty", NULL); |
| 63 | } |
| 64 | |
| 65 | int main(void) |
| 66 | { |
| 67 | test_config_parse_basic(); |
| 68 | test_config_parse_no_key(); |
| 69 | test_config_parse_no_value(); |
| 70 | test_config_parse_no_operator(); |
| 71 | test_config_parse_no_spaces(); |
| 72 | test_config_parse_empty(); |
| 73 | |
| 74 | return EXIT_SUCCESS; |
| 75 | } |