hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

+test.ha (3017B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use fnmatch;
      5 use strings;
      6 
      7 @test fn glob() void = {
      8 	const cases: [_](str, flag) = [
      9 		("/u??/*in/a*", flag::NONE),
     10 		("/u*r/l?[bc]*/[bg]*", flag::NOSORT),
     11 		("/?sr/[sb]in/*[[:digit:]]*", flag::MARK),
     12 		("/h??\\e/*/.*", flag::NOSORT | flag::MARK),
     13 		("/\\h??e/*/.*", flag::NOSORT | flag::NOESCAPE),
     14 		("/r*/*", flag::NOSORT),
     15 		("/*.?a", flag::NOCHECK),
     16 		("./*.[[:alpha:]]a", flag::NONE),
     17 		("./\\a[bd]c", flag::NOESCAPE),
     18 		("./\\a[be]c", flag::NONE),
     19 		("[[:punct:]]*", flag::NONE),
     20 		("/", flag::NONE),
     21 		("//", flag::NONE),
     22 		(".", flag::NONE),
     23 		("..", flag::NONE),
     24 		("\\*", flag::NONE),
     25 	];
     26 	for (let c .. cases) {
     27 		let gen = glob(c.0, c.1);
     28 		defer finish(&gen);
     29 		for (true) match (next(&gen)) {
     30 		case done =>
     31 			break;
     32 		case failure =>
     33 			continue;
     34 		case let s: str =>
     35 			let bs = fnmatch::flag::PATHNAME;
     36 			if (c.1 & flag::NOESCAPE != 0) {
     37 				bs |= fnmatch::flag::NOESCAPE;
     38 			};
     39 			assert(fnmatch::fnmatch(c.0, s, bs)
     40 				|| c.1 & flag::MARK != 0
     41 				&& fnmatch::fnmatch(
     42 					c.0,
     43 					strings::rtrim(s, '/'),
     44 					bs
     45 				)
     46 			);
     47 		};
     48 	};
     49 };
     50 
     51 @test fn pattern_parse() void = {
     52 	const cases: [_](str, bool, str, str, str) = [
     53 		("foo/bar/baz", true, "foo/bar/baz", "", ""),
     54 		("foo/b\\ar/baz", true, "foo/b\\ar/baz", "", ""),
     55 		("foo/b\\ar/baz", false, "foo/bar/baz", "", ""),
     56 		("/foo/bar/baz", true, "/foo/bar/baz", "", ""),
     57 		("/foo\\/bar/baz", true, "/foo\\/bar/baz", "", ""),
     58 		("/foo\\/bar/baz", false, "/foo/bar/baz", "", ""),
     59 		("/foo/bar\\/baz", true, "/foo/bar\\/baz", "", ""),
     60 		("/foo/bar\\/baz", false, "/foo/bar/baz", "", ""),
     61 		("/foobarbaz", true, "/foobarbaz", "", ""),
     62 		("foo/bar/baz/", true, "foo/bar/baz/", "", ""),
     63 		("foobarbaz/", true, "foobarbaz/", "", ""),
     64 		("foobarbaz", true, "foobarbaz", "", ""),
     65 		("foo/b?r/baz", true, "foo/", "b?r/", "baz"),
     66 		("foo/b?r\\/baz", true, "foo/", "b?r\\/", "baz"),
     67 		("foo/b?r\\/baz", false, "foo/", "b?r/", "baz"),
     68 		("foob*rbaz/", true, "", "foob*rbaz/", ""),
     69 		("foo[bar]baz", true, "", "foo[bar]baz", ""),
     70 		("foo/b[ar]/baz/", true, "foo/", "b[ar]/", "baz/"),
     71 		("foo/b[a\\r]/baz/", false, "foo/", "b[a\\r]/", "baz/"),
     72 		("foo/b[a\\r]/baz/", true, "foo/", "b[a\\r]/", "baz/"),
     73 		("foo/b[ar]/baz\\/", true, "foo/", "b[ar]/", "baz\\/"),
     74 		("foo/b[ar]/baz\\/", false, "foo/", "b[ar]/", "baz\\/"),
     75 		("foo/b\\[ar]/baz\\/", true, "foo/", "b\\[ar]/", "baz\\/"),
     76 		("foo/b\\[ar]/baz\\/", false, "foo/b[ar]/baz/", "", ""),
     77 		("fo[o/ba[r/baz", true, "fo[o/ba[r/baz", "", ""),
     78 		("fo]o/bar/b[az", false, "fo]o/bar/b[az", "", ""),
     79 		("foo/ba]r/b]az", true, "foo/ba]r/b]az", "", ""),
     80 		("foo/ba[r/b]az", false, "foo/ba[r/", "b]az", ""),
     81 		("fo[o/bar/b]az", true, "fo[o/bar/", "b]az", ""),
     82 	];
     83 	let p = pattern_init();
     84 	defer pattern_free(&p);
     85 	for (let c .. cases) {
     86 		pattern_parse(&p, c.0, c.1);
     87 		const dir = pattern_dir(&p);
     88 		const pat = pattern_pat(&p);
     89 		const rem = pattern_rem(&p);
     90 		assert(dir == c.2);
     91 		assert(pat == c.3);
     92 		assert(rem == c.4);
     93 	};
     94 };