test/harness/testcaseoutput.c
changeset 103 d22d8cd4f628
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/harness/testcaseoutput.c	Sun Oct 27 17:01:47 2013 +0000
     1.3 @@ -0,0 +1,140 @@
     1.4 +#include <stdlib.h>
     1.5 +#include <string.h>
     1.6 +#include <errno.h>
     1.7 +#include <glib.h>
     1.8 +#include <bl/bl.h>
     1.9 +#include "testcase.h"
    1.10 +#include "testcaseoutput.h"
    1.11 +
    1.12 +/*
    1.13 + * Replace \r\n with \n, \n with U+240A (visible symbol for LF)
    1.14 + * and \r with U+240D (visible symbol for CR).
    1.15 + */
    1.16 +static char *dos2unix(const char *text)
    1.17 +{
    1.18 +    gunichar c;
    1.19 +    gboolean cr=FALSE;
    1.20 +    const gunichar visible_lf=0x240A;
    1.21 +    const gunichar visible_cr=0x240D;
    1.22 +    GString *string;
    1.23 +    string=g_string_new(NULL);
    1.24 +    while(*text)
    1.25 +    {
    1.26 +	c=g_utf8_get_char(text);
    1.27 +	text=g_utf8_next_char(text);
    1.28 +	if (cr)
    1.29 +	{
    1.30 +	    cr=FALSE;
    1.31 +	    if (c=='\n')
    1.32 +	    {
    1.33 +		g_string_append_c(string,'\n');
    1.34 +		continue;
    1.35 +	    }
    1.36 +	    else
    1.37 +		g_string_append_unichar(string,visible_cr);
    1.38 +	}
    1.39 +	if (c=='\r')
    1.40 +	    cr=TRUE;
    1.41 +	else if (c=='\n')
    1.42 +	    g_string_append_unichar(string,visible_lf);
    1.43 +	else
    1.44 +	    g_string_append_unichar(string,c);
    1.45 +    }
    1.46 +    if (cr)
    1.47 +	g_string_append_unichar(string,visible_cr);
    1.48 +    return g_string_free(string,FALSE);
    1.49 +}
    1.50 +
    1.51 +/*
    1.52 + * Read an output file needed for a testcase (as specified in <output>).
    1.53 + * The file is read in the encoding specified for communicating with
    1.54 + * bookloupe.
    1.55 + */
    1.56 +gboolean testcase_output_read(Testcase *testcase,TestcaseOutput *output,
    1.57 +  gchar **contents,gsize *length,GError **error)
    1.58 +{
    1.59 +    char *filename,*s,*t;
    1.60 +    gboolean retval;
    1.61 +    GError *tmp_err=NULL;
    1.62 +    if (!strcmp(output->name,"stdout"))
    1.63 +    {
    1.64 +	*contents=g_strdup(testcase->test_output);
    1.65 +	if (length)
    1.66 +	    *length=strlen(testcase->test_output);
    1.67 +    }
    1.68 +    else
    1.69 +    {
    1.70 +	if (testcase->tmpdir)
    1.71 +	    filename=g_build_filename(testcase->tmpdir,output->name,NULL);
    1.72 +	else
    1.73 +	    filename=g_strdup(output->name);
    1.74 +	if (!g_file_get_contents(filename,&s,NULL,error))
    1.75 +	{
    1.76 +	    g_free(filename);
    1.77 +	    return FALSE;
    1.78 +	}
    1.79 +	g_free(filename);
    1.80 +	if (testcase->encoding)
    1.81 +	{
    1.82 +	    t=dos2unix(s);
    1.83 +	    g_free(s);
    1.84 +	    s=g_convert(t,-1,"UTF-8",testcase->encoding,NULL,length,&tmp_err);
    1.85 +	    g_free(t);
    1.86 +	    if (!s)
    1.87 +	    {
    1.88 +		g_propagate_prefixed_error(error,tmp_err,
    1.89 +		  "Conversion from %s failed: ",testcase->encoding);
    1.90 +		return FALSE;
    1.91 +	    }
    1.92 +	    *contents=s;
    1.93 +	}
    1.94 +	else
    1.95 +	{
    1.96 +	    *contents=dos2unix(s);
    1.97 +	    if (length)
    1.98 +		*length=strlen(*contents);
    1.99 +	}
   1.100 +    }
   1.101 +    return TRUE;
   1.102 +}
   1.103 +
   1.104 +/*
   1.105 + * Remove an output file created by program under test.
   1.106 + */
   1.107 +gboolean testcase_output_remove(Testcase *testcase,TestcaseOutput *output,
   1.108 +  GError **error)
   1.109 +{
   1.110 +    char *filename;
   1.111 +    if (!strcmp(output->name,"stdout"))
   1.112 +	return TRUE;
   1.113 +    if (testcase->tmpdir)
   1.114 +	filename=g_build_filename(testcase->tmpdir,output->name,NULL);
   1.115 +    else
   1.116 +	filename=g_strdup(output->name);
   1.117 +    if (g_unlink(filename)<0)
   1.118 +    {
   1.119 +	g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno),
   1.120 +	  "%s: %s",filename,g_strerror(errno));
   1.121 +	return FALSE;
   1.122 +    }
   1.123 +    g_free(filename);
   1.124 +    return TRUE;
   1.125 +}
   1.126 +
   1.127 +/* Create a new description of an output file expected by a testcase */
   1.128 +TestcaseOutput *testcase_output_new(const char *name,const char *contents)
   1.129 +{
   1.130 +    TestcaseOutput *output;
   1.131 +    output=g_new0(TestcaseOutput,1);
   1.132 +    output->name=g_strdup(name);
   1.133 +    output->contents=g_strdup(contents);
   1.134 +    return output;
   1.135 +}
   1.136 +
   1.137 +/* Free the description of a testcase output file */
   1.138 +void testcase_output_free(TestcaseOutput *output)
   1.139 +{
   1.140 +    g_free(output->name);
   1.141 +    g_free(output->contents);
   1.142 +    g_free(output);
   1.143 +}