1 #include <msp/strings/utils.h>
2 #include <msp/test/test.h>
7 class StringUtilsTests: public Test::RegisteredTest<StringUtilsTests>
10 static const char *strings[];
15 static const char *get_name() { return "stringutils"; }
20 void unescape_invalid();
24 const char *StringUtilsTests::strings[] =
26 "new\nline", "new\\nline",
28 "\"quotes'", "\\\"quotes\\'",
29 "back\\slash", "back\\\\slash",
30 "\001ACTION ducks\001", "\\001ACTION ducks\\001",
31 "g\303\266tterd\303\244mmerung", "g\\303\\266tterd\\303\\244mmerung",
35 StringUtilsTests::StringUtilsTests()
37 add(&StringUtilsTests::escape, "c_escape");
38 add(&StringUtilsTests::unescape, "c_unescape");
39 add(&StringUtilsTests::unescape_invalid, "c_unescape (invalid)").expect_throw<invalid_argument>();
40 add(&StringUtilsTests::split, "split");
43 void StringUtilsTests::escape()
45 for(const char **i=strings; *i; i+=2)
47 string str = c_escape(*i);
48 expect_equal(str, str==*(i+1), format("str == '%s'", *(i+1)));
52 void StringUtilsTests::unescape()
54 for(const char **i=strings; *i; i+=2)
56 string str = c_unescape(*(i+1));
57 expect_equal(str, str==*i, format("str == '%s'", *i));
61 void StringUtilsTests::unescape_invalid()
66 void StringUtilsTests::split()
68 vector<string> parts = ::split("foo bar baz");
69 EXPECT_EQUAL(parts.size(), 3);
70 EXPECT_EQUAL(parts[0], "foo");
71 EXPECT_EQUAL(parts[1], "bar");
72 EXPECT_EQUAL(parts[2], "baz");
74 parts = split_fields("one::three", ':');
75 EXPECT_EQUAL(parts.size(), 3);
76 EXPECT_EQUAL(parts[0], "one");
77 EXPECT(parts[1].empty());
78 EXPECT_EQUAL(parts[2], "three");