-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_detail.cpp
198 lines (191 loc) · 6.52 KB
/
test_detail.cpp
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
#include <iostream>
#include <catch2/catch_test_macros.hpp>
#include "wapopp/detail.hpp"
using namespace wapopp;
using namespace wapopp::detail;
TEST_CASE("Read simple content", "[unit]")
{
SECTION("Missing content")
{
auto j = R"({
"mime": "text/plain",
"type": "kicker"
})"_json;
auto content = read_simple_content<Kicker>(j);
REQUIRE(std::get_if<Error>(&content) != nullptr);
REQUIRE(std::get_if<Error>(&content)->msg == "Missing content");
}
SECTION("Missing mime")
{
auto j = R"({
"content": "Colleges",
"type": "kicker"
})"_json;
auto content = read_simple_content<Kicker>(j);
REQUIRE(std::get_if<Error>(&content) != nullptr);
REQUIRE(std::get_if<Error>(&content)->msg == "Missing mime");
}
SECTION("Kicker")
{
auto j = R"({
"content": "Colleges",
"mime": "text/plain",
"type": "kicker"
})"_json;
auto content = read_simple_content<Kicker>(j);
REQUIRE(std::get_if<Kicker>(&content) != nullptr);
REQUIRE(std::get_if<Kicker>(&content)->content == j["content"]);
REQUIRE(std::get_if<Kicker>(&content)->mime == j["mime"]);
}
SECTION("Title")
{
auto j = R"({
"content": "Danny Coale, Jarrett Boykin are a perfect 1-2 punch for Virginia Tech",
"mime": "text/plain",
"type": "title"
})"_json;
auto content = read_simple_content<Title>(j);
REQUIRE(std::get_if<Title>(&content) != nullptr);
REQUIRE(std::get_if<Title>(&content)->content == j["content"]);
REQUIRE(std::get_if<Title>(&content)->mime == j["mime"]);
}
SECTION("Byline")
{
auto j = R"({
"content": "By Mark Giannotto",
"mime": "text/plain",
"type": "title"
})"_json;
auto content = read_simple_content<Byline>(j);
REQUIRE(std::get_if<Byline>(&content) != nullptr);
REQUIRE(std::get_if<Byline>(&content)->content == j["content"]);
REQUIRE(std::get_if<Byline>(&content)->mime == j["mime"]);
}
SECTION("Text")
{
auto j = R"({
"content": "<span>paragraph</span>",
"subtype":"paragraph",
"mime": "text/html",
"type": "sanitized_html"
})"_json;
auto content = read_simple_content<Text>(j);
REQUIRE(std::get_if<Text>(&content) != nullptr);
REQUIRE(std::get_if<Text>(&content)->content == j["content"]);
REQUIRE(std::get_if<Text>(&content)->mime == j["mime"]);
}
}
TEST_CASE("Read date content", "[unit]")
{
SECTION("Missing date field")
{
auto j = R"({
"mime": "text/plain",
"type": "date"
})"_json;
auto content = read_date(j);
REQUIRE(std::get_if<Error>(&content) != nullptr);
REQUIRE(std::get_if<Error>(&content)->msg == "Missing content");
}
SECTION("Correct date")
{
auto j = R"({
"mime": "text/plain",
"content": 1325376562000,
"type": "date"
})"_json;
auto content = read_date(j);
REQUIRE(std::get_if<Date>(&content) != nullptr);
REQUIRE(std::get_if<Date>(&content)->timestamp == j["content"]);
}
SECTION("Corrupted date")
{
auto j = R"({
"mime": "text/plain",
"content": "oops",
"type": "date"
})"_json;
auto content = read_date(j);
REQUIRE(std::get_if<Error>(&content) != nullptr);
REQUIRE(std::get_if<Error>(&content)->msg ==
"Failed to parse content (\"oops\") to a requested type");
}
}
TEST_CASE("Read author info", "[unit]")
{
SECTION("Correct author info")
{
auto j = R"({
"role":"Reporter",
"type":"author_info",
"name":"Mark Giannotto",
"bio":"Mark Giannotto is a Montgomery County native ..."
})"_json;
auto content = read_author_info(j);
REQUIRE(std::get_if<AuthorInfo>(&content) != nullptr);
REQUIRE(std::get_if<AuthorInfo>(&content)->role == "Reporter");
REQUIRE(std::get_if<AuthorInfo>(&content)->name == "Mark Giannotto");
REQUIRE(std::get_if<AuthorInfo>(&content)->bio ==
"Mark Giannotto is a Montgomery County native ...");
}
SECTION("Missing role")
{
auto j = R"({
"type":"author_info",
"name":"Mark Giannotto",
"bio":"Mark Giannotto is a Montgomery County native ..."
})"_json;
auto content = read_author_info(j);
REQUIRE(std::get_if<Error>(&content) != nullptr);
REQUIRE(std::get_if<Error>(&content)->msg == "Missing role");
}
SECTION("Missing name")
{
auto j = R"({
"role":"Reporter",
"type":"author_info",
"bio":"Mark Giannotto is a Montgomery County native ..."
})"_json;
auto content = read_author_info(j);
REQUIRE(std::get_if<Error>(&content) != nullptr);
REQUIRE(std::get_if<Error>(&content)->msg == "Missing name");
}
SECTION("Missing bio")
{
auto j = R"({
"role": "Reporter",
"type": "author_info",
"name": "Mark Giannotto"
})"_json;
auto content = read_author_info(j);
REQUIRE(std::get_if<Error>(&content) != nullptr);
REQUIRE(std::get_if<Error>(&content)->msg == "Missing bio");
}
}
TEST_CASE("Read image", "[unit]")
{
SECTION("Correct image")
{
auto j = R"({
"fullcaption": "Full Caption",
"imageURL": "https://img.washingtonpost.com/somepic.jpg",
"mime": "image/jpeg",
"imageHeight": 2260,
"imageWidth": 2775,
"type": "image",
"blurb": "Virginia Tech wide receiver Danny Coale..."
})"_json;
auto content = read_image(j);
if (holds<Error>(content)) {
std::cerr << take<Error>(std::move(content)).msg << std::endl;
FAIL("");
}
REQUIRE(std::get_if<Image>(&content) != nullptr);
REQUIRE(std::get_if<Image>(&content)->caption == "Full Caption");
REQUIRE(std::get_if<Image>(&content)->url == "https://img.washingtonpost.com/somepic.jpg");
REQUIRE(std::get_if<Image>(&content)->blurb ==
"Virginia Tech wide receiver Danny Coale...");
REQUIRE(std::get_if<Image>(&content)->height == 2260);
REQUIRE(std::get_if<Image>(&content)->width == 2775);
}
}