Skip to content

Commit

Permalink
Merge branch 'main' into feat-string
Browse files Browse the repository at this point in the history
  • Loading branch information
Lampese authored Mar 11, 2024
2 parents 8c242a4 + c424b68 commit dc6bbfb
Show file tree
Hide file tree
Showing 4 changed files with 337 additions and 14 deletions.
2 changes: 1 addition & 1 deletion array/array.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,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_is_not(arr[0], arr[1])?
}

/// Create a new array. Values are built from indexes.
Expand Down
120 changes: 113 additions & 7 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,87 @@ 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_true.false" {
assert_ne(assert_true(false), Ok(()))?
}

test "assert_eq.eq" {
assert_eq(1, 1)?
assert_eq("123", "123")?
/// Assert referential equality of two values.
///
/// Returns Ok if the two arguments are the same object by reference, using the
/// `===` operator; raises an Error otherwise. Certain objects may be equal by
/// value, but they are different objects in the memory. This function checks
/// the latter.
///
/// # Examples
///
/// ```
/// let a = "4" + "2"
/// let b = "4" + "2"
/// assert_is(a, a)? // this is okay
/// assert_is(a, b)? // yields an error
/// ```
pub fn assert_is[T : Debug](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)`")
}
}

test "assert_is.is" {
let x = 1
let s = "x:\(x)"
assert_eq(assert_is(s, s), Ok(()))?
}

test "assert_is.is.not" {
let x = 1
let s1 = "x:\(x)"
let s2 = "x:\(x)"
assert_eq(assert_eq(s1, s2), Ok(()))?
assert_ne(assert_is(s1, s2), Ok(()))?
}

/// Assert referential inequality of two values.
///
/// Returns Ok if the two arguments are NOT the same object by reference, using
/// the `===` operator; raises an Error otherwise. Certain objects may be equal
/// by value, but they are different objects in the memory. This function
/// checks the latter.
///
/// # Examples
///
/// ```
/// let a = "4" + "2"
/// let b = "4" + "2"
/// assert_is_not(a, b)? // this is okay
/// assert_is_not(a, a)? // yields an error
/// ```
pub fn assert_is_not[T : Debug](a : T, b : T) -> Result[Unit, String] {
if not(a === b) {
Ok(())
} else {
let a = debug_string(a)
let b = debug_string(b)
Err("assertion failed for `not(\(a) === \(b))`")
}
}

test "assert_is_not.is" {
let x = 1
let s = "x:\(x)"
assert_ne(assert_is_not(s, s), Ok(()))?
}

test "assert_is_not.is.not" {
let x = 1
let s1 = "x:\(x)"
let s2 = "x:\(x)"
assert_eq(assert_eq(s1, s2), Ok(()))?
assert_eq(assert_is_not(s1, s2), Ok(()))?
}
Loading

0 comments on commit dc6bbfb

Please sign in to comment.