| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
|
| 3 |
// Copyright (C) 2025 Aleksa Sarai <[email protected]> |
| 4 |
// Copyright (C) 2025 SUSE LLC |
| 5 |
// |
| 6 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
|
| 10 |
// Package assert provides some basic assertion helpers for Go. |
| 11 |
package assert |
| 12 |
|
| 13 |
import ( |
| 14 |
"fmt" |
| 15 |
) |
| 16 |
|
| 17 |
// Assert panics if the predicate is false with the provided argument. |
| 18 |
func Assert(predicate bool, msg any) { |
| 19 |
if !predicate { |
| 20 |
panic(msg) |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
// Assertf panics if the predicate is false and formats the message using the |
| 25 |
// same formatting as [fmt.Printf]. |
| 26 |
// |
| 27 |
// [fmt.Printf]: https://pkg.go.dev/fmt#Printf |
| 28 |
func Assertf(predicate bool, fmtMsg string, args ...any) { |
| 29 |
Assert(predicate, fmt.Sprintf(fmtMsg, args...)) |
| 30 |
} |