[go: up one dir, main page]

File: json.c

package info (click to toggle)
criterion 2.4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 3,864 kB
  • sloc: ansic: 17,945; cpp: 774; python: 74; makefile: 25; sh: 19
file content (199 lines) | stat: -rw-r--r-- 6,735 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * The MIT License (MIT)
 *
 * Copyright © 2015-2016 Franklin "Snaipe" Mathieu <http://snai.pe/>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "criterion/stats.h"
#include "criterion/options.h"
#include "criterion/internal/ordered-set.h"
#include "log/logging.h"
#include "compat/posix.h"
#include "compat/strtok.h"
#include "compat/time.h"
#include "config.h"
#include "common.h"

#define JSON_TEST_TEMPLATE_BEGIN                     \
    "        {\n"                                    \
    "          \"name\": \"%s\",\n"                  \
    "          \"assertions\": %" CRI_PRIuSIZE ",\n" \
    "          \"status\": \"%s\""

#define JSON_TEST_TEMPLATE_END \
    "\n"                       \
    "        }"

#define JSON_TEST_FAILED_TEMPLATE_BEGIN \
    ",\n"                               \
    "          \"messages\": [\n"

#define JSON_TEST_FAILED_TEMPLATE_END \
    "\n"                              \
    "          ]"

#define JSON_FAILURE_MSG_ENTRY \
    "            \"%s:%u: %s\""

#define JSON_CRASH_MSG_ENTRY \
    ",\n"                    \
    "          \"messages\": [\"The test crashed.\"]"

#define JSON_TIMEOUT_MSG_ENTRY \
    ",\n"                      \
    "          \"messages\": [\"The test timed out.\"]"

#define JSON_TEST_SKIPPED_TEMPLATE_BEGIN \
    ",\n"                                \
    "          \"messages\": [\""

#define JSON_TEST_SKIPPED_TEMPLATE_END \
    "\"]"

#define JSON_SKIPPED_MSG_ENTRY \
    "The test was skipped."

#define JSON_TEST_LIST_TEMPLATE_BEGIN \
    "      \"tests\": [\n"

#define JSON_TEST_LIST_TEMPLATE_END \
    "      ]\n"

#define JSON_TESTSUITE_TEMPLATE_BEGIN         \
    "    {\n"                                 \
    "      \"name\": \"%s\",\n"               \
    "      \"passed\": %" CRI_PRIuSIZE ",\n"  \
    "      \"failed\": %" CRI_PRIuSIZE ",\n"  \
    "      \"errored\": %" CRI_PRIuSIZE ",\n" \
    "      \"skipped\": %" CRI_PRIuSIZE ",\n"

#define JSON_TESTSUITE_TEMPLATE_END \
    "    }"

#define JSON_TESTSUITE_LIST_TEMPLATE_BEGIN \
    "  \"test_suites\": [\n"

#define JSON_TESTSUITE_LIST_TEMPLATE_END \
    "  ]\n"

#define JSON_BASE_TEMPLATE_BEGIN              \
    "{\n"                                     \
    "  \"id\": \"Criterion " VERSION "\",\n"  \
    "  \"passed\": %" CRI_PRIuSIZE ",\n"      \
    "  \"failed\": %" CRI_PRIuSIZE ",\n"      \
    "  \"errored\": %" CRI_PRIuSIZE ",\n"     \
    "  \"skipped\": %" CRI_PRIuSIZE ",\n"     \

#define JSON_BASE_TEMPLATE_END \
    "}\n"

static CR_INLINE const char *get_status_string(struct criterion_test_stats *ts)
{
    return (ts->crashed || ts->timed_out)           ? "ERRORED"
           : ts->test_status == CR_STATUS_FAILED     ? "FAILED"
           : ts->test_status == CR_STATUS_SKIPPED    ? "SKIPPED"
           : "PASSED";
}

static void print_test(FILE *f, struct criterion_test_stats *ts)
{
    fprintf(f, JSON_TEST_TEMPLATE_BEGIN,
            ts->test->name,
            (size_t) (ts->passed_asserts + ts->failed_asserts),
            get_status_string(ts)
            );

    if (ts->test_status == CR_STATUS_SKIPPED) {
        fprintf(f, "%s%s%s", JSON_TEST_SKIPPED_TEMPLATE_BEGIN,
                ts->message ? ts->message : JSON_SKIPPED_MSG_ENTRY,
                JSON_TEST_SKIPPED_TEMPLATE_END);
    } else if (ts->crashed) {
        fprintf(f, JSON_CRASH_MSG_ENTRY);
    } else if (ts->timed_out) {
        fprintf(f, JSON_TIMEOUT_MSG_ENTRY);
    } else if (ts->test_status == CR_STATUS_FAILED) {
        fprintf(f, JSON_TEST_FAILED_TEMPLATE_BEGIN);

        bool first = true;
        for (struct criterion_assert_stats *asrt = ts->asserts; asrt; asrt = asrt->next) {
            if (!asrt->passed) {
                if (!first)
                    fprintf(f, ",\n");
                else
                    first = false;

                bool sf = criterion_options.short_filename;
                char *dup = strdup(*asrt->message ? asrt->message : "");
                char *saveptr = NULL;
                char *line = strtok_r(dup, "\n", &saveptr);

                fprintf(f, JSON_FAILURE_MSG_ENTRY,
                        sf ? basename_compat(asrt->file) : asrt->file,
                        asrt->line,
                        line
                        );

                while ((line = strtok_r(NULL, "\n", &saveptr)))
                    fprintf(f, ",\n            \"  %s\"", line);
                free(dup);
            }
        }
        fprintf(f, JSON_TEST_FAILED_TEMPLATE_END);
    }

    fprintf(f, JSON_TEST_TEMPLATE_END);
}

void json_report(FILE *f, struct criterion_global_stats *stats)
{
    fprintf(f, JSON_BASE_TEMPLATE_BEGIN,
            stats->tests_passed,
            stats->tests_failed,
            stats->tests_crashed,
            stats->tests_skipped
            );

    fprintf(f, JSON_TESTSUITE_LIST_TEMPLATE_BEGIN);
    for (struct criterion_suite_stats *ss = stats->suites; ss; ss = ss->next) {
        fprintf(f, JSON_TESTSUITE_TEMPLATE_BEGIN,
                ss->suite->name,
                ss->tests_passed,
                ss->tests_failed,
                ss->tests_crashed,
                ss->tests_skipped
                );

        fprintf(f, JSON_TEST_LIST_TEMPLATE_BEGIN);
        for (struct criterion_test_stats *ts = ss->tests; ts; ts = ts->next) {
            print_test(f, ts);
            fprintf(f, ts->next ? ",\n" : "\n");
        }
        fprintf(f, JSON_TEST_LIST_TEMPLATE_END);

        fprintf(f, JSON_TESTSUITE_TEMPLATE_END);
        fprintf(f, ss->next ? ",\n" : "\n");
    }
    fprintf(f, JSON_TESTSUITE_LIST_TEMPLATE_END);

    fprintf(f, JSON_BASE_TEMPLATE_END);
}