Added testing! call kraken like so ./kraken --test ../path/to/test/name_of_test_without_extention This will make kraken compile and run name_of_test_without_extention.krak and compare the output it generates on stdout to name_of_test_without_extention.expected_results. If they pass, then it records the pass, if not, it records the failure and saves the intermediate files generated. It has revealed some bugs which I will fix in upcoming commits.
This commit is contained in:
69
src/Tester.cpp
Normal file
69
src/Tester.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#include "Tester.h"
|
||||
|
||||
Tester::Tester(std::string krakenInvocation, std::string krakenGrammerLocation) : krakenInvocation(krakenInvocation), krakenGrammerLocation(krakenGrammerLocation) {
|
||||
//initlization list
|
||||
removeCmd = "rm";
|
||||
resultsExtention = ".results";
|
||||
expectedExtention = ".expected_results";
|
||||
krakenExtention = ".krak";
|
||||
changePermissions = "chmod 755";
|
||||
shell = "sh";
|
||||
redirect = ">";
|
||||
}
|
||||
|
||||
Tester::~Tester() {
|
||||
//Nothing
|
||||
}
|
||||
|
||||
int Tester::ssystem(std::string command) {
|
||||
return system(command.c_str());
|
||||
}
|
||||
|
||||
void Tester::cleanExtras(std::string fileName) {
|
||||
ssystem(removeCmd + " " + fileName);
|
||||
ssystem(removeCmd + " " + fileName + krakenExtention + "out*");
|
||||
ssystem(removeCmd + " " + fileName + krakenExtention + ".c");
|
||||
ssystem(removeCmd + " " + fileName + ".sh");
|
||||
ssystem(removeCmd + " " + fileName + resultsExtention);
|
||||
}
|
||||
|
||||
bool Tester::run(std::string fileName) {
|
||||
std::cout << "Testing: " << fileName << " with " << krakenInvocation << " and " << krakenGrammerLocation << std::endl;
|
||||
|
||||
cleanExtras(fileName);
|
||||
|
||||
ssystem(changePermissions + " " + fileName);
|
||||
ssystem(krakenInvocation + " " + fileName + krakenExtention + " " + krakenGrammerLocation + " " + fileName);
|
||||
ssystem(shell + " " + fileName + ".sh");
|
||||
ssystem(fileName + " " + redirect + " " + fileName + resultsExtention);
|
||||
|
||||
bool result = compareFiles(fileName + expectedExtention, fileName + resultsExtention);
|
||||
|
||||
//If the test was succesful, we don't need all the extra files
|
||||
if (result)
|
||||
cleanExtras(fileName);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool Tester::compareFiles(std::string file1Path, std::string file2Path) {
|
||||
std::ifstream file1, file2;
|
||||
file1.open(file1Path);
|
||||
if (!file1.is_open()) {
|
||||
std::cout << file1Path << " could not be opened!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
file2.open(file2Path);
|
||||
if (!file2.is_open()) {
|
||||
std::cout << file2Path << " could not be opened!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string file1contents = readFile(file1);
|
||||
std::string file2contents = readFile(file2);
|
||||
|
||||
// std::cout << "file1: " << file1contents << std::endl;
|
||||
// std::cout << "file2: " << file2contents << std::endl;
|
||||
// std::cout << "comp: " << file1contents.compare(file2contents) << std::endl;
|
||||
return file1contents.compare(file2contents) == 0;
|
||||
}
|
||||
@@ -71,4 +71,13 @@ std::string join(const std::vector<std::string> &strVec, std::string joinStr) {
|
||||
return joinedStr;
|
||||
}
|
||||
|
||||
std::string readFile(std::istream &file) {
|
||||
std::string line, contents;
|
||||
while(file.good()) {
|
||||
getline(file, line);
|
||||
contents.append(line+"\n");
|
||||
}
|
||||
return contents;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user