xref: /aosp_15_r20/external/bazelbuild-rules_android/test/rules/android_revision/test.bzl (revision 9e965d6fece27a77de5377433c2f7e6999b8cc0b)
1# Copyright 2023 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#    http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Bazel rules that test the Android revision parsing.
16
17The following are test rules that can be used to test the AndroidRevisionInfo provider.
18
19android_revision_test: Inspect providers with the given set of expected values.
20"""
21
22load(
23    "//rules:android_revision.bzl",
24    "compare_android_revisions",
25    "parse_android_revision",
26)
27load(
28    "//test/utils:lib.bzl",
29    "asserts",
30    "unittest",
31)
32
33def _android_revision_test_impl(ctx):
34    env = unittest.begin(ctx)
35    input = ctx.attr.input
36    revision = parse_android_revision(input)
37
38    asserts.equals(
39        env,
40        ctx.attr.expected_major,
41        revision.major,
42    )
43    asserts.equals(
44        env,
45        ctx.attr.expected_minor,
46        revision.minor,
47    )
48    asserts.equals(
49        env,
50        ctx.attr.expected_micro,
51        revision.micro,
52    )
53    asserts.equals(
54        env,
55        ctx.attr.expected_version,
56        revision.version,
57    )
58    asserts.equals(
59        env,
60        ctx.attr.expected_dir,
61        revision.dir,
62    )
63
64    return unittest.end(env)
65
66android_revision_test = unittest.make(
67    impl = _android_revision_test_impl,
68    attrs = {
69        "input": attr.string(),
70        "expected_major": attr.int(),
71        "expected_minor": attr.int(),
72        "expected_micro": attr.int(),
73        "expected_version": attr.string(),
74        "expected_dir": attr.string(),
75    },
76)
77
78def _assert_revisions_equal(env, expected, value):
79    asserts.equals(env, expected.major, value.major)
80    asserts.equals(env, expected.minor, value.minor)
81    asserts.equals(env, expected.major, value.major)
82
83def _android_revision_comparision_test_impl(ctx):
84    env = unittest.begin(ctx)
85    higher = parse_android_revision(ctx.attr.higher)
86    lower = parse_android_revision(ctx.attr.lower)
87
88    result = compare_android_revisions(higher, lower)
89    _assert_revisions_equal(
90        env,
91        higher,
92        result,
93    )
94
95    return unittest.end(env)
96
97android_revision_comparision_test = unittest.make(
98    impl = _android_revision_comparision_test_impl,
99    attrs = {
100        "higher": attr.string(),
101        "lower": attr.string(),
102    },
103)
104