From 1813bbeb1b82616e00a29885a561055a19b86cf5 Mon Sep 17 00:00:00 2001 From: CPunch Date: Fri, 19 Mar 2021 22:23:04 -0500 Subject: [PATCH] Added minimal testsuite for IC - main.c will now report errors for passed scripts --- examples/testsuite.cosmo | 45 ++++++++++++++++++++++++++++++++++++++++ main.c | 16 ++++++++++---- 2 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 examples/testsuite.cosmo diff --git a/examples/testsuite.cosmo b/examples/testsuite.cosmo new file mode 100644 index 0000000..1b10c04 --- /dev/null +++ b/examples/testsuite.cosmo @@ -0,0 +1,45 @@ +/* + This script tests cosmo and makes sure everything still runs correctly. Pretty minimal for now +*/ + +print("starting Testsuite...") + +// tests the string.* library + +assert("Hello world!":sub(6) == "world!", "string.sub() failed!") +assert("A":rep(6) == "AAAAAA", "string.red() failed!") + +// tests some basic PEMDAS arithmetic + +assert(2 * (2 + 6) == 16, "PEMDAS check #1 failed!") +assert(2 / 5 + 3 / 5 == 1, "PEMDAS check #2 failed!") + +// iterator test + +proto Range + function __init(self, x) + self.max = x + end + + function __iter(self) + self.i = 0 + return self + end + + function __next(self) + if self.i >= self.max then + return nil // exit iterator loop + end + + return self.i++ + end +end + +var total = 0 +for i in Range(100) do + total = total + i +end + +assert(total == 4950, "Iterator check failed!") + +print("Testsuite passed!") \ No newline at end of file diff --git a/main.c b/main.c index 0826c9a..51f45b5 100644 --- a/main.c +++ b/main.c @@ -31,7 +31,9 @@ int cosmoB_input(CState *state, int nargs, CValue *args) { return 1; // 1 return value } -static void interpret(CState *state, const char *script, const char *mod) { +static bool interpret(CState *state, const char *script, const char *mod) { + bool ret; + // cosmoV_compileString pushes the result onto the stack (COBJ_ERROR or COBJ_CLOSURE) if (cosmoV_compileString(state, script, mod)) { COSMOVMRESULT res = cosmoV_call(state, 0, 0); // 0 args being passed, 0 results expected @@ -43,7 +45,9 @@ static void interpret(CState *state, const char *script, const char *mod) { cosmoV_printError(state, state->error); } + ret = state->panic; state->panic = false; // so our repl isn't broken + return !ret; } static void repl() { @@ -110,7 +114,8 @@ static char *readFile(const char* path) { return buffer; } -static void runFile(const char* fileName) { +static bool runFile(const char* fileName) { + bool ret; char* script = readFile(fileName); CState *state = cosmoV_newState(); cosmoB_loadLibrary(state); @@ -122,10 +127,11 @@ static void runFile(const char* fileName) { cosmoV_register(state, 1); - interpret(state, script, fileName); + ret = interpret(state, script, fileName); cosmoV_freeState(state); free(script); + return ret; // let the caller know if the script failed } int main(int argc, const char *argv[]) { @@ -133,7 +139,9 @@ int main(int argc, const char *argv[]) { repl(); } else if (argc >= 2) { // they passed a file (or more lol) for (int i = 1; i < argc; i++) { - runFile(argv[i]); + if (!runFile(argv[i])) { + exit(EXIT_FAILURE); + } } }