diff --git a/array/array.mbt b/array/array.mbt index 59296cc54..6560a6ee1 100644 --- a/array/array.mbt +++ b/array/array.mbt @@ -18,11 +18,16 @@ pub fn iter[T](self : Array[T], f : (T) -> Unit) { } } -test "iteri" { +test "iter" { let mut i = 0 + let mut failed = false [1, 2, 3, 4, 5].iter( - fn(elem) { @assertion.assert_eq(elem, i + 1); i = i + 1 }, + fn(elem) { + if elem != i + 1 { failed = true } + i = i + 1 + } ) + if failed { return Err("iter test failed") } } /// Iterates over the array with index. @@ -49,13 +54,14 @@ pub fn iteri[T](self : Array[T], f : (Int, T) -> Unit) { test "iteri" { let mut i = 0 + let mut failed = false [1, 2, 3, 4, 5].iteri( fn(index, elem) { - @assertion.assert_eq(index, i) - @assertion.assert_eq(elem, i + 1) + if index != i || elem != i + 1 { failed = true } i = i + 1 }, ) + if failed { return Err("iteri test failed") } } /// Applies a function to each element of the array and returns a new array with the results. @@ -80,7 +86,7 @@ pub fn map[T, U](self : Array[T], f : (T) -> U) -> Array[U] { test "map" { let arr = [1, 2, 3, 4, 5] let doubled = arr.map(fn(x) { x * 2 }) - @assertion.assert_eq(doubled, [2, 4, 6, 8, 10]) + @assertion.assert_eq(doubled, [2, 4, 6, 8, 10])? } pub fn map_with_index[T, U](self : Array[T], f : (T, Int) -> U) -> Array[U] { @@ -97,7 +103,7 @@ pub fn map_with_index[T, U](self : Array[T], f : (T, Int) -> U) -> Array[U] { test "map with index" { let arr = [1, 2, 3, 4, 5] let doubled = arr.map_with_index(fn(x, i) { x * 2 + i }) - @assertion.assert_eq(doubled, [2, 5, 8, 11, 14]) + @assertion.assert_eq(doubled, [2, 5, 8, 11, 14])? } pub fn op_equal[T : Eq](self : Array[T], that : Array[T]) -> Bool { @@ -132,7 +138,7 @@ pub fn new[T](length : Int, value : () -> T) -> Array[T] { test "new" { let arr = new(2, fn() { { val: 1 } }) - @assertion.assert_false(arr[0] === arr[1]) + @assertion.assert_false(arr[0] === arr[1])? } /// Create a new array. Values are built from indexes. @@ -153,6 +159,6 @@ pub fn new_with_index[T](length : Int, value : (Int) -> T) -> Array[T] { test "new index" { let arr = new_with_index(2, fn { i => i }) - @assertion.assert_eq(arr[0], 0) - @assertion.assert_eq(arr[1], 1) + @assertion.assert_eq(arr[0], 0)? + @assertion.assert_eq(arr[1], 1)? } diff --git a/assertion/assertion.mbt b/assertion/assertion.mbt index e081e084f..6c029212e 100644 --- a/assertion/assertion.mbt +++ b/assertion/assertion.mbt @@ -1,45 +1,55 @@ -pub fn assert_eq[T : Debug + Eq](a : T, b : T) { +fn debug_string[T : Debug](t : T) -> String { + let buf = Buffer::make(50) + t.debug_write(buf) + buf.to_string() +} + +pub fn assert_eq[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)`") + } +} + +pub fn assert_ne[T : Debug + Eq](a : T, b : T) -> Result[Unit,String] { if a != b { - let buf = Buffer::make(100) - "assertion failed: ".debug_write(buf) - a.debug_write(buf) - " == ".debug_write(buf) - b.debug_write(buf) - "\n".debug_write(buf) - abort(buf.to_string()) + Ok(()) + } else { + let a = debug_string(a) + let b = debug_string(b) + Err("assertion failed for `\(a) == \(b)`") } } -pub fn assert_false(b : Bool) { - if b { - let buf = Buffer::make(100) - "assertion failed: ".debug_write(buf) - b.debug_write(buf) - " == false".debug_write(buf) - "\n".debug_write(buf) - abort(buf.to_string()) +pub fn assert_false(x : Bool) -> Result[Unit,String] { + if x == false { + Ok(()) + } else { + Err("assert_false failed") } } -pub fn assert_true(b : Bool) { - if not (b) { - let buf = Buffer::make(100) - buf.write_string("assertion failed: ") - b.debug_write(buf) - buf.write_string(" == true\n") - abort(buf.to_string()) +pub fn assert_true(x : Bool) -> Result[Unit,String] { + if x { + Ok(()) + } else { + Err("assert_true failed") } } test "assert_true.true" { - assert_true(true) + assert_true(true)? } test "assert_false.false" { - assert_false(false) + assert_false(false)? } test "assert_eq.eq" { - assert_eq(1, 1) + assert_eq(1, 1)? + assert_eq("123","123")? } diff --git a/list/list.mbt b/list/list.mbt index 92d26daf0..45f244f8e 100644 --- a/list/list.mbt +++ b/list/list.mbt @@ -3,10 +3,10 @@ /// # Example /// /// ``` -/// let ls = of_array([1, 2, 3, 4, 5]) -/// println(ls) // output: of_array([1, 2, 3, 4, 5]) +/// let ls = from_array([1, 2, 3, 4, 5]) +/// println(ls) // output: from_array([1, 2, 3, 4, 5]) /// ``` -pub fn of_array[T](arr : Array[T]) -> List[T] { +pub fn from_array[T](arr : Array[T]) -> List[T] { let mut list = List::Nil for i = arr.length() - 1; i >= 0; i = i - 1 { list = List::Cons(arr[i], list) @@ -14,9 +14,9 @@ pub fn of_array[T](arr : Array[T]) -> List[T] { return list } -test "of_array" { - let ls = of_array([1, 2, 3, 4, 5]) - @assertion.assert_eq(ls, Cons(1, Cons(2, Cons(3, Cons(4, Cons(5, Nil)))))) +test "from_array" { + let ls = from_array([1, 2, 3, 4, 5]) + @assertion.assert_eq(ls, Cons(1, Cons(2, Cons(3, Cons(4, Cons(5, Nil))))))? } /// Get the length of the list. @@ -28,8 +28,8 @@ pub fn length[T](self : List[T]) -> Int { } test "length" { - let ls = of_array([1, 2, 3, 4, 5]) - @assertion.assert_eq(ls.length(), 5) + let ls = from_array([1, 2, 3, 4, 5]) + @assertion.assert_eq(ls.length(), 5)? } /// Iterates over the list. @@ -37,7 +37,7 @@ test "length" { /// # Example /// /// ``` -/// of_array([1, 2, 3, 4, 5]).iter(print) // output: 12345 +/// from_array([1, 2, 3, 4, 5]).iter(print) // output: 12345 /// ``` pub fn iter[T](self : List[T], f : (T) -> Unit) { match self { @@ -50,9 +50,11 @@ pub fn iter[T](self : List[T], f : (T) -> Unit) { } test "iter" { - let ls = of_array([1, 2, 3, 4, 5]) + let ls = from_array([1, 2, 3, 4, 5]) let mut i = 0 - ls.iter(fn(x) { @assertion.assert_eq(x, i + 1); i = i + 1 }) + let mut failed = false + ls.iter(fn(x) { i = i + 1; if x != i { failed = true }}) + if failed { return Err("iter test failed") } } /// Iterates over the list with index. @@ -60,7 +62,7 @@ test "iter" { /// # Example /// /// ``` -/// of_array([1, 2, 3, 4, 5]).iteri(fn(i, x) { print("(\(i),\(x)) ") }) +/// from_array([1, 2, 3, 4, 5]).iteri(fn(i, x) { print("(\(i),\(x)) ") }) /// // output: (0,1) (1,2) (2,3) (3,4) (4,5) /// ``` pub fn iteri[T](self : List[T], f : (Int, T) -> Unit) { @@ -70,14 +72,15 @@ pub fn iteri[T](self : List[T], f : (Int, T) -> Unit) { test "iteri" { let mut v = 0 - let ls = of_array([1, 2, 3, 4, 5]) + let mut failed = false + let ls = from_array([1, 2, 3, 4, 5]) ls.iteri( fn(i, x) { - @assertion.assert_eq(x, v + 1) - @assertion.assert_eq(i, v) + if (x != v + 1 || i != v) { failed = true } v = v + 1 - }, + } ) + if failed { return Err("iteri test failed") } } /// Maps the list. @@ -85,8 +88,8 @@ test "iteri" { /// # Example /// /// ``` -/// debug(of_array([1, 2, 3, 4, 5]).map(fn(x){ x * 2})) -/// // output: of_array([2, 4, 6, 8, 10]) +/// debug(from_array([1, 2, 3, 4, 5]).map(fn(x){ x * 2})) +/// // output: from_array([2, 4, 6, 8, 10]) /// ``` pub fn map[T, U](self : List[T], f : (T) -> U) -> List[U] { match self { @@ -96,9 +99,9 @@ pub fn map[T, U](self : List[T], f : (T) -> U) -> List[U] { } test "map" { - let ls = of_array([1, 2, 3, 4, 5]) - let rs = of_array([2, 4, 6, 8, 10]) - @assertion.assert_eq(ls.map(fn(x) { x * 2 }), rs) + let ls = from_array([1, 2, 3, 4, 5]) + let rs = from_array([2, 4, 6, 8, 10]) + @assertion.assert_eq(ls.map(fn(x) { x * 2 }), rs)? } /// Convert list to array. @@ -113,8 +116,8 @@ pub fn to_array[T : Default](self : List[T]) -> Array[T] { /// # Example /// /// ``` -/// debug(of_array([1, 2, 3, 4, 5]).filter(fn(x){ x % 2 == 0})) -/// // output: of_array([2, 4]) +/// debug(from_array([1, 2, 3, 4, 5]).filter(fn(x){ x % 2 == 0})) +/// // output: from_array([2, 4]) /// ``` pub fn filter[T](self : List[T], f : (T) -> Bool) -> List[T] { match self { @@ -129,9 +132,9 @@ pub fn filter[T](self : List[T], f : (T) -> Bool) -> List[T] { } test "filter" { - let ls = of_array([1, 2, 3, 4, 5]) - let rs = of_array([2, 4]) - @assertion.assert_eq(ls.filter(fn(x) { x % 2 == 0 }), rs) + let ls = from_array([1, 2, 3, 4, 5]) + let rs = from_array([2, 4]) + @assertion.assert_eq(ls.filter(fn(x) { x % 2 == 0 }), rs)? } /// Tail of the list. @@ -139,8 +142,8 @@ test "filter" { /// # Example /// /// ``` -/// debug(of_array([1, 2, 3, 4, 5]).tail()) -/// // output: of_array([2, 3, 4, 5]) +/// debug(from_array([1, 2, 3, 4, 5]).tail()) +/// // output: from_array([2, 3, 4, 5]) /// ``` pub fn tail[T](self : List[T]) -> List[T] { match self { @@ -150,9 +153,9 @@ pub fn tail[T](self : List[T]) -> List[T] { } test "tail" { - let ls = of_array([1, 2, 3, 4, 5]) - let rs = of_array([2, 3, 4, 5]) - @assertion.assert_eq(ls.tail(), rs) + let ls = from_array([1, 2, 3, 4, 5]) + let rs = from_array([2, 3, 4, 5]) + @assertion.assert_eq(ls.tail(), rs)? } /// Get first element of the list. @@ -160,7 +163,7 @@ test "tail" { /// # Example /// /// ``` -/// debug(of_array([1, 2, 3, 4, 5]).head()) +/// debug(from_array([1, 2, 3, 4, 5]).head()) /// // output: 1 /// ``` pub fn head[T](self : List[T]) -> T { @@ -171,8 +174,8 @@ pub fn head[T](self : List[T]) -> T { } test "head" { - let ls = of_array([1, 2, 3, 4, 5]) - @assertion.assert_eq(ls.head(), 1) + let ls = from_array([1, 2, 3, 4, 5]) + @assertion.assert_eq(ls.head(), 1)? } /// Get first element of the list. @@ -180,9 +183,9 @@ test "head" { /// # Example /// /// ``` -/// debug(of_array([1, 2, 3, 4, 5]).head()) +/// debug(from_array([1, 2, 3, 4, 5]).head()) /// // output: Some(1) -/// debug(of_array([]).head()) +/// debug(from_array([]).head()) /// // output: None /// ``` pub fn head_option[T](self : List[T]) -> Option[T] { @@ -193,10 +196,10 @@ pub fn head_option[T](self : List[T]) -> Option[T] { } test "head option" { - let ls = of_array([1, 2, 3, 4, 5]) - @assertion.assert_eq(ls.head_option(), Some(1)) - let ls2 : List[Unit] = of_array([]) - @assertion.assert_eq(ls2, Nil) + let ls = from_array([1, 2, 3, 4, 5]) + @assertion.assert_eq(ls.head_option(), Some(1))? + let ls2 : List[Unit] = from_array([]) + @assertion.assert_eq(ls2, Nil)? } /// Last element of the list. @@ -204,7 +207,7 @@ test "head option" { /// # Example /// /// ``` -/// debug(of_array([1, 2, 3, 4, 5]).last()) +/// debug(from_array([1, 2, 3, 4, 5]).last()) /// // output: 5 /// ``` pub fn last[T](self : List[T]) -> T { @@ -216,8 +219,8 @@ pub fn last[T](self : List[T]) -> T { } test "last" { - let ls = of_array([1, 2, 3, 4, 5]) - @assertion.assert_eq(ls.last(), 5) + let ls = from_array([1, 2, 3, 4, 5]) + @assertion.assert_eq(ls.last(), 5)? } /// Init of the list. @@ -225,8 +228,8 @@ test "last" { /// # Example /// /// ``` -/// debug(of_array([1, 2, 3, 4, 5]).init_()) -/// // output: of_array([1, 2, 3, 4]) +/// debug(from_array([1, 2, 3, 4, 5]).init_()) +/// // output: from_array([1, 2, 3, 4]) /// ``` pub fn init_[T](self : List[T]) -> List[T] { match self { @@ -237,9 +240,9 @@ pub fn init_[T](self : List[T]) -> List[T] { } test "init_" { - let ls = of_array([1, 2, 3, 4, 5]) - let rs = of_array([1, 2, 3, 4]) - @assertion.assert_eq(ls.init_(), rs) + let ls = from_array([1, 2, 3, 4, 5]) + let rs = from_array([1, 2, 3, 4]) + @assertion.assert_eq(ls.init_(), rs)? } /// Concatenate two lists. @@ -247,8 +250,8 @@ test "init_" { /// # Example /// /// ``` -/// let ls = of_array([1, 2, 3, 4, 5]).concat(of_array([6, 7, 8, 9, 10])) -/// debug(ls) // output: of_array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) +/// let ls = from_array([1, 2, 3, 4, 5]).concat(from_array([6, 7, 8, 9, 10])) +/// debug(ls) // output: from_array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) /// ``` pub fn concat[T](self : List[T], other : List[T]) -> List[T] { match self { @@ -258,10 +261,10 @@ pub fn concat[T](self : List[T], other : List[T]) -> List[T] { } test "concat" { - let ls = of_array([1, 2, 3, 4, 5]) - let rs = of_array([6, 7, 8, 9, 10]) - let ts = of_array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) - @assertion.assert_eq(ls.concat(rs), ts) + let ls = from_array([1, 2, 3, 4, 5]) + let rs = from_array([6, 7, 8, 9, 10]) + let ts = from_array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) + @assertion.assert_eq(ls.concat(rs), ts)? } /// Reverse the list. @@ -269,8 +272,8 @@ test "concat" { /// # Example /// /// ``` -/// debug(of_array([1, 2, 3, 4, 5]).reverse()) -/// // output: of_array([5, 4, 3, 2, 1]) +/// debug(from_array([1, 2, 3, 4, 5]).reverse()) +/// // output: from_array([5, 4, 3, 2, 1]) /// ``` pub fn reverse[T](self : List[T]) -> List[T] { match self { @@ -280,8 +283,8 @@ pub fn reverse[T](self : List[T]) -> List[T] { } test "reverse" { - let ls = of_array([1, 2, 3, 4, 5]) - @assertion.assert_eq(ls, ls.reverse().reverse()) + let ls = from_array([1, 2, 3, 4, 5]) + @assertion.assert_eq(ls, ls.reverse().reverse())? } /// Fold the list. @@ -289,7 +292,7 @@ test "reverse" { /// # Example /// /// ``` -/// let r = of_array([1, 2, 3, 4, 5]).fold(0, fn(acc, x) { acc + x }) +/// let r = from_array([1, 2, 3, 4, 5]).fold(0, fn(acc, x) { acc + x }) /// debug(r) // output: 15 /// ``` pub fn fold[T, U](self : List[T], initial : U, f : (U, T) -> U) -> U { @@ -300,8 +303,8 @@ pub fn fold[T, U](self : List[T], initial : U, f : (U, T) -> U) -> U { } test "fold" { - let ls = of_array([1, 2, 3, 4, 5]) - @assertion.assert_eq(ls.fold(0, fn(acc, x) { acc + x }), 15) + let ls = from_array([1, 2, 3, 4, 5]) + @assertion.assert_eq(ls.fold(0, fn(acc, x) { acc + x }), 15)? } /// Zip two lists. @@ -309,8 +312,8 @@ test "fold" { /// # Example /// /// ``` -/// let r = zip(of_array([1, 2, 3, 4, 5]), of_array([6, 7, 8, 9, 10])) -/// debug(r) // output: of_array([(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)] +/// let r = zip(from_array([1, 2, 3, 4, 5]), from_array([6, 7, 8, 9, 10])) +/// debug(r) // output: from_array([(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)] /// ``` /// /// # Panics @@ -332,9 +335,9 @@ pub fn zip[T, U](self : List[T], other : List[U]) -> List[(T, U)] { /// # Example /// /// ``` -/// let ls = of_array([1, 2, 3]) +/// let ls = from_array([1, 2, 3]) /// let r = ls.concat_map(fn(x) { [x, x * 2] }) -/// debug(r) // output: of_array([1, 2, 2, 4, 3, 6]) +/// debug(r) // output: from_array([1, 2, 2, 4, 3, 6]) /// ``` pub fn concat_map[T, U](self : List[T], f : (T) -> List[U]) -> List[U] { match self { @@ -344,9 +347,9 @@ pub fn concat_map[T, U](self : List[T], f : (T) -> List[U]) -> List[U] { } test "concat_map" { - let ls = of_array([1, 2, 3]) - let rs = of_array([1, 2, 2, 4, 3, 6]) - @assertion.assert_eq(ls.concat_map(fn(x) { of_array([x, x * 2]) }), rs) + let ls = from_array([1, 2, 3]) + let rs = from_array([1, 2, 2, 4, 3, 6]) + @assertion.assert_eq(ls.concat_map(fn(x) { from_array([x, x * 2]) }), rs)? } /// Get nth element of the list @@ -358,12 +361,12 @@ pub fn nth[T](self : List[T], n : Int) -> T { } test "nth" { - let ls = of_array([1, 2, 3, 4, 5]) - @assertion.assert_eq(ls.nth(0), 1) - @assertion.assert_eq(ls.nth(1), 2) - @assertion.assert_eq(ls.nth(2), 3) - @assertion.assert_eq(ls.nth(3), 4) - @assertion.assert_eq(ls.nth(4), 5) + let ls = from_array([1, 2, 3, 4, 5]) + @assertion.assert_eq(ls.nth(0), 1)? + @assertion.assert_eq(ls.nth(1), 2)? + @assertion.assert_eq(ls.nth(2), 3)? + @assertion.assert_eq(ls.nth(3), 4)? + @assertion.assert_eq(ls.nth(4), 5)? } /// Create a list of length n with the given value @@ -371,7 +374,7 @@ test "nth" { /// # Example /// /// ``` -/// debug(repeat(5, 1)) // output: of_array([1, 1, 1, 1, 1]) +/// debug(repeat(5, 1)) // output: from_array([1, 1, 1, 1, 1]) /// ``` pub fn repeat[T](n : Int, x : T) -> List[T] { if n == 0 { @@ -382,8 +385,8 @@ pub fn repeat[T](n : Int, x : T) -> List[T] { } test "repeat" { - let ls = of_array([1, 1, 1, 1, 1]) - @assertion.assert_eq(repeat(5, 1), ls) + let ls = from_array([1, 1, 1, 1, 1]) + @assertion.assert_eq(repeat(5, 1), ls)? } /// Insert separator to the list. @@ -391,8 +394,8 @@ test "repeat" { /// # Example /// /// ``` -/// let ls = of_array(["1", "2", "3", "4", "5"]).separate_by("|") -/// debug(ls) // output: of_array(["1", "|", "2", "|", "3", "|", "4", "|", "5"]) +/// let ls = from_array(["1", "2", "3", "4", "5"]).separate_by("|") +/// debug(ls) // output: from_array(["1", "|", "2", "|", "3", "|", "4", "|", "5"]) /// ``` pub fn separate_by[T](self : List[T], seperator : T) -> List[T] { match self { @@ -404,8 +407,8 @@ pub fn separate_by[T](self : List[T], seperator : T) -> List[T] { } test "separate_by" { - let ls = of_array(["1", "|", "2", "|", "3", "|", "4", "|", "5"]) - @assertion.assert_eq(of_array(["1", "2", "3", "4", "5"]).separate_by("|"), ls) + let ls = from_array(["1", "|", "2", "|", "3", "|", "4", "|", "5"]) + @assertion.assert_eq(from_array(["1", "2", "3", "4", "5"]).separate_by("|"), ls)? } /// Check if the list is empty. @@ -417,9 +420,9 @@ pub fn is_empty[T](self : List[T]) -> Bool { } test "is_empty" { - let ls = of_array([1, 2, 3, 4, 5]) - @assertion.assert_false(ls.is_empty()) - @assertion.assert_true((List::Nil : List[Unit]).is_empty()) + let ls = from_array([1, 2, 3, 4, 5]) + @assertion.assert_false(ls.is_empty())? + @assertion.assert_true((List::Nil : List[Unit]).is_empty())? } /// Unzip two lists. @@ -427,9 +430,9 @@ test "is_empty" { /// # Example /// /// ``` -/// let (a,b) = unzip(of_array([(1,2),(3,4),(5,6)])) -/// debug(a) // output: of_array([1, 3, 5]) -/// debug(b) // output: of_array([2, 4, 6]) +/// let (a,b) = unzip(from_array([(1,2),(3,4),(5,6)])) +/// debug(a) // output: from_array([1, 3, 5]) +/// debug(b) // output: from_array([2, 4, 6]) /// ``` pub fn unzip[T, U](list : List[(T, U)]) -> (List[T], List[U]) { match list { @@ -442,10 +445,10 @@ pub fn unzip[T, U](list : List[(T, U)]) -> (List[T], List[U]) { } test "unzip" { - let ls = of_array([(1, 2), (3, 4), (5, 6)]) + let ls = from_array([(1, 2), (3, 4), (5, 6)]) let (a, b) = unzip(ls) - @assertion.assert_eq(a, of_array([1, 3, 5])) - @assertion.assert_eq(b, of_array([2, 4, 6])) + @assertion.assert_eq(a, from_array([1, 3, 5]))? + @assertion.assert_eq(b, from_array([2, 4, 6]))? } /// flatten a list of lists. @@ -453,8 +456,8 @@ test "unzip" { /// # Example /// /// ``` -/// let ls = flatten(of_array([of_array([1,2,3]), of_array([4,5,6]), of_array([7,8,9])])) -/// debug(ls) // output: of_array([1, 2, 3, 4, 5, 6, 7, 8, 9]) +/// let ls = flatten(from_array([from_array([1,2,3]), from_array([4,5,6]), from_array([7,8,9])])) +/// debug(ls) // output: from_array([1, 2, 3, 4, 5, 6, 7, 8, 9]) /// ``` pub fn flatten[T](list : List[List[T]]) -> List[T] { match list { @@ -464,11 +467,11 @@ pub fn flatten[T](list : List[List[T]]) -> List[T] { } test "flatten" { - let ls = of_array( - [of_array([1, 2, 3]), of_array([4, 5, 6]), of_array([7, 8, 9])], + let ls = from_array( + [from_array([1, 2, 3]), from_array([4, 5, 6]), from_array([7, 8, 9])], ) - let rs = of_array([1, 2, 3, 4, 5, 6, 7, 8, 9]) - @assertion.assert_eq(flatten(ls), rs) + let rs = from_array([1, 2, 3, 4, 5, 6, 7, 8, 9]) + @assertion.assert_eq(flatten(ls), rs)? } pub fn to_string[T : Show](self : List[T]) -> String { @@ -480,7 +483,7 @@ pub fn to_string[T : Show](self : List[T]) -> String { } } - "of_array([" + aux(self) + "])" + "from_array([" + aux(self) + "])" } /// Get maximum element of the list. @@ -500,8 +503,8 @@ pub fn maximum[T : Compare](self : List[T]) -> T { } test "maximum" { - let ls = of_array([1, 123, 52, 3, 6, 0, -6, -76]) - @assertion.assert_eq(ls.maximum(), 123) + let ls = from_array([1, 123, 52, 3, 6, 0, -6, -76]) + @assertion.assert_eq(ls.maximum(), 123)? } /// Get minimum element of the list. @@ -521,8 +524,8 @@ pub fn minimum[T : Compare](self : List[T]) -> T { } test "minimum" { - let ls = of_array([1, 123, 52, 3, 6, 0, -6, -76]) - @assertion.assert_eq(ls.minimum(), -76) + let ls = from_array([1, 123, 52, 3, 6, 0, -6, -76]) + @assertion.assert_eq(ls.minimum(), -76)? } /// Sort the list in ascending order. @@ -530,8 +533,8 @@ test "minimum" { /// # Example /// /// ``` -/// let ls = sort(of_array([1,123,52,3,6,0,-6,-76])) -/// debug(ls) // output: of_array([-76, -6, 0, 1, 3, 6, 52, 123]) +/// let ls = sort(from_array([1,123,52,3,6,0,-6,-76])) +/// debug(ls) // output: from_array([-76, -6, 0, 1, 3, 6, 52, 123]) /// ``` pub fn sort[T : Compare](self : List[T]) -> List[T] { match self { @@ -545,9 +548,9 @@ pub fn sort[T : Compare](self : List[T]) -> List[T] { } test "sort" { - let ls = of_array([1, 123, 52, 3, 6, 0, -6, -76]) - let rs = of_array([-76, -6, 0, 1, 3, 6, 52, 123]) - @assertion.assert_eq(ls.sort(), rs) + let ls = from_array([1, 123, 52, 3, 6, 0, -6, -76]) + let rs = from_array([-76, -6, 0, 1, 3, 6, 52, 123]) + @assertion.assert_eq(ls.sort(), rs)? } /// Concatenate two lists. @@ -566,12 +569,12 @@ pub fn contain[T : Eq](self : List[T], value : T) -> Bool { } test "contain" { - let ls = of_array([1, 2, 3]) - @assertion.assert_true(ls.contain(1)) - @assertion.assert_true(ls.contain(2)) - @assertion.assert_true(ls.contain(3)) - @assertion.assert_false(ls.contain(0)) - @assertion.assert_false(ls.contain(4)) + let ls = from_array([1, 2, 3]) + @assertion.assert_true(ls.contain(1))? + @assertion.assert_true(ls.contain(2))? + @assertion.assert_true(ls.contain(3))? + @assertion.assert_false(ls.contain(0))? + @assertion.assert_false(ls.contain(4))? } /// Produces a collection iteratively. @@ -579,7 +582,7 @@ test "contain" { /// # Example /// /// ``` -/// // of_array([0, 1, 2]) +/// // from_array([0, 1, 2]) /// unfold(0, fn { i => if i == 3 { None } else { Some(i, i + 1) } }) |> debug /// ``` pub fn unfold[T, State](init : State, f : (State) -> Option[(T, State)]) -> @@ -592,5 +595,5 @@ pub fn unfold[T, State](init : State, f : (State) -> Option[(T, State)]) -> test "unfold" { let ls = unfold(0, fn { i => if i == 3 { None } else { Some(i, i + 1) } }) - @assertion.assert_eq(ls, Cons(0, Cons(1, Cons(2, Nil)))) + @assertion.assert_eq(ls, Cons(0, Cons(1, Cons(2, Nil))))? } diff --git a/option/option.mbt b/option/option.mbt index 342ed61fb..5aafb07f9 100644 --- a/option/option.mbt +++ b/option/option.mbt @@ -86,8 +86,8 @@ pub fn bind[T, U](self : Option[T], f : (T) -> Option[U]) -> Option[U] { test "bind" { let a = Option::Some(5) let b : Option[Int] = None - @assertion.assert_eq(a.bind(fn(x){ Some(x * 2) }), Some(10)) - @assertion.assert_eq(b.bind(fn(x){ Some(x * 2) }), None) + @assertion.assert_eq(a.bind(fn(x){ Some(x * 2) }), Some(10))? + @assertion.assert_eq(b.bind(fn(x){ Some(x * 2) }), None)? } /// Flattens an `Option` of `Option` into a single `Option`. @@ -108,9 +108,9 @@ pub fn flatten[T](self : Option[Option[T]]) -> Option[T] { test "flatten" { let a : Option[Option[Int]] = Some(Some(42)); - @assertion.assert_eq(flatten(a),Some(42)) + @assertion.assert_eq(flatten(a),Some(42))? let b : Option[Option[Int]] = Some(None) - @assertion.assert_eq(flatten(b),None) + @assertion.assert_eq(flatten(b),None)? } /// Checks if the option is empty. @@ -124,8 +124,8 @@ pub fn is_empty[T](self : Option[T]) -> Bool { test "is_empty" { let x = Option::Some(3) let y: Option[Int] = None - @assertion.assert_false(x.is_empty()) - @assertion.assert_true(y.is_empty()) + @assertion.assert_false(x.is_empty())? + @assertion.assert_true(y.is_empty())? } /// Filters the option by applying the given predicate function `f`. @@ -149,8 +149,8 @@ pub fn filter[T](self : Option[T], f : (T) -> Bool) -> Option[T] { test "filter" { let x = Option::Some(3) - @assertion.assert_eq(x.filter(fn(x){ x > 5 }),None) - @assertion.assert_eq(x.filter(fn(x){ x < 5 }),Some(3)) + @assertion.assert_eq(x.filter(fn(x){ x > 5 }),None)? + @assertion.assert_eq(x.filter(fn(x){ x < 5 }),Some(3))? } /// Return the contained `Some` value or the provided default. @@ -163,8 +163,8 @@ pub fn or[T](self : Option[T], default : T) -> T { test "or" { let x = Option::Some(3) - @assertion.assert_eq(x.or(5), 3) - @assertion.assert_eq((None : Option[Int]).or(5), 5) + @assertion.assert_eq(x.or(5), 3)? + @assertion.assert_eq((None : Option[Int]).or(5), 5)? } /// Return the contained `Some` value or the provided default. @@ -179,6 +179,6 @@ pub fn or_else[T](self : Option[T], default : () -> T) -> T { test "or else" { let x = Option::Some(3) - @assertion.assert_eq(x.or_else(fn () { 5 }), 3) - @assertion.assert_eq((None : Option[Int]).or_else(fn () { 5 }), 5) + @assertion.assert_eq(x.or_else(fn () { 5 }), 3)? + @assertion.assert_eq((None : Option[Int]).or_else(fn () { 5 }), 5)? } \ No newline at end of file diff --git a/ref/ref.mbt b/ref/ref.mbt index c0dbe3bc6..965e2f643 100644 --- a/ref/ref.mbt +++ b/ref/ref.mbt @@ -19,7 +19,7 @@ pub fn map[T,R](self : Ref[T], f : (T) -> R) -> Ref[R] { test "map" { let x = ref(1) let y = map(x, fn(a){ a + 1 }) - @assertion.assert_eq(y.val, 2) + @assertion.assert_eq(y.val, 2)? } @@ -57,11 +57,11 @@ pub fn protect[T,R](self : Ref[T], a : T, f : () -> R) -> R { test "protect" { let x = ref(1) - @assertion.assert_eq(x.val, 1) + @assertion.assert_eq(x.val, 1)? protect(x, 2, fn(){ @assertion.assert_eq(x.val, 2) - }) - @assertion.assert_eq(x.val, 1) + })? + @assertion.assert_eq(x.val, 1)? } /// Swaps the values of two references. @@ -85,8 +85,8 @@ test "swap" { let x = ref(1) let y = ref(2) swap(x, y) - @assertion.assert_eq(x.val, 2) - @assertion.assert_eq(y.val, 1) + @assertion.assert_eq(x.val, 2)? + @assertion.assert_eq(y.val, 1)? } pub fn update[T](self : Ref[T], f : (T) -> T) {