Skip to content

Commit 25a0dfd

Browse files
committed
extend assert functions by add fail_msg~ argument
1 parent 11f1cdb commit 25a0dfd

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

builtin/assert.mbt

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,14 @@ 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 = "`\{debug_string(a)} != \{debug_string(b)}`",
54+
loc~ : SourceLoc = _
55+
) -> Unit! {
5156
if a != b {
52-
fail("`\{a} != \{b}`", loc~)
57+
fail(fail_msg, loc~)
5358
}
5459
}
5560
@@ -83,12 +88,11 @@ pub fn[T : Eq + Show] assert_eq(a : T, b : T, loc~ : SourceLoc = _) -> Unit! {
8388
pub fn[T : Eq + Show] assert_not_eq(
8489
a : T,
8590
b : T,
91+
fail_msg~ : String = "`\{debug_string(a)} == \{debug_string(b)}`",
8692
loc~ : SourceLoc = _
8793
) -> Unit! {
8894
if not(a != b) {
89-
let a = debug_string(a)
90-
let b = debug_string(b)
91-
fail("`\{a} == \{b}`", loc~)
95+
fail(fail_msg, loc~)
9296
}
9397
}
9498
@@ -117,9 +121,13 @@ pub fn[T : Eq + Show] assert_not_eq(
117121
/// }
118122
/// ```
119123
#coverage.skip
120-
pub fn assert_true(x : Bool, loc~ : SourceLoc = _) -> Unit! {
124+
pub fn assert_true(
125+
x : Bool,
126+
fail_msg~ : String = "`\{x}` is not true",
127+
loc~ : SourceLoc = _
128+
) -> Unit! {
121129
if not(x) {
122-
fail("`\{x}` is not true", loc~)
130+
fail(fail_msg, loc~)
123131
}
124132
}
125133
@@ -149,9 +157,12 @@ pub fn assert_true(x : Bool, loc~ : SourceLoc = _) -> Unit! {
149157
/// }
150158
/// ```
151159
#coverage.skip
152-
pub fn assert_false(x : Bool, loc~ : SourceLoc = _) -> Unit! {
160+
pub fn assert_false(
161+
x : Bool,
162+
fail_msg~ : String = "`\{x}` is not false",
163+
loc~ : SourceLoc = _
164+
) -> Unit! {
153165
if x {
154-
let x = debug_string(x)
155-
fail("`\{x}` is not false", loc~)
166+
fail(fail_msg, loc~)
156167
}
157168
}

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)