diff options
author | Rebecca Turner <rbt@sent.as> | 2024-03-28 16:26:42 -0700 |
---|---|---|
committer | jade <lix@jade.fyi> | 2024-04-03 16:25:54 +0000 |
commit | ea10fe7ab0df085b51189adabdb079fc4442c6be (patch) | |
tree | 7183a388ed91b09978fe333b880985ef3c2ed49f /tests/unit | |
parent | 56c7dfd65290dea957de6fad4e2df26f1630e407 (diff) |
Add `EscapeStringOptions` and `escapeString` tests
Change-Id: I86ead2f969c9e03c9edfa51bbc92ee06393fd7d6
Diffstat (limited to 'tests/unit')
-rw-r--r-- | tests/unit/libutil/escape-string.cc | 35 | ||||
-rw-r--r-- | tests/unit/meson.build | 1 |
2 files changed, 36 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 diff --git a/tests/unit/meson.build b/tests/unit/meson.build index a5523a813..60bb2de89 100644 --- a/tests/unit/meson.build +++ b/tests/unit/meson.build @@ -39,6 +39,7 @@ libutil_tests_sources = files( 'libutil/closure.cc', 'libutil/compression.cc', 'libutil/config.cc', + 'libutil/escape-string.cc', 'libutil/git.cc', 'libutil/hash.cc', 'libutil/hilite.cc', |