]> git.tdb.fi Git - libs/core.git/blob - tests/stringutils.cpp
Add utility functions to search and sort containers based on a member
[libs/core.git] / tests / stringutils.cpp
1 #include <msp/strings/utils.h>
2 #include <msp/test/test.h>
3
4 using namespace std;
5 using namespace Msp;
6
7 class StringUtilsTests: public Test::RegisteredTest<StringUtilsTests>
8 {
9 private:
10         static const char *strings[];
11
12 public:
13         StringUtilsTests();
14
15         static const char *get_name() { return "stringutils"; }
16
17 private:
18         void escape();
19         void unescape();
20         void unescape_invalid();
21         void split();
22 };
23
24 const char *StringUtilsTests::strings[] =
25 {
26         "new\nline", "new\\nline",
27         "\ttab", "\\ttab",
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",
32         0
33 };
34
35 StringUtilsTests::StringUtilsTests()
36 {
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");
41 }
42
43 void StringUtilsTests::escape()
44 {
45         for(const char **i=strings; *i; i+=2)
46         {
47                 string str = c_escape(*i);
48                 expect_equal(str, str==*(i+1), format("str == '%s'", *(i+1)));
49         }
50 }
51
52 void StringUtilsTests::unescape()
53 {
54         for(const char **i=strings; *i; i+=2)
55         {
56                 string str = c_unescape(*(i+1));
57                 expect_equal(str, str==*i, format("str == '%s'", *i));
58         }
59 }
60
61 void StringUtilsTests::unescape_invalid()
62 {
63         c_unescape("\\q");
64 }
65
66 void StringUtilsTests::split()
67 {
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");
73
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");
79 }