aboutsummaryrefslogtreecommitdiff
path: root/src/test/testLibbible.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/testLibbible.cc')
-rw-r--r--src/test/testLibbible.cc265
1 files changed, 265 insertions, 0 deletions
diff --git a/src/test/testLibbible.cc b/src/test/testLibbible.cc
new file mode 100644
index 0000000..d3a265a
--- /dev/null
+++ b/src/test/testLibbible.cc
@@ -0,0 +1,265 @@
+//#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(testDownload);
+ 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 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::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::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;
+}