Skip to content

Commit f68a1d2

Browse files
JakobDegenfacebook-github-bot
authored andcommitted
prelude: Re-implement asserts.bzl
Summary: This is a clean-room reimplementation of this module by Stiopa. The previous implementation was derived from some Bazel thing which did not have the right licenses to be included in the prelude Reviewed By: ndmitchell Differential Revision: D64676495 fbshipit-source-id: 4ef1bb9b3cd6cfaba72bf2b82986c730412fd999
1 parent 7bdd023 commit f68a1d2

File tree

1 file changed

+22
-60
lines changed

1 file changed

+22
-60
lines changed

prelude/asserts.bzl

Lines changed: 22 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,33 @@
1-
# Copyright 2017 The Bazel Authors. All rights reserved.
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
22
#
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.
237

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):
339
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))
3712
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))
4514

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):
5116
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")
5821

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):
6423
if condition:
65-
fail(msg)
24+
if msg != None:
25+
fail(msg)
26+
else:
27+
fail("Condition is expected to be false")
6628

6729
asserts = struct(
68-
equals = _assert_equals,
69-
true = _assert_true,
70-
false = _assert_false,
30+
equals = _equals,
31+
true = _true,
32+
false = _false,
7133
)

0 commit comments

Comments
 (0)