Skip to content

Commit

Permalink
prelude: Re-implement asserts.bzl
Browse files Browse the repository at this point in the history
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
  • Loading branch information
JakobDegen authored and facebook-github-bot committed Oct 21, 2024
1 parent 7bdd023 commit f68a1d2
Showing 1 changed file with 22 additions and 60 deletions.
82 changes: 22 additions & 60 deletions prelude/asserts.bzl
Original file line number Diff line number Diff line change
@@ -1,71 +1,33 @@
# Copyright 2017 The Bazel Authors. All rights reserved.
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# @lint-ignore-every LICENSELINT

"""Testing support.
This is a modified version of https://github.com/bazelbuild/bazel-skylib/blob/main/lib/unittest.bzl.
Currently, if there are any failures, these are raised immediately by calling fail(),
which trigger an analysis-time build error.
"""
# This source code is licensed under both the MIT license found in the
# LICENSE-MIT file in the root directory of this source tree and the Apache
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree.

def _assert_equals(expected, actual, msg = None):
"""Asserts that the given `expected` and `actual` are equal.
Args:
expected: the expected value of some computation.
actual: the actual value return by some computation.
msg: An optional message that will be printed that describes the failure.
If omitted, a default will be used.
"""
def _equals(expected, actual, msg = None):
if expected != actual:
expectation_msg = 'Expected "%s", but got "%s"' % (expected, actual)
if msg:
full_msg = "%s (%s)" % (msg, expectation_msg)
if msg == None:
fail("expected: {}, got: {}".format(expected, actual))
else:
full_msg = expectation_msg
fail(full_msg)

def _assert_true(
condition,
msg = "Expected condition to be true, but was false."):
"""Asserts that the given `condition` is true.
fail("{}: expected: {}, got: {}{}".format(msg, expected, actual))

Args:
condition: A value that will be evaluated in a Boolean context.
msg: An optional message that will be printed that describes the failure.
If omitted, a default will be used.
"""
def _true(condition, msg = None):
if not condition:
fail(msg)

def _assert_false(
condition,
msg = "Expected condition to be false, but was true."):
"""Asserts that the given `condition` is false.
if msg != None:
fail(msg)
else:
fail("Condition is not met")

Args:
condition: A value that will be evaluated in a Boolean context.
msg: An optional message that will be printed that describes the failure.
If omitted, a default will be used.
"""
def _false(condition, msg = None):
if condition:
fail(msg)
if msg != None:
fail(msg)
else:
fail("Condition is expected to be false")

asserts = struct(
equals = _assert_equals,
true = _assert_true,
false = _assert_false,
equals = _equals,
true = _true,
false = _false,
)

0 comments on commit f68a1d2

Please sign in to comment.