xref: /aosp_15_r20/external/bazelbuild-rules_rust/test/unit/platform_triple/platform_triple_test.bzl (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1"""Tests for the platform triple constructor"""
2
3load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
4load("//rust/platform:triple.bzl", "triple")
5load("//rust/platform:triple_mappings.bzl", "SUPPORTED_PLATFORM_TRIPLES")
6
7def _construct_platform_triple_test_impl(ctx):
8    env = unittest.begin(ctx)
9
10    imaginary_triple = triple("arch-vendor-system-abi")
11
12    asserts.equals(
13        env,
14        "arch",
15        imaginary_triple.arch,
16    )
17
18    asserts.equals(
19        env,
20        "vendor",
21        imaginary_triple.vendor,
22    )
23
24    asserts.equals(
25        env,
26        "system",
27        imaginary_triple.system,
28    )
29
30    asserts.equals(
31        env,
32        "abi",
33        imaginary_triple.abi,
34    )
35
36    asserts.equals(
37        env,
38        "arch-vendor-system-abi",
39        imaginary_triple.str,
40    )
41
42    return unittest.end(env)
43
44def _construct_minimal_platform_triple_test_impl(ctx):
45    env = unittest.begin(ctx)
46
47    imaginary_triple = triple("arch-vendor-system")
48
49    asserts.equals(
50        env,
51        "arch",
52        imaginary_triple.arch,
53    )
54
55    asserts.equals(
56        env,
57        "vendor",
58        imaginary_triple.vendor,
59    )
60
61    asserts.equals(
62        env,
63        "system",
64        imaginary_triple.system,
65    )
66
67    asserts.equals(
68        env,
69        None,
70        imaginary_triple.abi,
71    )
72
73    asserts.equals(
74        env,
75        "arch-vendor-system",
76        imaginary_triple.str,
77    )
78
79    return unittest.end(env)
80
81def _supported_platform_triples_test_impl(ctx):
82    env = unittest.begin(ctx)
83
84    for supported_triple in SUPPORTED_PLATFORM_TRIPLES:
85        asserts.equals(
86            env,
87            supported_triple,
88            triple(supported_triple).str,
89        )
90
91    return unittest.end(env)
92
93def _assert_parts(env, triple, arch, vendor, system, abi):
94    asserts.equals(
95        env,
96        arch,
97        triple.arch,
98        "{} did not parse {} correctly".format(triple.str, "arch"),
99    )
100    asserts.equals(
101        env,
102        vendor,
103        triple.vendor,
104        "{} did not parse {} correctly".format(triple.str, "vendor"),
105    )
106    asserts.equals(
107        env,
108        system,
109        triple.system,
110        "{} did not parse {} correctly".format(triple.str, "system"),
111    )
112    asserts.equals(
113        env,
114        abi,
115        triple.abi,
116        "{} did not parse {} correctly".format(triple.str, "abi"),
117    )
118
119def _construct_known_triples_test_impl(ctx):
120    env = unittest.begin(ctx)
121
122    _assert_parts(env, triple("aarch64-apple-darwin"), "aarch64", "apple", "darwin", None)
123    _assert_parts(env, triple("aarch64-fuchsia"), "aarch64", "fuchsia", "fuchsia", None)
124    _assert_parts(env, triple("aarch64-unknown-linux-musl"), "aarch64", "unknown", "linux", "musl")
125    _assert_parts(env, triple("thumbv7em-none-eabi"), "thumbv7em", None, "none", "eabi")
126    _assert_parts(env, triple("thumbv8m.main-none-eabi"), "thumbv8m.main", None, "none", "eabi")
127    _assert_parts(env, triple("wasm32-unknown-unknown"), "wasm32", "unknown", "unknown", None)
128    _assert_parts(env, triple("wasm32-wasi"), "wasm32", "wasi", "wasi", None)
129    _assert_parts(env, triple("x86_64-fuchsia"), "x86_64", "fuchsia", "fuchsia", None)
130
131    return unittest.end(env)
132
133construct_platform_triple_test = unittest.make(_construct_platform_triple_test_impl)
134construct_minimal_platform_triple_test = unittest.make(_construct_minimal_platform_triple_test_impl)
135supported_platform_triples_test = unittest.make(_supported_platform_triples_test_impl)
136construct_known_triples_test = unittest.make(_construct_known_triples_test_impl)
137
138def platform_triple_test_suite(name, **kwargs):
139    """Define a test suite for testing the `triple` constructor
140
141    Args:
142        name (str): The name of the test suite.
143        **kwargs (dict): Additional keyword arguments for the test_suite.
144    """
145    construct_platform_triple_test(
146        name = "construct_platform_triple_test",
147    )
148    construct_minimal_platform_triple_test(
149        name = "construct_minimal_platform_triple_test",
150    )
151    supported_platform_triples_test(
152        name = "supported_platform_triples_test",
153    )
154    construct_known_triples_test(
155        name = "construct_known_triples_test",
156    )
157
158    native.test_suite(
159        name = name,
160        tests = [
161            ":construct_platform_triple_test",
162            ":construct_minimal_platform_triple_test",
163            ":supported_platform_triples_test",
164            ":construct_known_triples_test",
165        ],
166        **kwargs
167    )
168