Repository navigation
Expand file tree
/
Copy pathrender_test.cpp
More file actions
200 lines (170 loc) · 5.07 KB
/
Copy pathrender_test.cpp
File metadata and controls
200 lines (170 loc) · 5.07 KB
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
#include <filesystem>
#include <fstream>
#include <cctype>
#include <locale>
#include <vector>
#include <string>
#include <algorithm>
#include "fonts.h"
#include "render2png.h"
#include "litehtml/html.h"
namespace fs = std::filesystem;
std::vector<std::string> find_htm_files(std::string dir);
bool test(std::string filename);
#if STANDALONE
#include <chrono>
using namespace chrono;
string test_dir = ".";
auto usage =
"Usage: \n"
"render_test [folder | html-file]+ \n"
"render_test -d png-file png-file ";
#define RED(str) "\33[91m" str "\33[0m"
#define GREEN(str) "\33[92m" str "\33[0m"
void error(string msg) { printf(RED("%s\n"), msg.c_str()); exit(1); }
int main(int argc, char* argv[])
{
auto start = steady_clock::now();
// Using a hack to enable ANSI escape sequences in a Windows console.
// Whenif it stops working ENABLE_VIRTUAL_TERMINAL_PROCESSING can be used instead.
system(" ");
if (argc == 1) error(usage);
else if (string(argv[1]) == "-d")
{
if (argc != 4) error(usage);
string pngfile1 = argv[2];
string pngfile2 = argv[3];
if (!exists(pngfile1)) error(pngfile1 + " not found");
if (!exists(pngfile2)) error(pngfile2 + " not found");
printf("max_color_diff = %d", max_color_diff(pngfile1, pngfile2));
return 0;
}
vector<string> files;
for (int i = 1; i < argc; i++)
{
if (is_directory(argv[i]))
files += find_htm_files(argv[i]);
else if (exists(argv[i]))
files.push_back(argv[i]);
else
printf(RED("%s not found\n"), argv[i]);
}
if (files.empty())
error("No html files found");
vector<string> failed_tests;
for (auto file : files)
{
if (test(file))
printf(GREEN("pass")" %s\n", file.c_str());
else
printf(RED("FAILURE %s\n"), file.c_str()),
failed_tests.push_back(file);
}
if (failed_tests.empty())
printf(GREEN("\nAll tests passed\n"));
else
{
int count = (int)failed_tests.size();
printf(RED("\n%d %s FAILED:\n"), count, count == 1 ? "test" : "tests");
for (auto file : failed_tests)
printf(RED("%s\n"), file.c_str());
}
auto stop = steady_clock::now();
printf("\nTime: %d sec\n", (int)((stop - start).count() / 1'000'000'000));
}
#else
#include <gtest/gtest.h>
std::string test_dir = fs::path(__FILE__).parent_path() / "render";
using render_test = testing::TestWithParam<std::string>;
TEST_P(render_test, _)
{
ASSERT_TRUE(test(GetParam()));
}
INSTANTIATE_TEST_SUITE_P(, render_test, testing::ValuesIn(find_htm_files("")));
#endif // STANDALONE
std::vector<std::string> find_htm_files(std::string dir)
{
std::vector<std::string> files;
for (const auto& entry : std::filesystem::directory_iterator(test_dir + "/" + dir))
{
std::string name = entry.path().filename().string();
std::string path;
if(dir.empty())
{
path = name;
} else
{
path = dir + "/" + name;
}
if (entry.is_directory())
{
if (name[0] != '-' && name != "support")
{
auto tmp_files = find_htm_files(path);
files.insert(files.end(), tmp_files.begin(), tmp_files.end());
}
} else
{
if (name[0] != '-' && litehtml::is_one_of(std::filesystem::path(name).extension(), ".htm", ".html"))
files.push_back(path);
}
}
std::sort(files.begin(), files.end());
return files;
}
bool test(std::string filename)
{
std::string html_path = test_dir + "/" + filename;
std::string png_path = test_dir + "/" + filename + ".png";
std::string failed_png_path = test_dir + "/" + filename + "-FAILED.png";
// Some tests depend on locale-sensitive parsing. The sibling .cfg can request a
// comma-decimal/space-thousands numeric locale ("numeric_locale: space-thousands").
std::locale previous_locale;
bool locale_changed = false;
{
auto trim = [](std::string str) {
auto not_space = [](unsigned char c) { return !std::isspace(c); };
str.erase(str.begin(), std::find_if(str.begin(), str.end(), not_space));
str.erase(std::find_if(str.rbegin(), str.rend(), not_space).base(), str.end());
return str;
};
std::ifstream cfg(html_path + ".cfg");
for(std::string line; std::getline(cfg, line);)
{
auto colon = line.find(':');
if(colon == std::string::npos)
continue;
if(trim(line.substr(0, colon)) == "numeric_locale" &&
trim(line.substr(colon + 1)) == "space-thousands")
{
struct space_thousands_numpunct : std::numpunct<char>
{
char do_decimal_point() const override { return ','; }
char do_thousands_sep() const override { return ' '; }
std::string do_grouping() const override { return "\3"; }
};
previous_locale = std::locale();
std::locale::global(std::locale(std::locale::classic(), new space_thousands_numpunct));
locale_changed = true;
}
}
}
auto font_options = prepare_fonts_for_testing();
html2png::converter converter(800, 600, 96, "serif", font_options);
auto pixbuf = converter.to_pixbuf(html_path);
if(font_options)
{
cairo_font_options_destroy(font_options);
}
if(locale_changed)
{
std::locale::global(previous_locale);
}
bool res = html2png::pngcmp(pixbuf, png_path) == html2png::png_diff_same;
if(!res)
{
gdk_pixbuf_save(pixbuf, failed_png_path.c_str(), "png", nullptr, nullptr);
}
g_object_unref(pixbuf);
return res;
}