///|
test "Set constructor from prelude" {
let set = Set([1, 2, 3, 4])
inspect(set.length(), content="4")
inspect(set.contains(3), content="true")
}test {
let arr = [1, 2, 3, 4, 5]
let view = arr[1:4] // Creates a view of elements at indices 1,2,3
@test.assert_eq(view[0], 2)
@test.assert_eq(view.length(), 3)
} let buf = Buffer(size_hint=100)
buf.write_string_utf16le("Tes")
buf.write_char_utf16le('t')
inspect(
buf.contents(),
content=(
#|b"T\x00e\x00s\x00t\x00"
),
)test {
let err : Failure = Failure("Test assertion failed")
match err {
Failure(msg) => inspect(msg, content="Test assertion failed")
}
@json.json_inspect(err, content=["Failure", "Test assertion failed"])
}test {
let hasher = Hasher(seed=0)
hasher.combine_int(42)
hasher.combine_string("hello")
inspect(hasher.finalize(), content="860601284")
}test {
let x : Int = 42
inspect(x, content="42") // Raises InspectError with detailed failure message
}test {
let value : Json = Json::array([Json::number(1.0), Json::null()])
inspect(value.stringify(), content="[1,null]")
}test {
let map = { 3: "three", 8: "eight", 1: "one" }
@test.assert_eq(map.get(2), None)
@test.assert_eq(map.get(3), Some("three"))
map.set(3, "updated")
@test.assert_eq(map.get(3), Some("updated"))
}test {
let arr = [1, 2, 3, 4, 5]
let view = arr.mut_view(start=1, end=4) // Creates a view of elements at indices 1,2,3
@test.assert_eq(view[0], 2)
@test.assert_eq(view.length(), 3)
}test {
let x = @ref.Ref(42)
@test.assert_eq(x.val, 42)
x.val = 100
@test.assert_eq(x.val, 100)
}test {
let set = @set.Set(["three", "eight", "one"])
@test.assert_eq(set.contains("two"), false)
@test.assert_eq(set.contains("three"), true)
set.add("three") // no effect since it already exists
set.add("two")
@test.assert_eq(set.contains("two"), true)
}test {
let err : SnapshotError = SnapshotError("failed to load snapshot")
match err {
SnapshotError(msg) => @test.assert_eq(msg, "failed to load snapshot")
}
}#callsite(autofill(loc))
fn assert_false(x : Bool, msg? : StringView, loc~ : SourceLoc) -> Unit raisetest {
assert_false(false)
assert_false(1 > 2)
}test {
assert_true(true)
}test {
inspect(compare(1, 2).is_neg(), content="true")
inspect(compare("abc", "abc"), content="0")
}fn debug_assert(x : () -> Bool) -> Unit#callsite(autofill(args_loc, loc))
fn debug_inspect(obj : &Debug, content? : String, loc~ : SourceLoc, args_loc~ : ArgsLoc) -> Unit raise InspectErrortest {
@debug.debug_inspect(42, content="42")
@debug.debug_inspect('c', content="'c'")
@debug.debug_inspect(
"hello",
content=(
#|"hello"
),
)
@debug.debug_inspect(
([1, 2, 3, 4], "string", Some(3.14)),
content=(
#|([1, 2, 3, 4], "string", Some(3.14))
),
)
}test {
inspect(hash(42) == hash(42), content="true")
}fn[T] ignore(t : T) -> Unittest {
let x = 42
ignore(x) // Explicitly ignore the value
let mut sum = 0
ignore(1, 2, 3.each(x => sum x)) // Ignore the Unit return value of each()
inspect(sum, content="6")
}#callsite(autofill(args_loc, loc))
fn inspect(obj : &Show, content? : String, loc~ : SourceLoc, args_loc~ : ArgsLoc) -> Unit raise InspectErrortest {
inspect(42, content="42")
inspect("hello", content="hello")
debug_inspect([1, 2, 3], content="[1, 2, 3]")
}#callsite(autofill(args_loc, loc))
fn json_inspect(obj : &ToJson, content? : Json, loc~ : SourceLoc, args_loc~ : ArgsLoc) -> Unit raise InspectError#deprecated("Use !expr instead")
fn not(x : Bool) -> Boolfn[T] physical_equal(a : T, b : T) -> Booltest {
let arr1 = [1, 2, 3]
let arr2 = arr1
let arr3 = [1, 2, 3]
inspect(physical_equal(arr1, arr2), content="true") // Same object
inspect(physical_equal(arr1, arr3), content="false") // Different objects with same content
}test {
if false {
println(42)
println("Hello, World!")
}
}test {
let str = @debug.to_string([1, 2, 3])
@test.assert_eq(str, "[1, 2, 3]")
// Same function under a shorter name.
@test.assert_eq(repr("hi"), "\"hi\"")
}#deprecated("use `value |> x => { ... ; x} instead")
fn[T] tap(value : T, f : (T) -> Unit raise) -> T raise#deprecated("use `value |> (x) => {...}` operator instead")
fn[T, R] then(value : T, f : (T) -> R raise?) -> R raise?test {
let x = 5
let result = x |> n => { n * 2 }
@test.assert_eq(result, 10)
}test {
try {
let _ : Unit = 5 |> x => { fail(x.to_string()) }
} catch {
Failure(_) => ()
} noraise {
_ => fail("expected failure")
}
}Install
Installed by default