Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2020-12-10 05:33:55 +0300
committerCampbell Barton <ideasman42@gmail.com>2020-12-10 06:40:01 +0300
commit7fc1d760378f5c538c44bc4730c98af820678e4d (patch)
tree6980ad22bf2811344c4c4e61610dc173ad2edef5 /source/blender/blenlib/tests/BLI_string_test.cc
parent65f139117db4aa23f0a59aba788cec843a43b098 (diff)
Fix BLI_str_escape with control characters, add unit tests
Diffstat (limited to 'source/blender/blenlib/tests/BLI_string_test.cc')
-rw-r--r--source/blender/blenlib/tests/BLI_string_test.cc66
1 files changed, 66 insertions, 0 deletions
diff --git a/source/blender/blenlib/tests/BLI_string_test.cc b/source/blender/blenlib/tests/BLI_string_test.cc
index 58135adbcca..7ef2f5a888e 100644
--- a/source/blender/blenlib/tests/BLI_string_test.cc
+++ b/source/blender/blenlib/tests/BLI_string_test.cc
@@ -802,3 +802,69 @@ TEST_F(StringCasecmpNatural, TextAndNumbers)
testReturnsLessThanZeroForAll(negative);
testReturnsMoreThanZeroForAll(positive);
}
+
+/* BLI_str_escape */
+
+class StringEscape : public testing::Test {
+ protected:
+ StringEscape()
+ {
+ }
+
+ using CompareWordsArray = vector<std::array<const char *, 2>>;
+
+ void testEscapeWords(const CompareWordsArray &items)
+ {
+ size_t dst_test_len;
+ char dst_test[64];
+ for (const auto &item : items) {
+ /* Escape the string. */
+ dst_test_len = BLI_str_escape(dst_test, item[0], SIZE_MAX);
+ EXPECT_STREQ(dst_test, item[1]);
+ EXPECT_EQ(dst_test_len, strlen(dst_test));
+ }
+ }
+};
+
+TEST_F(StringEscape, Simple)
+{
+ const CompareWordsArray equal{
+ {"", ""},
+ {"/", "/"},
+ {"'", "'"},
+ {"?", "?"},
+ };
+
+ const CompareWordsArray escaped{
+ {"\\", "\\\\"},
+ {"A\\", "A\\\\"},
+ {"\\A", "\\\\A"},
+ {"A\\B", "A\\\\B"},
+ {"?", "?"},
+ {"\"\\", "\\\"\\\\"},
+ {"\\\"", "\\\\\\\""},
+ {"\"\\\"", "\\\"\\\\\\\""},
+
+ {"\"\"\"", "\\\"\\\"\\\""},
+ {"\\\\\\", "\\\\\\\\\\\\"},
+ };
+
+ testEscapeWords(equal);
+ testEscapeWords(escaped);
+}
+
+TEST_F(StringEscape, Control)
+{
+ const CompareWordsArray escaped{
+ {"\n", "\\n"},
+ {"\r", "\\r"},
+ {"\t", "\\t"},
+ {"A\n", "A\\n"},
+ {"\nA", "\\nA"},
+ {"\n\r\t", "\\n\\r\\t"},
+ {"\n_\r_\t", "\\n_\\r_\\t"},
+ {"\n\\\r\\\t", "\\n\\\\\\r\\\\\\t"},
+ };
+
+ testEscapeWords(escaped);
+}