cpp-peglib/language/culebra/samples/test.cul

240 lines
3.6 KiB
Plaintext
Raw Normal View History

2015-08-04 22:03:05 +00:00
/*
* Unit tests
*/
2015-07-07 19:44:33 +00:00
test_call = fn () {
ret = fn(){[1,fn(){[4,5,6]},3]}()[1]()[1]
assert(ret == 5)
}
2015-08-04 22:03:05 +00:00
test_return = fn () {
f = fn (x) {
2015-09-16 15:30:08 +00:00
if x % 2 {
return 'odd'
}
'even'
}
2015-08-04 22:03:05 +00:00
assert(f(3) == 'odd')
assert(f(4) == 'even')
2015-09-16 15:30:08 +00:00
mut val = 0
f2 = fn () {
val = 1
return // comment
val = 2
}
f2()
2015-08-04 22:03:05 +00:00
assert(val == 1)
}
2015-07-25 17:34:10 +00:00
test_undefined = fn () {
assert(undefined == undefined)
assert(!(undefined != undefined))
a = undefined
assert(a == undefined)
assert(!(a != undefined))
assert(!(a <= undefined))
assert(!(a < undefined))
assert(!(a >= undefined))
assert(!(a > undefined))
assert(undefined == a)
assert(!(undefined != a))
assert(!(undefined <= a))
assert(!(undefined < a))
assert(!(undefined >= a))
assert(!(undefined > a))
}
test_closure = fn () {
make_func = fn (mut x) {
mut n = 100
fn () {
n = n + 1
x = x + 1 + n
}
}
f = make_func(10)
f()
f()
ret = f()
assert(ret == 319)
}
2015-07-21 10:45:24 +00:00
test_array = fn () {
a = [1,2,3]
assert(a.size() == 3)
a.push(4)
assert(a.size() == 4)
b = []
assert(b.size() == 0)
2015-07-26 00:22:06 +00:00
c = [1]
assert(c.size() == 1)
2015-07-21 10:45:24 +00:00
}
2015-07-23 01:14:55 +00:00
g_ = 1
test_function = fn () {
a = 1
make = fn () {
b = 1
fn (c) {
g_ + a + b + c
}
}
f = make()
assert(f(1) == 4)
}
test_object = fn () {
n = 1
o = {
2015-09-16 15:30:08 +00:00
n: 123,
s: 'str',
f1: fn (x) { x + this.n },
f2: fn (x) { x + n }
2015-07-23 01:14:55 +00:00
}
assert(o.size() == 4)
assert(o.f1(10) == 133)
assert(o.f2(10) == 11)
a = {}
a.b = 1
assert(a.a == undefined)
assert(a.b == 1)
assert(a.size() == 1)
2015-07-23 01:14:55 +00:00
}
test_object_factory = fn () {
ctor = fn (init) {
mut n = init
{
add: fn (x) {
n = n + x
},
sub: fn (x) {
n = n - x
},
val: fn () {
n
}
}
}
calc = ctor(10)
assert(calc.val() == 10)
assert(calc.add(1) == 11)
assert(calc.sub(1) == 10)
}
2015-07-27 01:10:50 +00:00
test_class = fn () {
// TODO: support 'prototype' property
Car = {
new: fn(miles_per_run) {
mut total_miles = 0
{
run: fn (times) {
total_miles = total_miles + miles_per_run * times
},
total: fn () {
total_miles
}
}
}
}
car = Car.new(5)
car.run(1)
car.run(2)
assert(car.total() == 15)
}
2015-07-23 01:14:55 +00:00
test_sum = fn () {
mut i = 1
mut ret = 0
while i <= 10 {
ret = ret + i
i = i + 1
}
assert(ret == 55)
}
2015-07-07 19:44:33 +00:00
test_fib = fn () {
fib = fn (x) {
if x < 2 {
x
} else {
self(x - 2) + self(x -1)
}
}
ret = fib(15)
assert(ret == 610)
}
2015-07-23 01:14:55 +00:00
test_interpolated_string = fn () {
hello = "Hello"
2015-08-04 22:03:05 +00:00
world = "World!"
2015-07-23 01:14:55 +00:00
ret = "{hello} {world}"
assert(ret == 'Hello World!')
}
2015-09-25 19:20:55 +00:00
test_lexical_scope = fn () {
a = 0
{
let a = 1;
assert(a == 1)
}
assert(a == 0)
mut b = 0
{
b = 1;
assert(b == 1)
}
assert(b == 1)
c = 0
{
let mut c = 0;
c = 1
assert(c == 1)
}
assert(c == 0)
obj = {
name: 'object'
}
assert(obj.name == 'object')
}
debugger
test_call()
2015-08-04 22:03:05 +00:00
test_return()
test_closure()
2015-07-25 17:34:10 +00:00
test_undefined()
2015-07-21 10:45:24 +00:00
test_array()
2015-07-23 01:14:55 +00:00
test_function()
test_object()
test_object_factory()
2015-07-27 01:10:50 +00:00
test_class()
2015-08-04 22:03:05 +00:00
debugger
2015-07-07 19:44:33 +00:00
test_sum()
test_fib()
2015-07-23 01:14:55 +00:00
test_interpolated_string()
2015-09-25 19:20:55 +00:00
test_lexical_scope()
2015-08-04 22:03:05 +00:00
2015-08-06 03:35:36 +00:00
return // end