scriptLoadHandler used fopen/fread directly, but every .ds a script include()s is packed into dusk.dsk (compiled to .dsb) like every other asset now, not a loose file next to the binary - raw filesystem reads never found them outside a dev checkout. It now goes through assetFileInit/Open/Read/Close like the rest of the engine's asset loaders, failing the load the same way (returning SCRIPT_LOAD_FAILED) for a missing entry or one bigger than capacity. test_duskscript's fixtures moved to their own assets/ subfolder and pack into a small test.dsk with the same tools.asset.pack used for the real dusk.dsk, and the test opens that archive straight into ASSET.zip (skipping the full assetInit()/assetInitPlatform(), which expects a launched engine) so the tests exercise the real compile -> pack -> asset -> runtime pipeline instead of a filesystem shortcut around it. Co-Authored-By: Claude Sonnet 5 <[email protected]>
82 lines
2.5 KiB
C
82 lines
2.5 KiB
C
/**
|
|
* Copyright (c) 2026 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#include "dusktest.h"
|
|
#include "script/script.h"
|
|
#include "console/console.h"
|
|
#include "asset/asset.h"
|
|
#include "asset/assetdsk.h"
|
|
#include "util/memory.h"
|
|
|
|
static void test_scriptRunsAndPrintsToConsole(void **state) {
|
|
scripterror_t error;
|
|
scriptresult_t result;
|
|
|
|
consoleInit();
|
|
assert_int_equal(errorIsOk(scriptInit()), true);
|
|
|
|
result = scriptRuntimeRunFile(SCRIPT.runtime, DUSKSCRIPT_TEST_FIXTURE, true, &error);
|
|
assert_int_equal(result, SCRIPT_OK);
|
|
|
|
// print() reaches the console through scriptPrintHandler.
|
|
assert_string_equal(CONSOLE.line[CONSOLE_HISTORY_MAX - 2], "print hello");
|
|
// consolePrint(text), the native, writes directly.
|
|
assert_string_equal(CONSOLE.line[CONSOLE_HISTORY_MAX - 1], "consolePrint hello");
|
|
|
|
assert_int_equal(errorIsOk(scriptDispose()), true);
|
|
consoleDispose();
|
|
}
|
|
|
|
static void test_scriptNumbersAreAllFloats(void **state) {
|
|
scripterror_t error;
|
|
scriptresult_t result;
|
|
// In the order fixture_numbers.ds prints them.
|
|
const char_t *expected[] = {
|
|
"5", "3.75", "2.5", "false", "10", "20", "1", "16", "3", "3.5"
|
|
};
|
|
const int32_t count = (int32_t)(sizeof(expected) / sizeof(expected[0]));
|
|
int32_t i;
|
|
|
|
consoleInit();
|
|
assert_int_equal(errorIsOk(scriptInit()), true);
|
|
|
|
result = scriptRuntimeRunFile(SCRIPT.runtime, DUSKSCRIPT_TEST_NUMBERS, true, &error);
|
|
assert_int_equal(result, SCRIPT_OK);
|
|
|
|
for(i = 0; i < count; i++) {
|
|
assert_string_equal(CONSOLE.line[CONSOLE_HISTORY_MAX - count + i], expected[i]);
|
|
}
|
|
|
|
assert_int_equal(errorIsOk(scriptDispose()), true);
|
|
consoleDispose();
|
|
}
|
|
|
|
// Fixtures load through the asset system now, so ASSET needs a real zip
|
|
// behind it - open the packed test.dsk directly rather than the full
|
|
// assetInit()/assetInitPlatform(), which expects a real launched engine.
|
|
static int duskscript_test_setup(void **state) {
|
|
memoryZero(&ASSET, sizeof(ASSET));
|
|
threadMutexInit(&ASSET.zipLock);
|
|
assert_int_equal(errorIsOk(assetDskOpenFromPath(DUSKSCRIPT_TEST_DSK, &ASSET.zip)), true);
|
|
return 0;
|
|
}
|
|
|
|
static int duskscript_test_teardown(void **state) {
|
|
zip_close(ASSET.zip);
|
|
threadMutexDispose(&ASSET.zipLock);
|
|
memoryZero(&ASSET, sizeof(ASSET));
|
|
return 0;
|
|
}
|
|
|
|
int main(void) {
|
|
const struct CMUnitTest tests[] = {
|
|
cmocka_unit_test(test_scriptRunsAndPrintsToConsole),
|
|
cmocka_unit_test(test_scriptNumbersAreAllFloats),
|
|
};
|
|
return cmocka_run_group_tests(tests, duskscript_test_setup, duskscript_test_teardown);
|
|
}
|