aboutsummaryrefslogtreecommitdiff
path: root/tests/matching/matching_tests.bzl
blob: 6ef67e376fcdaa6ad309197d3fca80b407d0318d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""Tests for matchers."""

load("//lib:test_suite.bzl", "test_suite")
load("//lib:truth.bzl", "matching")

_tests = []

def _file(path):
    _, _, basename = path.rpartition("/")
    _, _, extension = basename.rpartition(".")
    return struct(
        path = path,
        basename = basename,
        extension = extension,
    )

def _verify_matcher(env, matcher, match_true, match_false):
    # Test positive match
    env.expect.where(matcher = matcher.desc, value = match_true).that_bool(
        matcher.match(match_true),
        expr = "matcher.match(value)",
    ).equals(True)

    # Test negative match
    env.expect.where(matcher = matcher.desc, value = match_false).that_bool(
        matcher.match(match_false),
        expr = "matcher.match(value)",
    ).equals(False)

def _contains_test(env):
    _verify_matcher(
        env,
        matching.contains("x"),
        match_true = "YYYxZZZ",
        match_false = "zzzzz",
    )

_tests.append(_contains_test)

def _file_basename_equals_test(env):
    _verify_matcher(
        env,
        matching.file_basename_equals("bar.txt"),
        match_true = _file("foo/bar.txt"),
        match_false = _file("foo/bar.md"),
    )

_tests.append(_file_basename_equals_test)

def _file_extension_in_test(env):
    _verify_matcher(
        env,
        matching.file_extension_in(["txt", "rst"]),
        match_true = _file("foo.txt"),
        match_false = _file("foo.py"),
    )

_tests.append(_file_extension_in_test)

def _is_in_test(env):
    _verify_matcher(
        env,
        matching.is_in(["a", "b"]),
        match_true = "a",
        match_false = "z",
    )

_tests.append(_is_in_test)

def _str_matchers_test(env):
    _verify_matcher(
        env,
        matching.str_matches("f*b"),
        match_true = "foobar",
        match_false = "nope",
    )

    _verify_matcher(
        env,
        matching.str_endswith("123"),
        match_true = "abc123",
        match_false = "123xxx",
    )

    _verify_matcher(
        env,
        matching.str_startswith("true"),
        match_true = "truechew",
        match_false = "notbuck",
    )

_tests.append(_str_matchers_test)

def matching_test_suite(name):
    test_suite(
        name = name,
        basic_tests = _tests,
    )