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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
|
#define ENABLE_IMPLICIT_FILEPATH_CONVERSIONS
#include "itemlistformaction.h"
#include <fstream>
#include <tuple>
#include <unistd.h>
#include "3rd-party/catch.hpp"
#include "cache.h"
#include "configpaths.h"
#include "controller.h"
#include "itemlist.h"
#include "keymap.h"
#include "regexmanager.h"
#include "rssfeed.h"
#include "test_helpers/misc.h"
#include "test_helpers/tempfile.h"
using namespace newsboat;
TEST_CASE("OP_OPEN displays article using an external pager",
"[ItemListFormAction]")
{
ConfigPaths paths;
Controller c(paths);
newsboat::View v(&c);
test_helpers::TempFile pagerfile;
const std::string test_url = "http://test_url";
std::string test_title = "Article Title";
std::string test_author = "Article Author";
std::string test_description = "Article Description";
time_t test_pubDate = 42;
char test_pubDate_str[128];
strftime(test_pubDate_str,
sizeof(test_pubDate_str),
"%a, %d %b %Y %H:%M:%S %z",
localtime(&test_pubDate));
ConfigContainer cfg;
FilterContainer filters;
RegexManager rxman;
auto rsscache = Cache::in_memory(cfg);
cfg.set_configvalue("pager", "cat %f > " + pagerfile.get_path());
std::shared_ptr<RssFeed> feed = std::make_shared<RssFeed>(rsscache.get(), "");
std::shared_ptr<RssItem> item = std::make_shared<RssItem>(rsscache.get());
item->set_link(test_url);
item->set_title(test_title);
item->set_author(test_author);
item->set_description(test_description, "text/plain");
item->set_pubDate(test_pubDate);
item->set_unread(true);
feed->add_item(item);
v.set_config_container(&cfg);
c.set_view(&v);
ItemListFormAction itemlist(v, itemlist_str, rsscache.get(), filters, &cfg, rxman);
itemlist.set_feed(feed);
const std::vector<std::string> args;
REQUIRE_NOTHROW(itemlist.process_op(OP_OPEN, args));
test_helpers::assert_article_file_content(pagerfile.get_path(),
test_title,
test_author,
test_pubDate_str,
test_url,
test_description);
}
TEST_CASE("OP_PURGE_DELETED purges previously deleted items",
"[ItemListFormAction]")
{
ConfigPaths paths;
Controller c(paths);
newsboat::View v(&c);
ConfigContainer cfg;
auto rsscache = Cache::in_memory(cfg);
FilterContainer filters;
RegexManager rxman;
std::shared_ptr<RssFeed> feed = std::make_shared<RssFeed>(rsscache.get(), "");
std::shared_ptr<RssItem> item = std::make_shared<RssItem>(rsscache.get());
feed->add_item(item);
v.set_config_container(&cfg);
c.set_view(&v);
ItemListFormAction itemlist(v, itemlist_str, rsscache.get(), filters, &cfg, rxman);
itemlist.set_feed(feed);
SECTION("No items to purge") {
const std::vector<std::string> args;
REQUIRE_NOTHROW(itemlist.process_op(OP_PURGE_DELETED, args));
REQUIRE(feed->total_item_count() == 1);
}
SECTION("Deleted items are purged") {
item->set_deleted(true);
const std::vector<std::string> args;
REQUIRE_NOTHROW(itemlist.process_op(OP_PURGE_DELETED, args));
REQUIRE(feed->total_item_count() == 0);
}
}
TEST_CASE(
"OP_OPENBROWSER_AND_MARK passes the url to the browser and marks read",
"[ItemListFormAction]")
{
ConfigPaths paths;
Controller c(paths);
newsboat::View v(&c);
test_helpers::TempFile browserfile;
const std::string test_url = "http://test_url";
std::string line;
ConfigContainer cfg;
cfg.set_configvalue("browser", "echo %u >> " + browserfile.get_path());
auto rsscache = Cache::in_memory(cfg);
FilterContainer filters;
RegexManager rxman;
std::shared_ptr<RssFeed> feed = std::make_shared<RssFeed>(rsscache.get(), "");
std::shared_ptr<RssItem> item = std::make_shared<RssItem>(rsscache.get());
item->set_link(test_url);
item->set_unread(true);
feed->add_item(item);
v.set_config_container(&cfg);
c.set_view(&v);
ItemListFormAction itemlist(v, itemlist_str, rsscache.get(), filters, &cfg, rxman);
itemlist.set_feed(feed);
const std::vector<std::string> args;
itemlist.process_op(OP_OPENBROWSER_AND_MARK, args);
std::ifstream browserFileStream(browserfile.get_path());
REQUIRE(std::getline(browserFileStream, line));
REQUIRE(line == test_url);
REQUIRE(feed->unread_item_count() == 0);
}
TEST_CASE(
"OP_OPENBROWSER_AND_MARK does not mark read when browser fails",
"[ItemListFormAction]")
{
ConfigPaths paths;
Controller c(paths);
newsboat::View v(&c);
const std::string test_url = "http://test_url";
ConfigContainer cfg;
cfg.set_configvalue("browser", "false %u");
auto rsscache = Cache::in_memory(cfg);
FilterContainer filters;
RegexManager rxman;
std::shared_ptr<RssFeed> feed = std::make_shared<RssFeed>(rsscache.get(), "");
std::shared_ptr<RssItem> item = std::make_shared<RssItem>(rsscache.get());
item->set_link(test_url);
item->set_unread(true);
feed->add_item(item);
v.set_config_container(&cfg);
c.set_view(&v);
ItemListFormAction itemlist(v, itemlist_str, rsscache.get(), filters, &cfg, rxman);
itemlist.set_feed(feed);
const std::vector<std::string> args;
itemlist.process_op(OP_OPENBROWSER_AND_MARK, args);
REQUIRE(feed->unread_item_count() == 1);
}
TEST_CASE("OP_OPENINBROWSER passes the url to the browser",
"[ItemListFormAction]")
{
ConfigPaths paths;
Controller c(paths);
newsboat::View v(&c);
test_helpers::TempFile browserfile;
const std::string test_url = "http://test_url";
std::string line;
ConfigContainer cfg;
cfg.set_configvalue("browser", "echo %u >> " + browserfile.get_path());
auto rsscache = Cache::in_memory(cfg);
FilterContainer filters;
RegexManager rxman;
std::shared_ptr<RssFeed> feed = std::make_shared<RssFeed>(rsscache.get(), "");
std::shared_ptr<RssItem> item = std::make_shared<RssItem>(rsscache.get());
item->set_link(test_url);
feed->add_item(item);
v.set_config_container(&cfg);
c.set_view(&v);
ItemListFormAction itemlist(v, itemlist_str, rsscache.get(), filters, &cfg, rxman);
itemlist.set_feed(feed);
const std::vector<std::string> args;
itemlist.process_op(OP_OPENINBROWSER, args);
std::ifstream browserFileStream(browserfile.get_path());
REQUIRE(std::getline(browserFileStream, line));
REQUIRE(line == test_url);
}
TEST_CASE("OP_OPENINBROWSER_NONINTERACTIVE passes the url to the browser",
"[ItemListFormAction]")
{
ConfigPaths paths;
Controller c(paths);
newsboat::View v(&c);
test_helpers::TempFile browserfile;
const std::string test_url = "http://test_url";
std::string line;
ConfigContainer cfg;
cfg.set_configvalue("browser", "echo %u >> " + browserfile.get_path());
auto rsscache = Cache::in_memory(cfg);
FilterContainer filters;
RegexManager rxman;
std::shared_ptr<RssFeed> feed = std::make_shared<RssFeed>(rsscache.get(), "");
std::shared_ptr<RssItem> item = std::make_shared<RssItem>(rsscache.get());
item->set_link(test_url);
feed->add_item(item);
v.set_config_container(&cfg);
c.set_view(&v);
ItemListFormAction itemlist(v, itemlist_str, rsscache.get(), filters, &cfg, rxman);
itemlist.set_feed(feed);
const std::vector<std::string> args;
itemlist.process_op(newsboat::OP_OPENINBROWSER_NONINTERACTIVE, args);
std::ifstream browserFileStream(browserfile.get_path());
REQUIRE(std::getline(browserFileStream, line));
REQUIRE(line == test_url);
}
TEST_CASE("OP_OPENALLUNREADINBROWSER passes the url list to the browser",
"[ItemListFormAction]")
{
ConfigPaths paths;
Controller c(paths);
newsboat::View v(&c);
test_helpers::TempFile browserfile;
std::unordered_set<std::string> url_set;
const std::string test_url = "http://test_url";
std::string line;
int itemCount = 6;
ConfigContainer cfg;
cfg.set_configvalue("browser", "echo %u >> " + browserfile.get_path());
auto rsscache = Cache::in_memory(cfg);
FilterContainer filters;
RegexManager rxman;
std::shared_ptr<RssFeed> feed = std::make_shared<RssFeed>(rsscache.get(), "");
for (int i = 0; i < itemCount; i++) {
std::shared_ptr<RssItem> item =
std::make_shared<RssItem>(rsscache.get());
item->set_link(test_url + std::to_string(i));
url_set.insert(test_url + std::to_string(i));
item->set_unread(true);
feed->add_item(item);
}
v.set_config_container(&cfg);
c.set_view(&v);
ItemListFormAction itemlist(v, itemlist_str, rsscache.get(), filters, &cfg, rxman);
itemlist.set_feed(feed);
SECTION("unread >= max-browser-tabs") {
int maxItemsToOpen = 4;
int openedItemsCount = 0;
cfg.set_configvalue(
"max-browser-tabs", std::to_string(maxItemsToOpen));
const std::vector<std::string> args;
itemlist.process_op(OP_OPENALLUNREADINBROWSER, args);
std::ifstream browserFileStream(browserfile.get_path());
openedItemsCount = 0;
if (browserFileStream.is_open()) {
while (std::getline(browserFileStream, line)) {
INFO("Each URL should be present exactly once. "
"Erase urls after first match to fail if "
"an item opens twice.");
REQUIRE(url_set.count(line) == 1);
url_set.erase(url_set.find(line));
openedItemsCount += 1;
}
}
REQUIRE(openedItemsCount == maxItemsToOpen);
}
SECTION("unread < max-browser-tabs") {
int maxItemsToOpen = 9;
int openedItemsCount = 0;
cfg.set_configvalue(
"max-browser-tabs", std::to_string(maxItemsToOpen));
const std::vector<std::string> args;
itemlist.process_op(OP_OPENALLUNREADINBROWSER, args);
std::ifstream browserFileStream(browserfile.get_path());
if (browserFileStream.is_open()) {
while (std::getline(browserFileStream, line)) {
INFO("Each URL should be present exactly once. "
"Erase urls after first match to fail if "
"an item opens twice.");
REQUIRE(url_set.count(line) == 1);
url_set.erase(url_set.find(line));
openedItemsCount += 1;
}
}
REQUIRE(openedItemsCount == itemCount);
}
}
TEST_CASE(
"OP_OPENALLUNREADINBROWSER_AND_MARK passes the url list to the browser "
"and marks them read",
"[ItemListFormAction]")
{
ConfigPaths paths;
Controller c(paths);
newsboat::View v(&c);
test_helpers::TempFile browserfile;
std::unordered_set<std::string> url_set;
const std::string test_url = "http://test_url";
std::string line;
const unsigned int itemCount = 6;
ConfigContainer cfg;
cfg.set_configvalue("browser", "echo %u >> " + browserfile.get_path());
auto rsscache = Cache::in_memory(cfg);
FilterContainer filters;
RegexManager rxman;
std::shared_ptr<RssFeed> feed = std::make_shared<RssFeed>(rsscache.get(), "");
for (unsigned int i = 0; i < itemCount; i++) {
std::shared_ptr<RssItem> item =
std::make_shared<RssItem>(rsscache.get());
item->set_link(test_url + std::to_string(i));
url_set.insert(test_url + std::to_string(i));
item->set_unread(true);
feed->add_item(item);
}
v.set_config_container(&cfg);
c.set_view(&v);
ItemListFormAction itemlist(v, itemlist_str, rsscache.get(), filters, &cfg, rxman);
itemlist.set_feed(feed);
SECTION("unread >= max-browser-tabs") {
const unsigned int maxItemsToOpen = 4;
unsigned int openedItemsCount = 0;
cfg.set_configvalue(
"max-browser-tabs", std::to_string(maxItemsToOpen));
const std::vector<std::string> args;
itemlist.process_op(OP_OPENALLUNREADINBROWSER_AND_MARK, args);
std::ifstream browserFileStream(browserfile.get_path());
if (browserFileStream.is_open()) {
while (std::getline(browserFileStream, line)) {
INFO("Each URL should be present exactly once. "
"Erase urls after first match to fail if "
"an item opens twice.");
REQUIRE(url_set.count(line) == 1);
url_set.erase(url_set.find(line));
openedItemsCount += 1;
}
}
REQUIRE(openedItemsCount == maxItemsToOpen);
REQUIRE(feed->unread_item_count() ==
itemCount - maxItemsToOpen);
}
SECTION("unread < max-browser-tabs") {
int maxItemsToOpen = 9;
int openedItemsCount = 0;
cfg.set_configvalue(
"max-browser-tabs", std::to_string(maxItemsToOpen));
const std::vector<std::string> args;
itemlist.process_op(OP_OPENALLUNREADINBROWSER_AND_MARK, args);
std::ifstream browserFileStream(browserfile.get_path());
if (browserFileStream.is_open()) {
while (std::getline(browserFileStream, line)) {
INFO("Each URL should be present exactly once. "
"Erase urls after first match to fail if "
"an item opens twice.");
REQUIRE(url_set.count(line) == 1);
url_set.erase(url_set.find(line));
openedItemsCount += 1;
}
}
REQUIRE(openedItemsCount == itemCount);
REQUIRE(feed->unread_item_count() == 0);
}
}
TEST_CASE("OP_SHOWURLS shows the article's properties", "[ItemListFormAction]")
{
ConfigPaths paths;
Controller c(paths);
newsboat::View v(&c);
ConfigContainer cfg;
auto rsscache = Cache::in_memory(cfg);
FilterContainer filters;
RegexManager rxman;
test_helpers::TempFile urlFile;
const std::string test_url = "http://test_url";
std::string test_title = "Article Title";
std::string test_author = "Article Author";
std::string test_description = "Article Description";
time_t test_pubDate = 42;
char test_pubDate_str[128];
strftime(test_pubDate_str,
sizeof(test_pubDate_str),
"%a, %d %b %Y %H:%M:%S %z",
localtime(&test_pubDate));
v.set_config_container(&cfg);
c.set_view(&v);
std::shared_ptr<RssFeed> feed = std::make_shared<RssFeed>(rsscache.get(), "");
std::shared_ptr<RssItem> item = std::make_shared<RssItem>(rsscache.get());
item->set_link(test_url);
item->set_title(test_title);
item->set_author(test_author);
item->set_description(test_description, "text/plain");
item->set_pubDate(test_pubDate);
ItemListFormAction itemlist(v, itemlist_str, rsscache.get(), filters, &cfg, rxman);
SECTION("with external-url-viewer") {
feed->add_item(item);
itemlist.set_feed(feed);
cfg.set_configvalue(
"external-url-viewer", "tee > " + urlFile.get_path());
const std::vector<std::string> args;
REQUIRE_NOTHROW(itemlist.process_op(OP_SHOWURLS, args));
test_helpers::assert_article_file_content(urlFile.get_path(),
test_title,
test_author,
test_pubDate_str,
test_url,
test_description);
}
SECTION("internal url viewer") {
feed->add_item(item);
itemlist.set_feed(feed);
const std::vector<std::string> args;
REQUIRE_NOTHROW(itemlist.process_op(OP_SHOWURLS, args));
}
SECTION("no feed in formaction") {
const std::vector<std::string> args;
REQUIRE_NOTHROW(itemlist.process_op(OP_SHOWURLS, args));
}
}
TEST_CASE("OP_BOOKMARK pipes articles url and title to bookmark-command",
"[ItemListFormAction]")
{
ConfigPaths paths;
Controller c(paths);
newsboat::View v(&c);
ConfigContainer cfg;
auto rsscache = Cache::in_memory(cfg);
FilterContainer filters;
RegexManager rxman;
test_helpers::TempFile bookmarkFile;
std::string line;
std::vector<std::string> bookmark_args;
const std::string test_url = "http://test_url";
std::string test_title = "Article Title";
std::string feed_title = "Feed Title";
std::string separator = " ";
std::string extra_arg = "extra arg";
v.set_config_container(&cfg);
c.set_view(&v);
std::shared_ptr<RssFeed> feed = std::make_shared<RssFeed>(rsscache.get(), "");
feed->set_title(feed_title);
std::shared_ptr<RssItem> item = std::make_shared<RssItem>(rsscache.get());
item->set_link(test_url);
item->set_title(test_title);
ItemListFormAction itemlist(v, itemlist_str, rsscache.get(), filters, &cfg, rxman);
feed->add_item(item);
itemlist.set_feed(feed);
cfg.set_configvalue(
"bookmark-cmd", "echo > " + bookmarkFile.get_path());
bookmark_args.push_back(extra_arg);
auto checkOutput = [&] {
std::ifstream browserFileStream(bookmarkFile.get_path());
REQUIRE(std::getline(browserFileStream, line));
REQUIRE(line ==
test_url + separator + test_title + separator + extra_arg +
separator + feed_title);
};
SECTION("Macro") {
REQUIRE_NOTHROW(itemlist.process_op(OP_BOOKMARK, bookmark_args, BindingType::Macro));
checkOutput();
}
SECTION("Bind") {
REQUIRE_NOTHROW(itemlist.process_op(OP_BOOKMARK, bookmark_args, BindingType::Bind));
checkOutput();
}
}
TEST_CASE("OP_EDITFLAGS arguments are added to an item's flags",
"[ItemListFormAction]")
{
ConfigPaths paths;
Controller c(paths);
newsboat::View v(&c);
ConfigContainer cfg;
auto rsscache = Cache::in_memory(cfg);
FilterContainer filters;
RegexManager rxman;
std::vector<std::string> op_args;
v.set_config_container(&cfg);
c.set_view(&v);
std::shared_ptr<RssFeed> feed = std::make_shared<RssFeed>(rsscache.get(), "");
std::shared_ptr<RssItem> item = std::make_shared<RssItem>(rsscache.get());
ItemListFormAction itemlist(v, itemlist_str, rsscache.get(), filters, &cfg, rxman);
feed->add_item(item);
itemlist.set_feed(feed);
// <name, input, expected_result>
std::vector<std::tuple<std::string, std::string, std::string>> tests {
std::make_tuple("Single flag", "G", "G"),
std::make_tuple("Unordered flags", "abdefc", "abcdef"),
std::make_tuple("Duplicate flag in argument", "Abdddd", "Abd"),
std::make_tuple("Unauthorized values in arguments: Numbers", "Abd1236", "Abd"),
std::make_tuple("Unauthorized values in arguments: Symbolds", "Abd%^\\*;\'\"&~#{([-|`_/@)]=}$£€µ,;:!?./§", "Abd"),
std::make_tuple("Unauthorized values in arguments: Accents", "Abd¨^", "Abd"),
std::make_tuple("All possible flags at once", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"),
};
SECTION("Macro") {
for (const auto& testCase : tests) {
DYNAMIC_SECTION("Test " << std::get<0>(testCase)) {
std::vector<std::string> args = op_args;
args.push_back(std::get<1>(testCase));
REQUIRE_NOTHROW( itemlist.process_op(OP_EDITFLAGS, args, BindingType::Macro));
REQUIRE(item->flags() == std::get<2>(testCase));
}
}
}
SECTION("Bind") {
for (const auto& testCase : tests) {
DYNAMIC_SECTION("Test " << std::get<0>(testCase)) {
std::vector<std::string> args = op_args;
args.push_back(std::get<1>(testCase));
REQUIRE_NOTHROW( itemlist.process_op(OP_EDITFLAGS, args, BindingType::Bind));
REQUIRE(item->flags() == std::get<2>(testCase));
}
}
}
}
TEST_CASE("OP_SAVE writes an article's attributes to the specified file",
"[ItemListFormAction]")
{
ConfigPaths paths;
Controller c(paths);
newsboat::View v(&c);
test_helpers::TempFile saveFile;
ConfigContainer cfg;
auto rsscache = Cache::in_memory(cfg);
FilterContainer filters;
RegexManager rxman;
std::vector<std::string> op_args;
op_args.push_back(saveFile.get_path());
const std::string test_url = "http://test_url";
std::string test_title = "Article Title";
std::string test_author = "Article Author";
std::string test_description = "Article Description";
time_t test_pubDate = 42;
char test_pubDate_str[128];
strftime(test_pubDate_str,
sizeof(test_pubDate_str),
"%a, %d %b %Y %H:%M:%S %z",
localtime(&test_pubDate));
v.set_config_container(&cfg);
c.set_view(&v);
std::shared_ptr<RssFeed> feed = std::make_shared<RssFeed>(rsscache.get(), "");
std::shared_ptr<RssItem> item = std::make_shared<RssItem>(rsscache.get());
item->set_link(test_url);
item->set_title(test_title);
item->set_author(test_author);
item->set_pubDate(test_pubDate);
item->set_description(test_description, "text/plain");
ItemListFormAction itemlist(v, itemlist_str, rsscache.get(), filters, &cfg, rxman);
feed->add_item(item);
itemlist.set_feed(feed);
auto check = [&] {
test_helpers::assert_article_file_content(saveFile.get_path(),
test_title,
test_author,
test_pubDate_str,
test_url,
test_description);
};
SECTION("Macro") {
REQUIRE_NOTHROW(itemlist.process_op(OP_SAVE, op_args, BindingType::Macro));
check();
}
SECTION("Bind") {
REQUIRE_NOTHROW(itemlist.process_op(OP_SAVE, op_args, BindingType::Bind));
check();
}
}
TEST_CASE("OP_HELP command is processed", "[ItemListFormAction]")
{
ConfigPaths paths;
Controller c(paths);
newsboat::View v(&c);
ConfigContainer cfg;
auto rsscache = Cache::in_memory(cfg);
KeyMap k(KM_NEWSBOAT);
v.set_keymap(&k);
v.set_config_container(&cfg);
c.set_view(&v);
std::shared_ptr<RssFeed> feed = std::make_shared<RssFeed>(rsscache.get(), "");
std::shared_ptr<RssItem> item = std::make_shared<RssItem>(rsscache.get());
feed->add_item(item);
std::shared_ptr<ItemListFormAction> itemlist = v.push_itemlist(feed);
const std::vector<std::string> args;
REQUIRE_NOTHROW(itemlist->process_op(OP_HELP, args));
}
TEST_CASE("OP_HARDQUIT command is processed", "[ItemListFormAction]")
{
ConfigPaths paths;
Controller c(paths);
newsboat::View v(&c);
ConfigContainer cfg;
auto rsscache = Cache::in_memory(cfg);
FilterContainer filters;
RegexManager rxman;
KeyMap k(KM_NEWSBOAT);
v.set_keymap(&k);
v.set_config_container(&cfg);
c.set_view(&v);
std::shared_ptr<RssFeed> feed = std::make_shared<RssFeed>(rsscache.get(), "");
ItemListFormAction itemlist(v, itemlist_str, rsscache.get(), filters, &cfg, rxman);
itemlist.set_feed(feed);
const std::vector<std::string> args;
REQUIRE_NOTHROW(itemlist.process_op(OP_HARDQUIT, args));
}
TEST_CASE("Navigate back and forth using OP_NEXT and OP_PREV",
"[ItemListFormAction]")
{
// We are using the OP_SHOWURLS command to print the current
// article'attibutes to a file, and assert the position was indeed
// updated.
ConfigPaths paths;
Controller c(paths);
test_helpers::TempFile articleFile;
newsboat::View v(&c);
ConfigContainer cfg;
cfg.set_configvalue(
"external-url-viewer", "tee > " + articleFile.get_path());
auto rsscache = Cache::in_memory(cfg);
std::string line;
std::string first_article_title = "First_Article";
std::string second_article_title = "Second_Article";
std::string prefix_title = "Title: ";
KeyMap k(KM_NEWSBOAT);
v.set_keymap(&k);
v.set_config_container(&cfg);
c.set_view(&v);
std::shared_ptr<RssFeed> feed = std::make_shared<RssFeed>(rsscache.get(), "");
std::shared_ptr<RssItem> item = std::make_shared<RssItem>(rsscache.get());
item->set_title(first_article_title);
feed->add_item(item);
std::shared_ptr<RssItem> item2 = std::make_shared<RssItem>(rsscache.get());
item2->set_title(second_article_title);
feed->add_item(item2);
std::shared_ptr<ItemListFormAction> itemlist = v.push_itemlist(feed);
const std::vector<std::string> args;
REQUIRE_NOTHROW(itemlist->process_op(OP_NEXT, args));
itemlist->process_op(OP_SHOWURLS, args);
std::ifstream fileStream(articleFile.get_path());
std::getline(fileStream, line);
REQUIRE(line == prefix_title + second_article_title);
REQUIRE_NOTHROW(itemlist->process_op(OP_PREV, args));
itemlist->process_op(OP_SHOWURLS, args);
fileStream.seekg(0);
std::getline(fileStream, line);
REQUIRE(line == prefix_title + first_article_title);
}
TEST_CASE("OP_TOGGLESHOWREAD switches the value of show-read-articles",
"[ItemListFormAction]")
{
ConfigPaths paths;
Controller c(paths);
newsboat::View v(&c);
ConfigContainer cfg;
auto rsscache = Cache::in_memory(cfg);
KeyMap k(KM_NEWSBOAT);
v.set_keymap(&k);
v.set_config_container(&cfg);
c.set_view(&v);
std::shared_ptr<RssFeed> feed = std::make_shared<RssFeed>(rsscache.get(), "");
std::shared_ptr<RssItem> item = std::make_shared<RssItem>(rsscache.get());
feed->add_item(item);
std::shared_ptr<ItemListFormAction> itemlist = v.push_itemlist(feed);
SECTION("True to False") {
v.get_cfg()->set_configvalue("show-read-articles", "yes");
const std::vector<std::string> args;
REQUIRE_NOTHROW(itemlist->process_op(OP_TOGGLESHOWREAD, args));
REQUIRE_FALSE(v.get_cfg()->get_configvalue_as_bool(
"show-read-articles"));
}
SECTION("False to True") {
v.get_cfg()->set_configvalue("show-read-articles", "no");
const std::vector<std::string> args;
REQUIRE_NOTHROW(itemlist->process_op(OP_TOGGLESHOWREAD, args));
REQUIRE(v.get_cfg()->get_configvalue_as_bool(
"show-read-articles"));
}
}
TEST_CASE("OP_PIPE_TO pipes an article's content to an external command",
"[ItemListFormAction]")
{
ConfigPaths paths;
Controller c(paths);
newsboat::View v(&c);
test_helpers::TempFile articleFile;
ConfigContainer cfg;
auto rsscache = Cache::in_memory(cfg);
FilterContainer filters;
RegexManager rxman;
std::vector<std::string> op_args;
op_args.push_back("tee > " + articleFile.get_path());
const std::string test_url = "http://test_url";
std::string test_title = "Article Title";
std::string test_author = "Article Author";
std::string test_description = "Article Description";
time_t test_pubDate = 42;
char test_pubDate_str[128];
strftime(test_pubDate_str,
sizeof(test_pubDate_str),
"%a, %d %b %Y %H:%M:%S %z",
localtime(&test_pubDate));
v.set_config_container(&cfg);
c.set_view(&v);
std::shared_ptr<RssFeed> feed = std::make_shared<RssFeed>(rsscache.get(), "");
std::shared_ptr<RssItem> item = std::make_shared<RssItem>(rsscache.get());
item->set_link(test_url);
item->set_title(test_title);
item->set_author(test_author);
item->set_pubDate(test_pubDate);
item->set_description(test_description, "text/plain");
ItemListFormAction itemlist(v, itemlist_str, rsscache.get(), filters, &cfg, rxman);
feed->add_item(item);
itemlist.set_feed(feed);
auto check = [&] {
test_helpers::assert_article_file_content(articleFile.get_path(),
test_title,
test_author,
test_pubDate_str,
test_url,
test_description);
};
SECTION("Macro") {
REQUIRE_NOTHROW(itemlist.process_op(OP_PIPE_TO, op_args, BindingType::Macro));
check();
}
SECTION("Bind") {
REQUIRE_NOTHROW(itemlist.process_op(OP_PIPE_TO, op_args, BindingType::Bind));
check();
}
}
TEST_CASE("OP_OPENINBROWSER does not result in itemlist invalidation",
"[ItemListFormAction]")
{
ConfigPaths paths;
Controller c(paths);
ConfigContainer cfg;
KeyMap k(KM_NEWSBOAT);
newsboat::View v(&c);
v.set_config_container(&cfg);
v.set_keymap(&k);
auto rsscache = Cache::in_memory(cfg);
FilterContainer filters;
RegexManager rxman;
std::shared_ptr<RssItem> item1 = std::make_shared<RssItem>(rsscache.get());
item1->set_link("https://example.com/1");
std::shared_ptr<RssItem> item2 = std::make_shared<RssItem>(rsscache.get());
item2->set_link("https://example.com/2");
std::shared_ptr<RssItem> item3 = std::make_shared<RssItem>(rsscache.get());
item3->set_link("https://example.com/3");
std::shared_ptr<RssFeed> feed = std::make_shared<RssFeed>(rsscache.get(), "");
feed->add_item(item1);
feed->add_item(item2);
feed->add_item(item3);
cfg.set_configvalue("mark-as-read-on-hover", "yes");
cfg.set_configvalue("show-read-articles", "no");
cfg.set_configvalue("browser", "echo");
SECTION("by default, all items are marked as unread") {
REQUIRE(item1->unread());
REQUIRE(item2->unread());
REQUIRE(item3->unread());
}
// The following sections have some interactions with STFL.
// We call `Stfl::reset()` to make sure the terminal is in a regular mode
// before calling Catch2 functions. Without the reset, Catch2 might output
// text while the terminal is in application mode, which makes it invisble
// when back in regular mode.
// The `View` object calls `Stfl::reset()` in its destructor so we can be
// sure we always return to the regular terminal mode, even when an
// exception is thrown.
SECTION("when entering ItemList, the first item is marked as 'read' due to 'mark-as-read-on-hover'") {
auto itemlist = v.push_itemlist(feed);
itemlist->prepare();
Stfl::reset();
REQUIRE_FALSE(item1->unread());
REQUIRE(item2->unread());
REQUIRE(item3->unread());
}
SECTION("executing 'open-in-browser' operation does not cause the next item to be marked as read") {
auto itemlist = v.push_itemlist(feed);
itemlist->prepare();
const std::vector<std::string> args;
bool success = itemlist->process_op(OP_OPENINBROWSER, args);
Stfl::reset();
REQUIRE(success);
itemlist->prepare();
Stfl::reset();
// Verify that following regression is fixed:
// https://github.com/newsboat/newsboat/issues/1292
REQUIRE_FALSE(item1->unread());
REQUIRE(item2->unread());
REQUIRE(item3->unread());
}
}
|