aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/libutil
diff options
context:
space:
mode:
authorRebecca Turner <rbt@sent.as>2024-03-28 16:26:42 -0700
committerjade <lix@jade.fyi>2024-04-03 16:25:54 +0000
commitea10fe7ab0df085b51189adabdb079fc4442c6be (patch)
tree7183a388ed91b09978fe333b880985ef3c2ed49f /tests/unit/libutil
parent56c7dfd65290dea957de6fad4e2df26f1630e407 (diff)
Add `EscapeStringOptions` and `escapeString` tests
Change-Id: I86ead2f969c9e03c9edfa51bbc92ee06393fd7d6
Diffstat (limited to 'tests/unit/libutil')
-rw-r--r--tests/unit/libutil/escape-string.cc35
1 files changed, 35 insertions, 0 deletions
diff --git a/tests/unit/libutil/escape-string.cc b/tests/unit/libutil/escape-string.cc
new file mode 100644
index 000000000..5ce2b73d8
--- /dev/null
+++ b/tests/unit/libutil/escape-string.cc
@@ -0,0 +1,35 @@
+#include "escape-string.hh"
+#include "ansicolor.hh"
+#include <gtest/gtest.h>
+
+namespace nix {
+
+TEST(EscapeString, simple) {
+ auto escaped = escapeString("puppy");
+ ASSERT_EQ(escaped, "\"puppy\"");
+}
+
+TEST(EscapeString, escaping) {
+ auto escaped = escapeString("\n\r\t \" \\ ${ooga booga}");
+ ASSERT_EQ(escaped, R"RAW("\n\r\t \" \\ \${ooga booga}")RAW");
+}
+
+TEST(EscapeString, maxLength) {
+ auto escaped = escapeString("puppy", {.maxLength = 5});
+ ASSERT_EQ(escaped, "\"puppy\"");
+
+ escaped = escapeString("puppy doggy", {.maxLength = 5});
+ ASSERT_EQ(escaped, "\"puppy\" «6 bytes elided»");
+}
+
+TEST(EscapeString, ansiColors) {
+ auto escaped = escapeString("puppy doggy", {.maxLength = 5, .outputAnsiColors = true});
+ ASSERT_EQ(escaped, ANSI_MAGENTA "\"puppy\" " ANSI_FAINT "«6 bytes elided»" ANSI_NORMAL);
+}
+
+TEST(EscapeString, escapeNonPrinting) {
+ auto escaped = escapeString("puppy\u0005doggy", {.escapeNonPrinting = true});
+ ASSERT_EQ(escaped, "\"puppy\\x05doggy\"");
+}
+
+} // namespace nix