aboutsummaryrefslogtreecommitdiff
path: root/tests/test_plugin.c
blob: 505d3630c49396326fd867c7bbbe9258bc27a246 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "test_core.h"

#include "ecex.h"

#include <assert.h>
#include <stddef.h>
#include <string.h>

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);
}