/** * Copyright (c) 2026 Dominic Masters * * This software is released under the MIT License. * https://opensource.org/licenses/MIT */ #include "dusktest.h" #include "console/console.h" #include "script/scriptmanager.h" #include "ui/ui.h" #include "util/memory.h" static int ui_setup(void **state) { consoleInit(); memoryZero(UI_ELEMENTS, sizeof(UI_ELEMENTS)); memoryZero(&UI, sizeof(UI)); for(uint8_t i = 0; i < UI_ROOT_MAX; i++) UI.root[i] = UI_ELEMENT_ID_INVALID; errorret_t ret = scriptManagerInit(); if(errorIsNotOk(ret)) { errorCatch(ret); return -1; } return 0; } static int ui_teardown(void **state) { errorret_t ret = scriptManagerDispose(); if(errorIsNotOk(ret)) errorCatch(ret); consoleDispose(); return 0; } static void test_ui_add_requires_an_argument(void **state) { errorret_t ret = scriptManagerExec("UI.add();", NULL); assert_true(errorIsNotOk(ret)); errorCatch(ret); assert_int_equal(memoryGetAllocatedCount(), 0); } static void test_ui_add_requires_an_object(void **state) { errorret_t ret = scriptManagerExec("UI.add('button');", NULL); assert_true(errorIsNotOk(ret)); errorCatch(ret); assert_int_equal(memoryGetAllocatedCount(), 0); } static void test_ui_add_requires_a_uielement(void **state) { errorret_t ret = scriptManagerExec("UI.add({});", NULL); assert_true(errorIsNotOk(ret)); errorCatch(ret); assert_int_equal(memoryGetAllocatedCount(), 0); } static void test_ui_remove_of_untracked_element_is_a_noop(void **state) { jerry_value_t result; errorret_t ret = scriptManagerExec( "UI.remove(new UIElement());", &result ); assert_true(errorIsOk(ret)); jerry_value_free(result); } int main(void) { assertInit(); const struct CMUnitTest tests[] = { cmocka_unit_test_setup_teardown( test_ui_add_requires_an_argument, ui_setup, ui_teardown ), cmocka_unit_test_setup_teardown( test_ui_add_requires_an_object, ui_setup, ui_teardown ), cmocka_unit_test_setup_teardown( test_ui_add_requires_a_uielement, ui_setup, ui_teardown ), cmocka_unit_test_setup_teardown( test_ui_remove_of_untracked_element_is_a_noop, ui_setup, ui_teardown ), }; return cmocka_run_group_tests(tests, NULL, NULL); }