aboutsummaryrefslogtreecommitdiff
path: root/src/test/testLibbible.cc
blob: a4fbc32864de8a0f2ee049b93202b5313daa8497 (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
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
//#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(testGetBooks);
    CPPUNIT_TEST(testGetPassages);
    CPPUNIT_TEST(testGetText);
    CPPUNIT_TEST(testSettings);
    CPPUNIT_TEST(testLanguageNames);
    CPPUNIT_TEST(testDownload);
    CPPUNIT_TEST_SUITE_END();

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

    protected:
    void testGetModules(void);
    void testGetBooks(void);
    void testGetPassages(void);
    void testGetText(void);
    void testSettings(void);
    void testLanguageNames(void);
    void testDownload(void);

};

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

class StatusTester : public libbible::Status
{
    public:
    virtual void update(unsigned long totalBytes, unsigned long completedBytes, string message);
    bool hasBeenUpdated = false;
};

void StatusTester::update(unsigned long totalBytes, unsigned long completedBytes, string message) {
    hasBeenUpdated = true;
}

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

class CancelTester : public libbible::Status
{
    public:
    virtual void update(unsigned long totalBytes, unsigned long completedBytes, string message);
};

void CancelTester::update(unsigned long totalBytes, unsigned long completedBytes, string message) {
    libbible::terminateDownload();
}

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

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

void TestLibbible::testGetBooks(void) {
    vector<string> mods = libbible::getModuleNames();
    vector<string> kjvbooks = libbible::getBooks("KJV");
    CPPUNIT_ASSERT(kjvbooks.size() == 66);
    CPPUNIT_ASSERT(kjvbooks[7] == "Ruth");
    CPPUNIT_ASSERT(kjvbooks[42] == "John");
    vector<string> jpsbooks = libbible::getBooks("JPS");
    CPPUNIT_ASSERT(jpsbooks.size() == 39);
}

void TestLibbible::testGetPassages(void) {
    auto passages = libbible::getPassages("KJV", "Romans");
    CPPUNIT_ASSERT(passages[0].modName == "KJV");
    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 = "KJV";
    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 = "KJV";
    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 begotten Son, that whosoever believeth in him should not perish, but have everlasting life. ");
    
    text = libbible::getText(libbible::getPassage("KJV", "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 and said unto him,  Verily, verily, I say unto thee, Except a man be born again, he cannot see the kingdom of God. ");
    
    text = libbible::getText(libbible::getPassage("KJV", "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::getPassage("KJV", "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::getPassage("KJV", "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);

    text = libbible::getText(libbible::getPassage("KJV", "John 21"));
    CPPUNIT_ASSERT(text.back().verse == 25);
}

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

void TestLibbible::testLanguageNames(void) {
    std::map<std::string, std::string> langs = libbible::getLanguageNames();
    CPPUNIT_ASSERT(langs["en"] == "English");
    CPPUNIT_ASSERT(langs["es"] == "Spanish");
    CPPUNIT_ASSERT(langs["fr"] == "French");
}

void TestLibbible::testDownload(void) {
    map<string, vector<string>> modsAvailable = libbible::downloadModsAvailable();
    // We try installing the first available one
    string language;
    string name;
    for(auto pair : modsAvailable) {
        language = pair.first;
        name = pair.second[0];
        break;
    }
    CPPUNIT_ASSERT(!language.empty() && !name.empty());
    // Try uninstalling it (shouldn't crash or have nasty side effects!)
    libbible::uninstallMod(name);
    // Try installing it with cancel. Shoudn't work because it gets cancelled!
    CancelTester cancel;
    libbible::setStatusReporter(cancel);
    libbible::installModFromInternet(language, name);
    auto mods = libbible::getModules();
    CPPUNIT_ASSERT(mods.find(name) == mods.end());
    // Now we try with normal status
    StatusTester status;
    libbible::setStatusReporter(status);
    libbible::installModFromInternet(language, name);
    mods = libbible::getModules();
    CPPUNIT_ASSERT(mods.find(name) != mods.end());
    CPPUNIT_ASSERT(status.hasBeenUpdated);
    
}
//-----------------------------------------------------------------------------

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