Skip to content

Commit a00ede6

Browse files
committed
Add a test for conditionally including a file
1 parent a552f62 commit a00ede6

File tree

3 files changed

+66
-12
lines changed

3 files changed

+66
-12
lines changed

include/ur_client_library/control/script_reader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class ScriptReader
7878
std::filesystem::path script_path_;
7979

8080
static std::string readFileContent(const std::string& filename);
81-
void replaceIncludes(std::string& script_code);
81+
void replaceIncludes(std::string& script_code, const DataDict& data);
8282
static void replaceVariables(std::string& script_code, const DataDict& data);
8383
static void replaceConditionals(std::string& script_code, const DataDict& data);
8484
};

src/control/script_reader.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ std::string ScriptReader::readScriptFile(const std::string& filename, const Data
149149
script_path_ = filename;
150150
std::string script_code = readFileContent(filename);
151151

152-
replaceIncludes(script_code);
153152
replaceVariables(script_code, data);
154153
replaceConditionals(script_code, data);
154+
replaceIncludes(script_code, data);
155155

156156
return script_code;
157157
}
@@ -177,7 +177,7 @@ std::string ScriptReader::readFileContent(const std::string& filename)
177177
return content;
178178
}
179179

180-
void ScriptReader::replaceIncludes(std::string& script)
180+
void ScriptReader::replaceIncludes(std::string& script, const DataDict& data)
181181
{
182182
std::regex include_pattern(R"(\{\%\s*include\s*['|"]([^'"]+)['|"]\s*\%\})");
183183

@@ -187,7 +187,7 @@ void ScriptReader::replaceIncludes(std::string& script)
187187
while (std::regex_search(script, match, include_pattern))
188188
{
189189
std::filesystem::path file_path(match[1]);
190-
std::string file_content = readFileContent(script_path_.parent_path() / file_path.string());
190+
std::string file_content = readScriptFile(script_path_.parent_path() / file_path.string(), data);
191191
script.replace(match.position(0), match.length(0), file_content);
192192
}
193193
}
@@ -249,6 +249,7 @@ void ScriptReader::replaceConditionals(std::string& script_code, const DataDict&
249249
std::regex endif_pattern(R"(\{\%\s*endif\s*\%\})");
250250
std::smatch match;
251251

252+
bool first_line = true;
252253
while (std::getline(stream, line))
253254
{
254255
if (std::regex_search(line, match, if_pattern))
@@ -300,13 +301,19 @@ void ScriptReader::replaceConditionals(std::string& script_code, const DataDict&
300301
{
301302
if (block_stack.empty() ? true : block_stack.top().should_render)
302303
{
303-
output << line << "\n";
304+
if (!first_line)
305+
{
306+
output << "\n";
307+
}
308+
output << line;
309+
first_line = false;
304310
}
305311
}
306312
}
307313

308314
script_code = output.str();
309315
}
316+
310317
bool ScriptReader::checkCondition(const std::string& condition, const DataDict& data)
311318
{
312319
const std::string trimmed = std::regex_replace(condition, std::regex("^\\s+|\\s+$"), "");

tests/test_script_reader.cpp

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ TEST_F(ScriptReaderTest, ReadValidScript)
8585
{
8686
ScriptReader reader;
8787
std::string content = reader.readScriptFile(valid_script_path_);
88-
EXPECT_EQ(content, simple_script_.str() + "\n");
88+
EXPECT_EQ(content, simple_script_.str());
8989
}
9090

9191
TEST_F(ScriptReaderTest, ReadEmptyScript)
@@ -130,7 +130,7 @@ TEST_F(ScriptReaderTest, ReplaceIncludes)
130130
ofs_included.close();
131131

132132
std::string processed_script = reader.readScriptFile(existing_script_file);
133-
EXPECT_EQ(processed_script, "movej([1,2,3,4,5,6])\n");
133+
EXPECT_EQ(processed_script, "movej([1,2,3,4,5,6])");
134134

135135
std::remove(existing_script_file);
136136
std::remove(existing_include_file);
@@ -164,7 +164,7 @@ TEST_F(ScriptReaderTest, ReplaceVariables)
164164

165165
// By default std::to_string will convert double to 6 decimal places
166166
EXPECT_EQ(script, "movej([value1, 42, 6.280000, 0, 0, 0])\nlocal is_true = True\nlocal is_false = False\nThis is "
167-
"just a line without any replacement\n");
167+
"just a line without any replacement");
168168
}
169169

170170
TEST_F(ScriptReaderTest, VariableNotInDictThrowsError)
@@ -221,20 +221,20 @@ Please log in.
221221

222222
std::string script = reader.readScriptFile(existing_script_file, data);
223223

224-
EXPECT_EQ(script, "Welcome back, test_user!\n");
224+
EXPECT_EQ(script, "Welcome back, test_user!");
225225

226226
data["is_logged_in"] = false;
227227
data["is_guest"] = true;
228228
script = reader.readScriptFile(existing_script_file, data);
229-
EXPECT_EQ(script, "Welcome, test_user!\n");
229+
EXPECT_EQ(script, "Welcome, test_user!");
230230

231231
data["username"] = "";
232232
script = reader.readScriptFile(existing_script_file, data);
233-
EXPECT_EQ(script, "Welcome, guest!\n");
233+
EXPECT_EQ(script, "Welcome, guest!");
234234

235235
data["is_guest"] = false;
236236
script = reader.readScriptFile(existing_script_file, data);
237-
EXPECT_EQ(script, "Please log in.\n");
237+
EXPECT_EQ(script, "Please log in.");
238238
}
239239

240240
TEST_F(ScriptReaderTest, CheckCondition)
@@ -391,4 +391,51 @@ TEST_F(ScriptReaderTest, DataVariantOperators)
391391
EXPECT_THROW((void)(data["bool1"] > data["bool2"]), std::invalid_argument);
392392
EXPECT_THROW(data["str1"] == data["bool1"], std::invalid_argument);
393393
EXPECT_THROW(data["double1"] == data["str1"], std::invalid_argument);
394+
}
395+
396+
TEST_F(ScriptReaderTest, ConditionalInclude)
397+
{
398+
ScriptReader reader;
399+
400+
char existing_script_file[] = "main_script.XXXXXX";
401+
std::ignore = mkstemp(existing_script_file);
402+
char existing_include_file[] = "included_script.XXXXXX";
403+
std::ignore = mkstemp(existing_include_file);
404+
std::ofstream ofs(existing_script_file);
405+
if (ofs.bad())
406+
{
407+
std::cout << "Failed to create temporary files" << std::endl;
408+
GTEST_FAIL();
409+
}
410+
ofs << "textmsg(\"This is a test script\")" << std::endl;
411+
ofs << "{% if ROBOT_VERSION > 10.7 %}" << std::endl;
412+
ofs << "{% include '" << std::string(existing_include_file) << "' %}" << std::endl;
413+
ofs << "{% endif %}" << std::endl;
414+
ofs.close();
415+
416+
// Create a temporary included script
417+
std::ofstream ofs_included(existing_include_file);
418+
if (ofs_included.bad())
419+
{
420+
std::cout << "Failed to create temporary files" << std::endl;
421+
GTEST_FAIL();
422+
}
423+
ofs_included << "movej([1,2,3,4,5,6])";
424+
ofs_included.close();
425+
426+
ScriptReader::DataDict data;
427+
data["ROBOT_VERSION"] = 10.8; // Set a version greater than 10.7 to include the script
428+
429+
std::string processed_script = reader.readScriptFile(existing_script_file, data);
430+
std::string expected_script = "textmsg(\"This is a test script\")\n"
431+
"movej([1,2,3,4,5,6])";
432+
EXPECT_EQ(processed_script, expected_script);
433+
434+
data["ROBOT_VERSION"] = 10.6;
435+
processed_script = reader.readScriptFile(existing_script_file, data);
436+
expected_script = "textmsg(\"This is a test script\")";
437+
EXPECT_EQ(processed_script, expected_script);
438+
439+
std::remove(existing_script_file);
440+
std::remove(existing_include_file);
394441
}

0 commit comments

Comments
 (0)