regex_test.cpp (5182B)
1 #include "regex_yaml.h" 2 #include "stream.h" 3 #include "gtest/gtest.h" 4 5 using YAML::RegEx; 6 using YAML::Stream; 7 8 namespace { 9 const auto MIN_CHAR = Stream::eof() + 1; 10 11 TEST(RegExTest, Empty) { 12 RegEx empty; 13 EXPECT_TRUE(empty.Matches(std::string())); 14 EXPECT_EQ(0, empty.Match(std::string())); 15 for (int i = MIN_CHAR; i < 128; ++i) { 16 auto str = std::string(1, char(i)); 17 EXPECT_FALSE(empty.Matches(str)); 18 EXPECT_EQ(-1, empty.Match(str)); 19 } 20 } 21 22 TEST(RegExTest, Range) { 23 for (int i = MIN_CHAR; i < 128; ++i) { 24 for (int j = MIN_CHAR; j < 128; ++j) { 25 RegEx ex((char)i, (char)j); 26 for (int k = MIN_CHAR; k < 128; ++k) { 27 auto str = std::string(1, char(k)); 28 if (i <= k && k <= j) { 29 EXPECT_TRUE(ex.Matches(str)); 30 EXPECT_EQ(1, ex.Match(str)); 31 } else { 32 EXPECT_FALSE(ex.Matches(str)); 33 EXPECT_EQ(-1, ex.Match(str)); 34 } 35 } 36 } 37 } 38 } 39 40 TEST(RegExTest, EmptyString) { 41 RegEx ex = RegEx(std::string()); 42 EXPECT_TRUE(ex.Matches(std::string())); 43 EXPECT_EQ(0, ex.Match(std::string())); 44 45 // Matches anything, unlike RegEx()! 46 EXPECT_TRUE(ex.Matches(std::string("hello"))); 47 EXPECT_EQ(0, ex.Match(std::string("hello"))); 48 } 49 50 TEST(RegExTest, SingleCharacterString) { 51 for (int i = MIN_CHAR; i < 128; ++i) { 52 RegEx ex(std::string(1, (char)i)); 53 for (int j = MIN_CHAR; j < 128; ++j) { 54 auto str = std::string(1, char(j)); 55 if (j == i) { 56 EXPECT_TRUE(ex.Matches(str)); 57 EXPECT_EQ(1, ex.Match(str)); 58 // Match at start of string only! 59 std::string prefixed = 60 std::string(1, i + 1) + std::string("prefix: ") + str; 61 EXPECT_FALSE(ex.Matches(prefixed)); 62 EXPECT_EQ(-1, ex.Match(prefixed)); 63 } else { 64 EXPECT_FALSE(ex.Matches(str)); 65 EXPECT_EQ(-1, ex.Match(str)); 66 } 67 } 68 } 69 } 70 71 TEST(RegExTest, MultiCharacterString) { 72 RegEx ex(std::string("ab")); 73 74 EXPECT_FALSE(ex.Matches(std::string("a"))); 75 EXPECT_EQ(-1, ex.Match(std::string("a"))); 76 77 EXPECT_TRUE(ex.Matches(std::string("ab"))); 78 EXPECT_EQ(2, ex.Match(std::string("ab"))); 79 EXPECT_TRUE(ex.Matches(std::string("abba"))); 80 EXPECT_EQ(2, ex.Match(std::string("abba"))); 81 82 // match at start of string only! 83 EXPECT_FALSE(ex.Matches(std::string("baab"))); 84 EXPECT_EQ(-1, ex.Match(std::string("baab"))); 85 } 86 87 TEST(RegExTest, OperatorNot) { 88 RegEx ex = !RegEx(std::string("ab")); 89 90 EXPECT_TRUE(ex.Matches(std::string("a"))); 91 EXPECT_EQ(1, ex.Match(std::string("a"))); 92 93 EXPECT_FALSE(ex.Matches(std::string("ab"))); 94 EXPECT_EQ(-1, ex.Match(std::string("ab"))); 95 EXPECT_FALSE(ex.Matches(std::string("abba"))); 96 EXPECT_EQ(-1, ex.Match(std::string("abba"))); 97 98 // match at start of string only! 99 EXPECT_TRUE(ex.Matches(std::string("baab"))); 100 // Operator not causes only one character to be matched. 101 EXPECT_EQ(1, ex.Match(std::string("baab"))); 102 } 103 104 TEST(RegExTest, OperatorOr) { 105 for (int i = MIN_CHAR; i < 127; ++i) { 106 for (int j = i + 1; j < 128; ++j) { 107 auto iStr = std::string(1, char(i)); 108 auto jStr = std::string(1, char(j)); 109 RegEx ex1 = RegEx(iStr) | RegEx(jStr); 110 RegEx ex2 = RegEx(jStr) | RegEx(iStr); 111 112 for (int k = MIN_CHAR; k < 128; ++k) { 113 auto str = std::string(1, char(k)); 114 if (i == k || j == k) { 115 EXPECT_TRUE(ex1.Matches(str)); 116 EXPECT_TRUE(ex2.Matches(str)); 117 EXPECT_EQ(1, ex1.Match(str)); 118 EXPECT_EQ(1, ex2.Match(str)); 119 } else { 120 EXPECT_FALSE(ex1.Matches(str)); 121 EXPECT_FALSE(ex2.Matches(str)); 122 EXPECT_EQ(-1, ex1.Match(str)); 123 EXPECT_EQ(-1, ex2.Match(str)); 124 } 125 } 126 } 127 } 128 } 129 130 TEST(RegExTest, OperatorOrShortCircuits) { 131 RegEx ex1 = RegEx(std::string("aaaa")) | RegEx(std::string("aa")); 132 RegEx ex2 = RegEx(std::string("aa")) | RegEx(std::string("aaaa")); 133 134 EXPECT_TRUE(ex1.Matches(std::string("aaaaa"))); 135 EXPECT_EQ(4, ex1.Match(std::string("aaaaa"))); 136 137 EXPECT_TRUE(ex2.Matches(std::string("aaaaa"))); 138 EXPECT_EQ(2, ex2.Match(std::string("aaaaa"))); 139 } 140 141 TEST(RegExTest, OperatorAnd) { 142 RegEx emptySet = RegEx('a') & RegEx(); 143 EXPECT_FALSE(emptySet.Matches(std::string("a"))); 144 } 145 146 TEST(RegExTest, OperatorAndShortCircuits) { 147 RegEx ex1 = RegEx(std::string("aaaa")) & RegEx(std::string("aa")); 148 RegEx ex2 = RegEx(std::string("aa")) & RegEx(std::string("aaaa")); 149 150 EXPECT_TRUE(ex1.Matches(std::string("aaaaa"))); 151 EXPECT_EQ(4, ex1.Match(std::string("aaaaa"))); 152 153 EXPECT_TRUE(ex2.Matches(std::string("aaaaa"))); 154 EXPECT_EQ(2, ex2.Match(std::string("aaaaa"))); 155 } 156 157 TEST(RegExTest, OperatorPlus) { 158 RegEx ex = RegEx(std::string("hello ")) + RegEx(std::string("there")); 159 160 EXPECT_TRUE(ex.Matches(std::string("hello there"))); 161 EXPECT_FALSE(ex.Matches(std::string("hello "))); 162 EXPECT_FALSE(ex.Matches(std::string("there"))); 163 EXPECT_EQ(11, ex.Match(std::string("hello there"))); 164 } 165 166 TEST(RegExTest, StringOr) { 167 std::string str = "abcde"; 168 RegEx ex = RegEx(str, YAML::REGEX_OR); 169 170 for (size_t i = 0; i < str.size(); ++i) { 171 EXPECT_TRUE(ex.Matches(str.substr(i, 1))); 172 EXPECT_EQ(1, ex.Match(str.substr(i, 1))); 173 } 174 175 EXPECT_EQ(1, ex.Match(str)); 176 } 177 } // namespace