6 #include "testcaseparser.h"
7 #include "testcaseinput.h"
8 #include "testcaseoutput.h"
9 #include "testcaseio.h"
10 #include "warningsparser.h"
13 * Read a testcase in from a file.
14 * On error, print a suitable message using g_printerr and return NULL.
15 * The returned testcase should be freed with testcase_free().
17 Testcase *testcase_parse_file(const char *filename)
20 TestcaseParser *parser;
21 TestcaseInput *input=NULL;
22 GMarkupParseContext *context;
25 const char *tag,*text;
26 gboolean found_tag=FALSE,newlines_set=FALSE;
27 parser=testcase_parser_new_from_file(filename);
30 if (!*testcase_parser_get_flag(parser))
32 g_printerr("%s: Not a valid testcase (flag)\n",filename);
33 testcase_parser_free(parser);
36 testcase=g_new0(Testcase,1);
37 testcase->basename=g_path_get_basename(filename);
38 s=strrchr(testcase->basename,'.');
41 while(testcase_parser_get_next_tag(parser,&tag,&text))
43 if (!input && !strcmp(tag,"INPUT"))
44 input=testcase_input_new("TEST-XXXXXX",text);
45 else if (g_str_has_prefix(tag,"INPUT(") && tag[strlen(tag)-1]==')')
47 arg=g_strndup(tag+6,strlen(tag)-7);
48 s=g_path_get_dirname(arg);
52 * Otherwise it would be possible to overwrite an arbitary
53 * file on somebody's computer by getting them to run a
57 "%s: Input files may not have a directory component\n",arg);
60 testcase_free(testcase);
61 testcase_parser_free(parser);
65 testcase->inputs=g_slist_prepend(testcase->inputs,
66 testcase_input_new(arg,text));
67 if (!strstr(arg,"XXXXXX"))
68 testcase->flags|=TESTCASE_TMP_DIR;
71 else if (!testcase->expected && !testcase->warnings &&
72 !strcmp(tag,"EXPECTED"))
73 testcase->expected=g_strdup(text);
74 else if (g_str_has_prefix(tag,"EXPECTED(") && tag[strlen(tag)-1]==')')
76 arg=g_strndup(tag+9,strlen(tag)-10);
77 s=g_path_get_dirname(arg);
80 g_printerr("%s: Expected files may not have a "
81 "directory component\n",arg);
84 testcase_free(testcase);
85 testcase_parser_free(parser);
89 testcase->outputs=g_slist_prepend(testcase->outputs,
90 testcase_output_new(arg,text));
93 else if (!testcase->expected && !testcase->warnings &&
94 !strcmp(tag,"WARNINGS"))
96 context=warnings_parse_context_new(testcase);
97 if (!g_markup_parse_context_parse(context,text,-1,&err) ||
98 !g_markup_parse_context_end_parse(context,&err))
100 g_markup_parse_context_free(context);
101 g_printerr("%s\n",err->message);
103 testcase_free(testcase);
104 testcase_parser_free(parser);
107 g_markup_parse_context_free(context);
109 else if (!testcase->encoding && !strcmp(tag,"ENCODING"))
110 testcase->encoding=g_strchomp(g_strdup(text));
111 else if (!newlines_set && !strcmp(tag,"NEWLINES"))
117 testcase->flags|=TESTCASE_UNIX_NEWLINES;
118 else if (!strcmp(s,"CR"))
119 testcase->flags|=TESTCASE_OS9_NEWLINES;
120 else if (strcmp(s,"CRLF"))
123 "%s: Unrecognised style for newlines. Try CR or LF.\n",s);
125 testcase_free(testcase);
126 testcase_parser_free(parser);
131 else if (!testcase->options && !strcmp(tag,"OPTIONS"))
133 testcase->options=g_strsplit(text,"\n",0);
134 if (testcase->options && g_strv_length(testcase->options)>0)
136 g_free(testcase->options[g_strv_length(testcase->options)-1]);
137 testcase->options[g_strv_length(testcase->options)-1]=NULL;
142 g_printerr("%s: Not a valid testcase (%s)\n",filename,tag);
143 testcase_free(testcase);
144 testcase_parser_free(parser);
149 if (!testcase_parser_at_eof(parser))
152 g_printerr("%s: Not a valid testcase (garbage at end)\n",
155 g_printerr("%s: Not a valid testcase (no valid tags)\n",
157 testcase_free(testcase);
158 testcase_parser_free(parser);
162 input=testcase_input_new("TEST-XXXXXX",NULL);
163 testcase->inputs=g_slist_prepend(testcase->inputs,input);
164 testcase_parser_free(parser);