• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Copyright (C) 2022 The Android Open Source Project
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7     http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14"""
15
16load("@bazel_skylib//lib:paths.bzl", "paths")
17load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
18load(":yasm.bzl", "yasm")
19
20def _basic_yasm_test_impl(ctx):
21    env = analysistest.begin(ctx)
22    actions = analysistest.target_actions(env)
23
24    for action in actions:
25        asserts.equals(
26            env,
27            action.mnemonic,
28            "yasm",
29        )
30        src = action.argv[-1]
31        asserts.equals(
32            env,
33            action.argv[-3],
34            "-o",
35        )
36        asserts.true(
37            env,
38            action.argv[-2].endswith(paths.replace_extension(src, ".o")),
39            "-o argument is expected to end with the src file as a .o",
40        )
41        asserts.true(
42            env,
43            " ".join(ctx.attr.expected_flags) in " ".join(action.argv),
44            "Expected flags (%s) were not in actual flags (%s)" % (ctx.attr.expected_flags, action.argv),
45        )
46
47    return analysistest.end(env)
48
49basic_yasm_test = analysistest.make(
50    _basic_yasm_test_impl,
51    attrs = {
52        "expected_flags": attr.string_list(
53            doc = "Flags expected to be on the command line.",
54        ),
55    },
56)
57
58def test_single_file():
59    name = "test_single_file"
60    yasm(
61        name = name + "_target",
62        srcs = [name + "_file.asm"],
63        tags = ["manual"],
64    )
65    basic_yasm_test(
66        name = name,
67        target_under_test = name + "_target",
68    )
69    return name
70
71def test_multiple_files():
72    name = "test_multiple_files"
73    yasm(
74        name = name + "_target",
75        srcs = [
76            name + "_file1.asm",
77            name + "_file2.asm",
78        ],
79        tags = ["manual"],
80    )
81    basic_yasm_test(
82        name = name,
83        target_under_test = name + "_target",
84    )
85    return name
86
87def test_custom_flags():
88    name = "test_custom_flags"
89    yasm(
90        name = name + "_target",
91        srcs = [name + "_file.asm"],
92        flags = ["-DNEON_INTRINSICS", "-mfpu=neon"],
93        tags = ["manual"],
94    )
95    basic_yasm_test(
96        name = name,
97        target_under_test = name + "_target",
98        expected_flags = ["-DNEON_INTRINSICS", "-mfpu=neon"],
99    )
100    return name
101
102def test_include_dirs():
103    name = "test_include_dirs"
104    yasm(
105        name = name + "_target",
106        srcs = [name + "_file.asm"],
107        include_dirs = ["foo/bar"],
108        tags = ["manual"],
109    )
110    basic_yasm_test(
111        name = name,
112        target_under_test = name + "_target",
113        expected_flags = ["-Ibuild/bazel/rules/cc/foo/bar"],
114    )
115    return name
116
117def yasm_test_suite(name):
118    native.test_suite(
119        name = name,
120        tests = [
121            test_single_file(),
122            test_multiple_files(),
123            test_custom_flags(),
124            test_include_dirs(),
125        ],
126    )
127