///|
test "basic test example" {
let result = 2 + 2
inspect(result, content="4")
// Test passes if no errors are raised
}///|
priv struct User(Int) derive(Eq, @debug.Debug)
///|
test "equality assertions" {
@test.assert_eq(User(1), User(1))
@test.assert_not_eq(User(1), User(2))
}///|
test "object identity" {
let str1 = "hello"
let _str2 = "hello"
let str3 = str1
// Same object reference
@test.assert_same_object(str1, str3) // Passes - same reference
// Different objects (even if equal values)
// @test.assert_not_same_object(str1, str2)
// May or may not pass - different string objects
// depend on how compiler optimization works
// here we interned
// Arrays and object identity
let arr1 = [1, 2, 3]
let _arr2 = [1, 2, 3]
let arr3 = arr1
@test.assert_same_object(arr1, arr3) // Passes - same array reference
// @test.assert_not_same_object(arr1, arr2) // Passes - different array objects
}///|
test "conditional failure" {
let value = 10
if value < 0 {
@test.fail("Value should not be negative: \{value}")
}
// Test continues if condition is not met
inspect(value, content="10")
}///|
test "test output" {
let t = @test.Test("Example Test")
// Write output to test buffer
t.write("Testing basic functionality: ")
t.writeln("PASS")
// Write multiple lines
t.writeln("Step 1: Initialize data")
t.writeln("Step 2: Process data")
t.writeln("Step 3: Verify results")
// The test output is captured for reporting
}///|
test "snapshot testing" {
let t = @test.Test("Snapshot Test")
// Generate some output
t.writeln("Current timestamp: 2024-01-01")
t.writeln("Processing items: [1, 2, 3, 4, 5]")
t.writeln("Result: SUCCESS")
// Compare against snapshot file
// This will create or update a snapshot file
t.snapshot(filename="test_output")
}///|
test "complex data testing" {
// Test with arrays
let numbers = [1, 2, 3, 4, 5]
let doubled = numbers.map(fn(x) { x * 2 })
debug_inspect(doubled, content="[2, 4, 6, 8, 10]")
// Test with tuples (simpler than custom structs in test examples)
let person_data = ("Alice", 30)
inspect(person_data.0, content="Alice")
inspect(person_data.1, content="30")
}///|
test "error handling" {
fn safe_divide(a : Int, b : Int) -> Int? {
if b == 0 {
None
} else {
Some(a / b)
}
}
// Test normal case
let result = safe_divide(10, 2)
debug_inspect(result, content="Some(5)")
// Test error case
let error_result = safe_divide(10, 0)
debug_inspect(error_result, content="None")
}///|
test "property testing" {
fn is_even(n : Int) -> Bool {
n % 2 == 0
}
// Test the property with multiple values
let test_values = [0, 2, 4, 6, 8, 10]
for value in test_values {
if !is_even(value) {
@test.fail("Expected \{value} to be even")
}
}
// Test negative cases
let odd_values = [1, 3, 5, 7, 9]
for value in odd_values {
if is_even(value) {
@test.fail("Expected \{value} to be odd")
}
}
}///|
test "string operations - concatenation" {
let result = "hello" + " " + "world"
inspect(result, content="hello world")
}
///|
test "string operations - length" {
let text = "MoonBit"
inspect(text.length(), content="7")
}
///|
test "string operations - substring" {
let text = "Hello, World!"
let sub = text.length() // Just test length instead of substring
inspect(sub, content="13")
}///|
test "with setup helper" {
fn setup_test_data() -> Array[Int] {
[10, 20, 30, 40, 50]
}
fn cleanup_test_data(_data : Array[Int]) -> Unit {
// Cleanup logic here
}
let data = setup_test_data()
// Perform tests
inspect(data.length(), content="5")
inspect(data[0], content="10")
inspect(data[4], content="50")
cleanup_test_data(data)
}///|
test "user_can_login_with_valid_credentials" {
// Test implementation
}
///|
test "login_fails_with_invalid_password" {
// Test implementation
}
///|
test "shopping_cart_calculates_total_correctly" {
// Test implementation
}///|
/// Good - tests one specific behavior
test "array_push_increases_length" {
let arr = Array::new()
let initial_length = arr.length()
arr.push(42)
let new_length = arr.length()
inspect(new_length, content="\{initial_length + 1}")
}
///|
/// Good - tests another specific behavior
test "array_push_adds_element_at_end" {
let arr = Array::new()
arr.push(10)
arr.push(20)
inspect(arr[arr.length() - 1], content="20")
}///|
test "tax_calculation_for_standard_rate" {
let price = 100
let tax_rate = 8 // 8% tax as integer percentage
let calculated_tax = price * tax_rate / 100
inspect(calculated_tax, content="8")
}#callsite(autofill(args_loc, loc))
fn Test::snapshot(self : Test, filename~ : String, loc~ : SourceLoc, args_loc~ : ArgsLoc) -> Unit raise SnapshotErrortest {
let t = @test.Test("test.txt")
t.writeln("hello")
t.snapshot(filename="test.txt") // actual test block end
}#callsite(autofill(loc))
fn[T, E : Error] assert_raise(f : () -> T raise E, loc~ : SourceLoc) -> Unit raise#callsite(autofill(loc))
fn[T, E : Error] expect_error(f : () -> T raise E, loc~ : SourceLoc) -> E raiselet a = "4" + "2"
let b = "4" + "2"
@test.is_not(a, b)
@test.same_object(a, a)let a = "4" + "2"
let b = "4" + "2"
@test.same_object(a, a)
@test.is_not(a, b)Install
Installed by default