Add conversion for backwards links
[dueringa_WikiWalker.git] / test / ArticleTests.cpp
blob2b4ff47d5924c8f1a12a69158650e4a28903056e
1 #include <UnitTest++/UnitTest++.h>
3 #include <algorithm>
4 #include <iterator>
5 #include <memory>
6 #include <vector>
8 #include "Article.h"
9 #include "WalkerException.h"
11 SUITE(ArticleTests)
13 using namespace WikiWalker;
15 TEST(Article_GetCountLinks_Uninited_ThrowsException)
17 Article a("Foo");
18 CHECK_EQUAL(false, a.analyzed());
19 CHECK_THROW(a.countLinks(), WalkerException);
22 TEST(Article_GetCountLinks_Inited_ReturnsNull)
24 Article a("Foo");
25 a.analyzed(true);
26 CHECK_EQUAL(true, a.analyzed());
27 CHECK_EQUAL(0, a.countLinks());
30 TEST(Article_AddLinks_OneLinkAdded)
32 Article a("Foo");
34 auto link = std::make_shared<Article>("Barmiz");
35 a.addLink(link);
37 CHECK_EQUAL(true, a.analyzed());
38 CHECK_EQUAL(1, a.countLinks());
41 TEST(Article_AddLinks_DuplicateInstance)
43 Article a("Foo");
45 auto arl = std::make_shared<Article>("Barmiz");
46 CHECK(a.addLink(arl));
47 CHECK(!a.addLink(arl));
49 CHECK_EQUAL(true, a.analyzed());
50 CHECK_EQUAL(1, a.countLinks());
53 TEST(Article_AddLinks_WhenLinkExpiredToNulltpr)
55 Article a("Foo");
58 /* Checks for absence of bug with nullptr access when checking for
59 * duplicates */
60 auto arl = std::make_shared<Article>("Barmiz");
61 CHECK(a.addLink(arl));
64 auto arl2 = std::make_shared<Article>("Barmiz");
65 CHECK(a.addLink(arl2));
67 CHECK_EQUAL(a.analyzed(), true);
68 /* this one's actually awkward and is counterintuitive. But since the
69 * shared_ptr went out of scope, it expired. I chose to simply ignore
70 * expired smart pointers / nullptrs inside the link collection for actual
71 * processing purposes. They're still counted, however. */
72 CHECK_EQUAL(2, a.countLinks());
75 TEST(Article_Iterator_Test)
77 Article a("Foo");
79 std::vector<std::string> titles{"Barmiz", "Kodopa", "Minting"};
80 // must keep them in scope
81 std::vector<std::shared_ptr<Article>> articleLinks;
83 std::transform(titles.begin(),
84 titles.end(),
85 std::back_inserter(articleLinks),
86 [](std::string s) { return std::make_shared<Article>(s); });
88 for(const auto& s : articleLinks) {
89 a.addLink(s);
92 CHECK_EQUAL(true, a.analyzed());
93 CHECK_EQUAL(3, a.countLinks());
95 int num = 0;
96 for(auto x = a.linkBegin(); x != a.linkEnd(); x++) {
97 num++;
98 auto atitle = x->lock()->title();
99 auto findpos = std::find(titles.cbegin(), titles.cend(), atitle);
100 bool isFound = (titles.end() != findpos);
101 CHECK(isFound);
104 CHECK_EQUAL(num, titles.size());
107 TEST(Article_Marked_State)
109 Article a("Todo");
110 CHECK_EQUAL(false, a.marked());
111 a.marked(true);
112 CHECK_EQUAL(true, a.marked());
113 a.marked(false);
114 CHECK_EQUAL(false, a.marked());