summaryrefslogtreecommitdiff
path: root/test/itemrenderer.cpp
blob: f56d8d478ef729d0d95d6858e814e5970ffba23b (plain) (blame)
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
#include "itemrenderer.h"

#include "3rd-party/catch.hpp"

#include "cache.h"
#include "configcontainer.h"
#include "regexmanager.h"
#include "rssfeed.h"
#include "test_helpers/envvar.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]")
{
	test_helpers::TzEnvVar tzEnv;
	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, "text/html");

		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, "text/html");
		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, "text/html");
		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>", "text/html");

		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]")
{
	test_helpers::TzEnvVar tzEnv;
	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.", "text/html");

	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);
	}

	SECTION("angle brackets are only counted as one character") {
		cfg.set_configvalue("text-width", "37");

		item->set_description(
			"&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;" // 10 characters
			"&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;" // 10 characters
			"Lorem ipsum dolor sit amet", "text/html");

		const auto result = item_renderer::to_plain_text(cfg, item);

		const auto expected = header +
			"<<<<<<<<<<>>>>>>>>>>Lorem ipsum dolor\n"
			"sit amet\n";

		REQUIRE(result == expected);
	}
}

TEST_CASE("to_plain_text() does not escape '<' and '>' in header and body",
	"[item_renderer]")
{
	test_helpers::TzEnvVar tzEnv;
	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);

	feed->set_title("<" + FEED_TITLE + ">");
	item->set_title("<" + ITEM_TITLE + ">");
	item->set_author("<" + ITEM_AUTHOR + ">");
	item->set_description("&lt;test&gt;", "text/html");

	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" +
		"<test>\n";

	const auto result = item_renderer::to_plain_text(cfg, item);
	REQUIRE(result == expected);
}

TEST_CASE("Empty fields are not rendered", "[item_renderer]")
{
	test_helpers::TzEnvVar tzEnv;
	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, "text/html");

		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]")
{
	test_helpers::TzEnvVar tzEnv;
	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, "text/html");

		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, "text/html");

		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);

	const auto check = [&]() {
		const auto result = item_renderer::get_feedtitle(item);
		REQUIRE(result == "Welcome, lovely strangers!");
	};

	SECTION("Title without soft hyphens") {
		feed->set_title("Welcome, lovely strangers!");
		check();
	}

	SECTION("Title containing soft hyphens") {
		feed->set_title("Wel\u00ADcome, lo\u00ADve\u00ADly stran\u00ADgers!");
		check();
	}
}

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);

	const auto feedurl = std::string("https://example.com/~joe/entries.rss");

	std::shared_ptr<RssFeed> feed = std::make_shared<RssFeed>(&rsscache, feedurl);
	auto item = std::make_shared<RssItem>(&rsscache);
	item->set_feedptr(feed);


	feed->set_title("");
	feed->set_link("");

	const auto result = item_renderer::get_feedtitle(item);
	REQUIRE(result == feedurl);
}

TEST_CASE("Functions used for rendering articles escape '<' into `<>` for use with stfl",
	"[item_renderer]")
{
	test_helpers::TzEnvVar tzEnv;
	tzEnv.set("UTC");

	ConfigContainer cfg;
	RegexManager rxman;

	Cache rsscache(":memory:", &cfg);

	std::shared_ptr<RssItem> item;
	std::shared_ptr<RssFeed> feed;
	std::tie(item, feed) = create_test_item(&rsscache);

	feed->set_title("<" + FEED_TITLE + ">");
	item->set_title("<" + ITEM_TITLE + ">");
	item->set_author("<" + ITEM_AUTHOR + ">");
	item->set_description("<html>&lt;test&gt;</html>", "text/html");

	SECTION("to_stfl_list()") {
		const auto expected = std::string() +
			"{list" +
			"{listitem text:\"Feed: <>" + FEED_TITLE + ">\"}" +
			"{listitem text:\"Title: <>" + ITEM_TITLE + ">\"}" +
			"{listitem text:\"Author: <>" + ITEM_AUTHOR + ">\"}" +
			"{listitem text:\"Date: Sun, 30 Sep 2018 19:34:25 +0000\"}" +
			"{listitem text:\"Link: " + ITEM_LINK + "\"}" +
			"{listitem text:\"Flags: " + ITEM_FLAGS_RENDERED + "\"}" +
			"{listitem text:\" \"}" +
			"{listitem text:\"<>test>\"}" +
			"}";

		std::vector<LinkPair> links;
		const auto result = item_renderer::to_stfl_list(cfg, item, 80, 80, &rxman,
				"article", links);
		REQUIRE(result.first == expected);
	}

	SECTION("source_to_stfl_list()") {
		const auto expected = std::string() +
			"{list" +
			"{listitem text:\"Feed: <>" + FEED_TITLE + ">\"}" +
			"{listitem text:\"Title: <>" + ITEM_TITLE + ">\"}" +
			"{listitem text:\"Author: <>" + ITEM_AUTHOR + ">\"}" +
			"{listitem text:\"Date: Sun, 30 Sep 2018 19:34:25 +0000\"}" +
			"{listitem text:\"Link: " + ITEM_LINK + "\"}" +
			"{listitem text:\"Flags: " + ITEM_FLAGS_RENDERED + "\"}" +
			"{listitem text:\" \"}" +
			"{listitem text:\"<>html>&lt;test&gt;<>/html>\"}" +
			"}";

		std::vector<LinkPair> links;
		const auto result = item_renderer::source_to_stfl_list(item, 80, 80, &rxman,
				"article");
		REQUIRE(result.first == expected);
	}
}

TEST_CASE("item_renderer::render_plaintext() splits text on newlines", "[item_renderer]")
{
	using newsboat::item_renderer::OutputFormat;
	// Verifies that render_plaintext creates the expected lines, all marked as wrappable
	const auto check = [](const std::string input,
	const std::vector<std::string>& expected_lines, OutputFormat format) {
		std::vector<std::pair<LineType, std::string>> output;
		item_renderer::render_plaintext(input, output, format);

		REQUIRE(output.size() == expected_lines.size());
		for (std::size_t i = 0; i < output.size(); ++i) {
			INFO("i: " + std::to_string(i));
			REQUIRE(output[i].first == LineType::wrappable);
			REQUIRE(output[i].second == expected_lines[i]);
		}
	};

	SECTION(R"(regular text is split on \r and \n)") {
		const std::string text =
			"Lorem ipsum dolor sit amet\nconsectetur adipiscing elit\rsed do eiusmod tempor incididunt ut labore\net dolore magna aliqua.";

		check(text, {
			"Lorem ipsum dolor sit amet",
			"consectetur adipiscing elit",
			"sed do eiusmod tempor incididunt ut labore",
			"et dolore magna aliqua."
		}, OutputFormat::StflRichText);
	}

	SECTION("render_plaintext keeps indentation") {
		const std::string text =
			"The following lines are indented:\n    hello\n    this is a test\n\nback to no indentation";

		check(text, {
			"The following lines are indented:",
			"    hello",
			"    this is a test",
			"",
			"back to no indentation"
		}, OutputFormat::StflRichText);
	}

	SECTION(R"(text is split on sequences of \r\n, and on separate \r and \n characters)") {
		const std::string text =
			"Lorem ipsum\r\ndolor sit amet\n\rconsectetur adipiscing elit\rsed do eiusmod tempor incididunt ut labore\net dolore magna\r\n\r\naliqua.";

		check(text, {
			"Lorem ipsum",
			"dolor sit amet",
			"",
			"consectetur adipiscing elit",
			"sed do eiusmod tempor incididunt ut labore",
			"et dolore magna",
			"",
			"aliqua."
		}, OutputFormat::StflRichText);
	}

	SECTION("render_plaintext escapes angle brackets for STFL") {
		const std::string text = "this < should be escaped >\n<p>test text</p>";

		check(text, {
			"this <> should be escaped >",
			"<>p>test text<>/p>"
		}, OutputFormat::StflRichText);
	}

	SECTION("render_plaintext does not escape when rendering towards some plaintext output") {
		const std::string text = "this < should *not* be escaped >\n<p>test text</p>";

		check(text, {
			"this < should *not* be escaped >",
			"<p>test text</p>"
		}, OutputFormat::PlainText);
	}
}