File: report.cpp

package info (click to toggle)
mapnik 4.1.4%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,580 kB
  • sloc: cpp: 163,826; python: 1,265; sh: 690; xml: 161; makefile: 123; perl: 28; lisp: 13
file content (266 lines) | stat: -rw-r--r-- 7,484 bytes parent folder | download
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*****************************************************************************
 *
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2025 Artem Pavlenko
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *****************************************************************************/

// stl
#include <iomanip>
#include <fstream>
#include <numeric>
#include <map>

#include "report.hpp"

namespace visual_tests {

void console_report::report(result const& r)
{
    s << '"' << r.name << '-' << r.size.width << '-' << r.size.height;
    if (r.tiles.width > 1 || r.tiles.height > 1)
    {
        s << '-' << r.tiles.width << 'x' << r.tiles.height;
    }
    s << '-' << std::fixed << std::setprecision(1) << r.scale_factor << "\" with " << r.renderer_name << "... ";
    report_state(r);

    if (show_duration)
    {
        s << " (" << std::chrono::duration_cast<std::chrono::milliseconds>(r.duration).count() << " milliseconds)";
    }

    s << std::endl;
}

unsigned console_report::summary(result_list const& results)
{
    unsigned ok = 0;
    unsigned error = 0;
    unsigned fail = 0;
    unsigned overwrite = 0;

    using namespace std::chrono;
    using duration_map_type = std::map<std::string, high_resolution_clock::duration>;
    duration_map_type durations;

    for (auto const& r : results)
    {
        switch (r.state)
        {
            case STATE_OK:
                ok++;
                break;
            case STATE_FAIL:
                fail++;
                break;
            case STATE_ERROR:
                error++;
                break;
            case STATE_OVERWRITE:
                overwrite++;
                break;
        }

        if (show_duration)
        {
            duration_map_type::iterator duration = durations.find(r.renderer_name);
            if (duration == durations.end())
            {
                durations.emplace(r.renderer_name, r.duration);
            }
            else
            {
                duration->second += r.duration;
            }
        }
    }

    s << std::endl;
    s << "Visual rendering: " << fail << " failed / " << ok << " passed / " << overwrite << " overwritten / " << error
      << " errors" << std::endl;

    if (show_duration)
    {
        high_resolution_clock::duration total(0);
        for (auto const& duration : durations)
        {
            s << duration.first << ": \t" << duration_cast<milliseconds>(duration.second).count() << " milliseconds"
              << std::endl;
            total += duration.second;
        }
        s << "total: \t" << duration_cast<milliseconds>(total).count() << " milliseconds" << std::endl;
    }

    s << std::endl;
    report_failures(results);
    s << std::endl;

    return fail + error;
}

void console_report::report_state(result const& r)
{
    switch (r.state)
    {
        case STATE_OK:
            s << "OK";
            break;
        case STATE_FAIL:
            s << "FAILED (" << r.diff << " different pixels)";
            break;
        case STATE_OVERWRITE:
            s << "OVERWRITTEN (" << r.diff << " different pixels)";
            break;
        case STATE_ERROR:
            s << "ERROR (" << r.error_message << ")";
            break;
    }
}

void console_report::report_failures(result_list const& results)
{
    for (auto const& r : results)
    {
        if (r.state == STATE_OK)
        {
            continue;
        }

        s << r.name << " ";
        report_state(r);
        s << std::endl;
        if (!r.actual_image_path.empty())
        {
            s << "    " << r.actual_image_path << " (actual)" << std::endl;
            s << "    " << r.reference_image_path << " (reference)" << std::endl;
        }
    }
}

void console_short_report::report(result const& r)
{
    switch (r.state)
    {
        case STATE_OK:
            s << ".";
            break;
        case STATE_FAIL:
            s << "✘";
            break;
        case STATE_OVERWRITE:
            s << "✓";
            break;
        case STATE_ERROR:
            s << "ERROR (" << r.error_message << ")\n";
            break;
    }
}

void html_report::report(result const& r, mapnik::fs::path const& output_dir)
{
    if (r.state == STATE_ERROR)
    {
        s << "<div class=\"text\">Failed to render: " << r.name << "<br><em>" << r.error_message << "</em></div>\n";
    }
    else if (r.state == STATE_FAIL)
    {
        using namespace mapnik::fs;
        path reference = output_dir / r.reference_image_path.filename();
        path actual = output_dir / r.actual_image_path.filename();

        if (exists(reference))
        {
            remove(reference);
        }
        if (exists(actual))
        {
            remove(actual);
        }
        copy_file(r.reference_image_path, reference);
        copy_file(r.actual_image_path, actual);
        // clang-format off
        s << "<p>" << r.diff << "</p>\n"
            "<div class=\"r\">"
            "  <div class=\"i\">"
            "    <a href=" << reference.filename() << ">\n"
            "      <img src=" << reference.filename() << " width=\"100\%\">\n"
            "    </a>\n"
            "  </div>\n"
            "  <div class=\"i2\">\n"
            "    <a href=" << actual.filename() << ">\n"
            "      <img src=" << actual.filename() << " width=\"100\%\">\n"
            "    </a>\n"
            "  </div>\n"
            "</div>\n";
        // clang-format on
    }
}

constexpr char const* html_header = R"template(<!DOCTYPE html>
<html>
<head>
  <style>
    .r {
      width:100%;
      display: flex;
      position: relative;
      border:1px solid black;
      margin-bottom: 20px;
    }
    .i2 { max-width: 50%; width:50%; }
    .i { max-width: 50%; width:50%; }
    .i:hover{ position: absolute; top:0; left:0; }
    .i img, .i2 img { width: 100%; }
    .i img:hover { mix-blend-mode: difference; }
  </style>
</head>
<body>
)template";

constexpr char const* html_footer = R"template(</div>
</body>
</html>)template";

void html_report::summary(result_list const& results, mapnik::fs::path const& output_dir)
{
    s << html_header;

    for (auto const& r : results)
    {
        if (r.state != STATE_OK)
        {
            report(r, output_dir);
        }
    }

    s << html_footer;
}

void html_summary(result_list const& results, mapnik::fs::path output_dir)
{
    mapnik::fs::path html_root = output_dir / "visual-test-results";
    mapnik::fs::create_directories(html_root);
    mapnik::fs::path html_report_path = html_root / "index.html";
    std::clog << "View failure report at " << html_report_path << "\n";
    std::ofstream output_file(html_report_path.string());
    html_report report(output_file);
    report.summary(results, html_root);
}

} // namespace visual_tests