Add unit tests for StringReader. You can run them with ./kraken --test.
This commit is contained in:
@@ -16,6 +16,8 @@ class StringReader
|
||||
std::string line(bool truncateEnd = true);
|
||||
std::string getTokens(std::vector<std::string> get_chars, bool truncateEnd = true);
|
||||
std::string truncateEnd(std::string to_truncate);
|
||||
|
||||
static void test();
|
||||
protected:
|
||||
private:
|
||||
std::string rd_string;
|
||||
|
||||
4
main.cpp
4
main.cpp
@@ -17,6 +17,10 @@
|
||||
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc == 2 && std::string(argv[1]) == "--test") {
|
||||
StringReader::test();
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::ifstream programInFile, grammerInFile;
|
||||
std::ofstream outFile, outFileTransformed, outFileAST;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "StringReader.h"
|
||||
#include <cassert>
|
||||
|
||||
StringReader::StringReader()
|
||||
{
|
||||
@@ -144,3 +145,48 @@ std::string StringReader::truncateEnd(std::string to_truncate)
|
||||
to_return = to_return + to_truncate[i];
|
||||
return to_return;
|
||||
}
|
||||
|
||||
void StringReader::test()
|
||||
{
|
||||
{
|
||||
StringReader reader("\"x\"");
|
||||
assert(reader.word() == "\"x\"");
|
||||
assert(reader.word() == "");
|
||||
}
|
||||
|
||||
{
|
||||
StringReader reader("\"y\" ;\n");
|
||||
assert(reader.word() == "\"y\"");
|
||||
assert(reader.word() == ";");
|
||||
assert(reader.word() == "");
|
||||
}
|
||||
|
||||
{
|
||||
StringReader reader("Goal = greeting ;\n"
|
||||
"greeting = \"hello\" | greeting \"world\" ;\n");
|
||||
assert(reader.word() == "Goal");
|
||||
assert(reader.word() == "=");
|
||||
assert(reader.word() == "greeting");
|
||||
assert(reader.word() == ";");
|
||||
assert(reader.word() == "greeting");
|
||||
assert(reader.word() == "=");
|
||||
assert(reader.word() == "\"hello\"");
|
||||
assert(reader.word() == "|");
|
||||
assert(reader.word() == "greeting");
|
||||
assert(reader.word() == "\"world\"");
|
||||
assert(reader.word() == ";");
|
||||
assert(reader.word() == "");
|
||||
}
|
||||
|
||||
{
|
||||
StringReader reader("one # pretend this is a comment\n"
|
||||
" two\n");
|
||||
assert(reader.word() == "one");
|
||||
assert(reader.word() == "#");
|
||||
assert(reader.line() == "pretend this is a comment");
|
||||
assert(reader.word() == "two");
|
||||
assert(reader.word() == "");
|
||||
}
|
||||
|
||||
std::cout << "StringReader tests pass\n";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user