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); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 26 | if (!expected) { |
Andrew Jeffery | d30d757 | 2023-04-18 14:51:51 +0930 | [diff] [blame] | 27 | assert(!found); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 28 | } |
Andrew Jeffery | d30d757 | 2023-04-18 14:51:51 +0930 | [diff] [blame] | 29 | if (expected) { |
| 30 | assert(found); |
| 31 | assert(!strcmp(expected, found)); |
| 32 | } |
| 33 | config_fini(ctx); |
| 34 | } |
| 35 | |
| 36 | static void test_config_parse_basic(void) |
| 37 | { |
| 38 | execute_test("tty = ttyS0", "tty", "ttyS0"); |
| 39 | } |
| 40 | |
| 41 | static void test_config_parse_no_key(void) |
| 42 | { |
| 43 | execute_test("= ttyS0", "tty", NULL); |
| 44 | } |
| 45 | |
| 46 | static void test_config_parse_no_value(void) |
| 47 | { |
| 48 | execute_test("tty =", "tty", NULL); |
| 49 | } |
| 50 | |
| 51 | static void test_config_parse_no_operator(void) |
| 52 | { |
| 53 | execute_test("tty ttyS0", "tty", NULL); |
| 54 | } |
| 55 | |
| 56 | static void test_config_parse_no_spaces(void) |
| 57 | { |
| 58 | execute_test("tty=ttyS0", "tty", "ttyS0"); |
| 59 | } |
| 60 | |
| 61 | static void test_config_parse_empty(void) |
| 62 | { |
| 63 | execute_test("", "tty", NULL); |
| 64 | } |
| 65 | |
| 66 | int main(void) |
| 67 | { |
| 68 | test_config_parse_basic(); |
| 69 | test_config_parse_no_key(); |
| 70 | test_config_parse_no_value(); |
| 71 | test_config_parse_no_operator(); |
| 72 | test_config_parse_no_spaces(); |
| 73 | test_config_parse_empty(); |
| 74 | |
| 75 | return EXIT_SUCCESS; |
| 76 | } |