From c830f0c569e751b9fcca68b00181877b2020fc9e Mon Sep 17 00:00:00 2001 From: Fantix King Date: Sat, 9 Mar 2024 12:25:21 -0500 Subject: [PATCH 1/3] Fix error message of assert_ne() to use `!=` --- assertion/assertion.mbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assertion/assertion.mbt b/assertion/assertion.mbt index b5b34563f..7b6f09fb9 100644 --- a/assertion/assertion.mbt +++ b/assertion/assertion.mbt @@ -34,7 +34,7 @@ pub fn assert_ne[T : Debug + Eq](a : T, b : T) -> Result[Unit, String] { } else { let a = debug_string(a) let b = debug_string(b) - Err("assertion failed for `\(a) == \(b)`") + Err("assertion failed for `\(a) != \(b)`") } } From 1ed3347d44d8b1b23b46430d6c980b9738f8b968 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=9C=E7=81=AF?= Date: Sat, 9 Mar 2024 22:24:11 +0800 Subject: [PATCH 2/3] test(list): add test for to_array --- list/list.mbt | 6 ++++++ list/moon.pkg.json | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/list/list.mbt b/list/list.mbt index 75d8d9c62..9a999c261 100644 --- a/list/list.mbt +++ b/list/list.mbt @@ -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 diff --git a/list/moon.pkg.json b/list/moon.pkg.json index 461766b4f..96c642ffd 100644 --- a/list/moon.pkg.json +++ b/list/moon.pkg.json @@ -1,5 +1,6 @@ { "import": [ - { "path": "moonbitlang/core/assertion", "alias": "assertion" } + { "path": "moonbitlang/core/assertion", "alias": "assertion" }, + { "path": "moonbitlang/core/array", "alias": "array" } ] } \ No newline at end of file From 8ad7cfe771cda2a93fc8440024e530c48fe8ed2f Mon Sep 17 00:00:00 2001 From: Fantix King Date: Sat, 9 Mar 2024 12:23:55 -0500 Subject: [PATCH 3/3] Fix tests in @assertion --- assertion/assertion.mbt | 44 +++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/assertion/assertion.mbt b/assertion/assertion.mbt index 7b6f09fb9..14c1f6541 100644 --- a/assertion/assertion.mbt +++ b/assertion/assertion.mbt @@ -28,6 +28,20 @@ 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(()) @@ -38,6 +52,17 @@ pub fn assert_ne[T : Debug + Eq](a : T, b : T) -> Result[Unit, String] { } } +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(_) => () + } +} + pub fn assert_false(x : Bool) -> Result[Unit, String] { if x == false { Ok(()) @@ -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(()) @@ -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(()))? }