Skip to content

Commit

Permalink
Add comments to TestParser
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinMeimar committed May 23, 2024
1 parent 2d17f3e commit 6000286
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ bin/
# Ignore CLion files
cmake-build*/
.idea/
.vscode
.vscode
todo.md
8 changes: 3 additions & 5 deletions include/tests/TestParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ class TestParser {
: testfile(testfile), foundInput(false), foundInputFile(false),
foundCheck(false), insByteCount(0)
{
#if defined(DEBUG)
// std::cout << "Constructing Test Parser" << std::endl;
#endif
parseTest();
};

Expand All @@ -37,10 +34,11 @@ class TestParser {
// current input stream size
uint32_t insByteCount;

// helper method to return the path in a FILE directive if it is good
PathOrError parsePathFromLine(const std::string &line, const std::string &directive);

// match methods
ErrorState matchInputDirective(std::string &line);
// methods below look for INPUT, CHECK, INPUT_FILE, CHECK_FILE directive in a lines
ErrorState matchInputDirective(std::string &line);
ErrorState matchCheckDirective(std::string &line);
ErrorState matchInputFileDirective(std::string &line);
ErrorState matchCheckFileDirective(std::string &line);
Expand Down
47 changes: 37 additions & 10 deletions src/tests/TestParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@

namespace tester {

// returns true iff substring is contained fully within str
/**
* @param str the line being operated on
* @param substr the line to determine if exists in str fully
* @returns true if substr is fully within str
*/
bool fullyContains(const std::string &str, const std::string &substr) {
size_t pos = str.find(substr);
if (pos == std::string::npos)
return false;
return str.substr(pos, substr.length()) == substr;
}

// determines if the filepath following a directive exists, if so returns it
/**
* @param line a single line from the test file to parse
* @param directive which directive we attempt to match
* @returns a std::variant of either a path or an error type
*/
PathOrError TestParser::parsePathFromLine(
const std::string &line,
const std::string &directive
Expand All @@ -29,11 +37,14 @@ PathOrError TestParser::parsePathFromLine(
else if (fs::exists(relPath)) {
return relPath;
} else {
std::cout << "Return File Error: 33: " << line << std::endl;
return ErrorState::FileError;
}
}

/**
* @param line the line from testfile being parsed
* @returns An error state describing the error, if one exists
*/
ErrorState TestParser::matchInputDirective(std::string &line) {

if (!fullyContains(line, Constants::INPUT_DIRECTIVE))
Expand All @@ -59,6 +70,10 @@ ErrorState TestParser::matchInputDirective(std::string &line) {
return ErrorState::NoError;
}

/**
* @param line the line from testfile being parsed
* @returns An error state describing the error, if one exists
*/
ErrorState TestParser::matchCheckDirective(std::string &line) {

if (!fullyContains(line, Constants::CHECK_DIRECTIVE))
Expand All @@ -74,15 +89,16 @@ ErrorState TestParser::matchCheckDirective(std::string &line) {
return ErrorState::NoError;
}

/**
* @param line the line from testfile being parsed
* @returns An error state describing the error, if one exists
*/
ErrorState TestParser::matchInputFileDirective(std::string &line) {

if (!fullyContains(line, Constants::INPUT_FILE_DIRECTIVE)) {
if (!fullyContains(line, Constants::INPUT_FILE_DIRECTIVE))
return ErrorState::NoError;
}
if (foundInput) {
std::cout << "Throw DirectiveConflict in line: " << line << std::endl;
if (foundInput)
return ErrorState::DirectiveConflict;
}

PathOrError pathOrError = parsePathFromLine(line, Constants::INPUT_FILE_DIRECTIVE);
if (std::holds_alternative<fs::path>(pathOrError)) {
Expand All @@ -93,7 +109,10 @@ ErrorState TestParser::matchInputFileDirective(std::string &line) {
return std::get<ErrorState>(pathOrError);
}

//
/**
* @param line the line from testfile being parsed
* @returns An error state describing the error, if one exists
*/
ErrorState TestParser::matchCheckFileDirective(std::string &line) {

if (!fullyContains(line, Constants::CHECK_FILE_DIRECTIVE))
Expand All @@ -118,6 +137,10 @@ ErrorState TestParser::matchCheckFileDirective(std::string &line) {
return ErrorState::NoError;
}

/**
* @brief for each line in the testfile, attempt to parse and match one of the
* several directives. Should only be called if the parser knows we are in a comment.
*/
ErrorState TestParser::matchDirectives(std::string &line) {
ErrorState error;

Expand All @@ -130,10 +153,14 @@ ErrorState TestParser::matchDirectives(std::string &line) {
return ErrorState::NoError;
}

/**
* @brief open up the testfile and begin matching directives in each line, updating
* the state of the testfile with resource paths and other useful data.
*/
int TestParser::parseTest() {
std::ifstream testFileStream(testfile.getTestPath());
if (!testFileStream.is_open()) {
std::cout << "Failed to open the testfile" << std::endl;
std::cerr << "Failed to open the testfile" << std::endl;
return -1;
}

Expand Down

0 comments on commit 6000286

Please sign in to comment.