Skip to content

Commit

Permalink
Merge branch 'moonbitlang:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
muqiuhan authored Mar 11, 2024
2 parents 1e5165c + b1fb170 commit e590dcb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
46 changes: 37 additions & 9 deletions assertion/assertion.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,38 @@ pub fn assert_eq[T : Debug + Eq](a : T, b : T) -> Result[Unit, String] {
}
}

test "assert_eq.eq" {
match assert_eq(1, 1) {
Ok(_) => ()
Err(msg) => return Err("unexpected failure: \(msg)")
}
}

test "assert_eq.ne" {
match assert_eq(1, 2) {
Ok(_) => return Err("unexpected success")
Err(_) => ()
}
}

pub fn assert_ne[T : Debug + Eq](a : T, b : T) -> Result[Unit, String] {
if a != b {
Ok(())
} else {
let a = debug_string(a)
let b = debug_string(b)
Err("assertion failed for `\(a) == \(b)`")
Err("assertion failed for `\(a) != \(b)`")
}
}

test "assert_ne.ne" {
assert_eq(assert_ne(1, 2), Ok(()))?
}

test "assert_ne.eq" {
match assert_ne(1, 1) {
Ok(_) => return Err("unexpected success")
Err(_) => ()
}
}

Expand All @@ -46,6 +71,14 @@ pub fn assert_false(x : Bool) -> Result[Unit, String] {
}
}

test "assert_false.false" {
assert_eq(assert_false(false), Ok(()))?
}

test "assert_false.true" {
assert_ne(assert_false(true), Ok(()))?
}

pub fn assert_true(x : Bool) -> Result[Unit, String] {
if x {
Ok(())
Expand All @@ -55,14 +88,9 @@ pub fn assert_true(x : Bool) -> Result[Unit, String] {
}

test "assert_true.true" {
assert_true(true)?
assert_eq(assert_true(true), Ok(()))?
}

test "assert_false.false" {
assert_false(false)?
}

test "assert_eq.eq" {
assert_eq(1, 1)?
assert_eq("123", "123")?
test "assert_true.false" {
assert_ne(assert_true(false), Ok(()))?
}
6 changes: 6 additions & 0 deletions list/list.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ pub fn to_array[T : Default](self : List[T]) -> Array[T] {
return arr
}

test "to array" {
let list = List::[1, 2, 3, 4, 5]
let array = to_array(list)
@assertion.assert_eq(array, [1, 2, 3, 4, 5])?
}

/// Filter the list.
///
/// # Example
Expand Down
6 changes: 2 additions & 4 deletions list/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{
"import": [
{ "path": "moonbitlang/core/assertion", "alias": "assertion" }
]
}
"import": ["moonbitlang/core/assertion", "moonbitlang/core/array"]
}

0 comments on commit e590dcb

Please sign in to comment.