summaryrefslogtreecommitdiff
path: root/test/command.c
diff options
context:
space:
mode:
authorAlbert Cervin <albert@acervin.com>2023-01-23 23:56:38 +0100
committerAlbert Cervin <albert@acervin.com>2023-01-23 23:56:38 +0100
commit9a2b138a03e27d0f04101fe6ae3977d79518c513 (patch)
tree113fe14c15e93872e0fe8b6d7a4e56ed3398b375 /test/command.c
parent9eda570311ffd292d333f7687074403ff46cc838 (diff)
downloaddged-9a2b138a03e27d0f04101fe6ae3977d79518c513.tar.gz
dged-9a2b138a03e27d0f04101fe6ae3977d79518c513.tar.xz
dged-9a2b138a03e27d0f04101fe6ae3977d79518c513.zip
Add more tests and documentation
Both doxygen and man page
Diffstat (limited to 'test/command.c')
-rw-r--r--test/command.c79
1 files changed, 79 insertions, 0 deletions
diff --git a/test/command.c b/test/command.c
new file mode 100644
index 0000000..738f5f9
--- /dev/null
+++ b/test/command.c
@@ -0,0 +1,79 @@
+#include "assert.h"
+#include "test.h"
+
+#include "command.h"
+
+void test_command_registry_create() {
+ struct commands cmds = command_registry_create(10);
+
+ ASSERT(cmds.capacity == 10, "Expected capacity to be the specified value");
+ ASSERT(cmds.ncommands == 0,
+ "Expected number of commands to initially be empty");
+
+ command_registry_destroy(&cmds);
+}
+
+int32_t fake_command(struct command_ctx ctx, int argc, const char *argv[]) {
+ return 0;
+}
+
+struct commands single_fake_command(const char *name) {
+ struct commands cmds = command_registry_create(10);
+
+ struct command cmd = {
+ .fn = fake_command,
+ .name = name,
+ .userdata = NULL,
+ };
+ register_command(&cmds, cmd);
+
+ return cmds;
+}
+
+void test_register_command() {
+ struct commands cmds = command_registry_create(1);
+
+ struct command cmd = {
+ .fn = fake_command,
+ .name = "fake",
+ .userdata = NULL,
+ };
+ register_command(&cmds, cmd);
+
+ ASSERT(cmds.ncommands == 1,
+ "Expected number of commands to be 1 after inserting one");
+
+ struct command multi[] = {
+ {.fn = fake_command, .name = "fake1", .userdata = NULL},
+ {.fn = fake_command, .name = "fake2", .userdata = NULL},
+ };
+
+ register_commands(&cmds, multi, 2);
+ ASSERT(cmds.ncommands == 3,
+ "Expected number of commands to be 3 after inserting two more");
+ ASSERT(cmds.capacity > 1,
+ "Expected capacity to have increased to accommodate new commands");
+}
+
+void test_lookup_command() {
+ struct commands cmds = single_fake_command("fake");
+ struct command *cmd = lookup_command(&cmds, "fake");
+
+ ASSERT(cmd != NULL,
+ "Expected to be able to look up inserted command by name");
+ 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"));
+ ASSERT(cmd != NULL,
+ "Expected to be able to look up inserted command by hash");
+ ASSERT_STR_EQ(cmd->name, "fake",
+ "Expected the found function to have the correct name");
+}
+
+void run_command_tests() {
+ run_test(test_command_registry_create);
+ run_test(test_register_command);
+ run_test(test_lookup_command);
+}