From c6d44836fd8ed1442e01825cb0f9f97e7bf11515 Mon Sep 17 00:00:00 2001 From: David Moc Date: Wed, 3 Jun 2026 02:26:11 +0200 Subject: Harden editor logging and packaging --- tests/test_buffers.c | 87 +++++++++++++++++++++++++++++++++++++++++++ tests/test_completion_path.c | 59 +++++++++++++++++++++++++++++ tests/test_core.c | 88 -------------------------------------------- tests/test_core.h | 13 +++++++ tests/test_main.c | 13 +++++++ tests/test_plugin.c | 83 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 255 insertions(+), 88 deletions(-) create mode 100644 tests/test_buffers.c create mode 100644 tests/test_completion_path.c delete mode 100644 tests/test_core.c create mode 100644 tests/test_core.h create mode 100644 tests/test_main.c create mode 100644 tests/test_plugin.c (limited to 'tests') diff --git a/tests/test_buffers.c b/tests/test_buffers.c new file mode 100644 index 0000000..7b073f4 --- /dev/null +++ b/tests/test_buffers.c @@ -0,0 +1,87 @@ +#include "test_core.h" + +#include "ecex.h" + +#include +#include +#include +#include + +void test_buffer_editing_and_undo(void) { + buffer_t *buffer = buffer_new("edit", NULL, 0); + assert(buffer); + + assert(buffer_insert(buffer, "hello") == 0); + assert(buffer_insert_char(buffer, '\n') == 0); + assert(buffer_insert(buffer, "world") == 0); + assert(strcmp(buffer->data, "hello\nworld") == 0); + assert(buffer->len == strlen("hello\nworld")); + assert(buffer_line_count(buffer) == 2); + + buffer_set_point(buffer, 5); + assert(buffer_insert(buffer, ",") == 0); + assert(strcmp(buffer->data, "hello,\nworld") == 0); + + assert(buffer_undo(buffer) == 0); + assert(strcmp(buffer->data, "hello\nworld") == 0); + assert(buffer_redo(buffer) == 0); + assert(strcmp(buffer->data, "hello,\nworld") == 0); + + buffer_free(buffer); +} + +void test_buffer_selection_and_search(void) { + buffer_t *buffer = buffer_new("select", NULL, 0); + assert(buffer); + + assert(buffer_set_text(buffer, "alpha beta gamma beta") == 0); + buffer_set_mark(buffer, 6); + buffer_set_point(buffer, 10); + assert(buffer_has_selection(buffer) == 1); + + size_t start = 0; + size_t end = 0; + buffer_selection_range(buffer, &start, &end); + assert(start == 6); + assert(end == 10); + + char *selection = buffer_substring(buffer, start, end); + assert(selection); + assert(strcmp(selection, "beta") == 0); + free(selection); + + assert(buffer_replace_selection(buffer, "delta") == 0); + assert(strcmp(buffer->data, "alpha delta gamma beta") == 0); + + size_t pos = 0; + assert(buffer_search_forward(buffer, "gamma", 0, &pos) == 0); + assert(pos == 12); + assert(buffer_search_backward(buffer, "alpha", buffer->len, &pos) == 0); + assert(pos == 0); + assert(buffer_search_forward(buffer, "missing", 0, &pos) != 0); + assert(buffer_undo(buffer) == 0); + assert(strcmp(buffer->data, "alpha beta gamma beta") == 0); + + buffer_free(buffer); +} + +void test_buffer_file_round_trip(void) { + const char *path = "/tmp/ecex-test-core-buffer.txt"; + remove(path); + + buffer_t *writer = buffer_new("writer", NULL, 0); + assert(writer); + assert(buffer_set_text(writer, "one\ntwo\n") == 0); + assert(buffer_save_as(writer, path) == 0); + assert(writer->modified == 0); + buffer_free(writer); + + buffer_t *reader = buffer_new("reader", path, 0); + assert(reader); + assert(buffer_load_file(reader, path) == 0); + assert(strcmp(reader->data, "one\ntwo\n") == 0); + assert(reader->modified == 0); + buffer_free(reader); + + remove(path); +} diff --git a/tests/test_completion_path.c b/tests/test_completion_path.c new file mode 100644 index 0000000..8170ee9 --- /dev/null +++ b/tests/test_completion_path.c @@ -0,0 +1,59 @@ +#include "test_core.h" + +#include "completion.h" +#include "ecex.h" + +#include +#include +#include + +void test_completion_helpers(void) { + assert(ecex_ascii_strncasecmp("Alpha", "alpha", 5) == 0); + assert(ecex_ascii_strncasecmp("Alpha", "Alpine", 4) != 0); + assert(ecex_ascii_contains_ci("compile-buffer", "PILE") == 1); + assert(ecex_ascii_contains_ci("compile-buffer", "missing") == 0); + + assert(ecex_fuzzy_score("find-file", "ff") > 0); + assert(ecex_fuzzy_score("find-file", "zz") < 0); + assert(ecex_fuzzy_score("find-file", "find") > ecex_fuzzy_score("other-find", "find")); + + ecex_completion_item_t items[] = { + {"z-file", 10, 0, 0}, + {"a-dir", 10, 1, 1}, + {"b-file", 30, 0, 2}, + }; + qsort(items, 3, sizeof(items[0]), ecex_completion_item_compare); + assert(strcmp(items[0].value, "b-file") == 0); + assert(strcmp(items[1].value, "a-dir") == 0); + assert(strcmp(items[2].value, "z-file") == 0); +} + +void test_path_helpers(void) { + char small[4]; + assert(ecex_path_copy(small, sizeof(small), "abcd") != 0); + assert(strcmp(small, "abc") == 0); + + char *joined = ecex_path_join("/tmp", "ecex-path-test.txt"); + assert(joined); + assert(strcmp(joined, "/tmp/ecex-path-test.txt") == 0); + free(joined); + + char *absolute = ecex_path_join("/tmp", "/var/log"); + assert(absolute); + assert(strcmp(absolute, "/var/log") == 0); + free(absolute); + + char *dir = ecex_path_dirname("/tmp/ecex/file.txt"); + assert(dir); + assert(strcmp(dir, "/tmp/ecex") == 0); + free(dir); + + char *base = ecex_path_basename_dup("/tmp/ecex/file.txt"); + assert(base); + assert(strcmp(base, "file.txt") == 0); + free(base); + + assert(ecex_path_is_image("photo.PNG") == 1); + assert(ecex_path_is_video("clip.MKV") == 1); + assert(ecex_path_is_media("notes.txt") == 0); +} diff --git a/tests/test_core.c b/tests/test_core.c deleted file mode 100644 index 42dfd99..0000000 --- a/tests/test_core.c +++ /dev/null @@ -1,88 +0,0 @@ -#include "ecex.h" - -#include -#include -#include - -static void test_plugin_identity(void) { - ecex_t ed; - memset(&ed, 0, sizeof(ed)); - ed.plugins = ecex_plugin_runtime_new(); - assert(ed.plugins); - - ecex_plugin_t *a = ecex_plugin_register(&ed, "alpha", ECEX_PLUGIN_API_VERSION); - assert(a); - assert(ecex_plugin_find(&ed, "alpha") == a); - assert(ecex_plugin_register(&ed, "alpha", ECEX_PLUGIN_API_VERSION) == 0); - assert(ecex_plugin_require(&ed, "alpha", ECEX_PLUGIN_API_VERSION) == a); - assert(ecex_plugin_register(&ed, "Bad ID", ECEX_PLUGIN_API_VERSION) == 0); - assert(ecex_plugin_register(&ed, "beta", ECEX_PLUGIN_API_VERSION + 1) == 0); - - ecex_plugin_runtime_free(ed.plugins); -} - -static void test_plugin_slots_and_exports(void) { - ecex_t ed; - memset(&ed, 0, sizeof(ed)); - ed.plugins = ecex_plugin_runtime_new(); - assert(ed.plugins); - - ecex_plugin_t *owner = ecex_plugin_register(&ed, "owner", ECEX_PLUGIN_API_VERSION); - ecex_plugin_t *reader = ecex_plugin_register(&ed, "reader", ECEX_PLUGIN_API_VERSION); - assert(owner); - assert(reader); - - assert(ecex_plugin_slot_i32_set(owner, "score", 0, 42) == 0); - assert(ecex_plugin_slot_i32_get_scalar(owner, "score", -1) == 42); - assert(ecex_plugin_slot_i32_set_2d(owner, "board", 10, 3, 4, 99) == 0); - assert(ecex_plugin_slot_i32_get_2d(owner, "board", 10, 3, 4, -1) == 99); - assert(ecex_plugin_slot_i32_get_2d(owner, "board", 0, 3, 4, -7) == -7); - - int copied = 0; - size_t copied_len = 0; - assert(ecex_plugin_slot_read_exported(&ed, "owner", "score", &copied, sizeof(copied), &copied_len) != 0); - assert(ecex_plugin_slot_set_export_flags(owner, "score", ECEX_PLUGIN_I32, ECEX_PLUGIN_EXPORT_READ) == 0); - assert(ecex_plugin_slot_read_exported(&ed, "owner", "score", &copied, sizeof(copied), &copied_len) == 0); - assert(copied == 42); - assert(copied_len == sizeof(copied)); - - copied = 7; - assert(ecex_plugin_slot_read_exported(&ed, "owner", "score", &copied, 0, &copied_len) != 0); - assert(copied == 7); - assert(copied_len == sizeof(copied)); - - ecex_plugin_runtime_free(ed.plugins); -} - -static void test_plugin_objects_and_text(void) { - ecex_t ed; - memset(&ed, 0, sizeof(ed)); - ed.plugins = ecex_plugin_runtime_new(); - assert(ed.plugins); - - ecex_plugin_t *plugin = ecex_plugin_register(&ed, "objects", ECEX_PLUGIN_API_VERSION); - assert(plugin); - - unsigned char *object = ecex_plugin_object_calloc(plugin, "state", 1, 16); - assert(object); - assert(ecex_plugin_object_i32_set(plugin, object, 4, 1234) == 0); - assert(ecex_plugin_object_i32_get(plugin, object, 4, -1) == 1234); - assert(ecex_plugin_object_i32_set(plugin, object, 14, 1) != 0); - assert(ecex_plugin_object_valid(plugin, object) == 1); - assert(ecex_plugin_object_free(plugin, object) == 0); - assert(ecex_plugin_object_valid(plugin, object) == 0); - - assert(ecex_plugin_text_set(plugin, 1, "hello", -1) == 0); - assert(strcmp(ecex_plugin_text_get_drawable(&ed, plugin, 1), "hello") == 0); - assert(ecex_plugin_text_free_all(plugin) == 0); - assert(strcmp(ecex_plugin_text_get_drawable(&ed, plugin, 1), "") == 0); - - ecex_plugin_runtime_free(ed.plugins); -} - -int main(void) { - test_plugin_identity(); - test_plugin_slots_and_exports(); - test_plugin_objects_and_text(); - return 0; -} diff --git a/tests/test_core.h b/tests/test_core.h new file mode 100644 index 0000000..a9dd52f --- /dev/null +++ b/tests/test_core.h @@ -0,0 +1,13 @@ +#ifndef ECEX_TEST_CORE_H +#define ECEX_TEST_CORE_H + +void test_plugin_identity(void); +void test_plugin_slots_and_exports(void); +void test_plugin_objects_and_text(void); +void test_buffer_editing_and_undo(void); +void test_buffer_selection_and_search(void); +void test_buffer_file_round_trip(void); +void test_completion_helpers(void); +void test_path_helpers(void); + +#endif diff --git a/tests/test_main.c b/tests/test_main.c new file mode 100644 index 0000000..6a8dc5f --- /dev/null +++ b/tests/test_main.c @@ -0,0 +1,13 @@ +#include "test_core.h" + +int main(void) { + test_plugin_identity(); + test_plugin_slots_and_exports(); + test_plugin_objects_and_text(); + test_buffer_editing_and_undo(); + test_buffer_selection_and_search(); + test_buffer_file_round_trip(); + test_completion_helpers(); + test_path_helpers(); + return 0; +} diff --git a/tests/test_plugin.c b/tests/test_plugin.c new file mode 100644 index 0000000..505d363 --- /dev/null +++ b/tests/test_plugin.c @@ -0,0 +1,83 @@ +#include "test_core.h" + +#include "ecex.h" + +#include +#include +#include + +void test_plugin_identity(void) { + ecex_t ed; + memset(&ed, 0, sizeof(ed)); + ed.plugins = ecex_plugin_runtime_new(); + assert(ed.plugins); + + ecex_plugin_t *a = ecex_plugin_register(&ed, "alpha", ECEX_PLUGIN_API_VERSION); + assert(a); + assert(ecex_plugin_find(&ed, "alpha") == a); + assert(ecex_plugin_register(&ed, "alpha", ECEX_PLUGIN_API_VERSION) == 0); + assert(ecex_plugin_require(&ed, "alpha", ECEX_PLUGIN_API_VERSION) == a); + assert(ecex_plugin_register(&ed, "Bad ID", ECEX_PLUGIN_API_VERSION) == 0); + assert(ecex_plugin_register(&ed, "beta", ECEX_PLUGIN_API_VERSION + 1) == 0); + + ecex_plugin_runtime_free(ed.plugins); +} + +void test_plugin_slots_and_exports(void) { + ecex_t ed; + memset(&ed, 0, sizeof(ed)); + ed.plugins = ecex_plugin_runtime_new(); + assert(ed.plugins); + + ecex_plugin_t *owner = ecex_plugin_register(&ed, "owner", ECEX_PLUGIN_API_VERSION); + ecex_plugin_t *reader = ecex_plugin_register(&ed, "reader", ECEX_PLUGIN_API_VERSION); + assert(owner); + assert(reader); + + assert(ecex_plugin_slot_i32_set(owner, "score", 0, 42) == 0); + assert(ecex_plugin_slot_i32_get_scalar(owner, "score", -1) == 42); + assert(ecex_plugin_slot_i32_set_2d(owner, "board", 10, 3, 4, 99) == 0); + assert(ecex_plugin_slot_i32_get_2d(owner, "board", 10, 3, 4, -1) == 99); + assert(ecex_plugin_slot_i32_get_2d(owner, "board", 0, 3, 4, -7) == -7); + + int copied = 0; + size_t copied_len = 0; + assert(ecex_plugin_slot_read_exported(&ed, "owner", "score", &copied, sizeof(copied), &copied_len) != 0); + assert(ecex_plugin_slot_set_export_flags(owner, "score", ECEX_PLUGIN_I32, ECEX_PLUGIN_EXPORT_READ) == 0); + assert(ecex_plugin_slot_read_exported(&ed, "owner", "score", &copied, sizeof(copied), &copied_len) == 0); + assert(copied == 42); + assert(copied_len == sizeof(copied)); + + copied = 7; + assert(ecex_plugin_slot_read_exported(&ed, "owner", "score", &copied, 0, &copied_len) != 0); + assert(copied == 7); + assert(copied_len == sizeof(copied)); + + ecex_plugin_runtime_free(ed.plugins); +} + +void test_plugin_objects_and_text(void) { + ecex_t ed; + memset(&ed, 0, sizeof(ed)); + ed.plugins = ecex_plugin_runtime_new(); + assert(ed.plugins); + + ecex_plugin_t *plugin = ecex_plugin_register(&ed, "objects", ECEX_PLUGIN_API_VERSION); + assert(plugin); + + unsigned char *object = ecex_plugin_object_calloc(plugin, "state", 1, 16); + assert(object); + assert(ecex_plugin_object_i32_set(plugin, object, 4, 1234) == 0); + assert(ecex_plugin_object_i32_get(plugin, object, 4, -1) == 1234); + assert(ecex_plugin_object_i32_set(plugin, object, 14, 1) != 0); + assert(ecex_plugin_object_valid(plugin, object) == 1); + assert(ecex_plugin_object_free(plugin, object) == 0); + assert(ecex_plugin_object_valid(plugin, object) == 0); + + assert(ecex_plugin_text_set(plugin, 1, "hello", -1) == 0); + assert(strcmp(ecex_plugin_text_get_drawable(&ed, plugin, 1), "hello") == 0); + assert(ecex_plugin_text_free_all(plugin) == 0); + assert(strcmp(ecex_plugin_text_get_drawable(&ed, plugin, 1), "") == 0); + + ecex_plugin_runtime_free(ed.plugins); +} -- cgit v1.2.3