Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
CSE3150_Project/DocumentTest.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
178 lines (156 sloc)
5.07 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "DocumentTest.h" | |
#include "Document.h" | |
#include <string> | |
#include <memory> | |
#include <vector> | |
using namespace std; | |
namespace DocumentTesting { | |
typedef vector<shared_ptr<string>> _vs; | |
static EditorDataController GetExampleEDC() | |
{ | |
EditorDataController edc; | |
string str = "Hello World. This is an example paragraph being loaded into the edc so that the document can be tested"; | |
for (int i = 0; i<str.length(); ++i) { | |
edc.InsertCharacter(str[i],0,i); | |
} | |
edc.InsertNewLine(0,edc.GetLineLength(0)); | |
str = "This is the second paragraph. I'm just trying to figure out how to test this thing."; | |
edc.InsertNewLine(1,0); | |
// Why not have an empty paragraph in here | |
for (int i = 0; i<str.length(); ++i) { | |
edc.InsertCharacter(str[i],2,i); | |
} | |
return edc; | |
} | |
class TestInit: public UnitTest | |
{ | |
public: | |
TestInit() { testName = "Test Init"; } | |
void PerformTest() | |
{ | |
EditorDataController edc; | |
Document doc(&edc); | |
AssertEquals(doc.GetNumPages(),0); | |
doc.BuildDocument(); | |
AssertEquals(doc.GetNumPages(),1); | |
AssertEquals(doc.GetActivePage()->GetNumLines(),1); | |
} | |
}; | |
class TestSplitString: public UnitTest | |
{ | |
public: | |
TestSplitString() { testName = "SplitString"; } | |
void PerformTest() | |
{ | |
string str = ""; // Let's see what happens with an empty string | |
cout << "First we will pass an empty string. Hopefully we will get an empty vector in return" << endl; | |
_vs result = Document::SplitString(str); | |
AssertTrue(result.size() == 0); | |
cout << "Empty string produces an empty vector: " << Passed() << endl; | |
cout << endl << "Now let's try a string with just one space" << endl; | |
str = " "; | |
result = Document::SplitString(str); | |
AssertTrue(result.size() == 2); | |
cout << "A string with a single space produced a vector of two words" << endl; | |
cout << "Passed: " << Passed() << endl; | |
str = "This is a couple words"; | |
cout << "Now we have five words" << endl; | |
result = Document::SplitString(str); | |
AssertTrue(result.size() == 5); | |
cout << "Expected size 5: " << result.size() << endl; | |
AssertEquals(*(result[0]),string("This")); | |
AssertEquals(*(result[1]),string("is")); | |
AssertEquals(*(result[2]),string("a")); | |
AssertEquals(*(result[3]),string("couple")); | |
AssertEquals(*(result[4]),string("words")); | |
str = "a"; | |
result = Document::SplitString(str); | |
AssertTrue(result.size() == 1); | |
AssertEquals(*result[0],str); | |
} | |
}; | |
class TestBuildDocument: public UnitTest | |
{ | |
public: | |
TestBuildDocument() { testName = "BuildDocument"; } | |
void PerformTest() | |
{ | |
EditorDataController edc = GetExampleEDC(); | |
AssertEquals(edc.GetNumLines(),3); | |
cout << "Expected 3 lines in example controller: " << edc.GetNumLines() << endl; | |
Document doc(&edc); | |
doc.SetMaxWidth(15); | |
doc.SetMaxHeight(1); | |
try { | |
doc.BuildDocument(); | |
} catch (string e) { | |
cout << "Caught: " << e << endl; | |
} | |
shared_ptr<DocumentPage> page = doc.GetActivePage(); | |
shared_ptr<DocumentParagraph> para = page->GetLastParagraph(); | |
cout << "I have a problem where the paragraph string is receiving all of the information." << endl; | |
cout << "Let's see if I can fix that" << endl; | |
// What's wrong with the Build document algorithm? | |
cout << "The number of paragraphs in the document are: " << doc.GetNumParagraphs() << endl; | |
AssertEquals(doc.GetNumParagraphs(),3); | |
cout << "Paragraph String :" << para->GetString() << endl; | |
cout << "Data Controller String :" << edc.GetLine(0) << endl; | |
AssertEquals(para->GetString(),edc.GetLine(0)); | |
} | |
}; | |
class TestBuildDocumentWithEmptyString: public UnitTest | |
{ | |
public: | |
TestBuildDocumentWithEmptyString() { testName = "Build a document using empty strings"; } | |
void PerformTest() | |
{ | |
EditorDataController edc; | |
Document doc(&edc); | |
doc.SetMaxHeight(10); | |
doc.SetMaxWidth(10); | |
doc.BuildDocument(); | |
shared_ptr<DocumentPage> activePage = doc.GetActivePage(); | |
AssertEquals(doc.GetNumPages(),1); | |
AssertEquals(activePage->GetNumLines(),1); | |
AssertEquals(activePage->GetNumParagraphs(),1); | |
AssertEquals(activePage->GetLine(0),string()); | |
} | |
}; | |
class TestSplitStringWordAndSpace: public UnitTest | |
{ | |
public: | |
TestSplitStringWordAndSpace() { testName = "SplitStringWordAndSpace"; } | |
void PerformTest() | |
{ | |
EditorDataController edc; | |
string str = "This "; | |
int i = 0; | |
for (char c : str) { | |
edc.InsertCharacter(c,0,i++); | |
} | |
AssertEquals(edc.GetLine(0),str); | |
_vs split = Document::SplitString(str); | |
AssertEquals(*split[0],string("This")); | |
AssertEquals(*split[1],string()); | |
Document doc(&edc); | |
doc.SetMaxHeight(10); | |
doc.SetMaxWidth(10); | |
doc.BuildDocument(); | |
string line = doc.GetActivePage()->GetLine(0); | |
AssertEquals(line,str); | |
} | |
}; | |
} | |
DocumentTest :: DocumentTest() | |
{ | |
using namespace DocumentTesting; | |
testName = "DocumentTest"; | |
tests.push_back(new TestInit()); | |
tests.push_back(new TestSplitString()); | |
tests.push_back(new TestBuildDocument()); | |
tests.push_back(new TestBuildDocumentWithEmptyString()); | |
tests.push_back(new TestSplitStringWordAndSpace()); | |
} | |
DocumentTest :: ~DocumentTest() | |
{ | |
} |