aboutsummaryrefslogtreecommitdiff
path: root/testLibbible.cc
blob: ad51c44157c10dc3d960f17165acde88f8f8aec6 (plain)
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
//#include <libbible.h>
#include "libbible.h"
#include <string>
#include <map>
#include <vector>
#include <cppunit/TestCase.h>
#include <cppunit/TestFixture.h>
#include <cppunit/ui/text/TextTestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/XmlOutputter.h>
#include <netinet/in.h>
#include <iostream>

using namespace CppUnit;
using namespace std;

//-----------------------------------------------------------------------------

class TestLibbible : public CppUnit::TestFixture
{
    CPPUNIT_TEST_SUITE(TestLibbible);
    CPPUNIT_TEST(testGetModules);
    CPPUNIT_TEST(testGetPassages);
    CPPUNIT_TEST(testGetText);
    CPPUNIT_TEST(testSettings);
    CPPUNIT_TEST_SUITE_END();

    //public:
    //void setUp(void);
    //void tearDown(void);

    protected:
    void testGetModules(void);
    void testGetPassages(void);
    void testGetText(void);
    void testSettings(void);

};

//-----------------------------------------------------------------------------

void TestLibbible::testGetModules(void) {
    map<string, vector<string>> mods = libbible::getModules();
    for(auto pair : mods) {
        libbible::uninstallMod(pair.first);
    }
    CPPUNIT_ASSERT(libbible::getModules().empty());
    libbible::installModFromZip("modules/ESV2011.zip");
    libbible::installModFromZip("modules/JPS.zip");
    mods = libbible::getModules();
    CPPUNIT_ASSERT(mods.find("ESV2011") != mods.end());
    CPPUNIT_ASSERT(mods["ESV2011"].size() == 66);
    CPPUNIT_ASSERT(mods["ESV2011"][7] == "Ruth");
    CPPUNIT_ASSERT(mods["ESV2011"][42] == "John");
    CPPUNIT_ASSERT(mods.find("JPS") != mods.end());
    CPPUNIT_ASSERT(mods["JPS"].size() == 39);
}

void TestLibbible::testGetPassages(void) {
    auto passages = libbible::getPassages("ESV2011", "Romans");
    CPPUNIT_ASSERT(passages[0].modName == "ESV2011");
    CPPUNIT_ASSERT(passages[0].book == "Romans");
    CPPUNIT_ASSERT(passages[0].bookShort == "Rom");
    CPPUNIT_ASSERT(passages[0].chapterStart == 1);
    CPPUNIT_ASSERT(passages[0].verseStart == 1);
    CPPUNIT_ASSERT(passages[0].chapterEnd == 1);
    CPPUNIT_ASSERT(passages[0].verseEnd == 32);
    CPPUNIT_ASSERT(passages.size() == 16);
}

vector<pair<int, int>> getChapVerses(std::vector<libbible::text> text) {
    vector<pair<int, int>> chapVerses;
    for(auto tex : text) {
        //printf("Text is: `%s`\n", tex.text.c_str());
        //for(auto modifier : tex.modifiers) {
        //    printf("\tModifiers include: %s\n", modifier.c_str());
        //}
        if(chapVerses.empty() ||
                chapVerses.back().first != tex.chapter ||
                chapVerses.back().second != tex.verse) {
            chapVerses.push_back(pair<int, int>(tex.chapter, tex.verse));
        }
    }
    return chapVerses;
}

void TestLibbible::testGetText(void) {
    libbible::passage pass;
    pass.modName = "ESV2011";
    pass.bookShort = "Matt";
    pass.chapterStart = 3;
    pass.verseStart = 16;
    pass.chapterEnd = 4;
    pass.verseEnd = 7;
    auto text = libbible::getText(pass);
    // Verify that it includes every verse (3:16-17 + 4:1-7)
    vector<pair<int, int>> chapVerses = getChapVerses(text);
    vector<pair<int, int>> shouldContain = vector<pair<int, int>>({pair<int, int>(3, 16),
            pair<int, int>(3, 17),
            pair<int, int>(4, 1),
            pair<int, int>(4, 2),
            pair<int, int>(4, 3),
            pair<int, int>(4, 4),
            pair<int, int>(4, 5),
            pair<int, int>(4, 6),
            pair<int, int>(4, 7)});
    CPPUNIT_ASSERT(chapVerses == shouldContain);
    libbible::passage pass2;
    pass2.modName = "ESV2011";
    pass2.book = "John";
    pass2.chapterStart = 3;
    pass2.verseStart = 16;
    pass2.chapterEnd = 3;
    pass2.verseEnd = 16;
    text = libbible::getText(pass2);
    string allText;
    for(auto tex : text) {
        allText += tex.text;
    }
    //printf("Text is: `%s`\n", allText.c_str());
    CPPUNIT_ASSERT(allText == " “For God so loved the world, that he gave his only Son, that whoever believes in him should not perish but have eternal life. ");
    
    text = libbible::getText(libbible::passage{.modName = "ESV2011", .reference="John 3:3"});
    allText.clear();
    for(auto tex : text) {
        allText += tex.text;
    }
    //printf("Text is: `%s`\n", allText.c_str());
    CPPUNIT_ASSERT(allText == "Jesus answered him,  “Truly, truly, I say to you, unless one is born again he cannot see the kingdom of God.” ");
    
    text = libbible::getText(libbible::passage{.modName = "ESV2011", .reference="Gal 5:22-23"});
    chapVerses = getChapVerses(text);
    shouldContain = vector<pair<int, int>>({pair<int, int>(5, 22), pair<int, int>(5, 23)});
    CPPUNIT_ASSERT(chapVerses == shouldContain);

    text = libbible::getText(libbible::passage{.modName = "ESV2011", .reference="1 cor 1:31-2:1"});
    chapVerses = getChapVerses(text);
    shouldContain = vector<pair<int, int>>({pair<int, int>(1, 31), pair<int, int>(2, 1)});
    CPPUNIT_ASSERT(chapVerses == shouldContain);
    
    text = libbible::getText(libbible::passage{.modName = "ESV2011", .reference="ps 14-15"});
    chapVerses = getChapVerses(text);
    shouldContain = vector<pair<int, int>>({pair<int, int>(14, 1),
        pair<int, int>(14, 2),
        pair<int, int>(14, 3),
        pair<int, int>(14, 4),
        pair<int, int>(14, 5),
        pair<int, int>(14, 6),
        pair<int, int>(14, 7),
        pair<int, int>(15, 1),
        pair<int, int>(15, 2),
        pair<int, int>(15, 3),
        pair<int, int>(15, 4),
        pair<int, int>(15, 5)});
    CPPUNIT_ASSERT(chapVerses == shouldContain);
}

void TestLibbible::testSettings(void) {
    libbible::settingsWrite("test", "foo");
    CPPUNIT_ASSERT(libbible::settingsRead("test") == "foo");
    libbible::settingsWrite("test", "bar");
    CPPUNIT_ASSERT(libbible::settingsRead("test") == "bar");
    libbible::settingsWriteInt("test", 5);
    CPPUNIT_ASSERT(libbible::settingsReadInt("test") == 5);
    libbible::settingsWrite("test", "");
    CPPUNIT_ASSERT(libbible::settingsRead("test") == "");
}
//-----------------------------------------------------------------------------

CPPUNIT_TEST_SUITE_REGISTRATION( TestLibbible );

int main(int argc, char* argv[]) {
    // informs test-listener about testresults
    CPPUNIT_NS::TestResult testresult;

    // register listener for collecting the test-results
    CPPUNIT_NS::TestResultCollector collectedresults;
    testresult.addListener (&collectedresults);

    // register listener for per-test progress output
    CPPUNIT_NS::BriefTestProgressListener progress;
    testresult.addListener (&progress);

    // insert test-suite at test-runner by registry
    CPPUNIT_NS::TestRunner testrunner;
    testrunner.addTest (CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest ());
    testrunner.run(testresult);

    // output results in compiler-format
    CPPUNIT_NS::CompilerOutputter compileroutputter(&collectedresults, std::cerr);
    compileroutputter.write ();

    // Output XML for Jenkins CPPunit plugin
    //ofstream xmlFileOut("testLibbibleResults.xml");
    //XmlOutputter xmlOut(&collectedresults, xmlFileOut);
    //xmlOut.write();

    // return 0 if tests were successful
    return collectedresults.wasSuccessful() ? 0 : 1;
}