summaryrefslogtreecommitdiffstats
path: root/Sencha-lang/Tests/TestParser.cpp
blob: 633fcc60e6c1d9c043b855228c3f0db6f4894d63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * 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;
	ContextManager 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_last();
		parser.erase_all();
		muu_assert("Logical value isn't correct", value.truthy == logical_case.second);
	}
	return test_report;
}

std::string TestParser::test_changing_letters_in_string()
{
	std::string test_report = "";
	std::string preparations = "text = \"AlfaBetaGamma\";";
	std::string changes = "text[0] = \"a\";";
	changes += "text[4] = \"b\";";
	changes += "text[8] = \"g\";";
	changes += "text;";

	ContextManager context_manager;
	Parser parser(&context_manager);
	Lexer lexer;
	auto tokens = lexer.parse_line(preparations + changes);
	parser.add_tokens(tokens);
	parser.interpret();
	SenchaObject value = parser.program->execute();
	//std::cout << value.repr() << std::endl;
	muu_assert("Changed text isn't alfabetagamma", value.text == "alfabetagamma");

	return test_report;
}



std::string TestParser::test_adding_new_function()
{
	std::string test_report = "";
	std::string function_declaration = "def print_hello(how_many_times) {";
	function_declaration += " repeat(how_many_times) { print(\"Hello\");}";

	ContextManager context_manager;
	Parser parser(&context_manager);
	Lexer lexer;
	auto tokens = lexer.parse_line(function_declaration);
	parser.add_tokens(tokens);
	parser.interpret();

	muu_assert("Function couldn't be added", context_manager.get_top()->contains_function("print_hello"));

	return test_report;
}

std::string TestParser::test_writing_and_accessing_array_elements()
{
	std::string test_report = "";
	std::string table_definition = "array data[4];";
	table_definition += "data[0] = 3; data[1] = -88; data[2] = true; data[3] = 1.222;";

	std::string data_access = "data[0];";
	ContextManager context;
	Parser parser(&context);
	Lexer lexer;
	auto tokens = lexer.parse_line(table_definition + data_access);
	parser.add_tokens(tokens);
	parser.interpret();
	SenchaObject value = parser.program->execute();

	muu_assert("data[0] != 3", (value == SenchaObject(3)).truthy );
	data_access = "data[1];";
	tokens = lexer.parse_line(data_access);
	parser.add_tokens(tokens);
	parser.interpret();
	value = parser.program->execute();
	muu_assert("data[1] != -88", (value == SenchaObject(-88)).truthy );

	data_access = "data[2];";
	tokens = lexer.parse_line(data_access);
	parser.add_tokens(tokens);
	parser.interpret();
	value = parser.program->execute();
	muu_assert("data[2] != true", (value == SenchaObject(true)).truthy );

	data_access = "data[3];";
	tokens = lexer.parse_line(data_access);
	parser.add_tokens(tokens);
	parser.interpret();
	value = parser.program->execute();
	muu_assert("data[3] != 1.222", (value == SenchaObject(1.222)).truthy );


	return test_report;
}

std::string TestParser::test_calling_funtion()
{
	std::string test_report = "";
	Lexer lexer;
	ContextManager context;
	Parser parser(&context);
	std::string call_max = " max(77, 12);";
	std::string declarations = prepare_some_function_declarations();
	auto tokens = lexer.parse_line( declarations + call_max);
	parser.add_tokens(tokens);
	parser.interpret();
	auto value1 =  parser.program->execute();
	std::string call_multiply = " multiply_string(\"banana\", 3);";
	muu_assert("function max doesn't work", value1.integer == 77);

	parser.erase_all();
	parser.add_tokens(lexer.parse_line(declarations + call_multiply));
	parser.interpret();
	auto value2 = parser.program->execute();
	muu_assert("function multiply_string doesn't work", value2.text == "bananabananabanana");
	return test_report;
}

std::string TestParser::prepare_some_function_declarations()
{

	std::string how_long_declaration = "def how_long(some_text) {";
	how_long_declaration += " return len(some_text);}";


	std::string max_declaration = "def max(a, b) { ";
	max_declaration += "if(a > b) {  return a; }";
	max_declaration += "else {  return b;}  }";

	std::string min_declaration = "def min(a, b) { ";
	min_declaration += "if(a < b) return a;";
	min_declaration += "else return b; }";

	std::string multiply_string_declaration = "def multiply_string(text, i) {";
	multiply_string_declaration += "result = \"\"; ";
	multiply_string_declaration += "repeat(i) { result = result + text; }";
	multiply_string_declaration += " return result;} ";


	return max_declaration + min_declaration + multiply_string_declaration;
}


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);
	mu_run_test(test_calling_funtion);
	mu_run_test(test_changing_letters_in_string);
	//mu_run_test(test_adding_new_function);
	mu_run_test(test_writing_and_accessing_array_elements);

	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);
>>     }
 */