1 // 2 // Automated Testing Framework (atf) 3 // 4 // Copyright (c) 2007 The NetBSD Foundation, Inc. 5 // All rights reserved. 6 // 7 // Redistribution and use in source and binary forms, with or without 8 // modification, are permitted provided that the following conditions 9 // are met: 10 // 1. Redistributions of source code must retain the above copyright 11 // notice, this list of conditions and the following disclaimer. 12 // 2. Redistributions in binary form must reproduce the above copyright 13 // notice, this list of conditions and the following disclaimer in the 14 // documentation and/or other materials provided with the distribution. 15 // 16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY 21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 // 29 30 #include <cstring> 31 32 #include "../macros.hpp" 33 34 #include "expand.hpp" 35 36 // XXX Many of the tests here are duplicated in atf-c/t_expand. Should 37 // find a way to easily share them, or maybe remove the ones here. 38 39 // ------------------------------------------------------------------------ 40 // Test cases for the free functions. 41 // ------------------------------------------------------------------------ 42 43 ATF_TEST_CASE(is_glob); 44 ATF_TEST_CASE_HEAD(is_glob) 45 { 46 set_md_var("descr", "Tests the is_glob function."); 47 } 48 ATF_TEST_CASE_BODY(is_glob) 49 { 50 using atf::expand::is_glob; 51 52 ATF_REQUIRE(!is_glob("")); 53 ATF_REQUIRE(!is_glob("a")); 54 ATF_REQUIRE(!is_glob("foo")); 55 56 ATF_REQUIRE( is_glob("*")); 57 ATF_REQUIRE( is_glob("a*")); 58 ATF_REQUIRE( is_glob("*a")); 59 ATF_REQUIRE( is_glob("a*b")); 60 61 ATF_REQUIRE( is_glob("?")); 62 ATF_REQUIRE( is_glob("a?")); 63 ATF_REQUIRE( is_glob("?a")); 64 ATF_REQUIRE( is_glob("a?b")); 65 } 66 67 ATF_TEST_CASE(matches_glob_plain); 68 ATF_TEST_CASE_HEAD(matches_glob_plain) 69 { 70 set_md_var("descr", "Tests the matches_glob function by using plain " 71 "text strings as patterns only."); 72 } 73 ATF_TEST_CASE_BODY(matches_glob_plain) 74 { 75 using atf::expand::matches_glob; 76 77 ATF_REQUIRE( matches_glob("", "")); 78 ATF_REQUIRE(!matches_glob("a", "")); 79 ATF_REQUIRE(!matches_glob("", "a")); 80 81 ATF_REQUIRE( matches_glob("ab", "ab")); 82 ATF_REQUIRE(!matches_glob("abc", "ab")); 83 ATF_REQUIRE(!matches_glob("ab", "abc")); 84 } 85 86 ATF_TEST_CASE(matches_glob_star); 87 ATF_TEST_CASE_HEAD(matches_glob_star) 88 { 89 set_md_var("descr", "Tests the matches_glob function by using the '*' " 90 "meta-character as part of the pattern."); 91 } 92 ATF_TEST_CASE_BODY(matches_glob_star) 93 { 94 using atf::expand::matches_glob; 95 96 ATF_REQUIRE( matches_glob("*", "")); 97 ATF_REQUIRE( matches_glob("*", "a")); 98 ATF_REQUIRE( matches_glob("*", "ab")); 99 100 ATF_REQUIRE(!matches_glob("a*", "")); 101 ATF_REQUIRE( matches_glob("a*", "a")); 102 ATF_REQUIRE( matches_glob("a*", "aa")); 103 ATF_REQUIRE( matches_glob("a*", "ab")); 104 ATF_REQUIRE( matches_glob("a*", "abc")); 105 ATF_REQUIRE(!matches_glob("a*", "ba")); 106 107 ATF_REQUIRE( matches_glob("*a", "a")); 108 ATF_REQUIRE( matches_glob("*a", "ba")); 109 ATF_REQUIRE(!matches_glob("*a", "bc")); 110 ATF_REQUIRE(!matches_glob("*a", "bac")); 111 112 ATF_REQUIRE( matches_glob("*ab", "ab")); 113 ATF_REQUIRE( matches_glob("*ab", "aab")); 114 ATF_REQUIRE( matches_glob("*ab", "aaab")); 115 ATF_REQUIRE( matches_glob("*ab", "bab")); 116 ATF_REQUIRE(!matches_glob("*ab", "bcb")); 117 ATF_REQUIRE(!matches_glob("*ab", "bacb")); 118 119 ATF_REQUIRE( matches_glob("a*b", "ab")); 120 ATF_REQUIRE( matches_glob("a*b", "acb")); 121 ATF_REQUIRE( matches_glob("a*b", "acdeb")); 122 ATF_REQUIRE(!matches_glob("a*b", "acdebz")); 123 ATF_REQUIRE(!matches_glob("a*b", "zacdeb")); 124 } 125 126 ATF_TEST_CASE(matches_glob_question); 127 ATF_TEST_CASE_HEAD(matches_glob_question) 128 { 129 set_md_var("descr", "Tests the matches_glob function by using the '?' " 130 "meta-character as part of the pattern."); 131 } 132 ATF_TEST_CASE_BODY(matches_glob_question) 133 { 134 using atf::expand::matches_glob; 135 136 ATF_REQUIRE(!matches_glob("?", "")); 137 ATF_REQUIRE( matches_glob("?", "a")); 138 ATF_REQUIRE(!matches_glob("?", "ab")); 139 140 ATF_REQUIRE( matches_glob("?", "b")); 141 ATF_REQUIRE( matches_glob("?", "c")); 142 143 ATF_REQUIRE( matches_glob("a?", "ab")); 144 ATF_REQUIRE( matches_glob("a?", "ac")); 145 ATF_REQUIRE(!matches_glob("a?", "ca")); 146 147 ATF_REQUIRE( matches_glob("???", "abc")); 148 ATF_REQUIRE( matches_glob("???", "def")); 149 ATF_REQUIRE(!matches_glob("???", "a")); 150 ATF_REQUIRE(!matches_glob("???", "ab")); 151 ATF_REQUIRE(!matches_glob("???", "abcd")); 152 } 153 154 ATF_TEST_CASE(expand_glob_base); 155 ATF_TEST_CASE_HEAD(expand_glob_base) 156 { 157 set_md_var("descr", "Tests the expand_glob function with random " 158 "patterns."); 159 } 160 ATF_TEST_CASE_BODY(expand_glob_base) 161 { 162 using atf::expand::expand_glob; 163 164 std::vector< std::string > candidates; 165 candidates.push_back("foo"); 166 candidates.push_back("bar"); 167 candidates.push_back("baz"); 168 candidates.push_back("foobar"); 169 candidates.push_back("foobarbaz"); 170 candidates.push_back("foobarbazfoo"); 171 172 std::vector< std::string > exps; 173 174 exps = expand_glob("foo", candidates); 175 ATF_REQUIRE_EQ(exps.size(), 1); 176 ATF_REQUIRE(exps[0] == "foo"); 177 178 exps = expand_glob("bar", candidates); 179 ATF_REQUIRE_EQ(exps.size(), 1); 180 ATF_REQUIRE(exps[0] == "bar"); 181 182 exps = expand_glob("foo*", candidates); 183 ATF_REQUIRE_EQ(exps.size(), 4); 184 ATF_REQUIRE(exps[0] == "foo"); 185 ATF_REQUIRE(exps[1] == "foobar"); 186 ATF_REQUIRE(exps[2] == "foobarbaz"); 187 ATF_REQUIRE(exps[3] == "foobarbazfoo"); 188 189 exps = expand_glob("*foo", candidates); 190 ATF_REQUIRE_EQ(exps.size(), 2); 191 ATF_REQUIRE(exps[0] == "foo"); 192 ATF_REQUIRE(exps[1] == "foobarbazfoo"); 193 194 exps = expand_glob("*foo*", candidates); 195 ATF_REQUIRE_EQ(exps.size(), 4); 196 ATF_REQUIRE(exps[0] == "foo"); 197 ATF_REQUIRE(exps[1] == "foobar"); 198 ATF_REQUIRE(exps[2] == "foobarbaz"); 199 ATF_REQUIRE(exps[3] == "foobarbazfoo"); 200 201 exps = expand_glob("ba", candidates); 202 ATF_REQUIRE_EQ(exps.size(), 0); 203 204 exps = expand_glob("ba*", candidates); 205 ATF_REQUIRE_EQ(exps.size(), 2); 206 ATF_REQUIRE(exps[0] == "bar"); 207 ATF_REQUIRE(exps[1] == "baz"); 208 209 exps = expand_glob("*ba", candidates); 210 ATF_REQUIRE_EQ(exps.size(), 0); 211 212 exps = expand_glob("*ba*", candidates); 213 ATF_REQUIRE_EQ(exps.size(), 5); 214 ATF_REQUIRE(exps[0] == "bar"); 215 ATF_REQUIRE(exps[1] == "baz"); 216 ATF_REQUIRE(exps[2] == "foobar"); 217 ATF_REQUIRE(exps[3] == "foobarbaz"); 218 ATF_REQUIRE(exps[4] == "foobarbazfoo"); 219 } 220 221 ATF_TEST_CASE(expand_glob_tps); 222 ATF_TEST_CASE_HEAD(expand_glob_tps) 223 { 224 set_md_var("descr", "Tests the expand_glob function with patterns that " 225 "match typical test program names. This is just a subcase " 226 "of expand_base, but it is nice to make sure that it really " 227 "works."); 228 } 229 ATF_TEST_CASE_BODY(expand_glob_tps) 230 { 231 using atf::expand::expand_glob; 232 233 std::vector< std::string > candidates; 234 candidates.push_back("Atffile"); 235 candidates.push_back("h_foo"); 236 candidates.push_back("t_foo"); 237 candidates.push_back("t_bar"); 238 candidates.push_back("t_baz"); 239 candidates.push_back("foo_helper"); 240 candidates.push_back("foo_test"); 241 candidates.push_back("bar_test"); 242 candidates.push_back("baz_test"); 243 244 std::vector< std::string > exps; 245 246 exps = expand_glob("t_*", candidates); 247 ATF_REQUIRE_EQ(exps.size(), 3); 248 ATF_REQUIRE(exps[0] == "t_foo"); 249 ATF_REQUIRE(exps[1] == "t_bar"); 250 ATF_REQUIRE(exps[2] == "t_baz"); 251 252 exps = expand_glob("*_test", candidates); 253 ATF_REQUIRE_EQ(exps.size(), 3); 254 ATF_REQUIRE(exps[0] == "foo_test"); 255 ATF_REQUIRE(exps[1] == "bar_test"); 256 ATF_REQUIRE(exps[2] == "baz_test"); 257 } 258 259 // ------------------------------------------------------------------------ 260 // Main. 261 // ------------------------------------------------------------------------ 262 263 ATF_INIT_TEST_CASES(tcs) 264 { 265 // Add the tests for the free functions. 266 ATF_ADD_TEST_CASE(tcs, is_glob); 267 ATF_ADD_TEST_CASE(tcs, matches_glob_plain); 268 ATF_ADD_TEST_CASE(tcs, matches_glob_star); 269 ATF_ADD_TEST_CASE(tcs, matches_glob_question); 270 ATF_ADD_TEST_CASE(tcs, expand_glob_base); 271 ATF_ADD_TEST_CASE(tcs, expand_glob_tps); 272 } 273