diff options
| author | Albert Cervin <albert@acervin.com> | 2023-02-21 22:26:36 +0100 |
|---|---|---|
| committer | Albert Cervin <albert@acervin.com> | 2023-02-21 22:26:36 +0100 |
| commit | 44fd8cde61e3e89e5f83c98900a403e922073727 (patch) | |
| tree | 22ed65a8b3c766fa21c35fe4d567399e3810454a /test | |
| parent | d7bf8702bf32720d93c4e690937bc8b683926be1 (diff) | |
| download | dged-44fd8cde61e3e89e5f83c98900a403e922073727.tar.gz dged-44fd8cde61e3e89e5f83c98900a403e922073727.tar.xz dged-44fd8cde61e3e89e5f83c98900a403e922073727.zip | |
Implement support for settings
Settings are a flat "dictionary" containing
paths to settings on the format:
<category>.<sub-category>.<setting>.
Diffstat (limited to 'test')
| -rw-r--r-- | test/command.c | 4 | ||||
| -rw-r--r-- | test/main.c | 3 | ||||
| -rw-r--r-- | test/settings.c | 73 | ||||
| -rw-r--r-- | test/test.h | 1 |
4 files changed, 79 insertions, 2 deletions
diff --git a/test/command.c b/test/command.c index 09de7f4..52a67ac 100644 --- a/test/command.c +++ b/test/command.c @@ -2,6 +2,7 @@ #include "test.h" #include "command.h" +#include "hash.h" void test_command_registry_create() { struct commands cmds = command_registry_create(10); @@ -66,8 +67,7 @@ void test_lookup_command() { ASSERT_STR_EQ(cmd->name, "fake", "Expected the found function to have the correct name"); - struct command *also_cmd = - lookup_command_by_hash(&cmds, hash_command_name("fake")); + struct command *also_cmd = lookup_command_by_hash(&cmds, hash_name("fake")); ASSERT(cmd != NULL, "Expected to be able to look up inserted command by hash"); ASSERT_STR_EQ(cmd->name, "fake", diff --git a/test/main.c b/test/main.c index a999b43..c4b50d0 100644 --- a/test/main.c +++ b/test/main.c @@ -39,6 +39,9 @@ int main() { printf("\nš \x1b[1;36mRunning minibuffer tests...\x1b[0m\n"); run_minibuffer_tests(); + printf("\n š \x1b[1;36mRunning settings tests...\x1b[0m\n"); + run_settings_tests(); + struct timespec elapsed; clock_gettime(CLOCK_MONOTONIC, &elapsed); uint64_t elapsed_nanos = diff --git a/test/settings.c b/test/settings.c new file mode 100644 index 0000000..181cd73 --- /dev/null +++ b/test/settings.c @@ -0,0 +1,73 @@ +#include "assert.h" +#include "test.h" + +#include "settings.h" +#include <stdlib.h> + +void test_get() { + settings_init(10); + settings_register_setting( + "my.setting", + (struct setting_value){.type = Setting_Bool, .bool_value = false}); + + struct setting *s = settings_get("my.setting"); + ASSERT(s != NULL, "Expected setting to exist after being inserted"); + ASSERT(s->value.type == Setting_Bool, + "Expected inserted setting to have the same type when retrieved"); + ASSERT(!s->value.bool_value, + "Expected inserted setting to have the same value when retrieved"); + + settings_register_setting( + "other.setting", + (struct setting_value){.type = Setting_Number, .number_value = 28}); + + struct setting **res = NULL; + uint32_t nres = 0; + settings_get_prefix("my", &res, &nres); + + ASSERT(nres == 1, "Expected to get one result back"); + ASSERT(s->value.type == Setting_Bool, "Expected inserted setting to have the " + "same type when retrieved by prefix"); + ASSERT(!s->value.bool_value, "Expected inserted setting to have the same " + "value when retrieved by prefix"); + + free(res); + + settings_destroy(); +} + +void test_set() { + settings_init(10); + settings_register_setting( + "my.setting", + (struct setting_value){.type = Setting_Bool, .bool_value = false}); + + // test that wrong type is ignored; + settings_set("my.setting", (struct setting_value){.type = Setting_String, + .string_value = "bonan"}); + + struct setting *s = settings_get("my.setting"); + ASSERT(s != NULL, "Expected setting to exist after being inserted"); + ASSERT(s->value.type == Setting_Bool, + "Expected inserted setting type to not have been changed"); + ASSERT(!s->value.bool_value, + "Expected inserted setting value to not have been changed"); + + // test that correct type is indeed changed + settings_set("my.setting", (struct setting_value){.type = Setting_Bool, + .bool_value = true}); + + s = settings_get("my.setting"); + ASSERT(s != NULL, "Expected setting to exist"); + ASSERT(s->value.type == Setting_Bool, + "Expected inserted setting type to not have been changed"); + ASSERT(s->value.bool_value, + "Expected inserted setting value to _have_ been changed"); + + settings_destroy(); +} + +void run_settings_tests() { + run_test(test_get); + run_test(test_set); +} diff --git a/test/test.h b/test/test.h index 4dab62e..047bbfb 100644 --- a/test/test.h +++ b/test/test.h @@ -17,5 +17,6 @@ void run_command_tests(); void run_keyboard_tests(); void run_allocator_tests(); void run_minibuffer_tests(); +void run_settings_tests(); #endif |
