6 #include "testcaseparser.h"
7 #include "testcaseinput.h"
8 #include "testcaseio.h"
9 #include "warningsparser.h"
12 * Read a testcase in from a file.
13 * On error, print a suitable message using g_printerr and return NULL.
14 * The returned testcase should be freed with testcase_free().
16 Testcase *testcase_parse_file(const char *filename)
19 TestcaseParser *parser;
20 TestcaseInput *input=NULL;
21 GMarkupParseContext *context;
24 const char *tag,*text;
25 gboolean found_tag=FALSE,newlines_set=FALSE;
26 parser=testcase_parser_new_from_file(filename);
29 if (!*testcase_parser_get_flag(parser))
31 g_printerr("%s: Not a valid testcase (flag)\n",filename);
32 testcase_parser_free(parser);
35 testcase=g_new0(Testcase,1);
36 testcase->basename=g_path_get_basename(filename);
37 s=strrchr(testcase->basename,'.');
40 while(testcase_parser_get_next_tag(parser,&tag,&text))
42 if (!input && !strcmp(tag,"INPUT"))
43 input=testcase_input_new("TEST-XXXXXX",text);
44 else if (g_str_has_prefix(tag,"INPUT(") && tag[strlen(tag)-1]==')')
46 arg=g_strndup(tag+6,strlen(tag)-7);
47 s=g_path_get_dirname(arg);
51 * Otherwise it would be possible to overwrite an arbitary
52 * file on somebody's computer by getting them to run a
56 "%s: Input files may not have a directory component\n",arg);
59 testcase_free(testcase);
60 testcase_parser_free(parser);
64 testcase->inputs=g_slist_prepend(testcase->inputs,
65 testcase_input_new(arg,text));
66 if (!strstr(arg,"XXXXXX"))
67 testcase->flags|=TESTCASE_TMP_DIR;
70 else if (!testcase->expected && !testcase->warnings &&
71 !strcmp(tag,"EXPECTED"))
72 testcase->expected=g_strdup(text);
73 else if (!testcase->expected && !testcase->warnings &&
74 !strcmp(tag,"WARNINGS"))
76 context=warnings_parse_context_new(testcase);
77 if (!g_markup_parse_context_parse(context,text,-1,&err) ||
78 !g_markup_parse_context_end_parse(context,&err))
80 g_markup_parse_context_free(context);
81 g_printerr("%s\n",err->message);
83 testcase_free(testcase);
84 testcase_parser_free(parser);
87 g_markup_parse_context_free(context);
89 else if (!testcase->encoding && !strcmp(tag,"ENCODING"))
90 testcase->encoding=g_strchomp(g_strdup(text));
91 else if (!newlines_set && !strcmp(tag,"NEWLINES"))
97 testcase->flags|=TESTCASE_UNIX_NEWLINES;
98 else if (!strcmp(s,"CR"))
99 testcase->flags|=TESTCASE_OS9_NEWLINES;
100 else if (strcmp(s,"CRLF"))
103 "%s: Unrecognised style for newlines. Try CR or LF.\n",s);
105 testcase_free(testcase);
106 testcase_parser_free(parser);
111 else if (!testcase->encoding && !strcmp(tag,"OPTIONS"))
113 testcase->options=g_strsplit(text,"\n",0);
114 g_free(testcase->options[g_strv_length(testcase->options)-1]);
115 testcase->options[g_strv_length(testcase->options)-1]=NULL;
119 g_printerr("%s: Not a valid testcase (%s)\n",filename,tag);
120 testcase_free(testcase);
121 testcase_parser_free(parser);
126 if (!testcase_parser_at_eof(parser))
129 g_printerr("%s: Not a valid testcase (garbage at end)\n",
132 g_printerr("%s: Not a valid testcase (no valid tags)\n",
134 testcase_free(testcase);
135 testcase_parser_free(parser);
139 input=testcase_input_new("TEST-XXXXXX",NULL);
140 testcase->inputs=g_slist_prepend(testcase->inputs,input);
141 testcase_parser_free(parser);