diff options
author | Your Name <you@example.com> | 2020-11-05 15:50:11 -0500 |
---|---|---|
committer | Your Name <you@example.com> | 2020-11-05 15:50:11 -0500 |
commit | 2fe897e2cf750339a7e466aeafe64f45fb650f10 (patch) | |
tree | d27bbda7c1ef5368a90704843deea014e6c810de /testLibbible.cc | |
download | libbible-2fe897e2cf750339a7e466aeafe64f45fb650f10.tar.gz libbible-2fe897e2cf750339a7e466aeafe64f45fb650f10.tar.bz2 libbible-2fe897e2cf750339a7e466aeafe64f45fb650f10.zip |
Initial commit
Diffstat (limited to 'testLibbible.cc')
-rw-r--r-- | testLibbible.cc | 206 |
1 files changed, 206 insertions, 0 deletions
diff --git a/testLibbible.cc b/testLibbible.cc new file mode 100644 index 0000000..ad51c44 --- /dev/null +++ b/testLibbible.cc @@ -0,0 +1,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; +} |