sencha-lang/Sencha-lang/Tests/TestParser.cpp

79 lines
2.0 KiB
C++

/*
* TestParser.cpp
*
* Created on: Dec 22, 2012
* Author: att
*/
#include "TestParser.h"
TestParser::TestParser() {
}
TestParser::~TestParser() {
}
std::string TestParser::test_parsing_and_evaluating_logical_expressions()
{
std::string test_report = "";
std::vector<InputOutputPair> logical_inputs = prepare_logical_input();
Lexer lexer;
Context context;
Parser parser(&context);
for(auto logical_case : logical_inputs)
{
auto tokens = lexer.parse_line(logical_case.first);
parser.add_tokens(tokens);
parser.interpret();
SenchaObject value = parser.program->evaluate();
parser.erase_all();
muu_assert("Logical value isn't correct", value.truthy == logical_case.second);
}
return test_report;
}
std::vector<TestParser::InputOutputPair> TestParser::prepare_logical_input()
{
std::vector<InputOutputPair> logical_inputs;
logical_inputs.push_back(InputOutputPair("false and false", false));
logical_inputs.push_back(InputOutputPair("false and true", false));
logical_inputs.push_back(InputOutputPair("true and false", false));
logical_inputs.push_back(InputOutputPair("true and true", true));
logical_inputs.push_back(InputOutputPair("false or false", false));
logical_inputs.push_back(InputOutputPair("false or true", true));
logical_inputs.push_back(InputOutputPair("true or false", true));
logical_inputs.push_back(InputOutputPair("true or true", true));
logical_inputs.push_back(InputOutputPair("(true or false) and (true or false)", true));
logical_inputs.push_back(InputOutputPair("7 + 2 > 55 or 6-5 == 1", true));
return logical_inputs;
}
std::string TestParser::all_tests()
{
std::string test_report = "";
mu_run_test(test_parsing_and_evaluating_logical_expressions);
return test_report;
}
/*TODO
* write test of evaluating logical expression
* possible input:
*
* some variable == "g" and other != "a"
*
* some easy expression like:
* if("aaa" != "bbb" and 2 < 5) {
>> print(1);
>> } else {
>> print(0);
>> }
*/