tests: Added first failure tests
authorLukas Krickl <lukas@krickl.dev>
Thu, 20 Mar 2025 04:22:30 +0000 (05:22 +0100)
committerLukas Krickl <lukas@krickl.dev>
Thu, 20 Mar 2025 04:22:30 +0000 (05:22 +0100)
src/test.c
src/ulas.c
tests/fail_endscope.s [new file with mode: 0644]
tests/fail_scope_nesting.s [new file with mode: 0644]

index 444c1d50fc1718f2405f0a6a13a6a1319025935d..6ee4b6115c6e4b49cdd622057654b31f37c4082d 100644 (file)
@@ -90,7 +90,7 @@ void test_strbuf(void) {
     FILE *dst = fmemopen(dstbuf, ULAS_LINEMAX, "we");                          \
     assert(ulas_preproc(dst, src) == (expect_ret));                            \
     fclose(src);                                                               \
-    fclose(dst);                                                              \
+    fclose(dst);                                                               \
     assert(strcmp(dstbuf, (expect_dst)) == 0);                                 \
   }
 
@@ -411,7 +411,8 @@ void test_symscope(void) {
   {                                                                            \
     struct ulas_config cfg = ulas_cfg_from_env();                              \
     char *defs[] = {"ULAS_PREDEF"};                                            \
-    cfg.defs = defs;              cfg.defslen = 1;                                             \
+    cfg.defs = defs;                                                           \
+    cfg.defslen = 1;                                                           \
     ASSERT_FULL(expect_rc, in_path, expect_path)                               \
   }
 
@@ -424,6 +425,32 @@ void test_full_asm(void) {
   TESTEND("testfullasm");
 }
 
+#define ASSERT_FULL_ASM_FAIL(expect_rc, in_path)                               \
+  {                                                                            \
+    struct ulas_config cfg = ulas_cfg_from_env();                              \
+    printf("[source: %s;]\n", in_path);                \
+    ulaslstout = stdout;                                                       \
+    ulassymout = stdout;                                                       \
+    cfg.verbose = 1;                                                           \
+    char dstbuf[ULAS_FULLEN];                                                  \
+    memset(dstbuf, 0, ULAS_FULLEN);                                            \
+    ulasout = fmemopen(dstbuf, ULAS_FULLEN, "we");                             \
+    ulasin = fopen(in_path, "re");                                             \
+    assert(ulas_main(cfg) == (expect_rc));                                     \
+    fclose(ulasout);                                                           \
+    ulasin = stdin;                                                            \
+    ulasout = stdout;                                                          \
+  }
+
+void test_full_asm_fail(void) {
+  TESTBEGIN("testfullasmfail");
+
+  ASSERT_FULL_ASM_FAIL(-1, "tests/fail_scope_nesting.s");
+  ASSERT_FULL_ASM_FAIL(-1, "tests/fail_endscope.s");
+
+  TESTEND("testfullasm");
+}
+
 #define ASSERT_FULL_DASM(expect_rc, in_path, expect_path)                      \
   {                                                                            \
     struct ulas_config cfg = ulas_cfg_from_env();                              \
@@ -492,6 +519,7 @@ int main(int argc, char **argv) {
   // this will re-init everything on its own,
   // so call after free
   test_full_dasm();
+  test_full_asm_fail();
   test_full_asm();
 
   TESTEND("ulas test");
index 9c265094c990624e820d7119eaecb528947adb40..1f8d8a708929cbfef2698954bf06c09b836bb9a9 100644 (file)
@@ -2621,14 +2621,16 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) {
       break;
     case ULAS_ASMDIR_BEGIN_SCOPE:
       if (ulas.prev_scope != 0) {
-        ULASPANIC("nested .beginscope detected\n");
+        ULASERR("nested .beginscope detected\n");
+        rc = -1;
       }
       ulas.prev_scope = ulas.scope;
       ulas.scope = -1 - ulas_icntr();
       break;
     case ULAS_ASMDIR_END_SCOPE:
       if (ulas.prev_scope == 0) {
-        ULASPANIC("calling .endscope without starting a scope\n");
+        ULASERR("calling .endscope without starting a scope\n");
+        rc = -1;
       }
       ulas.scope = ulas.prev_scope;
       ulas.prev_scope = 0;
diff --git a/tests/fail_endscope.s b/tests/fail_endscope.s
new file mode 100644 (file)
index 0000000..410dd82
--- /dev/null
@@ -0,0 +1 @@
+.endscope
diff --git a/tests/fail_scope_nesting.s b/tests/fail_scope_nesting.s
new file mode 100644 (file)
index 0000000..17a97f0
--- /dev/null
@@ -0,0 +1,2 @@
+.beginscope
+.beginscope