|
1 |
| -# Copyright 2017 The Bazel Authors. All rights reserved. |
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
2 | 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 |
| -# @lint-ignore-every LICENSELINT |
16 |
| - |
17 |
| -"""Testing support. |
18 |
| -
|
19 |
| -This is a modified version of https://github.com/bazelbuild/bazel-skylib/blob/main/lib/unittest.bzl. |
20 |
| -Currently, if there are any failures, these are raised immediately by calling fail(), |
21 |
| -which trigger an analysis-time build error. |
22 |
| -""" |
| 3 | +# This source code is licensed under both the MIT license found in the |
| 4 | +# LICENSE-MIT file in the root directory of this source tree and the Apache |
| 5 | +# License, Version 2.0 found in the LICENSE-APACHE file in the root directory |
| 6 | +# of this source tree. |
23 | 7 |
|
24 |
| -def _assert_equals(expected, actual, msg = None): |
25 |
| - """Asserts that the given `expected` and `actual` are equal. |
26 |
| -
|
27 |
| - Args: |
28 |
| - expected: the expected value of some computation. |
29 |
| - actual: the actual value return by some computation. |
30 |
| - msg: An optional message that will be printed that describes the failure. |
31 |
| - If omitted, a default will be used. |
32 |
| - """ |
| 8 | +def _equals(expected, actual, msg = None): |
33 | 9 | if expected != actual:
|
34 |
| - expectation_msg = 'Expected "%s", but got "%s"' % (expected, actual) |
35 |
| - if msg: |
36 |
| - full_msg = "%s (%s)" % (msg, expectation_msg) |
| 10 | + if msg == None: |
| 11 | + fail("expected: {}, got: {}".format(expected, actual)) |
37 | 12 | else:
|
38 |
| - full_msg = expectation_msg |
39 |
| - fail(full_msg) |
40 |
| - |
41 |
| -def _assert_true( |
42 |
| - condition, |
43 |
| - msg = "Expected condition to be true, but was false."): |
44 |
| - """Asserts that the given `condition` is true. |
| 13 | + fail("{}: expected: {}, got: {}{}".format(msg, expected, actual)) |
45 | 14 |
|
46 |
| - Args: |
47 |
| - condition: A value that will be evaluated in a Boolean context. |
48 |
| - msg: An optional message that will be printed that describes the failure. |
49 |
| - If omitted, a default will be used. |
50 |
| - """ |
| 15 | +def _true(condition, msg = None): |
51 | 16 | if not condition:
|
52 |
| - fail(msg) |
53 |
| - |
54 |
| -def _assert_false( |
55 |
| - condition, |
56 |
| - msg = "Expected condition to be false, but was true."): |
57 |
| - """Asserts that the given `condition` is false. |
| 17 | + if msg != None: |
| 18 | + fail(msg) |
| 19 | + else: |
| 20 | + fail("Condition is not met") |
58 | 21 |
|
59 |
| - Args: |
60 |
| - condition: A value that will be evaluated in a Boolean context. |
61 |
| - msg: An optional message that will be printed that describes the failure. |
62 |
| - If omitted, a default will be used. |
63 |
| - """ |
| 22 | +def _false(condition, msg = None): |
64 | 23 | if condition:
|
65 |
| - fail(msg) |
| 24 | + if msg != None: |
| 25 | + fail(msg) |
| 26 | + else: |
| 27 | + fail("Condition is expected to be false") |
66 | 28 |
|
67 | 29 | asserts = struct(
|
68 |
| - equals = _assert_equals, |
69 |
| - true = _assert_true, |
70 |
| - false = _assert_false, |
| 30 | + equals = _equals, |
| 31 | + true = _true, |
| 32 | + false = _false, |
71 | 33 | )
|
0 commit comments