chiark / gitweb /
Further macro tests
authorRichard Kettlewell <rjk@greenend.org.uk>
Sun, 4 May 2008 15:57:30 +0000 (16:57 +0100)
committerRichard Kettlewell <rjk@greenend.org.uk>
Sun, 4 May 2008 15:57:30 +0000 (16:57 +0100)
lib/Makefile.am
lib/macros-builtin.c
lib/macros.c
lib/macros.h
lib/t-macros-1.tmpl [new file with mode: 0644]
lib/t-macros-2 [new file with mode: 0644]
lib/t-macros.c

index 2d3856a2b3876d15bd6bb2d8fc98af3eb2da200e..eae7288bd13a13ec83694bddbc6bd26856f02be8 100644 (file)
@@ -245,4 +245,4 @@ rebuild-unicode:
 
 CLEANFILES=definitions.h definitions.h.new
 
 
 CLEANFILES=definitions.h definitions.h.new
 
-EXTRA_DIST=trackdb.c trackdb-stub.c
+EXTRA_DIST=trackdb.c trackdb-stub.c t-macros-1.tmpl t-macros-2
index 53b2712f3f44eb6d5c29f51a55976423ba9a6fb9..b3f69f19e1ff555e2822b28fc33eedd3ed1dd92b 100644 (file)
@@ -101,7 +101,7 @@ static int exp_include(int attribute((unused)) nargs,
       error(errno, "cannot read template %s", name);
       if(sink_printf(output, "[[cannot open template '%s']]", name) < 0)
         return -1;
       error(errno, "cannot read template %s", name);
       if(sink_printf(output, "[[cannot open template '%s']]", name) < 0)
         return -1;
-      return -3;
+      return 0;
     }
     path = xstrdup(name);
   } else {
     }
     path = xstrdup(name);
   } else {
@@ -117,7 +117,7 @@ static int exp_include(int attribute((unused)) nargs,
       error(0, "cannot find template '%s'",  name);
       if(sink_printf(output, "[[cannot find template '%s']]", name) < 0)
         return -1;
       error(0, "cannot find template '%s'",  name);
       if(sink_printf(output, "[[cannot find template '%s']]", name) < 0)
         return -1;
-      return -3;
+      return 0;
     }
   }
   /* If it's a template expand it */
     }
   }
   /* If it's a template expand it */
@@ -388,6 +388,7 @@ static int exp_define(int attribute((unused)) nargs,
   return 0;
 }
 
   return 0;
 }
 
+/** @brief Register built-in expansions */
 void mx_register_builtin(void) {
   mx_register_magic("#", 0, INT_MAX, exp_comment);
   mx_register_magic("and", 0, INT_MAX, exp_and);
 void mx_register_builtin(void) {
   mx_register_magic("#", 0, INT_MAX, exp_comment);
   mx_register_magic("and", 0, INT_MAX, exp_and);
@@ -403,6 +404,13 @@ void mx_register_builtin(void) {
   mx_register("urlquote", 1, 1, exp_urlquote);
 }
 
   mx_register("urlquote", 1, 1, exp_urlquote);
 }
 
+/** @brief Add a directory to the search path
+ * @param s Directory to add
+ */
+void mx_search_path(const char *s) {
+  vector_append(&include_path, xstrdup(s));
+}
+
 /*
 Local Variables:
 c-basic-offset:2
 /*
 Local Variables:
 c-basic-offset:2
index 98e1436fa779787ccab487b1de3d4f59bf353378..2a5559c6b064ed895db4ad116e39771100dd002b 100644 (file)
@@ -416,17 +416,17 @@ int mx_expand(const struct mx_node *m,
     if(!(e = hash_find(expansions, m->name))) {
       error(0, "%s:%d: unknown expansion name '%s'",
             m->filename, m->line, m->name);
     if(!(e = hash_find(expansions, m->name))) {
       error(0, "%s:%d: unknown expansion name '%s'",
             m->filename, m->line, m->name);
-      if(sink_printf(output, "[[%s unknown]]", m->name) < 0)
+      if(sink_printf(output, "[['%s' unknown]]", m->name) < 0)
         return -1;
     } else if(m->nargs < e->min) {
       error(0, "%s:%d: expansion '%s' requires %d args, only %d given",
             m->filename, m->line, m->name, e->min, m->nargs);
         return -1;
     } else if(m->nargs < e->min) {
       error(0, "%s:%d: expansion '%s' requires %d args, only %d given",
             m->filename, m->line, m->name, e->min, m->nargs);
-      if(sink_printf(output, "[[%s too few args]]", m->name) < 0)
+      if(sink_printf(output, "[['%s' too few args]]", m->name) < 0)
         return -1;
     } else if(m->nargs > e->max) {
       error(0, "%s:%d: expansion '%s' takes at most %d args, but %d given",
             m->filename, m->line, m->name, e->max, m->nargs);
         return -1;
     } else if(m->nargs > e->max) {
       error(0, "%s:%d: expansion '%s' takes at most %d args, but %d given",
             m->filename, m->line, m->name, e->max, m->nargs);
-      if(sink_printf(output, "[[%s too many args]]", m->name) < 0)
+      if(sink_printf(output, "[['%s' too many args]]", m->name) < 0)
         return -1;
     } else switch(e->flags & EXP_TYPE_MASK) {
       case EXP_MAGIC: {
         return -1;
     } else switch(e->flags & EXP_TYPE_MASK) {
       case EXP_MAGIC: {
@@ -470,7 +470,7 @@ int mx_expand(const struct mx_node *m,
     if(rc) {
       /* For non-IO errors we generate some backtrace */
       if(rc != -1)
     if(rc) {
       /* For non-IO errors we generate some backtrace */
       if(rc != -1)
-        error(0,  "  ...in '%s' at %s:%d",
+        error(0,  "  ...in @%s at %s:%d",
               m->name, m->filename, m->line);
       return rc;
     }
               m->name, m->filename, m->line);
       return rc;
     }
index 8a557fd1422a26da847c4c5a621dddc66aa2e0a7..8eb6c4aa3d5f90bc2266d6435701b024f6561395 100644 (file)
@@ -105,6 +105,7 @@ int mx_register_macro(const char *name,
                       const struct mx_node *definition);
 
 void mx_register_builtin(void);
                       const struct mx_node *definition);
 
 void mx_register_builtin(void);
+void mx_search_path(const char *s);
 
 int mx_expand_file(const char *path,
                    struct sink *output,
 
 int mx_expand_file(const char *path,
                    struct sink *output,
diff --git a/lib/t-macros-1.tmpl b/lib/t-macros-1.tmpl
new file mode 100644 (file)
index 0000000..9a3d4e8
--- /dev/null
@@ -0,0 +1 @@
+@if{true}{yes}{no}
diff --git a/lib/t-macros-2 b/lib/t-macros-2
new file mode 100644 (file)
index 0000000..15363c9
--- /dev/null
@@ -0,0 +1 @@
+wibble
index 79c430eeea0dfe4c0555196e6066ce93d5bb061c..a34ff47b4f0a951cc0fd5e3f2771db2b09706e94 100644 (file)
@@ -26,6 +26,7 @@ static void test_macros(void) {
 #define L2 "plain text\n"
   static const char plain[] = L1 L2;
   char *s;
 #define L2 "plain text\n"
   static const char plain[] = L1 L2;
   char *s;
+  const char *cs;
 
   /* Plain text ------------------------------------------------------------- */
   
 
   /* Plain text ------------------------------------------------------------- */
   
@@ -176,10 +177,14 @@ static void test_macros(void) {
   /* Simple expansions ------------------------------------------------------ */
 
   mx_register_builtin();
   /* Simple expansions ------------------------------------------------------ */
 
   mx_register_builtin();
+  mx_search_path(".");
+  mx_search_path("lib");
+  if((cs = getenv("srcdir")))
+    mx_search_path(cs);
   
   
-#define check_macro(NAME, INPUT, OUTPUT) do {                   \
+#define check_macro(NAME, INPUT, OUTPUT, RET) do {              \
   m = mx_parse(NAME, 1, INPUT, NULL);                           \
   m = mx_parse(NAME, 1, INPUT, NULL);                           \
-  check_integer(mx_expandstr(m, &s, 0/*u*/, NAME), 0);          \
+  check_integer(mx_expandstr(m, &s, 0/*u*/, NAME), (RET));      \
   if(s && strcmp(s, OUTPUT)) {                                  \
     fprintf(stderr, "%s:%d: test %s\n"                          \
             "     INPUT:\n%s\n"                                 \
   if(s && strcmp(s, OUTPUT)) {                                  \
     fprintf(stderr, "%s:%d: test %s\n"                          \
             "     INPUT:\n%s\n"                                 \
@@ -190,90 +195,107 @@ static void test_macros(void) {
   }                                                             \
 } while(0)
 
   }                                                             \
 } while(0)
 
-  check_macro("empty", "", "");
-  check_macro("plain", plain, plain);
-
-  check_macro("if1", "@if{true}{yes}{no}", "yes");
-  check_macro("if2", "@if{true}{yes}", "yes");
-  check_macro("if3", "@if{false}{yes}{no}", "no");
-  check_macro("if4", "@if{false}{yes}", "");
-  check_macro("if5", "@if{ true}{yes}", "");
-
-  check_macro("and1", "@and", "true");
-  check_macro("and2", "@and{true}", "true");
-  check_macro("and3", "@and{false}", "false");
-  check_macro("and4", "@and{true}{true}", "true");
-  check_macro("and5", "@and{false}{true}", "false");
-  check_macro("and6", "@and{true}{false}", "false");
-  check_macro("and7", "@and{false}{false}", "false");
-
-  check_macro("or1", "@or", "false");
-  check_macro("or2", "@or{true}", "true");
-  check_macro("or2", "@or{false}", "false");
-  check_macro("or3", "@or{true}{true}", "true");
-  check_macro("or4", "@or{false}{true}", "true");
-  check_macro("or5", "@or{true}{false}", "true");
-  check_macro("or7", "@or{false}{false}", "false");
-
-  check_macro("not1", "@not{true}", "false");
-  check_macro("not2", "@not{false}", "true");
-  check_macro("not3", "@not{wibble}", "true");
-
-  check_macro("comment1", "@#{wibble}", "");
-  check_macro("comment2", "@#{comment with a\nnewline in}", "");
-
-  check_macro("discard1", "@discard{wibble}", "");
-  check_macro("discard2", "@discard{comment with a\nnewline in}", "");
-
-  check_macro("eq1", "@eq", "true");
-  check_macro("eq2", "@eq{}", "true");
-  check_macro("eq3", "@eq{a}", "true");
-  check_macro("eq4", "@eq{a}{a}", "true");
-  check_macro("eq5", "@eq{a}{a}{a}", "true");
-  check_macro("eq7", "@eq{a}{b}", "false");
-  check_macro("eq8", "@eq{a}{b}{a}", "false");
-  check_macro("eq9", "@eq{a}{a}{b}", "false");
-  check_macro("eq10", "@eq{b}{a}{a}", "false");
-
-  check_macro("ne1", "@ne", "true");
-  check_macro("ne2", "@ne{}", "true");
-  check_macro("ne3", "@ne{a}", "true");
-  check_macro("ne4", "@ne{a}{a}", "false");
-  check_macro("ne5", "@ne{a}{a}{a}", "false");
-  check_macro("ne7", "@ne{a}{b}", "true");
-  check_macro("ne8", "@ne{a}{b}{a}", "false");
-  check_macro("ne9", "@ne{a}{a}{b}", "false");
-  check_macro("ne10", "@ne{b}{a}{a}", "false");
-  check_macro("ne11", "@ne{a}{b}{c}", "true");
-
-  check_macro("sh1", "@shell{true}", "");
-  check_macro("sh2", "@shell{echo spong}", "spong\n");
-  fprintf(stderr, "expect error message from macro expander:\n");
-  check_macro("sh3", "@shell{echo spong;exit 3}", "spong\n");
-
-  check_macro("url1", "@urlquote{unreserved}", "unreserved");
-  check_macro("url2", "@urlquote{has space}", "has%20space");
-  check_macro("url3", "@urlquote{\xc0\xc1}", "%c0%c1");
-
+  check_macro("empty", "", "", 0);
+  check_macro("plain", plain, plain, 0);
+
+  check_macro("if1", "@if{true}{yes}{no}", "yes", 0);
+  check_macro("if2", "@if{true}{yes}", "yes", 0);
+  check_macro("if3", "@if{false}{yes}{no}", "no", 0);
+  check_macro("if4", "@if{false}{yes}", "", 0);
+  check_macro("if5", "@if{ true}{yes}", "", 0);
+
+  check_macro("and1", "@and", "true", 0);
+  check_macro("and2", "@and{true}", "true", 0);
+  check_macro("and3", "@and{false}", "false", 0);
+  check_macro("and4", "@and{true}{true}", "true", 0);
+  check_macro("and5", "@and{false}{true}", "false", 0);
+  check_macro("and6", "@and{true}{false}", "false", 0);
+  check_macro("and7", "@and{false}{false}", "false", 0);
+
+  check_macro("or1", "@or", "false", 0);
+  check_macro("or2", "@or{true}", "true", 0);
+  check_macro("or2", "@or{false}", "false", 0);
+  check_macro("or3", "@or{true}{true}", "true", 0);
+  check_macro("or4", "@or{false}{true}", "true", 0);
+  check_macro("or5", "@or{true}{false}", "true", 0);
+  check_macro("or7", "@or{false}{false}", "false", 0);
+
+  check_macro("not1", "@not{true}", "false", 0);
+  check_macro("not2", "@not{false}", "true", 0);
+  check_macro("not3", "@not{wibble}", "true", 0);
+
+  check_macro("comment1", "@#{wibble}", "", 0);
+  check_macro("comment2", "@#{comment with a\nnewline in}", "", 0);
+
+  check_macro("discard1", "@discard{wibble}", "", 0);
+  check_macro("discard2", "@discard{comment with a\nnewline in}", "", 0);
+
+  check_macro("eq1", "@eq", "true", 0);
+  check_macro("eq2", "@eq{}", "true", 0);
+  check_macro("eq3", "@eq{a}", "true", 0);
+  check_macro("eq4", "@eq{a}{a}", "true", 0);
+  check_macro("eq5", "@eq{a}{a}{a}", "true", 0);
+  check_macro("eq7", "@eq{a}{b}", "false", 0);
+  check_macro("eq8", "@eq{a}{b}{a}", "false", 0);
+  check_macro("eq9", "@eq{a}{a}{b}", "false", 0);
+  check_macro("eq10", "@eq{b}{a}{a}", "false", 0);
+
+  check_macro("ne1", "@ne", "true", 0);
+  check_macro("ne2", "@ne{}", "true", 0);
+  check_macro("ne3", "@ne{a}", "true", 0);
+  check_macro("ne4", "@ne{a}{a}", "false", 0);
+  check_macro("ne5", "@ne{a}{a}{a}", "false", 0);
+  check_macro("ne7", "@ne{a}{b}", "true", 0);
+  check_macro("ne8", "@ne{a}{b}{a}", "false", 0);
+  check_macro("ne9", "@ne{a}{a}{b}", "false", 0);
+  check_macro("ne10", "@ne{b}{a}{a}", "false", 0);
+  check_macro("ne11", "@ne{a}{b}{c}", "true", 0);
+
+  check_macro("sh1", "@shell{true}", "", 0);
+  check_macro("sh2", "@shell{echo spong}", "spong\n", 0);
+  fprintf(stderr, ">>> expect error message about shell command:\n");
+  check_macro("sh3", "@shell{echo spong;exit 3}", "spong\n", 0);
+
+  check_macro("url1", "@urlquote{unreserved}", "unreserved", 0);
+  check_macro("url2", "@urlquote{has space}", "has%20space", 0);
+  check_macro("url3", "@urlquote{\xc0\xc1}", "%c0%c1", 0);
+
+  check_macro("include1", "@include{t-macros-1.tmpl}",
+              "yes\n", 0);
+  check_macro("include2", "@include{t-macros-2}",
+              "wibble\n", 0);
+  fprintf(stderr, ">>> expect error message about t-macros-nonesuch:\n");
+  check_macro("include3", "<@include{t-macros-nonesuch}@>",
+              "<[[cannot find template 't-macros-nonesuch']]>", 0);
+  fprintf(stderr, ">>> expect error message about 'wibble':\n");
+  check_macro("badex1", "<@wibble@>",
+              "<[['wibble' unknown]]>", 0);
+  fprintf(stderr, ">>> expect error message about 'if':\n");
+  check_macro("badex2", "<@if@>",
+              "<[['if' too few args]]>", 0);
+  fprintf(stderr, ">>> expect error message about 'if':\n");
+  check_macro("badex3", "<@if:1:2:3:4:5@>",
+              "<[['if' too many args]]>", 0);
+  
   /* Macro definitions ------------------------------------------------------ */
 
   check_macro("macro1", "@define{m}{a b c}{@c@ @b@ @a@}@"
               "@m{1}{2}{3}",
   /* Macro definitions ------------------------------------------------------ */
 
   check_macro("macro1", "@define{m}{a b c}{@c@ @b@ @a@}@"
               "@m{1}{2}{3}",
-              "3 2 1");
+              "3 2 1", 0);
   check_macro("macro2", "@m{b}{c}{a}",
   check_macro("macro2", "@m{b}{c}{a}",
-              "a c b");
+              "a c b", 0);
   check_macro("macro3", "@m{@eq{z}{z}}{p}{q}",
   check_macro("macro3", "@m{@eq{z}{z}}{p}{q}",
-              "q p true");
+              "q p true", 0);
   check_macro("macro4",
               "@discard{\n"
               "  @define{n}{a b c}\n"
               "    {@if{@eq{@a@}{@b@}} {@c@} {no}}\n"
               "}@"
               "@n{x}{y}{z}",
   check_macro("macro4",
               "@discard{\n"
               "  @define{n}{a b c}\n"
               "    {@if{@eq{@a@}{@b@}} {@c@} {no}}\n"
               "}@"
               "@n{x}{y}{z}",
-              "no");
+              "no", 0);
   check_macro("macro5",
               "@n{x}{x}{z}",
   check_macro("macro5",
               "@n{x}{x}{z}",
-              "z");
+              "z", 0);
 
 }
 
 
 }