test/harness/testcaseio.c
author ali <ali@juiblex.co.uk>
Mon Jan 30 00:36:31 2012 +0000 (2012-01-30)
changeset 9 6a13fe0fc19e
parent 8 cf332d440466
child 11 4a80c6053a66
permissions -rw-r--r--
Add a testcase for user-defined typos
ali@0
     1
#include <stdlib.h>
ali@0
     2
#include <stdio.h>
ali@0
     3
#include <string.h>
ali@6
     4
#include <glib.h>
ali@5
     5
#include <bl/bl.h>
ali@0
     6
#include "testcaseparser.h"
ali@9
     7
#include "testcaseinput.h"
ali@0
     8
#include "testcaseio.h"
ali@0
     9
ali@0
    10
/*
ali@0
    11
 * Read a testcase in from a file.
ali@0
    12
 * On error, print a suitable message on stderr and return NULL.
ali@0
    13
 * The returned testcase should be freed with testcase_free().
ali@0
    14
 */
ali@0
    15
Testcase *testcase_parse_file(const char *filename)
ali@0
    16
{
ali@0
    17
    Testcase *testcase;
ali@0
    18
    TestcaseParser *parser;
ali@9
    19
    TestcaseInput *input=NULL;
ali@9
    20
    char *s,*arg;
ali@0
    21
    const char *tag,*text;
ali@6
    22
    gboolean found_tag=FALSE;
ali@0
    23
    parser=testcase_parser_new_from_file(filename);
ali@0
    24
    if (!parser)
ali@0
    25
	return NULL;
ali@0
    26
    if (!*testcase_parser_get_flag(parser))
ali@0
    27
    {
ali@0
    28
	fprintf(stderr,"%s: Not a valid testcase (flag)\n",filename);
ali@0
    29
	testcase_parser_free(parser);
ali@0
    30
	return NULL;
ali@0
    31
    }
ali@6
    32
    testcase=g_new0(Testcase,1);
ali@6
    33
    testcase->basename=g_path_get_basename(filename);
ali@0
    34
    s=strrchr(testcase->basename,'.');
ali@0
    35
    if (s)
ali@0
    36
	*s='\0';
ali@0
    37
    while(testcase_parser_get_next_tag(parser,&tag,&text))
ali@0
    38
    {
ali@9
    39
	if (!input && !strcmp(tag,"INPUT"))
ali@9
    40
	    input=testcase_input_new("TEST-XXXXXX",text);
ali@9
    41
	else if (g_str_has_prefix(tag,"INPUT(") && tag[strlen(tag)-1]==')')
ali@9
    42
	{
ali@9
    43
	    arg=g_strndup(tag+6,strlen(tag)-7);
ali@9
    44
	    s=g_path_get_dirname(arg);
ali@9
    45
	    if (strcmp(s,"."))
ali@9
    46
	    {
ali@9
    47
		/*
ali@9
    48
		 * Otherwise it would be possible to overwrite an arbitary
ali@9
    49
		 * file on somebody's computer by getting them to run a
ali@9
    50
		 * testcase!
ali@9
    51
		 */
ali@9
    52
		fprintf(stderr,
ali@9
    53
		  "%s: Input files may not have a directory component\n",arg);
ali@9
    54
		g_free(s);
ali@9
    55
		g_free(arg);
ali@9
    56
		testcase_free(testcase);
ali@9
    57
		testcase_parser_free(parser);
ali@9
    58
		return NULL;
ali@9
    59
	    }
ali@9
    60
	    g_free(s);
ali@9
    61
	    testcase->inputs=g_slist_prepend(testcase->inputs,
ali@9
    62
	      testcase_input_new(arg,text));
ali@9
    63
	    if (!strstr(arg,"XXXXXX"))
ali@9
    64
		testcase->flags|=TESTCASE_TMP_DIR;
ali@9
    65
	    g_free(arg);
ali@9
    66
	}
ali@0
    67
	else if (!testcase->expected && !strcmp(tag,"EXPECTED"))
ali@6
    68
	    testcase->expected=g_strdup(text);
ali@7
    69
	else if (!testcase->encoding && !strcmp(tag,"ENCODING"))
ali@8
    70
	    testcase->encoding=g_strchomp(g_strdup(text));
ali@9
    71
	else if (!testcase->encoding && !strcmp(tag,"OPTIONS"))
ali@9
    72
	{
ali@9
    73
	    testcase->options=g_strsplit(text,"\n",0);
ali@9
    74
	    g_free(testcase->options[g_strv_length(testcase->options)-1]);
ali@9
    75
	    testcase->options[g_strv_length(testcase->options)-1]=NULL;
ali@9
    76
	}
ali@0
    77
	else
ali@0
    78
	{
ali@0
    79
	    fprintf(stderr,"%s: Not a valid testcase (%s)\n",filename,tag);
ali@0
    80
	    testcase_free(testcase);
ali@0
    81
	    testcase_parser_free(parser);
ali@0
    82
	    return NULL;
ali@0
    83
	}
ali@0
    84
	found_tag=TRUE;
ali@0
    85
    }
ali@0
    86
    if (!testcase_parser_at_eof(parser))
ali@0
    87
    {
ali@0
    88
	if (found_tag)
ali@0
    89
	    fprintf(stderr,"%s: Not a valid testcase (garbage at end)\n",
ali@0
    90
	      filename);
ali@0
    91
	else
ali@0
    92
	    fprintf(stderr,"%s: Not a valid testcase (no valid tags)\n",
ali@0
    93
	      filename);
ali@0
    94
	testcase_free(testcase);
ali@0
    95
	testcase_parser_free(parser);
ali@0
    96
	return NULL;
ali@0
    97
    }
ali@9
    98
    if (!input)
ali@9
    99
	input=testcase_input_new("TEST-XXXXXX",NULL);
ali@9
   100
    testcase->inputs=g_slist_prepend(testcase->inputs,input);
ali@0
   101
    testcase_parser_free(parser);
ali@0
   102
    return testcase;
ali@0
   103
}