Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assert_is() and assert_is_not() #34

Merged
merged 3 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion array/array.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,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
78 changes: 78 additions & 0 deletions assertion/assertion.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,81 @@ test "assert_true.true" {
test "assert_true.false" {
assert_ne(assert_true(false), Ok(()))?
}

/// 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)`")
}
Comment on lines +114 to +120
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is preferably @option.unless(a === b, fn() { ... }), but crippled by the lack of test_import in moon.pkg.json, and/or a better handling of cyclic dependencies.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_import will be added as soon as next week

}

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] {
fantix marked this conversation as resolved.
Show resolved Hide resolved
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(()))?
}