xref: /aosp_15_r20/external/bazelbuild-rules_testing/tests/matching/matching_tests.bzl (revision d605057434dcabba796c020773aab68d9790ff9f)
1"""Tests for matchers."""
2
3load("//lib:test_suite.bzl", "test_suite")
4load("//lib:truth.bzl", "matching")
5
6_tests = []
7
8def _file(path):
9    _, _, basename = path.rpartition("/")
10    _, _, extension = basename.rpartition(".")
11    return struct(
12        path = path,
13        basename = basename,
14        extension = extension,
15    )
16
17def _verify_matcher(env, matcher, match_true, match_false):
18    # Test positive match
19    env.expect.where(matcher = matcher.desc, value = match_true).that_bool(
20        matcher.match(match_true),
21        expr = "matcher.match(value)",
22    ).equals(True)
23
24    # Test negative match
25    env.expect.where(matcher = matcher.desc, value = match_false).that_bool(
26        matcher.match(match_false),
27        expr = "matcher.match(value)",
28    ).equals(False)
29
30def _contains_test(env):
31    _verify_matcher(
32        env,
33        matching.contains("x"),
34        match_true = "YYYxZZZ",
35        match_false = "zzzzz",
36    )
37
38_tests.append(_contains_test)
39
40def _file_basename_equals_test(env):
41    _verify_matcher(
42        env,
43        matching.file_basename_equals("bar.txt"),
44        match_true = _file("foo/bar.txt"),
45        match_false = _file("foo/bar.md"),
46    )
47
48_tests.append(_file_basename_equals_test)
49
50def _file_extension_in_test(env):
51    _verify_matcher(
52        env,
53        matching.file_extension_in(["txt", "rst"]),
54        match_true = _file("foo.txt"),
55        match_false = _file("foo.py"),
56    )
57
58_tests.append(_file_extension_in_test)
59
60def _is_in_test(env):
61    _verify_matcher(
62        env,
63        matching.is_in(["a", "b"]),
64        match_true = "a",
65        match_false = "z",
66    )
67
68_tests.append(_is_in_test)
69
70def _str_matchers_test(env):
71    _verify_matcher(
72        env,
73        matching.str_matches("f*b"),
74        match_true = "foobar",
75        match_false = "nope",
76    )
77
78    _verify_matcher(
79        env,
80        matching.str_endswith("123"),
81        match_true = "abc123",
82        match_false = "123xxx",
83    )
84
85    _verify_matcher(
86        env,
87        matching.str_startswith("true"),
88        match_true = "truechew",
89        match_false = "notbuck",
90    )
91
92_tests.append(_str_matchers_test)
93
94def matching_test_suite(name):
95    test_suite(
96        name = name,
97        basic_tests = _tests,
98    )
99