Skip to content

Commit c1c5e3a

Browse files
committed
Fix parsing elifs
Before, this was only checking boolean variables correctly.
1 parent 5fdc8d0 commit c1c5e3a

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/control/script_reader.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,7 @@ void ScriptReader::replaceConditionals(std::string& script_code, const DataDict&
260260
if (top.type == ELSE)
261261
continue;
262262
std::string condition = match[1];
263-
bool result = data.count(condition) && std::holds_alternative<bool>(data.at(condition)) &&
264-
std::get<bool>(data.at(condition));
263+
bool result = evaluateExpression(condition, data);
265264
top.type = ELIF;
266265
if (!top.condition_matched && result)
267266
{

tests/test_script_reader.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,48 @@ Please log in.
241241
std::remove(existing_script_file);
242242
}
243243

244+
TEST_F(ScriptReaderTest, TestNestedConditionals)
245+
{
246+
char existing_script_file[] = "main_script.XXXXXX";
247+
std::ignore = mkstemp(existing_script_file);
248+
std::ofstream ofs(existing_script_file);
249+
if (ofs.bad())
250+
{
251+
std::cout << "Failed to create temporary files" << std::endl;
252+
GTEST_FAIL();
253+
}
254+
ofs <<
255+
R"({% if PI < THE_ANSWER_TO_EVERYTHING %}
256+
{% if TAU < PI %}
257+
It's a strange universe you live in...
258+
{%elif pie_tastes == "great" %}
259+
What's better than a pie? 2 pies!
260+
{% else %}
261+
You don't like pie?
262+
{% endif %}
263+
{% else %}
264+
How can something be greater than the answer to everything?
265+
{% endif %}
266+
)";
267+
ofs.close();
268+
269+
ScriptReader reader;
270+
ScriptReader::DataDict data;
271+
data["PI"] = 3.14;
272+
data["TAU"] = 6.28;
273+
data["THE_ANSWER_TO_EVERYTHING"] = 42;
274+
275+
data["pie_tastes"] = "great";
276+
std::string script = reader.readScriptFile(existing_script_file, data);
277+
EXPECT_EQ(script, "What's better than a pie? 2 pies!");
278+
279+
data["pie_tastes"] = "aweful";
280+
script = reader.readScriptFile(existing_script_file, data);
281+
EXPECT_EQ(script, "You don't like pie?");
282+
283+
std::remove(existing_script_file);
284+
}
285+
244286
TEST_F(ScriptReaderTest, CheckCondition)
245287
{
246288
ScriptReader reader;

0 commit comments

Comments
 (0)