mirror of
https://github.com/bearlanguageorg/bear.git
synced 2026-08-26 15:17:19 +00:00
85 lines
1.3 KiB
Plaintext
85 lines
1.3 KiB
Plaintext
// tests.vr — functions named test_* are picked up by `vr test`.
|
|
// vr test examples/tests.vr
|
|
|
|
fn add(a, b) {
|
|
return a + b
|
|
}
|
|
|
|
fn test_add() {
|
|
assert add(2, 3) == 5
|
|
assert add(-1, 1) == 0
|
|
}
|
|
|
|
fn test_strings() {
|
|
let a = "foo"
|
|
let b = "bar"
|
|
assert a + b == "foobar"
|
|
assert a != b
|
|
assert a == "foo"
|
|
}
|
|
|
|
fn test_loops() {
|
|
let total = 0
|
|
let i = 1
|
|
while i <= 10 {
|
|
total = total + i
|
|
i = i + 1
|
|
}
|
|
assert total == 55
|
|
}
|
|
|
|
fn test_booleans() {
|
|
let x = 7
|
|
assert x > 3 and x < 10
|
|
assert x == 7 or x == 8
|
|
assert not (x == 0)
|
|
}
|
|
|
|
fn test_arrays() {
|
|
let a = [1, 2, 3]
|
|
assert len(a) == 3
|
|
assert a[0] == 1
|
|
assert a[2] == 3
|
|
a[1] = 42
|
|
assert a[1] == 42
|
|
push(a, 4)
|
|
assert len(a) == 4
|
|
assert a[3] == 4
|
|
let m = [[1, 2], [3, 4]]
|
|
assert m[1][0] == 3
|
|
}
|
|
|
|
fn test_for_range() {
|
|
let total = 0
|
|
for i in 0..5 {
|
|
total = total + i
|
|
}
|
|
assert total == 10 // 0+1+2+3+4
|
|
let inc = 0
|
|
for i in 1...3 {
|
|
inc = inc + i
|
|
}
|
|
assert inc == 6 // 1+2+3 (inclusive)
|
|
}
|
|
|
|
fn test_for_in() {
|
|
let a = [5, 6, 7]
|
|
let total = 0
|
|
for x in a {
|
|
total = total + x
|
|
}
|
|
assert total == 18
|
|
let squares = []
|
|
for i in 0..4 {
|
|
push(squares, i * i)
|
|
}
|
|
assert squares == squares // identity comparison
|
|
assert len(squares) == 4
|
|
assert squares[3] == 9
|
|
}
|
|
|
|
fn test_failing() {
|
|
// this one is meant to fail — shows up in `vr test` output
|
|
assert 1 == 2
|
|
}
|