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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
#include "itemrenderer.h"
#include "3rd-party/catch.hpp"
#include "cache.h"
#include "configcontainer.h"
#include "rssfeed.h"
#include "test-helpers.h"
using namespace newsboat;
static const auto FEED_TITLE = std::string("Funniest jokes ever");
std::shared_ptr<RssFeed> create_test_feed(Cache* c)
{
auto feed = std::make_shared<RssFeed>(c);
feed->set_title(FEED_TITLE);
return feed;
}
static const auto ITEM_TITLE = std::string("A frivolous test item");
static const auto ITEM_AUTHOR = std::string("Johnny Doe Jr.");
// Sun Sep 30 19:34:25 UTC 2018
static const auto ITEM_PUBDATE = time_t{1538336065};
static const auto ITEM_LINK = std::string("https://example.com/see-more");
static const auto ITEM_DESCRIPTON = std::string("<p>Hello, world!</p>");
static const auto ITEM_DESCRIPTON_RENDERED = std::string("Hello, world!");
static const auto ITEM_ENCLOSURE_URL =
std::string("https://example.com/see-more.mp3");
static const auto ITEM_ENCLOSURE_TYPE = std::string("audio/mpeg");
static const auto ITEM_FLAGS = std::string("wasdhjkl");
// Flags are sorted for rendering.
static const auto ITEM_FLAGS_RENDERED = std::string("adhjklsw");
std::pair<std::shared_ptr<RssItem>, std::shared_ptr<RssFeed>> create_test_item(Cache* c)
{
const auto feed = create_test_feed(c);
auto item = std::make_shared<RssItem>(c);
item->set_feedptr(feed);
item->set_title(ITEM_TITLE);
item->set_author(ITEM_AUTHOR);
item->set_pubDate(ITEM_PUBDATE);
item->set_link(ITEM_LINK);
item->set_flags(ITEM_FLAGS);
return {item, feed};
}
TEST_CASE("item_renderer::to_plain_text() produces a rendered representation "
"of an RSS item, ready to be displayed to the user",
"[item_renderer]")
{
TestHelpers::EnvVar tzEnv("TZ");
tzEnv.set("UTC");
ConfigContainer cfg;
// item_renderer uses that setting, so let's fix its value to make the test
// reproducible
cfg.set_configvalue("text-width", "80");
Cache rsscache(":memory:", &cfg);
std::shared_ptr<RssItem> item;
std::shared_ptr<RssFeed> feed;
std::tie(item, feed) = create_test_item(&rsscache);
SECTION("Item without an enclosure") {
item->set_description(ITEM_DESCRIPTON);
const auto result = item_renderer::to_plain_text(cfg, item);
const auto expected = std::string() +
"Feed: " + FEED_TITLE + '\n' +
"Title: " + ITEM_TITLE + '\n' +
"Author: " + ITEM_AUTHOR + '\n' +
"Date: Sun, 30 Sep 2018 19:34:25 +0000\n" +
"Link: " + ITEM_LINK + '\n' +
"Flags: " + ITEM_FLAGS_RENDERED + '\n' +
" \n" +
ITEM_DESCRIPTON_RENDERED + '\n';
REQUIRE(result == expected);
}
SECTION("Item with an enclosure") {
item->set_description(ITEM_DESCRIPTON);
item->set_enclosure_url(ITEM_ENCLOSURE_URL);
const auto result = item_renderer::to_plain_text(cfg, item);
const auto expected = std::string() +
"Feed: " + FEED_TITLE + '\n' +
"Title: " + ITEM_TITLE + '\n' +
"Author: " + ITEM_AUTHOR + '\n' +
"Date: Sun, 30 Sep 2018 19:34:25 +0000\n" +
"Link: " + ITEM_LINK + '\n' +
"Flags: " + ITEM_FLAGS_RENDERED + '\n' +
"Podcast Download URL: " + ITEM_ENCLOSURE_URL + '\n' +
" \n" +
ITEM_DESCRIPTON_RENDERED + '\n';
REQUIRE(result == expected);
}
SECTION("Item with an enclosure that has a MIME type") {
item->set_description(ITEM_DESCRIPTON);
item->set_enclosure_url(ITEM_ENCLOSURE_URL);
item->set_enclosure_type(ITEM_ENCLOSURE_TYPE);
const auto result = item_renderer::to_plain_text(cfg, item);
const auto expected = std::string() +
"Feed: " + FEED_TITLE + '\n' +
"Title: " + ITEM_TITLE + '\n' +
"Author: " + ITEM_AUTHOR + '\n' +
"Date: Sun, 30 Sep 2018 19:34:25 +0000\n" +
"Link: " + ITEM_LINK + '\n' +
"Flags: " + ITEM_FLAGS_RENDERED + '\n' +
"Podcast Download URL: " + ITEM_ENCLOSURE_URL
+ " (type: " + ITEM_ENCLOSURE_TYPE + ")\n" +
" \n" +
ITEM_DESCRIPTON_RENDERED + '\n';
REQUIRE(result == expected);
}
SECTION("Item with some links in the description") {
item->set_description(
ITEM_DESCRIPTON +
"<p>See also <a href='https://example.com'>this site</a>.</p>");
const auto result = item_renderer::to_plain_text(cfg, item);
const auto expected = std::string() +
"Feed: " + FEED_TITLE + '\n' +
"Title: " + ITEM_TITLE + '\n' +
"Author: " + ITEM_AUTHOR + '\n' +
"Date: Sun, 30 Sep 2018 19:34:25 +0000\n" +
"Link: " + ITEM_LINK + '\n' +
"Flags: " + ITEM_FLAGS_RENDERED + '\n' +
" \n" +
ITEM_DESCRIPTON_RENDERED + '\n' +
" \n" +
"See also this site[1].\n" +
" \n" +
"Links: \n" +
"[1]: https://example.com/ (link)\n";
REQUIRE(result == expected);
}
}
TEST_CASE("item_renderer::to_plain_text() renders text to the width specified "
"in `text-width` setting",
"[item_renderer]")
{
TestHelpers::EnvVar tzEnv("TZ");
tzEnv.set("UTC");
ConfigContainer cfg;
Cache rsscache(":memory:", &cfg);
std::shared_ptr<RssItem> item;
std::shared_ptr<RssFeed> feed;
std::tie(item, feed) = create_test_item(&rsscache);
item->set_description(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
"Pellentesque nisl massa, luctus ut ligula vitae, suscipit tempus "
"velit. Vivamus sodales, quam in convallis posuere, libero nisi "
"ultricies orci, nec lobortis.");
const auto header = std::string() +
"Feed: " + FEED_TITLE + '\n' +
"Title: " + ITEM_TITLE + '\n' +
"Author: " + ITEM_AUTHOR + '\n' +
"Date: Sun, 30 Sep 2018 19:34:25 +0000\n" +
"Link: " + ITEM_LINK + '\n' +
"Flags: " + ITEM_FLAGS_RENDERED + '\n' +
" \n";
SECTION("If `text-width` is not set, text is rendered in 80 columns") {
const auto result = item_renderer::to_plain_text(cfg, item);
const auto expected = header +
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque nisl \n" +
"massa, luctus ut ligula vitae, suscipit tempus velit. Vivamus sodales, quam in \n" +
"convallis posuere, libero nisi ultricies orci, nec lobortis.\n";
REQUIRE(result == expected);
}
SECTION("If `text-width` is set to zero, text is rendered in 80 columns") {
cfg.set_configvalue("text-width", "0");
const auto result = item_renderer::to_plain_text(cfg, item);
const auto expected = header +
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque nisl \n" +
"massa, luctus ut ligula vitae, suscipit tempus velit. Vivamus sodales, quam in \n" +
"convallis posuere, libero nisi ultricies orci, nec lobortis.\n";
REQUIRE(result == expected);
}
SECTION("Text is rendered in 37 columns") {
cfg.set_configvalue("text-width", "37");
const auto result = item_renderer::to_plain_text(cfg, item);
const auto expected = header +
"Lorem ipsum dolor sit amet, \n"
"consectetur adipiscing elit. \n"
"Pellentesque nisl massa, luctus ut \n"
"ligula vitae, suscipit tempus velit. \n"
"Vivamus sodales, quam in convallis \n"
"posuere, libero nisi ultricies orci, \n"
"nec lobortis.\n";
REQUIRE(result == expected);
}
SECTION("Text is rendered in 120 columns") {
cfg.set_configvalue("text-width", "120");
const auto result = item_renderer::to_plain_text(cfg, item);
const auto expected = header +
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
"Pellentesque nisl massa, luctus ut ligula vitae, suscipit \n"
"tempus velit. Vivamus sodales, quam in convallis posuere, libero "
"nisi ultricies orci, nec lobortis.\n";
REQUIRE(result == expected);
}
}
TEST_CASE("Empty fields are not rendered", "[item_renderer]")
{
TestHelpers::EnvVar tzEnv("TZ");
tzEnv.set("UTC");
ConfigContainer cfg;
// item_renderer uses that setting, so let's fix its value to make the test
// reproducible
cfg.set_configvalue("text-width", "80");
Cache rsscache(":memory:", &cfg);
std::shared_ptr<RssItem> item;
std::shared_ptr<RssFeed> feed;
std::tie(item, feed) = create_test_item(&rsscache);
SECTION("Item without a feed title") {
item->get_feedptr()->set_title("");
const auto result = item_renderer::to_plain_text(cfg, item);
const auto expected = std::string() +
"Title: " + ITEM_TITLE + '\n' +
"Author: " + ITEM_AUTHOR + '\n' +
"Date: Sun, 30 Sep 2018 19:34:25 +0000\n" +
"Link: " + ITEM_LINK + '\n' +
"Flags: " + ITEM_FLAGS_RENDERED + '\n' +
" \n";
REQUIRE(result == expected);
}
SECTION("Item without a title") {
item->set_title("");
const auto result = item_renderer::to_plain_text(cfg, item);
const auto expected = std::string() +
"Feed: " + FEED_TITLE + '\n' +
"Author: " + ITEM_AUTHOR + '\n' +
"Date: Sun, 30 Sep 2018 19:34:25 +0000\n" +
"Link: " + ITEM_LINK + '\n' +
"Flags: " + ITEM_FLAGS_RENDERED + '\n' +
" \n";
REQUIRE(result == expected);
}
SECTION("Item without an author") {
item->set_author("");
const auto result = item_renderer::to_plain_text(cfg, item);
const auto expected = std::string() +
"Feed: " + FEED_TITLE + '\n' +
"Title: " + ITEM_TITLE + '\n' +
"Date: Sun, 30 Sep 2018 19:34:25 +0000\n" +
"Link: " + ITEM_LINK + '\n' +
"Flags: " + ITEM_FLAGS_RENDERED + '\n' +
" \n";
REQUIRE(result == expected);
}
SECTION("Item without a link") {
item->set_link("");
const auto result = item_renderer::to_plain_text(cfg, item);
const auto expected = std::string() +
"Feed: " + FEED_TITLE + '\n' +
"Title: " + ITEM_TITLE + '\n' +
"Author: " + ITEM_AUTHOR + '\n' +
"Date: Sun, 30 Sep 2018 19:34:25 +0000\n" +
"Flags: " + ITEM_FLAGS_RENDERED + '\n' +
" \n";
REQUIRE(result == expected);
}
SECTION("Item without an enclosure") {
item->set_description(ITEM_DESCRIPTON);
const auto result = item_renderer::to_plain_text(cfg, item);
const auto expected = std::string() +
"Feed: " + FEED_TITLE + '\n' +
"Title: " + ITEM_TITLE + '\n' +
"Author: " + ITEM_AUTHOR + '\n' +
"Date: Sun, 30 Sep 2018 19:34:25 +0000\n" +
"Link: " + ITEM_LINK + '\n' +
"Flags: " + ITEM_FLAGS_RENDERED + '\n' +
" \n" +
ITEM_DESCRIPTON_RENDERED + '\n';
REQUIRE(result == expected);
}
SECTION("Item without flags") {
item->set_flags("");
const auto result = item_renderer::to_plain_text(cfg, item);
const auto expected = std::string() +
"Feed: " + FEED_TITLE + '\n' +
"Title: " + ITEM_TITLE + '\n' +
"Author: " + ITEM_AUTHOR + '\n' +
"Date: Sun, 30 Sep 2018 19:34:25 +0000\n" +
"Link: " + ITEM_LINK + '\n' +
" \n";
REQUIRE(result == expected);
}
}
TEST_CASE("item_renderer::to_plain_text honours `html-renderer` setting",
"[item_renderer][broken]")
{
TestHelpers::EnvVar tzEnv("TZ");
tzEnv.set("UTC");
ConfigContainer cfg;
cfg.set_configvalue("text-width", "80");
Cache rsscache(":memory:", &cfg);
std::shared_ptr<RssItem> item;
std::shared_ptr<RssFeed> feed;
std::tie(item, feed) = create_test_item(&rsscache);
SECTION("Single-paragraph description") {
const auto description = std::string() +
"<p>Hello, world! Check out "
"<a href='https://example.com'>our site</a>.</p>";
item->set_description(description);
SECTION("internal renderer") {
cfg.set_configvalue("html-renderer", "internal");
const auto result = item_renderer::to_plain_text(cfg, item);
const auto expected = std::string() +
"Feed: " + FEED_TITLE + '\n' +
"Title: " + ITEM_TITLE + '\n' +
"Author: " + ITEM_AUTHOR + '\n' +
"Date: Sun, 30 Sep 2018 19:34:25 +0000\n" +
"Link: " + ITEM_LINK + '\n' +
"Flags: " + ITEM_FLAGS_RENDERED + '\n' +
" \n" +
"Hello, world! Check out our site[1].\n" +
" \n" +
"Links: \n" +
"[1]: https://example.com/ (link)\n";
REQUIRE(result == expected);
}
// cat is pretty much guaranteed to be present in any Unix-like
// environment, so let's use that instead of the less common w3m.
SECTION("/bin/cat as a renderer") {
cfg.set_configvalue("html-renderer", "/bin/cat");
const auto result = item_renderer::to_plain_text(cfg, item);
const auto expected = std::string() +
"Feed: " + FEED_TITLE + '\n' +
"Title: " + ITEM_TITLE + '\n' +
"Author: " + ITEM_AUTHOR + '\n' +
"Date: Sun, 30 Sep 2018 19:34:25 +0000\n" +
"Link: " + ITEM_LINK + '\n' +
"Flags: " + ITEM_FLAGS_RENDERED + '\n' +
" \n" +
description + '\n';
REQUIRE(result == expected);
}
}
SECTION("Multi-paragraph description") {
const auto description = std::string() +
"<p>Hello, world!</p>\n\n"
"<p>Check out <a href='https://example.com'>our site</a>.</p>";
item->set_description(description);
SECTION("internal renderer") {
cfg.set_configvalue("html-renderer", "internal");
const auto result = item_renderer::to_plain_text(cfg, item);
const auto expected = std::string() +
"Feed: " + FEED_TITLE + '\n' +
"Title: " + ITEM_TITLE + '\n' +
"Author: " + ITEM_AUTHOR + '\n' +
"Date: Sun, 30 Sep 2018 19:34:25 +0000\n" +
"Link: " + ITEM_LINK + '\n' +
"Flags: " + ITEM_FLAGS_RENDERED + '\n' +
" \n" +
"Hello, world!\n" +
" \n" +
"Check out our site[1].\n" +
" \n" +
"Links: \n" +
"[1]: https://example.com/ (link)\n";
REQUIRE(result == expected);
}
// cat is pretty much guaranteed to be present in any Unix-like
// environment, so let's use that instead of the less common w3m.
SECTION("/bin/cat as a renderer") {
cfg.set_configvalue("html-renderer", "/bin/cat");
const auto result = item_renderer::to_plain_text(cfg, item);
const auto expected = std::string() +
"Feed: " + FEED_TITLE + '\n' +
"Title: " + ITEM_TITLE + '\n' +
"Author: " + ITEM_AUTHOR + '\n' +
"Date: Sun, 30 Sep 2018 19:34:25 +0000\n" +
"Link: " + ITEM_LINK + '\n' +
"Flags: " + ITEM_FLAGS_RENDERED + '\n' +
" \n" +
"<p>Hello, world!</p>\n" +
" \n" +
"<p>Check out <a href='https://example.com'>our site</a>.</p>\n";
REQUIRE(result == expected);
}
}
}
TEST_CASE("item_renderer::get_feedtitle() returns item's feed title without "
"soft hyphens if that's available",
"[item_renderer]")
{
ConfigContainer cfg;
Cache rsscache(":memory:", &cfg);
std::shared_ptr<RssItem> item;
std::shared_ptr<RssFeed> feed;
std::tie(item, feed) = create_test_item(&rsscache);
SECTION("Title without soft hyphens") {
feed->set_title("Welcome, lovely strangers!");
}
SECTION("Title containing soft hyphens") {
feed->set_title("Wel\u00ADcome, lo\u00ADve\u00ADly stran\u00ADgers!");
}
const auto result = item_renderer::get_feedtitle(item);
REQUIRE(result == "Welcome, lovely strangers!");
}
TEST_CASE("item_renderer::get_feedtitle() returns item's feed self-link "
"if its title is empty",
"[item_renderer]")
{
ConfigContainer cfg;
Cache rsscache(":memory:", &cfg);
std::shared_ptr<RssItem> item;
std::shared_ptr<RssFeed> feed;
std::tie(item, feed) = create_test_item(&rsscache);
const auto feedlink = std::string("https://rss.example.com/~joe/");
feed->set_title("");
feed->set_link(feedlink);
const auto result = item_renderer::get_feedtitle(item);
REQUIRE(result == feedlink);
}
TEST_CASE("item_renderer::get_feedtitle() returns item's feed URL "
"if both the title and self-link are empty",
"[item_renderer]")
{
ConfigContainer cfg;
Cache rsscache(":memory:", &cfg);
std::shared_ptr<RssItem> item;
std::shared_ptr<RssFeed> feed;
std::tie(item, feed) = create_test_item(&rsscache);
const auto feedurl = std::string("https://example.com/~joe/entries.rss");
feed->set_title("");
feed->set_link("");
feed->set_rssurl(feedurl);
const auto result = item_renderer::get_feedtitle(item);
REQUIRE(result == feedurl);
}
|