Skip to content
Permalink
master
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
#include "EditorDataControllerTest.h"
#include "EditorDataController.h"
#include <iostream>
#include <fstream>
#include <cstdio>
using namespace std;
namespace EditorDataControllerTesting {
class TestSaveFile: public UnitTest
{
public:
TestSaveFile() { testName = "SaveFile"; }
void PerformTest()
{
EditorDataController edc;
cout << endl << "Testing SaveFile" << endl;
int pos = 0;
for (char c = '0'; c<='9'; ++c) {
edc.InsertCharacter(c,0,pos++);
}
int check = edc.SaveFile("TestSaveFile.txt");
if (check) {
Fail();
}
if (!check) {
ifstream f;
f.open("TestSaveFile.txt");
string line;
getline(f,line);
f.close();
AssertTrue(line == "0123456789");
remove("TestSaveFile.txt");
}
}
};
class TestLoadFile: public UnitTest
{
public:
TestLoadFile()
{
testName = "LoadFile";
}
void PerformTest()
{
EditorDataController edc;
// Write the file initally
string str = "This is some dumb nonsense";
string str2 = "This is also some dumb nonsense";
string fname = "TestLoadFile.txt";
ofstream f;
f.open(fname);
f << str << endl << str2 << endl;
f.close();
int check = edc.LoadFile(fname);
AssertTrue(check == 0);
cout << "File Loaded Without Error: " << Passed() << endl;
cout << "Lines in the buffer: " << edc.GetNumLines() << endl;
AssertTrue(str == edc.GetLine(0));
AssertTrue(str2 == edc.GetLine(1));
remove(fname.c_str());
}
};
class TestInit: public UnitTest
{
public:
TestInit() { testName = "Initialize the Data Controller"; }
void PerformTest()
{
EditorDataController edc;
AssertEquals(edc.GetNumLines(),1);
AssertEquals(edc.GetLineLength(0),0);
AssertEquals(edc.GetLine(0),string());
}
};
class TestInvalidIndexException: public UnitTest
{
public:
TestInvalidIndexException() { testName = "Trigger a bunch of invalid index exceptions"; }
void PerformTest()
{
cout << "The goal of the this test is to trigger as many invalid indexes as I can. We want exceptions not segfaults" << endl;
EditorDataController edc;
try {
cout << "Attempting to snag a line which doesn't exist in an empty buffer" << endl;
edc.GetLine(10);
} catch (string e) {
cout << "Caught Exception: " << e << endl;
}
cout << "Putting 10 characters into the buffer" << endl;
for (int i = 0; i<10; ++i) {
edc.InsertCharacter('a',0,i);
}
try {
cout << "Attempt to insert a character 1 index out of place" << endl;
edc.InsertCharacter('a',0,11);
} catch (string e) {
cout << "Caught Exception: " << e << endl;
}
try {
cout << "Let's try to delete a line break which doesn't exist" << endl;
edc.DeleteNewLine(2);
} catch (string e) {
cout << "Caught Exception: " << e << endl;
}
try {
cout << "Let's insert a new line out of place" << endl;
edc.InsertNewLine(1,0);
} catch (string e) {
cout << "Caught Exception: " << e << endl;
}
try {
cout << "Let's make a new line and then delete it in the wrong spot" << endl;
edc.InsertNewLine(0,edc.GetLineLength(0));
edc.DeleteNewLine(2);
} catch (string e) {
cout << "Caught Exception: " << e << endl;
}
try {
cout << "Now let's keep deleting the first index until we get an exception" << endl;
while (1) { edc.DeleteCharacter(0,0); }
} catch (string e) {
cout << "Caught Exception: " << e << endl;
}
cout << "If that was done correctly we should have nothing in the buffer again" << endl;
cout << "Lines in Buffer should be 1: " << edc.GetNumLines() << endl;
cout << "Length of starting line should be 0: " << edc.GetLineLength(0) << endl;
AssertEquals(edc.GetNumLines(),1);
AssertEquals(edc.GetLineLength(0),0);
}
};
class TestInsertNewLines: public UnitTest
{
public:
TestInsertNewLines() { testName = "InsertNewLines"; }
void PerformTest()
{
EditorDataController edc;
string word = "One";
string Two = "Two";
for (int i = 0; i<3; ++i) {
edc.InsertCharacter(word[i],0,i);
}
string line1 = edc.GetLine(0);
AssertEquals(line1,word);
AssertEquals(edc.GetNumLines(),1);
edc.InsertNewLine(0,edc.GetLineLength(0));
AssertEquals(edc.GetNumLines(),2);
for (int i = 0; i<3; ++i) {
edc.InsertCharacter(Two[i],1,i);
}
AssertEquals(edc.GetLine(0),word);
AssertEquals(edc.GetLine(1),Two);
}
};
class TestDeleteNewLines: public UnitTest
{
public:
TestDeleteNewLines() { testName = "DeleteNewLines"; }
void PerformTest()
{
EditorDataController edc;
string one = "One";
string two = "Two";
string three = "Three";
cout << "This test is a bit comprehensive so stay with me here." << endl;
// This inserts three new lines. So we should be up to four
vector<string> words = { one, two, three };
for (int i = 0; i<words.size(); ++i) {
string &str = words[i];
int j = 0;
for (char c : str) {
edc.InsertCharacter(c,i,j++);
}
edc.InsertNewLine(i,edc.GetLineLength(i));
}
for (int i = 0; i<3; ++i) {
AssertEquals(edc.GetLine(i),words[i]);
}
cout << "The buffer has successfully been loaded with three words. One per line" << endl;
cout << "Sanity check. Are we passing before the test: " << Passed() << endl;
cout << endl << "Dumping buffer" << endl << endl;
for (int i = 0; i<edc.GetNumLines(); ++i) {
cout << edc.GetLine(i) << endl;
}
cout << endl;
edc.DeleteNewLine(2);
cout << "Deleted the newline at the beginning of line 3." << endl;
cout << "String should be '" << two << three << "' : " << edc.GetLine(1) << endl;
AssertEquals(edc.GetLine(1),two + three);
cout << "There should be three lines left in the buffer :" << edc.GetNumLines() << endl;
AssertEquals(edc.GetNumLines(),3);
edc.DeleteNewLine(1);
AssertEquals(edc.GetLine(0),one + two + three);
AssertEquals(edc.GetNumLines(),2);
}
};
class TestClearBuffer: public UnitTest
{
public:
TestClearBuffer() { testName = "ClearBuffer"; }
void PerformTest()
{
EditorDataController edc;
for (int i = 0; i<10; ++i) {
edc.InsertCharacter('a',i,0);
edc.InsertNewLine(i,1);
}
for (int i = 0; i<10; ++i) {
AssertEquals(edc.GetLine(i),string("a"));
}
AssertEquals(edc.GetNumLines(),11);
edc.ResetBuffer();
AssertEquals(edc.GetNumLines(),1);
AssertEquals(edc.GetLine(0),string());
}
};
}
EditorDataControllerTest :: EditorDataControllerTest()
{
using namespace EditorDataControllerTesting;
testName = "EditorDataController";
tests.push_back(new TestSaveFile());
tests.push_back(new TestLoadFile());
tests.push_back(new TestInit());
tests.push_back(new TestInvalidIndexException());
tests.push_back(new TestInsertNewLines());
tests.push_back(new TestDeleteNewLines());
tests.push_back(new TestClearBuffer());
}
EditorDataControllerTest :: ~EditorDataControllerTest()
{
}