Skip to content

Commit 733e691

Browse files
committed
extend assert functions by add fail_msg~ argument
1 parent b48061b commit 733e691

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

builtin/assert.mbt

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,15 @@ fn[T : Show] debug_string(t : T) -> String {
4747
/// }
4848
/// ```
4949
#coverage.skip
50-
pub fn[T : Eq + Show] assert_eq(a : T, b : T, loc~ : SourceLoc = _) -> Unit! {
50+
pub fn[T : Eq + Show] assert_eq(
51+
a : T,
52+
b : T,
53+
fail_msg~ : String = "",
54+
loc~ : SourceLoc = _
55+
) -> Unit! {
5156
if a != b {
52-
fail("`\{a} != \{b}`", loc~)
57+
let fail_msg = if fail_msg == "" { "`\{a} != \{b}`" } else { fail_msg }
58+
fail(fail_msg, loc~)
5359
}
5460
}
5561
@@ -83,12 +89,14 @@ pub fn[T : Eq + Show] assert_eq(a : T, b : T, loc~ : SourceLoc = _) -> Unit! {
8389
pub fn[T : Eq + Show] assert_not_eq(
8490
a : T,
8591
b : T,
92+
fail_msg~ : String = "",
8693
loc~ : SourceLoc = _
8794
) -> Unit! {
8895
if not(a != b) {
8996
let a = debug_string(a)
9097
let b = debug_string(b)
91-
fail("`\{a} == \{b}`", loc~)
98+
let fail_msg = if fail_msg == "" { "`\{a} == \{b}`" } else { fail_msg }
99+
fail(fail_msg, loc~)
92100
}
93101
}
94102
@@ -117,9 +125,13 @@ pub fn[T : Eq + Show] assert_not_eq(
117125
/// }
118126
/// ```
119127
#coverage.skip
120-
pub fn assert_true(x : Bool, loc~ : SourceLoc = _) -> Unit! {
128+
pub fn assert_true(
129+
x : Bool,
130+
fail_msg~ : String = "",
131+
loc~ : SourceLoc = _
132+
) -> Unit! {
121133
if not(x) {
122-
fail("`\{x}` is not true", loc~)
134+
fail(fail_msg, loc~)
123135
}
124136
}
125137
@@ -149,9 +161,12 @@ pub fn assert_true(x : Bool, loc~ : SourceLoc = _) -> Unit! {
149161
/// }
150162
/// ```
151163
#coverage.skip
152-
pub fn assert_false(x : Bool, loc~ : SourceLoc = _) -> Unit! {
164+
pub fn assert_false(
165+
x : Bool,
166+
fail_msg~ : String = "",
167+
loc~ : SourceLoc = _
168+
) -> Unit! {
153169
if x {
154-
let x = debug_string(x)
155-
fail("`\{x}` is not false", loc~)
170+
fail(fail_msg, loc~)
156171
}
157172
}

builtin/builtin.mbti

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package "moonbitlang/core/builtin"
33
// Values
44
fn[T] abort(String) -> T
55

6-
fn[T : Eq + Show] assert_eq(T, T, loc~ : SourceLoc = _) -> Unit!
6+
fn[T : Eq + Show] assert_eq(T, T, fail_msg~ : String = .., loc~ : SourceLoc = _) -> Unit!
77

8-
fn assert_false(Bool, loc~ : SourceLoc = _) -> Unit!
8+
fn assert_false(Bool, fail_msg~ : String = .., loc~ : SourceLoc = _) -> Unit!
99

10-
fn[T : Eq + Show] assert_not_eq(T, T, loc~ : SourceLoc = _) -> Unit!
10+
fn[T : Eq + Show] assert_not_eq(T, T, fail_msg~ : String = .., loc~ : SourceLoc = _) -> Unit!
1111

12-
fn assert_true(Bool, loc~ : SourceLoc = _) -> Unit!
12+
fn assert_true(Bool, fail_msg~ : String = .., loc~ : SourceLoc = _) -> Unit!
1313

1414
#deprecated
1515
fn[T] dump(T, name? : String, loc~ : SourceLoc = _) -> T

prelude/prelude.mbti

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import(
77
// Values
88
fn[T] abort(String) -> T
99

10-
fn[T : @builtin.Eq + @builtin.Show] assert_eq(T, T, loc~ : @builtin.SourceLoc = _) -> Unit!
10+
fn[T : @builtin.Eq + @builtin.Show] assert_eq(T, T, fail_msg~ : String = .., loc~ : @builtin.SourceLoc = _) -> Unit!
1111

12-
fn assert_false(Bool, loc~ : @builtin.SourceLoc = _) -> Unit!
12+
fn assert_false(Bool, fail_msg~ : String = .., loc~ : @builtin.SourceLoc = _) -> Unit!
1313

14-
fn[T : @builtin.Eq + @builtin.Show] assert_not_eq(T, T, loc~ : @builtin.SourceLoc = _) -> Unit!
14+
fn[T : @builtin.Eq + @builtin.Show] assert_not_eq(T, T, fail_msg~ : String = .., loc~ : @builtin.SourceLoc = _) -> Unit!
1515

16-
fn assert_true(Bool, loc~ : @builtin.SourceLoc = _) -> Unit!
16+
fn assert_true(Bool, fail_msg~ : String = .., loc~ : @builtin.SourceLoc = _) -> Unit!
1717

1818
#deprecated
1919
fn[T] dump(T, name? : String, loc~ : @builtin.SourceLoc = _) -> T

0 commit comments

Comments
 (0)