aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/libutil/escape-string.cc
blob: 5ce2b73d8a000b0b9f8cbbb89c888ad53d0c2ab5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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