Added more tests for macros
authorLukas Krickl <lukas@krickl.dev>
Sat, 11 Nov 2023 12:31:51 +0000 (13:31 +0100)
committerLukas Krickl <lukas@krickl.dev>
Sat, 11 Nov 2023 12:31:51 +0000 (13:31 +0100)
src/test.c
src/ulas.c

index 2e8ad8b349ddc5c3aac2e72255fc4e741c8e916e..48275028c4c760f00e9bed12f97cff26b507885b 100644 (file)
@@ -92,7 +92,6 @@ void test_strbuf(void) {
 void test_preproc(void) {
   TESTBEGIN("preproc");
 
-  // should just echo back line as is
   assert_preproc("  test line", 0, "  test line");
   assert_preproc(" 123", 0, "  #define test 123\ntest");
   assert_preproc("", -1, "  #define 1test 123\n");
@@ -101,6 +100,13 @@ void test_preproc(void) {
   assert_preproc("  line p1 1\n  line p2 2\n  line p3 3 p1, p2, p3\n", 0,
                  "#macro test\n  line $1 1\n  line $2 2\n  line $3 3 "
                  "$0\n#endmacro\ntest p1, p2, p3");
+  assert_preproc("test macro with no args\n", 0,
+                 "#macro test\ntest macro with no args\n#endmacro\ntest");
+  assert_preproc("", -1, "#macro test\n not terminated\n");
+  assert_preproc(
+      "nested macro t1\nafter\ncontent n1\n\n", 0,
+      "#macro test\nnested macro $1\n#macro "
+      "nested\ncontent $1\n#endmacro\nafter\nnested n1\n#endmacro\ntest t1");
 
   TESTEND("preproc");
 }
index 0a696880a8f91fa5f73e17ece9662fd7c1018a62..aecc66a02cc1b52405d7d92b1db673e59c5f8e8e 100644 (file)
@@ -419,6 +419,9 @@ found:
       int rc = 0;
       while ((rc = ulas_preprocnext(pp, NULL, src, buf, ULAS_LINEMAX)) > 0) {
         if (rc == ULAS_PPDIR_ENDMACRO) {
+          // we need to clear the line buffer to now echo back
+          // the #endmacro directive
+          pp->line.buf[0] = '\0';
           break;
         }
 
@@ -488,15 +491,18 @@ int ulas_preproc(FILE *dst, FILE *src) {
   memset(buf, 0, ULAS_LINEMAX);
   int rc = 0;
 
+  // init
   struct ulas_preproc pp = {NULL, 0, ulas_str(1), ulas_str(1)};
   for (size_t i = 0; i < ULAS_MACROPARAMMAX; i++) {
     pp.macroparam[i] = ulas_str(8);
   }
   pp.macrobuf = ulas_str(8);
 
+  // preproc
   while ((rc = ulas_preprocnext(&pp, dst, src, buf, ULAS_LINEMAX)) > 0) {
   }
 
+  // cleanup
   ulas_strfree(&pp.line);
   ulas_strfree(&pp.tok);