summary.h 3.14 KB
Newer Older
1 2 3 4

#include <stdio.h>
#include <time.h>

5 6
int clar_summary_close_tag(
    struct clar_summary *summary, const char *tag, int indent)
7 8 9 10 11 12 13
{
	const char *indt;

	if (indent == 0) indt = "";
	else if (indent == 1) indt = "\t";
	else indt = "\t\t";

14
	return fprintf(summary->fp, "%s</%s>\n", indt, tag);
15 16
}

17
int clar_summary_testsuites(struct clar_summary *summary)
18
{
19
	return fprintf(summary->fp, "<testsuites>\n");
20 21
}

22 23 24
int clar_summary_testsuite(struct clar_summary *summary,
    int idn, const char *name, const char *pkg, time_t timestamp,
    double elapsed, int test_count, int fail_count, int error_count)
25 26 27 28
{
	struct tm *tm = localtime(&timestamp);
	char iso_dt[20];

29
	if (strftime(iso_dt, sizeof(iso_dt), "%Y-%m-%dT%H:%M:%S", tm) == 0)
30 31
		return -1;

32
	return fprintf(summary->fp, "\t<testsuite "
33 34 35 36 37 38 39 40 41
		       " id=\"%d\""
		       " name=\"%s\""
		       " package=\"%s\""
		       " hostname=\"localhost\""
		       " timestamp=\"%s\""
		       " time=\"%.2f\""
		       " tests=\"%d\""
		       " failures=\"%d\""
		       " errors=\"%d\">\n",
42
		       idn, name, pkg, iso_dt, elapsed, test_count, fail_count, error_count);
43 44
}

45 46
int clar_summary_testcase(struct clar_summary *summary,
    const char *name, const char *classname, double elapsed)
47
{
48 49 50
	return fprintf(summary->fp,
	    "\t\t<testcase name=\"%s\" classname=\"%s\" time=\"%.2f\">\n",
		name, classname, elapsed);
51 52
}

53 54
int clar_summary_failure(struct clar_summary *summary,
    const char *type, const char *message, const char *desc)
55
{
56 57 58
	return fprintf(summary->fp,
	    "\t\t\t<failure type=\"%s\"><![CDATA[%s\n%s]]></failure>\n",
	    type, message, desc);
59 60
}

61
struct clar_summary *clar_summary_init(const char *filename)
62
{
63 64 65 66 67 68 69 70 71 72
	struct clar_summary *summary;
	FILE *fp;

	if ((fp = fopen(filename, "w")) == NULL)
		return NULL;

	if ((summary = malloc(sizeof(struct clar_summary))) == NULL) {
		fclose(fp);
		return NULL;
	}
73

74 75
	summary->filename = filename;
	summary->fp = fp;
76

77
	return summary;
78 79
}

80
int clar_summary_shutdown(struct clar_summary *summary)
81 82 83 84
{
	struct clar_report *report;
	const char *last_suite = NULL;

85 86
	if (clar_summary_testsuites(summary) < 0)
		goto on_error;
87 88 89 90 91 92

	report = _clar.reports;
	while (report != NULL) {
		struct clar_error *error = report->errors;

		if (last_suite == NULL || strcmp(last_suite, report->suite) != 0) {
93 94 95
			if (clar_summary_testsuite(summary, 0, report->suite, "",
			    time(NULL), 0, _clar.tests_ran, _clar.total_errors, 0) < 0)
				goto on_error;
96 97 98 99
		}

		last_suite = report->suite;

100
		clar_summary_testcase(summary, report->test, "what", 0);
101 102

		while (error != NULL) {
103 104 105 106
			if (clar_summary_failure(summary, "assert",
			    error->error_msg, error->description) < 0)
				goto on_error;

107 108 109
			error = error->next;
		}

110 111
		if (clar_summary_close_tag(summary, "testcase", 2) < 0)
			goto on_error;
112 113 114

		report = report->next;

115 116 117 118
		if (!report || strcmp(last_suite, report->suite) != 0) {
			if (clar_summary_close_tag(summary, "testsuite", 1) < 0)
				goto on_error;
		}
119 120
	}

121 122 123 124 125
	if (clar_summary_close_tag(summary, "testsuites", 0) < 0 ||
	    fclose(summary->fp) != 0)
		goto on_error;

	printf("written summary file to %s\n", summary->filename);
126

127 128
	free(summary);
	return 0;
129

130 131 132 133
on_error:
	fclose(summary->fp);
	free(summary);
	return -1;
134
}