tests: run vfmt over some of the tests in vlib/v/tests (#9455)

pull/9493/head
Lukas Neubert 2021-03-27 18:29:57 +01:00 committed by GitHub
parent 3b166d8327
commit 4a10514081
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
156 changed files with 1573 additions and 945 deletions

View File

@ -21,6 +21,13 @@ const (
'vlib/gg/m4/graphic.v' /* has hand crafted meaningful formatting of matrices */, 'vlib/gg/m4/graphic.v' /* has hand crafted meaningful formatting of matrices */,
'vlib/gg/m4/m4_test.v' /* has hand crafted meaningful formatting of matrices */, 'vlib/gg/m4/m4_test.v' /* has hand crafted meaningful formatting of matrices */,
'vlib/gg/m4/matrix.v' /* has hand crafted meaningful formatting of matrices */, 'vlib/gg/m4/matrix.v' /* has hand crafted meaningful formatting of matrices */,
'vlib/v/tests/array_append_short_struct_test.v', /* extra empty line */
'vlib/v/tests/fixed_array_const_size_test.v', /* fixed arr type is changed */
'vlib/v/tests/fn_high_test.v', /* param name removed */
'vlib/v/tests/fn_test.v', /* bad comment formatting */
'vlib/v/tests/generics_return_generics_struct_test.v', /* generic fn param removed */
'vlib/v/tests/interop_test.v', /* bad comment formatting */
'vlib/v/tests/generics_test.v', /* some silent error */
] ]
vfmt_verify_list = [ vfmt_verify_list = [
'cmd/', 'cmd/',
@ -58,7 +65,7 @@ const (
'vlib/v/preludes', 'vlib/v/preludes',
'vlib/v/scanner/', 'vlib/v/scanner/',
'vlib/v/table/', 'vlib/v/table/',
//'vlib/v/tests/', 'vlib/v/tests/',
'vlib/v/token/', 'vlib/v/token/',
'vlib/v/util/', 'vlib/v/util/',
'vlib/v/vcache/', 'vlib/v/vcache/',

View File

@ -1,10 +1,10 @@
fn test_anon_fn_call() { fn test_anon_fn_call() {
anon_fn := fn() string { anon_fn := fn () string {
return test() return test()
} }
assert anon_fn() == 'Test' assert anon_fn() == 'Test'
} }
fn test() string { fn test() string {
return "Test" return 'Test'
} }

View File

@ -1,36 +1,36 @@
fn test_anon_fn_in_map() { fn test_anon_fn_in_map() {
mut woop := map{ mut woop := map{
'what': fn() string { 'what': fn () string {
return 'whoopity whoop' return 'whoopity whoop'
} }
} }
assert woop['what']() == 'whoopity whoop' assert woop['what']() == 'whoopity whoop'
woop['shat'] = fn() string { woop['shat'] = fn () string {
return 'shoopity shoop' return 'shoopity shoop'
} }
assert woop['shat']() == 'shoopity shoop' assert woop['shat']() == 'shoopity shoop'
} }
fn test_anon_fn_in_array() { fn test_anon_fn_in_array() {
mut woop := [fn() string { mut woop := [fn () string {
return 'whoopity whoop' return 'whoopity whoop'
}] }]
assert woop[0]() == 'whoopity whoop' assert woop[0]() == 'whoopity whoop'
woop[0] = fn() string { woop[0] = fn () string {
return 'shoopity shoop' return 'shoopity shoop'
} }
assert woop[0]() == 'shoopity shoop' assert woop[0]() == 'shoopity shoop'
} }
fn test_anon_fn_in_fixed_array() { fn test_anon_fn_in_fixed_array() {
mut woop := [fn() string { mut woop := [fn () string {
return 'whoopity whoop' return 'whoopity whoop'
}]! }]!
assert woop[0]() == 'whoopity whoop' assert woop[0]() == 'whoopity whoop'
woop[0] = fn() string { woop[0] = fn () string {
return 'shoopity shoop' return 'shoopity shoop'
} }
assert woop[0]() == 'shoopity shoop' assert woop[0]() == 'shoopity shoop'

View File

@ -7,11 +7,11 @@ fn test_calling_an_anon_function_returning_question() {
fn create_and_call_anon_function() ? { fn create_and_call_anon_function() ? {
x := fn (a string, b int) ? { x := fn (a string, b int) ? {
println('test') println('test')
// NB: the anon function does NOT return explicitly, // NB: the anon function does NOT return explicitly,
// so V should generate an implicit "OK" value and // so V should generate an implicit "OK" value and
// return it. Previously, it created an implicit optional // return it. Previously, it created an implicit optional
// filled with 0s => .ok was false, and that was treated // filled with 0s => .ok was false, and that was treated
// as a failure, triggering or blocks. // as a failure, triggering or blocks.
} }
should_not_call_block(x) ? should_not_call_block(x) ?
assert true assert true

View File

@ -2,7 +2,7 @@ import sync
fn test_go_anon_fn() { fn test_go_anon_fn() {
mut wg := sync.new_waitgroup() mut wg := sync.new_waitgroup()
wg.add(1) wg.add(1)
go fn (mut wg sync.WaitGroup) { go fn (mut wg sync.WaitGroup) {
wg.done() wg.done()
}(mut wg) }(mut wg)

View File

@ -1,5 +1,5 @@
struct Page { struct Page {
contents int contents int
} }
fn test_array_append_short_struct() { fn test_array_append_short_struct() {
@ -8,7 +8,9 @@ fn test_array_append_short_struct() {
contents: 3 contents: 3
} }
println(pages) println(pages)
assert pages == [Page{contents: 3}] assert pages == [Page{
contents: 3
}]
} }
struct Container { struct Container {
@ -18,8 +20,12 @@ pub mut:
fn test_array_insert_or_prepend_short_struct() { fn test_array_insert_or_prepend_short_struct() {
mut a := []Container{} mut a := []Container{}
a.prepend({name: 'a'}) a.prepend(name: 'a')
a.insert(0, {name: 'b'}) a.insert(0, name: 'b')
println(a) println(a)
assert a == [Container{name: 'b'}, Container{name: 'a'}] assert a == [Container{
name: 'b'
}, Container{
name: 'a'
}]
} }

View File

@ -10,7 +10,7 @@ fn test_array_cast() {
} }
fn test_int() { fn test_int() {
mut arr := [2.3,3] mut arr := [2.3, 3]
unsafe { unsafe {
vp := voidptr(&arr) vp := voidptr(&arr)
p := &[]f64(vp) p := &[]f64(vp)

View File

@ -4,7 +4,9 @@ struct Tester {
} }
enum Color { enum Color {
red green blue red
green
blue
} }
fn test_array_equality() { fn test_array_equality() {
@ -52,15 +54,15 @@ fn test_nested_array_equality() {
a2 := [[[[1]]]] a2 := [[[[1]]]]
assert a2 == [[[[1]]]] assert a2 == [[[[1]]]]
assert a2 != [[[[2]]]] assert a2 != [[[[2]]]]
a3 := [[[1,2,3]]] a3 := [[[1, 2, 3]]]
assert a3 == [[[1,2,3]]] assert a3 == [[[1, 2, 3]]]
assert a3 != [[[1,0,3]]] assert a3 != [[[1, 0, 3]]]
a4 := [[1.1], [2.2]] a4 := [[1.1], [2.2]]
assert a4 == [[1.1], [2.2]] assert a4 == [[1.1], [2.2]]
assert a4 != [[2.1], [3.2]] assert a4 != [[2.1], [3.2]]
a5 := [[[[1,2], [2,3], [3,4]]]] a5 := [[[[1, 2], [2, 3], [3, 4]]]]
assert a5 == [[[[1,2], [2,3], [3,4]]]] assert a5 == [[[[1, 2], [2, 3], [3, 4]]]]
assert a5 != [[[[2,2], [2,4], [3,4]]]] assert a5 != [[[[2, 2], [2, 4], [3, 4]]]]
a6 := [[['aa', 'bb'], ['cc', 'dd']]] a6 := [[['aa', 'bb'], ['cc', 'dd']]]
assert a6 == [[['aa', 'bb'], ['cc', 'dd']]] assert a6 == [[['aa', 'bb'], ['cc', 'dd']]]
assert a6 != [[['a', 'b'], ['cc', 'dd']]] assert a6 != [[['a', 'b'], ['cc', 'dd']]]
@ -76,6 +78,7 @@ fn test_nested_array_equality() {
} }
type Literal = string type Literal = string
type Literals = []Literal type Literals = []Literal
fn (l1 Literal) concat(l2 Literal) Literals { fn (l1 Literal) concat(l2 Literal) Literals {

View File

@ -32,7 +32,7 @@ fn test_array_or_direct() {
} }
fn test_map_or() { fn test_map_or() {
m := { m := map{
'as': 3 'as': 3
'qw': 4 'qw': 4
'kl': 5 'kl': 5
@ -51,9 +51,12 @@ fn test_map_or() {
assert good == 5 assert good == 5
} }
fn get_map_el(key string) ?int { fn get_map_el(key string) ?int {
m := {'as': 3, 'qw': 4, 'kl': 5} m := map{
'as': 3
'qw': 4
'kl': 5
}
r := m[key] ? r := m[key] ?
return r return r
} }
@ -90,12 +93,8 @@ fn test_propagation() {
testvar2 = 177 testvar2 = 177
int(-67) int(-67)
} }
m := get_arr_el_direct(3) or { m := get_arr_el_direct(3) or { 17 }
17 n := get_arr_el_direct(0) or { -73 }
}
n := get_arr_el_direct(0) or {
-73
}
assert testvar1 == -34 assert testvar1 == -34
assert testvar2 == 99 assert testvar2 == 99
assert e == 7 assert e == 7
@ -114,8 +113,6 @@ fn get_arr_el_nested(i int) ?int {
} }
fn test_nested_array_propagation() { fn test_nested_array_propagation() {
g := get_arr_el_nested(3) or { g := get_arr_el_nested(3) or { 12 }
12
}
assert g == 12 assert g == 12
} }

View File

@ -17,9 +17,9 @@ fn test_array_map_ref() {
mut m := map[string]int{} mut m := map[string]int{}
mut m_ref := &map[string]f64{} mut m_ref := &map[string]f64{}
mut a := []int{len: 10} mut a := []int{len: 10}
mut a_ref := &[]f64{cap: 12, len: 2} mut a_ref := &[]f64{len: 2, cap: 12}
shared m_shared := &map[string]f64{} shared m_shared := &map[string]f64{}
shared a_shared := &[]f64{cap: 12, len: 9} shared a_shared := &[]f64{len: 9, cap: 12}
// test usage // test usage
m['a'] = 3 m['a'] = 3
unsafe { unsafe {

View File

@ -5,7 +5,9 @@ mut:
// if this is called more than once, the test'll fail // if this is called more than once, the test'll fail
fn (mut c Counter) new_arr(msg string) []int { fn (mut c Counter) new_arr(msg string) []int {
if c.val > 0 { panic(msg) } if c.val > 0 {
panic(msg)
}
c.val++ c.val++
return [1, 3, 2] return [1, 3, 2]
} }

View File

@ -43,20 +43,20 @@ fn test_fixed_array_slice() {
fixed_array1 := [1, 2, 3]! fixed_array1 := [1, 2, 3]!
arr1 := fixed_array1[0..] arr1 := fixed_array1[0..]
assert arr1 == [1, 2, 3] assert arr1 == [1, 2, 3]
fixed_array2 := [[1, 2], [2, 3], [3, 4],[4, 5]]! fixed_array2 := [[1, 2], [2, 3], [3, 4], [4, 5]]!
arr2 := fixed_array2[0..] arr2 := fixed_array2[0..]
assert arr2 == [[1, 2], [2, 3], [3, 4],[4, 5]] assert arr2 == [[1, 2], [2, 3], [3, 4], [4, 5]]
mut arr := [1,2,3]! mut arr := [1, 2, 3]!
fixed_array_slice(arr) fixed_array_slice(arr)
mut_fixed_array_slice(mut arr) mut_fixed_array_slice(mut arr)
} }
fn pointer_array_slice(mut a []int) { fn pointer_array_slice(mut a []int) {
assert a[0..] == [1,2,3] assert a[0..] == [1, 2, 3]
assert a[..a.len] == [1,2,3] assert a[..a.len] == [1, 2, 3]
} }
fn test_pointer_array_slice() { fn test_pointer_array_slice() {
mut arr := [1,2,3] mut arr := [1, 2, 3]
pointer_array_slice(mut arr) pointer_array_slice(mut arr)
} }

View File

@ -45,7 +45,13 @@ fn test_interpolation_array_to_string() {
fn test_interpolation_array_of_map_to_string() { fn test_interpolation_array_of_map_to_string() {
mut ams := []map[string]string{} mut ams := []map[string]string{}
ams << {'a': 'b', 'c': 'd'} ams << map{
ams << {'e': 'f', 'g': 'h'} 'a': 'b'
'c': 'd'
}
ams << map{
'e': 'f'
'g': 'h'
}
assert '$ams' == "[{'a': 'b', 'c': 'd'}, {'e': 'f', 'g': 'h'}]" assert '$ams' == "[{'a': 'b', 'c': 'd'}, {'e': 'f', 'g': 'h'}]"
} }

View File

@ -1,6 +1,16 @@
const( str = 'abcdefghijklmnopqrstuvwxyz' num = 1234567890 ) const (
struct S1 { s1 string = str } str = 'abcdefghijklmnopqrstuvwxyz'
struct S2 { s2 int = num } num = 1234567890
)
struct S1 {
s1 string = str
}
struct S2 {
s2 int = num
}
type Sum = S1 | S2 type Sum = S1 | S2
fn test_as_cast_with_sumtype_fn_result() { fn test_as_cast_with_sumtype_fn_result() {

View File

@ -1,7 +1,8 @@
struct Moon {} struct Moon {}
struct Mars {} struct Mars {}
type World = Moon | Mars type World = Mars | Moon
fn test_assert_sumtype() { fn test_assert_sumtype() {
w := World(Moon{}) w := World(Moon{})

View File

@ -22,9 +22,7 @@ pub enum PubEnumAttrTest {
two two
} }
[attrone; attrtwo]
[attrone]
[attrtwo]
pub enum EnumMultiAttrTest { pub enum EnumMultiAttrTest {
one one
two two
@ -40,8 +38,7 @@ pub fn test_pub_fn_attribute() {
assert true assert true
} }
[attrone] [attrone; attrtwo]
[attrtwo]
fn test_fn_multi_attribute() { fn test_fn_multi_attribute() {
assert true assert true
} }

View File

@ -1,5 +1,5 @@
/* /*
Test for backtrace capability Test for backtrace capability
*/ */
fn a_method() { fn a_method() {
print_backtrace() print_backtrace()

View File

@ -1,5 +1,5 @@
struct Object { struct Object {
name string name string
value int value int
} }
@ -32,61 +32,51 @@ fn test_assign_multi_expr() {
val2 := 2 val2 := 2
// simple case for match // simple case for match
a,b,c := match false { a, b, c := match false {
true { 1,2,3 } true { 1, 2, 3 }
false { 4,5,6 } false { 4, 5, 6 }
} }
assert a == 4 assert a == 4
assert b == 5 assert b == 5
assert c == 6 assert c == 6
// test with first value `literal` // test with first value `literal`
d, e, f := if true { d, e, f := if true { 1, 'awesome', [13] } else { 0, 'bad', [0] }
1, 'awesome', [13]
} else {
0, 'bad', [0]
}
assert d == 1 assert d == 1
assert e == 'awesome' assert e == 'awesome'
assert f == [13] assert f == [13]
// test with first value `literal expr` and statement // test with first value `literal expr` and statement
awesome := 'awesome' awesome := 'awesome'
g, h, i := if true { g, h, i := if true { 1 + val1, awesome, [13] } else { int(0), 'bad', [0] }
1 + val1, awesome, [13]
} else {
int(0), 'bad', [0]
}
assert g == 2 assert g == 2
assert h == 'awesome' assert h == 'awesome'
assert i == [13] assert i == [13]
// test with first value `.name` // test with first value `.name`
j, k, l := if true { j, k, l := if true { val1, 'awesome', [13] } else { val2, 'bad', [0] }
val1, 'awesome', [13]
} else {
val2, 'bad', [0]
}
assert j == 1 assert j == 1
assert k == 'awesome' assert k == 'awesome'
assert l == [13] assert l == [13]
// test with first value name and peek != .comma // test with first value name and peek != .comma
m, n, o := if true { m, n, o := if true { val1 + 1, val1, val1 } else { val2, val2, val2 }
val1 + 1, val1, val1
} else {
val2, val2, val2
}
assert m == val1 + 1 assert m == val1 + 1
assert n == val1 assert n == val1
assert o == val1 assert o == val1
// test practical complex expressions // test practical complex expressions
val3 := Object { name: 'initial', value: 19 } val3 := Object{
name: 'initial'
value: 19
}
mut q, mut r, mut s := if true { mut q, mut r, mut s := if true {
1 + 1, 'awe' + 'some', Object{ ...val3, name: 'ok' } 1 + 1, 'awe' + 'some', Object{
...val3
name: 'ok'
}
} else { } else {
0, '0', Object {} 0, '0', Object{}
} }
assert q == 2 assert q == 2
assert r == 'awesome' assert r == 'awesome'
@ -95,9 +85,12 @@ fn test_assign_multi_expr() {
// test assign to existing variables // test assign to existing variables
q, r, s = if false { q, r, s = if false {
0, '0', Object {} 0, '0', Object{}
} else { } else {
5, '55', Object{ ...val3, value: 555 } 5, '55', Object{
...val3
value: 555
}
} }
assert q == 5 assert q == 5
assert r == '55' assert r == '55'
@ -106,7 +99,7 @@ fn test_assign_multi_expr() {
} }
fn test_issue_9330() { fn test_issue_9330() {
arr := "0.1".split('.') arr := '0.1'.split('.')
a0, a1 := arr[0], arr[1].int() a0, a1 := arr[0], arr[1].int()
assert a0 == '0' assert a0 == '0'
assert a1 == 1 assert a1 == 1
@ -119,5 +112,4 @@ fn test_issue_9330() {
d0, d1 := arr[0].int(), arr[1].f64() d0, d1 := arr[0].int(), arr[1].f64()
assert d0 == 0 assert d0 == 0
assert d1 == 1.0 assert d1 == 1.0
} }

View File

@ -3,12 +3,15 @@ struct Test {}
fn (test Test) v() { fn (test Test) v() {
println('Test.v()') println('Test.v()')
} }
fn (test Test) i() int { fn (test Test) i() int {
return 4 return 4
} }
fn (test Test) s() string { fn (test Test) s() string {
return 'test' return 'test'
} }
fn (test Test) s2() string { fn (test Test) s2() string {
return 'Two' return 'Two'
} }
@ -33,15 +36,13 @@ fn test_for_methods() {
$if method.return_type is string { $if method.return_type is string {
v := test.$method() v := test.$method()
r += v.str() r += v.str()
} } $else $if method.return_type is int {
$else $if method.return_type is int {
// TODO // TODO
//v := test.$method() // v := test.$method()
v := '?' v := '?'
r += v.str() r += v.str()
assert method.name == 'i' assert method.name == 'i'
} } $else {
$else {
// no return type // no return type
test.$method() test.$method()
assert method.name == 'v' assert method.name == 'v'
@ -64,4 +65,3 @@ fn test_methods_arg() {
assert r == '!!!' assert r == '!!!'
} }
} }

View File

@ -26,4 +26,3 @@ fn test_is_or() {
assert g(i8(1)) == 1 assert g(i8(1)) == 1
assert g(1) == 2 assert g(1) == 2
} }

View File

@ -8,9 +8,11 @@ mut:
fn (mut t TestStruct) one_arg(a1 string) { fn (mut t TestStruct) one_arg(a1 string) {
t.one_arg_called = true t.one_arg_called = true
} }
fn (mut t TestStruct) two_args(a2 string, b2 int) { fn (mut t TestStruct) two_args(a2 string, b2 int) {
t.two_args_called = true t.two_args_called = true
} }
fn (mut t TestStruct) three_args(a3 string, b3 int, c3 []string) { fn (mut t TestStruct) three_args(a3 string, b3 int, c3 []string) {
t.three_args_called = true t.three_args_called = true
} }
@ -43,7 +45,7 @@ fn test_comptime_call_method() {
} else if method.name == 'two_args' { } else if method.name == 'two_args' {
t.$method('two', 2) t.$method('two', 2)
} else if method.name == 'three_args' { } else if method.name == 'three_args' {
t.$method('three', 3, ['th' 'ree']) t.$method('three', 3, ['th', 'ree'])
} }
} }
assert t.one_arg_called assert t.one_arg_called

View File

@ -1,4 +1,4 @@
pub const ( pub const (
a = b a = b
c = a + b c = a + b
b = 1 b = 1
@ -26,7 +26,7 @@ fn foo_decode(name string) ?Foo {
return Foo{name} return Foo{name}
} }
pub const ( pub const (
def = foo_decode('baz') or { Foo{} } def = foo_decode('baz') or { Foo{} }
bar = foo_decode('bar') ? bar = foo_decode('bar') ?
) )

View File

@ -8,7 +8,7 @@ fn test_conv_to_bool() {
assert int(b) == 1 assert int(b) == 1
// branchless tests (can be important for manual optimization) // branchless tests (can be important for manual optimization)
arr := [7,8]! arr := [7, 8]!
e := arr[int(b)] e := arr[int(b)]
assert e == 8 assert e == 8
b = e < 0 b = e < 0

View File

@ -18,7 +18,7 @@ fn foo1(mut arr []int) {
} }
fn test_cross_assign_of_array_in_fn() { fn test_cross_assign_of_array_in_fn() {
mut arr := [1,2] mut arr := [1, 2]
foo1(mut arr) foo1(mut arr)
assert arr[0] == 2 assert arr[0] == 2
assert arr[1] == 1 assert arr[1] == 1
@ -26,7 +26,10 @@ fn test_cross_assign_of_array_in_fn() {
// Test cross assign of map values // Test cross assign of map values
fn test_cross_assign_of_map() { fn test_cross_assign_of_map() {
mut a := {'one':1, 'two':2} mut a := map{
'one': 1
'two': 2
}
a['one'], a['two'] = a['two'], a['one'] a['one'], a['two'] = a['two'], a['one']
println(a) println(a)
assert a['one'] == 2 assert a['one'] == 2
@ -39,7 +42,10 @@ fn foo2(mut a map[string]int) {
} }
fn test_cross_assign_of_map_in_fn() { fn test_cross_assign_of_map_in_fn() {
mut a := {'one':1, 'two':2} mut a := map{
'one': 1
'two': 2
}
foo2(mut a) foo2(mut a)
assert a['one'] == 2 assert a['one'] == 2
assert a['two'] == 1 assert a['two'] == 1
@ -53,9 +59,12 @@ mut:
} }
fn test_cross_assign_of_struct() { fn test_cross_assign_of_struct() {
mut x := Zoo{a:1, b:2} mut x := Zoo{
a: 1
b: 2
}
x.a, x.b = x.b, x.a x.a, x.b = x.b, x.a
//println(x) // println(x)
assert x.a == 2 assert x.a == 2
assert x.b == 1 assert x.b == 1
} }
@ -72,7 +81,10 @@ fn foo3(mut f Foo) {
} }
fn test_cross_assign_of_struct_in_fn() { fn test_cross_assign_of_struct_in_fn() {
mut a := Foo{a:1, b:2} mut a := Foo{
a: 1
b: 2
}
foo3(mut a) foo3(mut a)
println(a) println(a)
assert a.a == 2 assert a.a == 2
@ -82,8 +94,14 @@ fn test_cross_assign_of_struct_in_fn() {
// Test cross assign of mixed types // Test cross assign of mixed types
fn test_cross_assign_of_mixed_types() { fn test_cross_assign_of_mixed_types() {
mut a := [0, 1] mut a := [0, 1]
mut m := {'one':1, 'two':2} mut m := map{
mut x := Zoo{a:1, b:2} 'one': 1
'two': 2
}
mut x := Zoo{
a: 1
b: 2
}
a[0], m['one'], x.a, a[1], m['two'], x.b = a[1], m['two'], x.b, a[0], m['one'], x.a a[0], m['one'], x.a, a[1], m['two'], x.b = a[1], m['two'], x.b, a[0], m['one'], x.a
@ -95,14 +113,20 @@ fn test_cross_assign_of_mixed_types() {
} }
// Test cross assign of mixed types in function // Test cross assign of mixed types in function
fn foo(mut a []int, mut m map[string]int, mut x Zoo) { fn foo(mut a []int, mut m map[string]int, mut x Zoo) {
a[0], m['one'], x.a, a[1], m['two'], x.b = a[1], m['two'], x.b, a[0], m['one'], x.a a[0], m['one'], x.a, a[1], m['two'], x.b = a[1], m['two'], x.b, a[0], m['one'], x.a
} }
fn test_cross_assign_of_mixed_types_in_fn() { fn test_cross_assign_of_mixed_types_in_fn() {
mut a := [0, 1] mut a := [0, 1]
mut m := {'one':1, 'two':2} mut m := map{
mut x := Zoo{a:1, b:2} 'one': 1
'two': 2
}
mut x := Zoo{
a: 1
b: 2
}
foo(mut a, mut m, mut x) foo(mut a, mut m, mut x)
@ -116,10 +140,16 @@ fn test_cross_assign_of_mixed_types_in_fn() {
// Test cross assign of complex types // Test cross assign of complex types
fn test_cross_assign_of_complex_types() { fn test_cross_assign_of_complex_types() {
mut a := [0, 1] mut a := [0, 1]
mut m := {'one':1, 'two':2} mut m := map{
mut x := Zoo{a:1, b:2} 'one': 1
'two': 2
}
mut x := Zoo{
a: 1
b: 2
}
a[0], m['one'], x.a, a[1], m['two'], x.b = a[1]+1, -m['two'], x.b, a[0]*2, m['one']*3, x.a-x.b a[0], m['one'], x.a, a[1], m['two'], x.b = a[1] + 1, -m['two'], x.b, a[0] * 2, m['one'] * 3, x.a - x.b
assert a == [2, 0] assert a == [2, 0]
assert m['one'] == -2 assert m['one'] == -2

View File

@ -1,8 +1,8 @@
fn test_cstring() { fn test_cstring() {
w := c'world' w := c'world'
hlen := unsafe{ C.strlen(c'hello') } hlen := unsafe { C.strlen(c'hello') }
hlen2 := unsafe{ C.strlen('hello') } hlen2 := unsafe { C.strlen('hello') }
wlen := unsafe{ C.strlen(w) } wlen := unsafe { C.strlen(w) }
assert hlen == 5 assert hlen == 5
assert hlen2 == 5 assert hlen2 == 5
assert wlen == 5 assert wlen == 5

View File

@ -59,7 +59,7 @@ fn test_defer_early_exit() {
fn test_defer_option() { fn test_defer_option() {
mut ok := Num{0} mut ok := Num{0}
set_num_opt(mut ok) or { } set_num_opt(mut ok) or {}
assert ok.val == 1 assert ok.val == 1
} }

View File

@ -27,7 +27,7 @@ fn test_enum_non_default_value() {
assert 't.e: $t.e | int(t.e): ${int(t.e).str()}' == 't.e: third | int(t.e): 22' assert 't.e: $t.e | int(t.e): ${int(t.e).str()}' == 't.e: third | int(t.e): 22'
} }
fn test_generation_of_string_interpolation_method_for_pointer_to_struct_containing_enum_fields(){ fn test_generation_of_string_interpolation_method_for_pointer_to_struct_containing_enum_fields() {
t := &MyStruct{ t := &MyStruct{
e: .third e: .third
} }

View File

@ -2,7 +2,7 @@ fn test_fixed_array_can_be_assigned() {
x := 2.32 x := 2.32
mut v := [8]f64{} mut v := [8]f64{}
assert v[1] == 0 assert v[1] == 0
v = [1.0, x, 3.0,4.0,5.0,6.0,7.0,8.0]! v = [1.0, x, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]!
assert v[1] == x assert v[1] == x
v[1] = 2.0 v[1] = 2.0
for i, e in v { for i, e in v {
@ -41,14 +41,13 @@ fn test_fixed_array_assignment() {
fn test_fixed_array_can_be_used_in_declaration() { fn test_fixed_array_can_be_used_in_declaration() {
x := 2.32 x := 2.32
v := [1.0, x, 3.0,4.0,5.0,6.0,7.0,8.0]! v := [1.0, x, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]!
assert v.len == 8 assert v.len == 8
assert v[1] == x assert v[1] == x
} }
struct Context { struct Context {
pub mut: pub mut:
vb [8]f64 vb [8]f64
} }
@ -72,7 +71,7 @@ fn test_fixed_array_can_be_assigned_to_a_struct_field() {
} }
fn multiply_by_two(mut arr [3]int) { fn multiply_by_two(mut arr [3]int) {
for i in 0..arr.len { for i in 0 .. arr.len {
arr[i] *= 2 arr[i] *= 2
} }
} }
@ -82,12 +81,12 @@ fn change_first_element(mut arr [3][3]int) {
} }
fn test_fixed_array_can_be_passed_as_mut_arg() { fn test_fixed_array_can_be_passed_as_mut_arg() {
mut arr := [1,2,3]! mut arr := [1, 2, 3]!
multiply_by_two(mut arr) multiply_by_two(mut arr)
assert arr == [2,4,6]! assert arr == [2, 4, 6]!
mut arr2 := [[1,2,3]!, [4,5,6]!, [7,8,9]!]! mut arr2 := [[1, 2, 3]!, [4, 5, 6]!, [7, 8, 9]!]!
change_first_element(mut arr2) change_first_element(mut arr2)
assert arr2 == [[0,2,3]!, [4,5,6]!, [7,8,9]!]! assert arr2 == [[0, 2, 3]!, [4, 5, 6]!, [7, 8, 9]!]!
} }
fn test_iteration_over_fixed_array() { fn test_iteration_over_fixed_array() {
@ -117,7 +116,6 @@ fn calc_size(a [3]int) {
} }
fn test_for_in_fixed_array() { fn test_for_in_fixed_array() {
arr := [1,2,3]! arr := [1, 2, 3]!
calc_size(arr) calc_size(arr)
} }

View File

@ -9,9 +9,9 @@ fn test1() int {
fn test_fn_assignment_var() { fn test_fn_assignment_var() {
mut a := 0 mut a := 0
mut b := 0 mut b := 0
a , b = test(), test1() a, b = test(), test1()
assert a == 10 assert a == 10
assert b == 11 assert b == 11
a, b = test(), test() a, b = test(), test()
@ -20,13 +20,13 @@ fn test_fn_assignment_var() {
a, b = test(), 12 a, b = test(), 12
assert a == 10 assert a == 10
assert b == 12 assert b == 12
a, b = 12, test() a, b = 12, test()
assert a == 12 assert a == 12
assert b == 10 assert b == 10
} }
fn test_fn_assignment_array() { fn test_fn_assignment_array() {
@ -34,22 +34,20 @@ fn test_fn_assignment_array() {
a[0], a[1] = test(), test1() a[0], a[1] = test(), test1()
assert a[0] == 10 assert a[0] == 10
assert a[1] == 11 assert a[1] == 11
a[0], a[1] = test() , test() a[0], a[1] = test(), test()
assert a[0] == 10 assert a[0] == 10
assert a[1] == 10 assert a[1] == 10
a[0], a[1] = test(), 12 a[0], a[1] = test(), 12
assert a[0] == 10 assert a[0] == 10
assert a[1] == 12 assert a[1] == 12
a[0], a[1] = 12 , test() a[0], a[1] = 12, test()
assert a[0] == 12 assert a[0] == 12
assert a[1] == 10 assert a[1] == 10
} }

View File

@ -1,9 +1,8 @@
fn cross_assign_anon_fn_one(a int, b bool) string {
fn cross_assign_anon_fn_one (a int, b bool) string {
return 'one' return 'one'
} }
fn cross_assign_anon_fn_two (a int, b bool) string { fn cross_assign_anon_fn_two(a int, b bool) string {
return 'two' return 'two'
} }
@ -23,11 +22,11 @@ fn cross_assign_anon_fn_six(a ...int) string {
return 'six' return 'six'
} }
fn cross_assign_anon_fn_seven (a int, b bool) string { fn cross_assign_anon_fn_seven(a int, b bool) string {
return 'seven' return 'seven'
} }
fn cross_assign_anon_fn_eight (a int, b bool) string { fn cross_assign_anon_fn_eight(a int, b bool) string {
return 'eight' return 'eight'
} }
@ -37,7 +36,7 @@ fn test_cross_assign_anon_fn() {
one, two = two, one one, two = two, one
foo := two(0, true) + one(0, true) foo := two(0, true) + one(0, true)
assert foo == 'onetwo' assert foo == 'onetwo'
mut three := cross_assign_anon_fn_three mut three := cross_assign_anon_fn_three
mut four := cross_assign_anon_fn_four mut four := cross_assign_anon_fn_four
three, four = four, three three, four = four, three
@ -47,20 +46,20 @@ fn test_cross_assign_anon_fn() {
foo3 += foo5 foo3 += foo5
assert foo2 == 'threefour' assert foo2 == 'threefour'
assert foo3 == 'threefour' assert foo3 == 'threefour'
mut five := cross_assign_anon_fn_five mut five := cross_assign_anon_fn_five
mut six := cross_assign_anon_fn_six mut six := cross_assign_anon_fn_six
five, six = six, five five, six = six, five
foo6 := six(1, 2, 3) + five(1, 2, 3) foo6 := six(1, 2, 3) + five(1, 2, 3)
assert foo6 == 'fivesix' assert foo6 == 'fivesix'
one, two, three, four, five, six = two, one, four, three, six, five one, two, three, four, five, six = two, one, four, three, six, five
mut foo7, _ := three() mut foo7, _ := three()
foo8, _ := four() foo8, _ := four()
foo7 += foo8 foo7 += foo8
foo9 := one(0, true) + two(0, true) + foo7 + five(1, 2, 3) + six(1, 2, 3) foo9 := one(0, true) + two(0, true) + foo7 + five(1, 2, 3) + six(1, 2, 3)
assert foo9 == 'onetwothreefourfivesix' assert foo9 == 'onetwothreefourfivesix'
mut seven := cross_assign_anon_fn_seven mut seven := cross_assign_anon_fn_seven
mut eight := cross_assign_anon_fn_eight mut eight := cross_assign_anon_fn_eight
one, two, seven, eight = two, seven, eight, one one, two, seven, eight = two, seven, eight, one

View File

@ -2,10 +2,12 @@ struct Foo {
x int x int
} }
pub fn (f Foo) str() string { return 'Foo{}' } pub fn (f Foo) str() string {
return 'Foo{}'
}
fn process_foo(foo &Foo) { fn process_foo(foo &Foo) {
println('>process_foo, called for ${foo} === ${*foo}') println('>process_foo, called for $foo === ${*foo}')
} }
fn get_foo() Foo { fn get_foo() Foo {

View File

@ -3,50 +3,51 @@ fn sqr(x int) int {
return x * x return x * x
} }
fn high_fn(f fn(int) int) { fn high_fn(f fn (int) int) {
x := f(111) x := f(111)
println('x == $x') println('x == $x')
} }
fn high_fn_no_ret(f fn(int)) { fn high_fn_no_ret(f fn (int)) {
f(111) f(111)
} }
fn high_fn_array(f fn(a []int) []int) { fn high_fn_array(f fn(a []int) []int) {
} }
fn high_fn_multi_return(a int, b fn (c []int, d []string) ([]int, []string)) { fn high_fn_multi_return(a int, b fn (c []int, d []string) ([]int, []string)) {
} }
fn high_fn_return_single_anon() (fn(int)f32) { fn high_fn_return_single_anon() fn (int) f32 {
_ = 1 _ = 1
correct := fn(n int)f32 { correct := fn (n int) f32 {
return f32(n * n) return f32(n * n)
} }
return correct return correct
} }
fn high_fn_return_multi_anons() (fn(int)f32, fn(int)string) {
fn high_fn_return_multi_anons() (fn (int) f32, fn (int) string) {
// parsing trap // parsing trap
_ = fn(n int)byte { _ = fn (n int) byte {
return 0x00 return 0x00
} }
correct_second := fn(n int)string { correct_second := fn (n int) string {
return '$n' return '$n'
} }
correct_first := fn(n int)f32 { correct_first := fn (n int) f32 {
return f32(n * n) return f32(n * n)
} }
// parsing trap // parsing trap
_ = fn(n int)[]int { _ = fn (n int) []int {
return [n] return [n]
} }
return correct_first, correct_second return correct_first, correct_second
} }
fn high_fn_return_named_fn() (fn(int)int) {
fn high_fn_return_named_fn() fn (int) int {
return sqr return sqr
} }
fn test_high_fn_ret_anons() { fn test_high_fn_ret_anons() {
param := 13 param := 13
func_sqr1 := high_fn_return_single_anon() func_sqr1 := high_fn_return_single_anon()
@ -63,6 +64,7 @@ fn test_high_fn_ret_anons() {
fn high_fn_applier(arg int, func fn(a int)string) string { fn high_fn_applier(arg int, func fn(a int)string) string {
return func(arg) return func(arg)
} }
fn test_high_fn_applier() { fn test_high_fn_applier() {
arg := 13 arg := 13
expect := '$arg $arg' expect := '$arg $arg'
@ -78,12 +80,12 @@ fn test_fns() {
} }
fn test_anon_fn() { fn test_anon_fn() {
f1 := fn(a int){ f1 := fn (a int) {
println('hello from f1') println('hello from f1')
} }
f1(1) f1(1)
f2 := fn(a int) int { f2 := fn (a int) int {
println('hello from f2') println('hello from f2')
return 10 return 10
} }
@ -102,12 +104,12 @@ fn test_anon_fn() {
} }
fn test_anon_fn_direct_call() { fn test_anon_fn_direct_call() {
fn(name string) { fn (name string) {
println('hello $name') println('hello $name')
}('from anon') }('from anon')
b := fn(n int) int { b := fn (n int) int {
return 11+n return 11 + n
}(100) }(100)
assert b == 111 assert b == 111
} }
@ -121,7 +123,7 @@ fn simple_fn1() int {
} }
fn simple_fn2(n f32) (int, string) { fn simple_fn2(n f32) (int, string) {
return int(1 + n), "fish" return int(1 + n), 'fish'
} }
fn test_assigning_fns() { fn test_assigning_fns() {
@ -131,13 +133,13 @@ fn test_assigning_fns() {
func2 := simple_fn2 func2 := simple_fn2
res2_1, res2_2 := func2(13.0) res2_1, res2_2 := func2(13.0)
assert res2_1 == 14.0 assert res2_1 == 14.0
assert res2_2 == "fish" assert res2_2 == 'fish'
anon_func1 := fn(s string)int { anon_func1 := fn (s string) int {
return s.len return s.len
} }
func3 := anon_func1 func3 := anon_func1
res3 := func3("fish") res3 := func3('fish')
assert res3 == 4 assert res3 == 4
} }

View File

@ -1,29 +1,33 @@
struct Placeholder { struct Placeholder {
name string name string
} }
struct FnStruct { struct FnStruct {
mut: mut:
array_of_fn []fn(int, &Placeholder, string)bool array_of_fn []fn (int, &Placeholder, string) bool
} }
fn test_fn_array_direct_call() { fn test_fn_array_direct_call() {
mut fs := FnStruct{} mut fs := FnStruct{}
fs.array_of_fn << fn(x int, y &Placeholder, z string) bool { fs.array_of_fn << fn (x int, y &Placeholder, z string) bool {
return false return false
} }
println(fs.array_of_fn[0](1, &Placeholder{name: 'Bob'}, 'Builder')) println(fs.array_of_fn[0](1, &Placeholder{ name: 'Bob' }, 'Builder'))
assert fs.array_of_fn[0](1, &Placeholder{name: 'Bob'}, 'Builder') == false assert fs.array_of_fn[0](1, &Placeholder{ name: 'Bob' }, 'Builder') == false
} }
fn test_fn_map_direct_call() { fn test_fn_map_direct_call() {
a := { a := map{
'aaa': fn()string {return 'aaa'}, 'aaa': fn () string {
'bbb': fn()string {return 'bbb'}, return 'aaa'
}
'bbb': fn () string {
return 'bbb'
}
} }
println(a['aaa']()) println(a['aaa']())
println(a['bbb']()) println(a['bbb']())
assert a['aaa']() == 'aaa' assert a['aaa']() == 'aaa'
assert a['bbb']() == 'bbb' assert a['bbb']() == 'bbb'
} }

View File

@ -14,7 +14,9 @@ fn test_fn_multiple_returns() {
fn fn_mr_get_user() (string, int, []string, UserData) { fn fn_mr_get_user() (string, int, []string, UserData) {
groups := ['admins', 'users'] groups := ['admins', 'users']
data := UserData{test: 'Test Data'} data := UserData{
test: 'Test Data'
}
return 'joe', 34, groups, data return 'joe', 34, groups, data
} }
@ -30,9 +32,7 @@ fn split_to_two(s string) ?(string, string) {
} }
fn returnable_fail() string { fn returnable_fail() string {
_,_ := split_to_two('bad') or { _, _ := split_to_two('bad') or { return 'ok' }
return 'ok'
}
return 'nok' return 'nok'
} }
@ -41,7 +41,7 @@ fn test_multiple_ret() {
assert returnable_fail() == 'ok' assert returnable_fail() == 'ok'
// good case // good case
res1_1, res1_2 := split_to_two("fish house") or { res1_1, res1_2 := split_to_two('fish house') or {
assert false assert false
return return
} }
@ -49,8 +49,8 @@ fn test_multiple_ret() {
assert res1_2 == 'house' assert res1_2 == 'house'
// none case // none case
wrapper1 := fn()(string, string){ wrapper1 := fn () (string, string) {
res2_1, res2_2 := split_to_two("") or { res2_1, res2_2 := split_to_two('') or {
assert err.msg == '' assert err.msg == ''
return 'replaced', 'val' return 'replaced', 'val'
} }
@ -61,7 +61,7 @@ fn test_multiple_ret() {
assert res2_2 == 'val' assert res2_2 == 'val'
// error case // error case
wrapper2 := fn()(string, string){ wrapper2 := fn () (string, string) {
res3_1, res3_2 := split_to_two('fishhouse') or { res3_1, res3_2 := split_to_two('fishhouse') or {
assert err.msg == 'error' assert err.msg == 'error'
return 'replaced', 'val' return 'replaced', 'val'

View File

@ -12,12 +12,16 @@ fn test_fn_mut_args_of_array() {
} }
fn init_map(mut n map[string]int) { fn init_map(mut n map[string]int) {
n = {'one': 1} n = map{
'one': 1
}
} }
fn test_fn_mut_args_of_map() { fn test_fn_mut_args_of_map() {
mut m := map[string]int{} mut m := map[string]int{}
init_map(mut m) init_map(mut m)
println(m) println(m)
assert m == {'one': 1} assert m == map{
'one': 1
}
} }

View File

@ -4,7 +4,9 @@ mut:
} }
fn f() shared St { fn f() shared St {
shared x := St{ x: 3.25 } shared x := St{
x: 3.25
}
return x return x
} }
@ -12,19 +14,25 @@ fn g(good bool) ?shared St {
if !good { if !good {
return error('no shared St created') return error('no shared St created')
} }
shared x := St{ x: 12.75 } shared x := St{
x: 12.75
}
return x return x
} }
fn test_shared_fn_return() { fn test_shared_fn_return() {
shared x := f() shared x := f()
val := rlock x { x.x } val := rlock x {
x.x
}
assert val == 3.25 assert val == 3.25
} }
fn shared_opt_propagate(good bool) ?f64 { fn shared_opt_propagate(good bool) ?f64 {
shared x := g(good) ? shared x := g(good) ?
ret := rlock x { x.x } ret := rlock x {
x.x
}
return ret return ret
} }
@ -37,18 +45,26 @@ fn test_shared_opt_propagate() {
fn test_shared_opt_good() { fn test_shared_opt_good() {
shared yy := g(true) or { shared yy := g(true) or {
shared my_default := St{ x: 37.5 } shared my_default := St{
my_default x: 37.5
}
my_default
}
val := rlock yy {
yy.x
} }
val := rlock yy { yy.x }
assert val == 12.75 assert val == 12.75
} }
fn test_shared_opt_bad() { fn test_shared_opt_bad() {
shared yy := g(false) or { shared yy := g(false) or {
shared my_default := St{ x: 37.5 } shared my_default := St{
my_default x: 37.5
}
my_default
}
val := rlock yy {
yy.x
} }
val := rlock yy { yy.x }
assert val == 37.5 assert val == 37.5
} }

View File

@ -7,7 +7,7 @@ fn foo2(a string) int {
} }
fn bar1(mut a [1]fn (string) int) int { fn bar1(mut a [1]fn (string) int) int {
a[0] = foo2 a[0] = foo2
return a[0]('hello') return a[0]('hello')
} }

View File

@ -1,6 +1,6 @@
fn test_for_c_multi_init_vars() { fn test_for_c_multi_init_vars() {
mut rets := []string{} mut rets := []string{}
for a,b := 0,1; a < 5; a++ { for a, b := 0, 1; a < 5; a++ {
if a == 3 { if a == 3 {
continue continue
} }
@ -18,7 +18,7 @@ fn test_for_c_multi_init_vars() {
fn test_for_c_multi_inc_vars() { fn test_for_c_multi_inc_vars() {
mut rets := []string{} mut rets := []string{}
mut b := 1 mut b := 1
for a := 0; a < 10; a,b = b,a+b { for a := 0; a < 10; a, b = b, a + b {
if a in [2, 3] { if a in [2, 3] {
continue continue
} }

View File

@ -64,7 +64,11 @@ fn test_for_in_fixed_array_of_fixed_array_literal() {
fn test_for_in_map_of_fixed_array() { fn test_for_in_map_of_fixed_array() {
mut rets := []string{} mut rets := []string{}
m := map{'aa': [1, 2]!, 'bb': [3, 4]!, 'cc': [5, 6]!} m := map{
'aa': [1, 2]!
'bb': [3, 4]!
'cc': [5, 6]!
}
for k, v in m { for k, v in m {
println('$k, $v') println('$k, $v')
@ -78,7 +82,11 @@ fn test_for_in_map_of_fixed_array() {
fn test_for_in_map_of_fixed_array_literal() { fn test_for_in_map_of_fixed_array_literal() {
mut rets := []string{} mut rets := []string{}
for k, v in map{'aa': [1, 2]!, 'bb': [3, 4]!, 'cc': [5, 6]!} { for k, v in map{
'aa': [1, 2]!
'bb': [3, 4]!
'cc': [5, 6]!
} {
println('$k, $v') println('$k, $v')
rets << '$k, $v' rets << '$k, $v'
} }
@ -89,7 +97,11 @@ fn test_for_in_map_of_fixed_array_literal() {
fn test_for_mut_in_map_of_fixed_array() { fn test_for_mut_in_map_of_fixed_array() {
mut rets := []string{} mut rets := []string{}
mut m := map{'aa': [1, 2]!, 'bb': [3, 4]!, 'cc': [5, 6]!} mut m := map{
'aa': [1, 2]!
'bb': [3, 4]!
'cc': [5, 6]!
}
for k, mut v in m { for k, mut v in m {
println('$k, $v') println('$k, $v')

View File

@ -1,6 +1,6 @@
struct Doubler { struct Doubler {
mut: mut:
val int val int
until int until int
} }

View File

@ -18,40 +18,47 @@ fn foo2(mut arr [3]int) {
} }
fn test_for_in_mut_val_of_fixed_array() { fn test_for_in_mut_val_of_fixed_array() {
mut arr := [1,2,3]! mut arr := [1, 2, 3]!
foo2(mut arr) foo2(mut arr)
println(arr) println(arr)
assert arr == [2, 4, 6]! assert arr == [2, 4, 6]!
} }
fn foo3(mut m map[string][3]int){ fn foo3(mut m map[string][3]int) {
for i in 0..m['hello'].len { for i in 0 .. m['hello'].len {
m['hello'][i] *= 2 m['hello'][i] *= 2
} }
} }
fn test_fn_mut_val_of_map() { fn test_fn_mut_val_of_map() {
mut m := {'hello': [1,2,3]!} mut m := map{
'hello': [1, 2, 3]!
}
foo3(mut m) foo3(mut m)
println(m) println(m)
assert '$m' == "{'hello': [2, 4, 6]}" assert '$m' == "{'hello': [2, 4, 6]}"
} }
fn foo4(mut m map[string][3]int){ fn foo4(mut m map[string][3]int) {
for _, mut j in m['hello'] { for _, mut j in m['hello'] {
j *= 2 j *= 2
} }
} }
fn test_for_in_mut_val_of_map() { fn test_for_in_mut_val_of_map() {
mut m := {'hello':[1,2,3]!} mut m := map{
'hello': [1, 2, 3]!
}
foo4(mut m) foo4(mut m)
println(m) println(m)
assert '$m' == "{'hello': [2, 4, 6]}" assert '$m' == "{'hello': [2, 4, 6]}"
} }
fn test_for_in_mut_val_of_map_direct() { fn test_for_in_mut_val_of_map_direct() {
mut m := {'foo': 1, 'bar': 2} mut m := map{
'foo': 1
'bar': 2
}
for _, mut j in m { for _, mut j in m {
j = 3 j = 3
} }
@ -60,9 +67,18 @@ fn test_for_in_mut_val_of_map_direct() {
} }
fn test_for_in_mut_val_of_map_fixed_array() { fn test_for_in_mut_val_of_map_fixed_array() {
mut m := {'foo': [{'a': 1}]!, 'bar': [{'b': 2}]!} mut m := map{
'foo': [map{
'a': 1
}]!
'bar': [map{
'b': 2
}]!
}
for _, mut j in m { for _, mut j in m {
j = [{'c': 3}]! j = [map{
'c': 3
}]!
} }
println(m) println(m)
assert '$m' == "{'foo': [{'c': 3}], 'bar': [{'c': 3}]}" assert '$m' == "{'foo': [{'c': 3}], 'bar': [{'c': 3}]}"

View File

@ -62,7 +62,14 @@ fn test_for_in_fixed_array_label_continue_break() {
fn test_for_in_map_label_continue_break() { fn test_for_in_map_label_continue_break() {
mut rets := []string{} mut rets := []string{}
m := map{'a': 4, 'b': 5, 'c': 6, 'd': 7, 'e': 8, 'f': 9} m := map{
'a': 4
'b': 5
'c': 6
'd': 7
'e': 8
'f': 9
}
outer: for k, v in m { outer: for k, v in m {
println('$k, $v') println('$k, $v')
rets << '$k, $v' rets << '$k, $v'

View File

@ -44,7 +44,7 @@ fn test_for_char_in_string() {
} }
fn test_for_string_in_map() { fn test_for_string_in_map() {
m := { m := map{
'a': 'b' 'a': 'b'
'c': 'd' 'c': 'd'
} }
@ -54,7 +54,11 @@ fn test_for_string_in_map() {
} }
assert acc == 'a: b, c: d, ' assert acc == 'a: b, c: d, '
mut m2 := {'a': 3, 'b': 4, 'c': 5} mut m2 := map{
'a': 3
'b': 4
'c': 5
}
m2.delete('b') m2.delete('b')
acc = '' acc = ''
for k, v in m2 { for k, v in m2 {

View File

@ -2,6 +2,7 @@ type Node = Expr | string
type Expr = IfExpr | IntegerLiteral type Expr = IfExpr | IntegerLiteral
struct IntegerLiteral {} struct IntegerLiteral {}
struct IfExpr { struct IfExpr {
pos int pos int
} }
@ -11,7 +12,9 @@ struct NodeWrapper {
} }
fn test_nested_sumtype_selector() { fn test_nested_sumtype_selector() {
c := NodeWrapper{Node(Expr(IfExpr{pos: 1}))} c := NodeWrapper{Node(Expr(IfExpr{
pos: 1
}))}
for c.node is Expr { for c.node is Expr {
assert typeof(c.node).name == 'Expr' assert typeof(c.node).name == 'Expr'
break break
@ -28,7 +31,7 @@ mut:
name string name string
} }
type Food = Milk | Eggs type Food = Eggs | Milk
struct FoodWrapper { struct FoodWrapper {
mut: mut:

View File

@ -1,32 +1,40 @@
struct BuildData { x int } struct BuildData {
struct Tensor { x int } x int
}
struct Tensor {
x int
}
fn new_tensor<T>(data BuildData) Tensor { fn new_tensor<T>(data BuildData) Tensor {
println('data: $data') println('data: $data')
x := T(data.x) x := T(data.x)
println(x) println(x)
return Tensor{ data.x } return Tensor{data.x}
} }
fn test_generic_function_returning_type_starting_with_t() { fn test_generic_function_returning_type_starting_with_t() {
ft := new_tensor<f64>(x:123) ft := new_tensor<f64>(x: 123)
println(ft) println(ft)
assert typeof(ft).name == 'Tensor' assert typeof(ft).name == 'Tensor'
assert '$ft' == 'Tensor{\n x: 123\n}' assert '$ft' == 'Tensor{\n x: 123\n}'
// //
it := new_tensor<int>(x:456) it := new_tensor<int>(x: 456)
println(it) println(it)
assert typeof(it).name == 'Tensor' assert typeof(it).name == 'Tensor'
assert '$it' == 'Tensor{\n x: 456\n}' assert '$it' == 'Tensor{\n x: 456\n}'
} }
// the following verifies that returning a generic type T // the following verifies that returning a generic type T
// works at the same time as returning a type starting with T // works at the same time as returning a type starting with T
fn new_t<T>(o T) T { fn new_t<T>(o T) T {
x := T(o) x := T(o)
return x return x
} }
fn test_generic_function_returning_t_type() { fn test_generic_function_returning_t_type() {
f := new_t<f64>(1.23) f := new_t<f64>(1.23)
i := new_t<int>(456) i := new_t<int>(456)
assert '$f' == '1.23' assert '$f' == '1.23'
assert '$i' == '456' assert '$i' == '456'
} }

View File

@ -11,8 +11,8 @@ fn show_result<T, U>(x T, y U) bool {
} }
fn test_generic_fn_upper_name_type() { fn test_generic_fn_upper_name_type() {
assert show_result<int, bool>(1, false) assert show_result<int, bool>(1, false)
assert show_result<string, XX>( "s", XX{}) assert show_result<string, XX>('s', XX{})
assert show_result< XX, string>(XX{}, "s") assert show_result<XX, string>(XX{}, 's')
assert show_result< XX, YY>(XX{}, YY{}) assert show_result<XX, YY>(XX{}, YY{})
} }

View File

@ -1,9 +1,9 @@
fn get<T>(typ T) T { fn get<T>(typ T) T {
return typ return typ
} }
fn get_string(typ string) string { fn get_string(typ string) string {
return 'boom' return 'boom'
} }
fn test_generic_with_same_type() { fn test_generic_with_same_type() {

View File

@ -1,6 +1,6 @@
module main module main
import genericmodule import v.tests.generics_from_modules.genericmodule
fn test_generic_function_from_another_module() { fn test_generic_function_from_another_module() {
v1 := genericmodule.take<int>(true, 10, 20) v1 := genericmodule.take<int>(true, 10, 20)

View File

@ -11,9 +11,11 @@ fn test_identity() {
assert simple<string>('g') + 'h' == 'gh' assert simple<string>('g') + 'h' == 'gh'
assert simple<[]int>([1])[0] == 1 assert simple<[]int>([1])[0] == 1
assert simple<map[string]string>({'a':'b'})['a'] == 'b' assert simple<map[string]string>(map{
'a': 'b'
})['a'] == 'b'
assert simple<simplemodule.Data>(simplemodule.Data{value: 0}).value == 0 assert simple<simplemodule.Data>(simplemodule.Data{ value: 0 }).value == 0
} }
fn plus<T>(xxx T, b T) T { fn plus<T>(xxx T, b T) T {
@ -118,16 +120,18 @@ fn test_return_array() {
} }
fn opt<T>(v T) ?T { fn opt<T>(v T) ?T {
if sizeof(T) > 1 {return v} if sizeof(T) > 1 {
return v
}
return none return none
} }
fn test_optional() { fn test_optional() {
s := opt('hi') or { '' } s := opt('hi') or { '' }
assert s == 'hi' assert s == 'hi'
i := opt(5) or {0} i := opt(5) or { 0 }
assert i == 5 assert i == 5
b := opt(s[0]) or {99} b := opt(s[0]) or { 99 }
assert b == 99 assert b == 99
} }
@ -141,7 +145,7 @@ fn test_ptr() {
assert *ptr('aa') == 'aa' assert *ptr('aa') == 'aa'
} }
fn map_f<T,U>(l []T, f fn(T)U) []U { fn map_f<T, U>(l []T, f fn (T) U) []U {
mut r := []U{} mut r := []U{}
for e in l { for e in l {
r << f(e) r << f(e)
@ -159,11 +163,11 @@ fn foldl<T>(l []T, nil T, f fn(T,T)T) T {
} }
*/ */
fn square(x int) int { fn square(x int) int {
return x*x return x * x
} }
fn mul_int(x int, y int) int { fn mul_int(x int, y int) int {
return x*y return x * y
} }
fn assert_eq<T>(a T, b T) { fn assert_eq<T>(a T, b T) {
@ -173,21 +177,21 @@ fn assert_eq<T>(a T, b T) {
fn print_nice<T>(x T, indent int) string { fn print_nice<T>(x T, indent int) string {
mut space := '' mut space := ''
for _ in 0..indent { for _ in 0 .. indent {
space = space + ' ' space = space + ' '
} }
return '$space$x' return '$space$x'
} }
fn test_generic_fn() { fn test_generic_fn() {
assert_eq(simple(0+1), 1) assert_eq(simple(0 + 1), 1)
assert_eq(simple('g') + 'h', 'gh') assert_eq(simple('g') + 'h', 'gh')
assert_eq(sum([5.1,6.2,7.0]), 18.3) assert_eq(sum([5.1, 6.2, 7.0]), 18.3)
assert_eq(plus(i64(4), i64(6)), i64(10)) assert_eq(plus(i64(4), i64(6)), i64(10))
a := [1,2,3,4] a := [1, 2, 3, 4]
b := map_f(a, square) b := map_f(a, square)
assert_eq(sum(b), 30) // 1+4+9+16 = 30 assert_eq(sum(b), 30) // 1+4+9+16 = 30
//assert_eq(foldl(b, 1, mul_int), 576) // 1*4*9*16 = 576 // assert_eq(foldl(b, 1, mul_int), 576) // 1*4*9*16 = 576
assert print_nice('str', 8) == ' str' assert print_nice('str', 8) == ' str'
} }
@ -243,7 +247,7 @@ pub mut:
} }
struct Repo<T, U> { struct Repo<T, U> {
db DB db DB
pub mut: pub mut:
model T model T
permission U permission U
@ -254,13 +258,13 @@ pub mut:
// return Repo<T,Permission>{db: db} // return Repo<T,Permission>{db: db}
// } // }
fn test_generic_struct() { fn test_generic_struct() {
mut a := Repo<User, Permission>{ mut a := Repo<User,Permission>{
model: User{ model: User{
name: 'joe' name: 'joe'
} }
} }
assert a.model.name == 'joe' assert a.model.name == 'joe'
mut b := Repo<Group, Permission>{ mut b := Repo<Group,Permission>{
permission: Permission{ permission: Permission{
name: 'superuser' name: 'superuser'
} }
@ -292,11 +296,10 @@ fn test_struct_from_other_module() {
} }
fn test_generic_struct_print_array_as_field() { fn test_generic_struct_print_array_as_field() {
foo := Foo<[]string>{ foo := Foo<[]string>{
data: []string{} data: []string{}
} }
assert foo.str() == 'Foo<array, string>{\n data: []\n}' assert foo.str() == 'Foo<array, string>{\n data: []\n}'
} }
/* /*
@ -338,7 +341,7 @@ fn test<T>(mut app T) {
} }
fn nested_test<T>(mut app T) { fn nested_test<T>(mut app T) {
app.context = Context {} app.context = Context{}
} }
fn test_pass_generic_to_nested_function() { fn test_pass_generic_to_nested_function() {
@ -361,10 +364,13 @@ fn method_test<T>(mut app T) {
fn test_pass_generic_to_nested_method() { fn test_pass_generic_to_nested_method() {
mut app := App{} mut app := App{}
method_test(mut app) method_test(mut app)
}*/ }
*/
fn generic_return_map<M>() map[string]M { fn generic_return_map<M>() map[string]M {
return {'': M{}} return map{
'': M{}
}
} }
fn test_generic_return_map() { fn test_generic_return_map() {
@ -372,7 +378,11 @@ fn test_generic_return_map() {
} }
fn generic_return_nested_map<M>() map[string]map[string]M { fn generic_return_nested_map<M>() map[string]map[string]M {
return {'': {'': M{}}} return map{
'': map{
'': M{}
}
}
} }
fn test_generic_return_nested_map() { fn test_generic_return_nested_map() {
@ -383,10 +393,13 @@ fn multi_return<A, B>() (A, B) {
return A{}, B{} return A{}, B{}
} }
struct Foo1{} struct Foo1 {}
struct Foo2{}
struct Foo3{} struct Foo2 {}
struct Foo4{}
struct Foo3 {}
struct Foo4 {}
fn test_multi_return() { fn test_multi_return() {
// compiles // compiles
@ -399,7 +412,7 @@ fn multi_generic_args<T, V>(t T, v V) bool {
} }
fn test_multi_generic_args() { fn test_multi_generic_args() {
assert multi_generic_args("Super", 2021) assert multi_generic_args('Super', 2021)
} }
fn new<T>() T { fn new<T>() T {
@ -429,9 +442,9 @@ fn test_generic_detection() {
v1, v2 := -1, 1 v1, v2 := -1, 1
// not generic // not generic
a1, a2 := v1<v2, v2> v1 a1, a2 := v1 < v2, v2 > v1
assert a1 && a2 assert a1 && a2
b1, b2 := v1 <simplemodule.zero, v2> v1 b1, b2 := v1 < simplemodule.zero, v2 > v1
assert b1 && b2 assert b1 && b2
// generic // generic

View File

@ -5,7 +5,7 @@ fn f(x f64) f64 {
fn test_array_thread_f64_wait() { fn test_array_thread_f64_wait() {
mut r := []thread f64{cap: 10} mut r := []thread f64{cap: 10}
for i in 0 .. 10 { for i in 0 .. 10 {
r << go f(f64(i) + 0.5) r << go f(f64(i) + 0.5)
} }
x := r.wait() x := r.wait()
@ -19,15 +19,15 @@ fn g(shared a []int, i int) {
} }
fn test_array_thread_void_wait() { fn test_array_thread_void_wait() {
shared a := [2 3 5 7 11 13 17] shared a := [2, 3, 5, 7, 11, 13, 17]
t := [ t := [
go g(shared a, 0) go g(shared a, 0),
go g(shared a, 3) go g(shared a, 3),
go g(shared a, 6) go g(shared a, 6),
go g(shared a, 2) go g(shared a, 2),
go g(shared a, 1) go g(shared a, 1),
go g(shared a, 5) go g(shared a, 5),
go g(shared a, 4) go g(shared a, 4),
] ]
println('threads started') println('threads started')
t.wait() t.wait()
@ -37,7 +37,7 @@ fn test_array_thread_void_wait() {
} }
fn test_void_thread_decl() { fn test_void_thread_decl() {
shared a := [2 3 9] shared a := [2, 3, 9]
mut t1 := thread(0) mut t1 := thread(0)
mut tarr := []thread{len: 2} mut tarr := []thread{len: 2}
t1 = go g(shared a, 0) t1 = go g(shared a, 0)

View File

@ -1,13 +1,13 @@
fn foo1() ?int { fn foo1() ?int {
return if true { 0 } else { none } return if true { 0 } else { none }
} }
fn foo2() ?int { fn foo2() ?int {
return if true { 1 } else { error('foo2 error') } return if true { 1 } else { error('foo2 error') }
} }
fn foo3() ?int { fn foo3() ?int {
return if false { 1 } else { error('foo3 error') } return if false { 1 } else { error('foo3 error') }
} }
fn foo4() ?int { fn foo4() ?int {

View File

@ -6,7 +6,7 @@ fn f(n int) ?f64 {
} }
fn test_fn_return() { fn test_fn_return() {
mut res := []f64{cap:2} mut res := []f64{cap: 2}
for m in [-3, 5] { for m in [-3, 5] {
if x := f(m) { if x := f(m) {
res << x res << x
@ -18,8 +18,11 @@ fn test_fn_return() {
} }
fn test_map_get() { fn test_map_get() {
mut m := {'xy': 5, 'zu': 7} mut m := map{
mut res := []int{cap:2} 'xy': 5
'zu': 7
}
mut res := []int{cap: 2}
for k in ['jk', 'zu'] { for k in ['jk', 'zu'] {
if x := m[k] { if x := m[k] {
res << x res << x
@ -32,7 +35,7 @@ fn test_map_get() {
fn test_array_get() { fn test_array_get() {
mut a := [12.5, 6.5, -17.25] mut a := [12.5, 6.5, -17.25]
mut res := []f64{cap:2} mut res := []f64{cap: 2}
for i in [1, 4] { for i in [1, 4] {
if x := a[i] { if x := a[i] {
res << x res << x
@ -44,13 +47,13 @@ fn test_array_get() {
} }
fn test_chan_pop() { fn test_chan_pop() {
mut res := []f64{cap:3} mut res := []f64{cap: 3}
ch := chan f64{cap: 10} ch := chan f64{cap: 10}
ch <- 6.75 ch <- 6.75
ch <- -3.25 ch <- -3.25
ch.close() ch.close()
for _ in 0 .. 3 { for _ in 0 .. 3 {
if x:= <-ch { if x := <-ch {
res << x res << x
} else { } else {
res << -37.5 res << -37.5

View File

@ -38,11 +38,11 @@ fn test_nested_if_smartcast() {
} }
} }
type Bar = string | Test type Bar = Test | string
type Xya = int | string type Xya = int | string
struct Test { struct Test {
x string x string
xya Xya xya Xya
} }
@ -69,10 +69,12 @@ fn test_nested_selector_smartcast() {
} }
type Inner = int | string type Inner = int | string
struct InnerStruct { struct InnerStruct {
x Inner x Inner
} }
type Outer = string | InnerStruct
type Outer = InnerStruct | string
fn test_nested_if_is() { fn test_nested_if_is() {
b := Outer(InnerStruct{Inner(0)}) b := Outer(InnerStruct{Inner(0)})
@ -168,11 +170,13 @@ fn test_mutability() {
} }
} }
type Expr = CallExpr | CTempVarExpr type Expr = CTempVarExpr | CallExpr
struct ExprWrapper { struct ExprWrapper {
mut: mut:
expr Expr expr Expr
} }
struct CallExpr { struct CallExpr {
y int y int
x string x string
@ -196,21 +200,21 @@ fn test_reassign_from_function_with_parameter_selector() {
type Node = Expr | string type Node = Expr | string
fn test_nested_sumtype() { fn test_nested_sumtype() {
c := Node(Expr(CallExpr{y: 1})) c := Node(Expr(CallExpr{
y: 1
}))
if c is Expr { if c is Expr {
if c is CallExpr { if c is CallExpr {
assert c.y == 1 assert c.y == 1
} } else {
else {
assert false assert false
} }
} } else {
else {
assert false assert false
} }
} }
type Food = Milk | Eggs type Food = Eggs | Milk
struct FoodWrapper { struct FoodWrapper {
mut: mut:
@ -240,16 +244,16 @@ struct NodeWrapper {
} }
fn test_nested_sumtype_selector() { fn test_nested_sumtype_selector() {
c := NodeWrapper{Node(Expr(CallExpr{y: 1}))} c := NodeWrapper{Node(Expr(CallExpr{
y: 1
}))}
if c.node is Expr { if c.node is Expr {
if c.node is CallExpr { if c.node is CallExpr {
assert c.node.y == 1 assert c.node.y == 1
} } else {
else {
assert false assert false
} }
} } else {
else {
assert false assert false
} }
} }
@ -279,13 +283,17 @@ type SumAll = Sum1 | Sum2
struct All_in_one { struct All_in_one {
pub mut: pub mut:
ptrs []&SumAll ptrs []&SumAll
ptr &SumAll ptr &SumAll
} }
fn test_nested_pointer_smartcast() { fn test_nested_pointer_smartcast() {
mut s := All_in_one{ mut s := All_in_one{
ptr: &Sum1(Foo1{a: 1}) ptr: &Sum1(Foo1{
ptrs: [&SumAll(Sum2(Bar1{a: 3}))] a: 1
})
ptrs: [&SumAll(Sum2(Bar1{
a: 3
}))]
} }
if mut s.ptr is Sum1 { if mut s.ptr is Sum1 {
@ -296,7 +304,7 @@ fn test_nested_pointer_smartcast() {
a := s.ptrs[0] a := s.ptrs[0]
if a is Sum1 { if a is Sum1 {
if a is Foo1{ if a is Foo1 {
assert a.a == 3 assert a.a == 3
} }
} }

View File

@ -1,24 +1,33 @@
module main module main
import geometry { Point, Line, Shape, point_str, module_name } import geometry { Line, Point, Shape, module_name, point_str }
fn test_imported_symbols_types() { fn test_imported_symbols_types() {
// struct init // struct init
p0 := Point{x: 10 y: 20} p0 := Point{
p1 := Point{x: 40 y: 60} x: 10
// array init y: 20
l0 := Line { }
ps: [p0, p1] p1 := Point{
} x: 40
assert l0.ps[0].y == 20 y: 60
}
// array init
l0 := Line{
ps: [p0, p1]
}
assert l0.ps[0].y == 20
} }
fn test_imported_symbols_functions() { fn test_imported_symbols_functions() {
p0 := Point{x: 20 y: 40} p0 := Point{
x: 20
y: 40
}
// method // method
assert p0.str() == '20 40' assert p0.str() == '20 40'
// function // function
assert point_str(p0) == '20 40' assert point_str(p0) == '20 40'
} }
fn test_imported_symbols_constants() { fn test_imported_symbols_constants() {

View File

@ -1,5 +1,8 @@
enum Colors { enum Colors {
red green blue yellow red
green
blue
yellow
} }
fn test_in_expression() { fn test_in_expression() {
@ -105,9 +108,9 @@ fn test_in_expression_with_string() {
} }
fn test_in_expression_in_map() { fn test_in_expression_in_map() {
m := { m := map{
'one': 1 'one': 1
'two': 2 'two': 2
'three': 3 'three': 3
} }
assert 'one' in m assert 'one' in m

View File

@ -21,9 +21,7 @@ fn test_all() {
println('no compiler tests found') println('no compiler tests found')
assert false assert false
} }
paths := vtest.filter_vtest_only(tests, paths := vtest.filter_vtest_only(tests, basepath: dir)
basepath: dir
)
for path in paths { for path in paths {
print(path + ' ') print(path + ' ')
program := path program := path
@ -40,13 +38,13 @@ fn test_all() {
panic(res.output) panic(res.output)
} }
$if windows { $if windows {
os.rm('./test.exe') or { } os.rm('./test.exe') or {}
$if msvc { $if msvc {
os.rm('./test.ilk') or { } os.rm('./test.ilk') or {}
os.rm('./test.pdb') or { } os.rm('./test.pdb') or {}
} }
} $else { } $else {
os.rm('./test') or { } os.rm('./test') or {}
} }
// println('============') // println('============')
// println(res.output) // println(res.output)

View File

@ -1,5 +1,10 @@
struct Dog { breed string } struct Dog {
struct Cat { breed string } breed string
}
struct Cat {
breed string
}
interface Animal { interface Animal {
breed string breed string
@ -19,15 +24,17 @@ struct Holder {
} }
struct Holder2 { struct Holder2 {
x map[string]Holder x map[string]Holder
breed string breed string
} }
fn test_auto_str_gen_for_complex_interface_types() { fn test_auto_str_gen_for_complex_interface_types() {
a := Animal(Dog{'hi'}) a := Animal(Dog{'hi'})
h := Holder{a} h := Holder{a}
m := map{'dsa': h} m := map{
h2 := Holder2{ m, 'N/A' } 'dsa': h
}
h2 := Holder2{m, 'N/A'}
a2 := Animal(h2) a2 := Animal(h2)
assert '$a2' == r" assert '$a2' == r"

View File

@ -39,7 +39,7 @@ fn test_interface_struct() {
name: 'Richard' name: 'Richard'
} }
} }
assert bz1.sp.say_hello() == "Hello, My name is Richard and I\'m the bawz" assert bz1.sp.say_hello() == "Hello, My name is Richard and I'm the bawz"
print('Test Boss inside Baz struct: ') print('Test Boss inside Baz struct: ')
bz1.sp.speak('Hello world!') bz1.sp.speak('Hello world!')
bz2 := Baz{ bz2 := Baz{
@ -59,7 +59,7 @@ fn test_interface_mut_struct() {
name: 'Derek' name: 'Derek'
} }
} }
assert mbaz.sp.say_hello() == "Hello, My name is Derek and I\'m the bawz" assert mbaz.sp.say_hello() == "Hello, My name is Derek and I'm the bawz"
mbaz.sp = Cat{ mbaz.sp = Cat{
name: 'Dog' name: 'Dog'
breed: 'Not a dog' breed: 'Not a dog'
@ -82,7 +82,7 @@ fn test_interface_struct_from_array() {
}, },
] ]
assert bazs[0].sp.say_hello() == 'Meow meow Kitty the Catty Koo meow' assert bazs[0].sp.say_hello() == 'Meow meow Kitty the Catty Koo meow'
assert bazs[1].sp.say_hello() == "Hello, My name is Bob and I\'m the bawz" assert bazs[1].sp.say_hello() == "Hello, My name is Bob and I'm the bawz"
} }
/* /*

View File

@ -308,17 +308,17 @@ fn animal_match(a Animal) {
interface II { interface II {
mut: mut:
my_field int my_field int
} }
struct AA { struct AA {
BB BB
} }
struct BB { struct BB {
pad [10]byte pad [10]byte
mut: mut:
my_field int my_field int
} }
fn main() { fn main() {

View File

@ -1,7 +1,5 @@
module local module local
pub fn local_fn() bool { pub fn local_fn() bool {
return true return true
} }

View File

@ -2,21 +2,33 @@ type Type = int
type RType = rune type RType = rune
fn test_map_key_alias() { fn test_map_key_alias() {
mut m_int := map{12: '12', 2: '2'} mut m_int := map{
12: '12'
2: '2'
}
m_int[14] = '14' m_int[14] = '14'
m_int[Type(15)] = '15' m_int[Type(15)] = '15'
assert m_int.str() == "{12: '12', 2: '2', 14: '14', 15: '15'}" assert m_int.str() == "{12: '12', 2: '2', 14: '14', 15: '15'}"
//// /// ///// // //// /// ///// //
mut m_rune := map{`a`: '12', `l`: '14'} mut m_rune := map{
`a`: '12'
`l`: '14'
}
m_rune[`g`] = '12' m_rune[`g`] = '12'
m_rune[RType(`$`)] = '16' m_rune[RType(`$`)] = '16'
assert m_rune.str() == "{`a`: '12', `l`: '14', `g`: '12', `$`: '16'}" assert m_rune.str() == "{`a`: '12', `l`: '14', `g`: '12', `$`: '16'}"
} }
fn test_map_alias_key_init() { fn test_map_alias_key_init() {
m_int := map{Type(12): '12', Type(2): '2'} m_int := map{
Type(12): '12'
Type(2): '2'
}
assert m_int.str() == "{12: '12', 2: '2'}" assert m_int.str() == "{12: '12', 2: '2'}"
//// // ///// // //// // ///// //
m_rune := map{RType(`a`): '12', RType(`l`): '14'} m_rune := map{
RType(`a`): '12'
RType(`l`): '14'
}
assert m_rune.str() == "{`a`: '12', `l`: '14'}" assert m_rune.str() == "{`a`: '12', `l`: '14'}"
} }

View File

@ -37,9 +37,15 @@ fn test_array_with_fns() {
} }
fn test_map_with_fns() { fn test_map_with_fns() {
mut a := {'one':foo, 'two':foo2} mut a := map{
'one': foo
'two': foo2
}
assert a.len == 2 assert a.len == 2
assert (a == {'one':foo, 'two':foo2}) == true assert (a == map{
'one': foo
'two': foo2
}) == true
f0 := a['one'] f0 := a['one']
assert f0('xx', '') == 12 assert f0('xx', '') == 12
f1 := a['two'] f1 := a['two']
@ -49,7 +55,9 @@ fn test_map_with_fns() {
assert f2('zzzz', '') == 24 assert f2('zzzz', '') == 24
f3 := a['two'] f3 := a['two']
assert f3('aaaaa', '') == 15 assert f3('aaaaa', '') == 15
mut b := {'one':foo} mut b := map{
'one': foo
}
b['one'] = a['one'] b['one'] = a['one']
f4 := b['one'] f4 := b['one']
assert f4('bbbbbb', '') == 26 assert f4('bbbbbb', '') == 26
@ -70,7 +78,9 @@ fn test_map_and_array_with_fns_typeof_and_direct_call() {
a := [foo3] a := [foo3]
assert typeof(a).name == '[]fn (string) int' assert typeof(a).name == '[]fn (string) int'
assert a[0]('hello') == 15 assert a[0]('hello') == 15
b := {'one': foo3} b := map{
'one': foo3
}
assert typeof(b).name == 'map[string]fn (string) int' assert typeof(b).name == 'map[string]fn (string) int'
assert b['one']('hi') == 12 assert b['one']('hi') == 12
} }
@ -96,13 +106,17 @@ fn bar3(m map[string]fn (string) int) int {
} }
fn bar4(mut m map[string]fn (string) int) int { fn bar4(mut m map[string]fn (string) int) int {
m['fn'] = foo4 m['fn'] = foo4
return m['fn']('hi') return m['fn']('hi')
} }
fn test_map_of_fns_as_argument() { fn test_map_of_fns_as_argument() {
m1 := {'fn': foo3} m1 := map{
'fn': foo3
}
assert bar3(m1) == 12 assert bar3(m1) == 12
mut m2 := {'fn': foo3} mut m2 := map{
'fn': foo3
}
assert bar4(mut m2) == 22 assert bar4(mut m2) == 22
} }

View File

@ -1,17 +1,25 @@
fn foo(mut m map[string][1][2]map[string]int) { fn foo(mut m map[string][1][2]map[string]int) {
m['foo'] = [[{'bar': 1}, {'baz':3}]!]! m['foo'] = [[map{
'bar': 1
}, map{
'baz': 3
}]!]!
} }
fn test_complex_map_fixed_array() { fn test_complex_map_fixed_array() {
mut m := map[string][1][2]map[string]int mut m := map[string][1][2]map[string]int{}
foo(mut m) foo(mut m)
println(m) println(m)
assert '$m' == "{'foo': [[{'bar': 1}, {'baz': 3}]]}" assert '$m' == "{'foo': [[{'bar': 1}, {'baz': 3}]]}"
} }
fn test_innermost_value_of_map_fixed_array() { fn test_innermost_value_of_map_fixed_array() {
mut m := map[string][1][2]map[string]int mut m := map[string][1][2]map[string]int{}
m['foo'] = [[{'bar': 1}, {'baz': 3}]!]! m['foo'] = [[map{
'bar': 1
}, map{
'baz': 3
}]!]!
println(m['foo'][0][0]['bar']) println(m['foo'][0][0]['bar'])
println(m['foo'][0][0]['bar'] == 1) println(m['foo'][0][0]['bar'] == 1)
assert m['foo'][0][0]['bar'] == 1 assert m['foo'][0][0]['bar'] == 1
@ -19,9 +27,18 @@ fn test_innermost_value_of_map_fixed_array() {
} }
fn test_complex_map_high_order_fixed_array() { fn test_complex_map_high_order_fixed_array() {
mut m := {'foo': [[{'a': 1}]!]!, 'bar': [[{'b': 2}]!]!} mut m := map{
'foo': [[map{
'a': 1
}]!]!
'bar': [[map{
'b': 2
}]!]!
}
for _, mut j in m { for _, mut j in m {
j = [[{'c': 3}]!]! j = [[map{
'c': 3
}]!]!
} }
println(m) println(m)
assert '$m' == "{'foo': [[{'c': 3}]], 'bar': [[{'c': 3}]]}" assert '$m' == "{'foo': [[{'c': 3}]], 'bar': [[{'c': 3}]]}"

View File

@ -1,18 +1,38 @@
fn test_map_equality() { fn test_map_equality() {
a1 := {'a':1, 'b':2} a1 := map{
b1 := {'b':2, 'a':1} 'a': 1
c1 := {'a':2, 'b':1} 'b': 2
}
b1 := map{
'b': 2
'a': 1
}
c1 := map{
'a': 2
'b': 1
}
assert a1 == b1 assert a1 == b1
assert a1 != c1 assert a1 != c1
a2 := {'a':1} a2 := map{
b2 := {'a':1, 'b':2} 'a': 1
}
b2 := map{
'a': 1
'b': 2
}
assert a2 != b2 assert a2 != b2
a3 := {'a':'1', 'b':'2'} a3 := map{
b3 := {'b':'2', 'a':'1'} 'a': '1'
'b': '2'
}
b3 := map{
'b': '2'
'a': '1'
}
assert a3 == b3 assert a3 == b3
} }

View File

@ -1,5 +1,5 @@
fn test_high_order_map_assign() { fn test_high_order_map_assign() {
mut m := map[string]map[string]int mut m := map[string]map[string]int{}
m['hello']['hi'] = 1 m['hello']['hi'] = 1
println(m) println(m)
assert '$m' == "{'hello': {'hi': 1}}" assert '$m' == "{'hello': {'hi': 1}}"

View File

@ -1,9 +1,9 @@
const ( const (
alpha = 'a' alpha = 'a'
beta = 'b' beta = 'b'
m = map{ m = map{
alpha : 'Alpha' alpha: 'Alpha'
beta : 'Beta' beta: 'Beta'
} }
) )
@ -13,13 +13,14 @@ fn test_const_keys() {
} }
enum Enum { enum Enum {
a b a
b
} }
const ( const (
m2 = map{ m2 = map{
Enum.a.str() : 'first' Enum.a.str(): 'first'
Enum.b.str() : 'second' Enum.b.str(): 'second'
} }
) )

View File

@ -5,28 +5,33 @@ struct Test {
} }
fn test_interpolation_map_to_string() { fn test_interpolation_map_to_string() {
mut a := map[string]string mut a := map[string]string{}
a['1'] = 'one' a['1'] = 'one'
a['2'] = 'two' a['2'] = 'two'
a['3'] = 'three' a['3'] = 'three'
assert '$a' == "{'1': 'one', '2': 'two', '3': 'three'}" assert '$a' == "{'1': 'one', '2': 'two', '3': 'three'}"
mut b := map[string]int mut b := map[string]int{}
b['1'] = 1 b['1'] = 1
b['2'] = 2 b['2'] = 2
b['3'] = 3 b['3'] = 3
assert '$b' == "{'1': 1, '2': 2, '3': 3}" assert '$b' == "{'1': 1, '2': 2, '3': 3}"
mut c := map[string]bool mut c := map[string]bool{}
c['1'] = true c['1'] = true
c['2'] = false c['2'] = false
assert '$c' == "{'1': true, '2': false}" assert '$c' == "{'1': true, '2': false}"
d := {'f1': 1.1, 'f2': 2.2, 'f3': 3.3, 'f4': 4.4} d := map{
'f1': 1.1
'f2': 2.2
'f3': 3.3
'f4': 4.4
}
println('d: $d') println('d: $d')
assert '$d' == "{'f1': 1.1, 'f2': 2.2, 'f3': 3.3, 'f4': 4.4}" assert '$d' == "{'f1': 1.1, 'f2': 2.2, 'f3': 3.3, 'f4': 4.4}"
mut e := map[string]Test mut e := map[string]Test{}
e['1'] = Test{true, 0, 'abc'} e['1'] = Test{true, 0, 'abc'}
e['2'] = Test{true, 1, 'def'} e['2'] = Test{true, 1, 'def'}
e['3'] = Test{false, 2, 'ghi'} e['3'] = Test{false, 2, 'ghi'}
@ -37,6 +42,8 @@ fn test_interpolation_map_to_string() {
assert s.contains("}, '2': Test{") assert s.contains("}, '2': Test{")
assert s.contains("y: 'def'") assert s.contains("y: 'def'")
f := {'hello': [1,2,3]!} f := map{
'hello': [1, 2, 3]!
}
assert '$f' == "{'hello': [1, 2, 3]}" assert '$f' == "{'hello': [1, 2, 3]}"
} }

View File

@ -1,6 +1,8 @@
type Test = map[string]string type Test = map[string]string
fn test_index() { fn test_index() {
t := Test({'c': 'abc'}) t := Test(map{
assert t['c'] == 'abc' 'c': 'abc'
})
assert t['c'] == 'abc'
} }

View File

@ -1,6 +1,10 @@
fn test_string_int() { fn test_string_int() {
mut m := {'hi':4} mut m := map{
m2:= {'hi':5} 'hi': 4
}
m2 := map{
'hi': 5
}
assert m != m2 assert m != m2
m['hi']++ m['hi']++
assert m == m2 assert m == m2

View File

@ -1,9 +1,10 @@
type SumType = int | string type SumType = int | string
fn s2s(s SumType) SumType { return s } fn s2s(s SumType) SumType {
return s
}
fn test_match_expression_on_sumtype_ordinary_branch(){ fn test_match_expression_on_sumtype_ordinary_branch() {
// tests whether an ordinary branch supports multiple statements, // tests whether an ordinary branch supports multiple statements,
// followed by a default expression // followed by a default expression
mut c := 0 mut c := 0
@ -13,7 +14,8 @@ fn test_match_expression_on_sumtype_ordinary_branch(){
c = 1 c = 1
eprintln('hi') eprintln('hi')
'a string' 'a string'
}else{ }
else {
'unknown' 'unknown'
} }
} }
@ -21,8 +23,7 @@ fn test_match_expression_on_sumtype_ordinary_branch(){
assert c == 1 assert c == 1
} }
fn test_match_expression_on_sumtype_else() {
fn test_match_expression_on_sumtype_else(){
// tests whether else branches support multiple statements, // tests whether else branches support multiple statements,
// when the other branches are simple default expressions // when the other branches are simple default expressions
mut c := 0 mut c := 0
@ -30,7 +31,8 @@ fn test_match_expression_on_sumtype_else(){
res := match s { res := match s {
string { string {
'a string' 'a string'
}else{ }
else {
c = 3 c = 3
eprintln('hi') eprintln('hi')
'unknown' 'unknown'
@ -40,7 +42,7 @@ fn test_match_expression_on_sumtype_else(){
assert c == 3 assert c == 3
} }
fn test_match_expression_on_sumtype_full(){ fn test_match_expression_on_sumtype_full() {
// tests whether all branches can have multiple statements, // tests whether all branches can have multiple statements,
// followed by a default expression // followed by a default expression
mut c := 0 mut c := 0

View File

@ -1,7 +1,11 @@
fn test_match_in_map_init() { fn test_match_in_map_init() {
ret := foo() ret := foo()
println(ret) println(ret)
assert ret == map{'token': 'a', 'sleep': '30', 'every': '1'} assert ret == map{
'token': 'a'
'sleep': '30'
'every': '1'
}
} }
fn foo() map[string]string { fn foo() map[string]string {

View File

@ -1,12 +1,22 @@
interface Animal { name string } interface Animal {
struct Dog { name string } name string
struct Cat { name string } }
struct Dog {
name string
}
struct Cat {
name string
}
fn test_interface_match() { fn test_interface_match() {
a := Animal(Dog{name: 'Jet'}) a := Animal(Dog{
match a { name: 'Jet'
Dog { assert true } })
Cat { assert false } match a {
else { assert false } Dog { assert true }
} Cat { assert false }
else { assert false }
}
} }

View File

@ -2,6 +2,7 @@ type Node = Expr | string
type Expr = IfExpr | IntegerLiteral type Expr = IfExpr | IntegerLiteral
struct IntegerLiteral {} struct IntegerLiteral {}
struct IfExpr { struct IfExpr {
pos int pos int
} }
@ -11,7 +12,9 @@ struct NodeWrapper {
} }
fn test_nested_sumtype_match_selector() { fn test_nested_sumtype_match_selector() {
c := NodeWrapper{Node(Expr(IfExpr{pos: 1}))} c := NodeWrapper{Node(Expr(IfExpr{
pos: 1
}))}
match c.node { match c.node {
Expr { Expr {
match c.node { match c.node {
@ -30,7 +33,9 @@ fn test_nested_sumtype_match_selector() {
} }
fn test_nested_sumtype_match() { fn test_nested_sumtype_match() {
c := Node(Expr(IfExpr{pos: 1})) c := Node(Expr(IfExpr{
pos: 1
}))
match c { match c {
Expr { Expr {
match c { match c {
@ -58,7 +63,7 @@ mut:
name string name string
} }
type Food = Milk | Eggs type Food = Eggs | Milk
struct FoodWrapper { struct FoodWrapper {
mut: mut:

View File

@ -1,11 +1,20 @@
struct Cat{name string} struct Cat {
struct Dog{name string} name string
}
struct Dog {
name string
}
type Animal = Cat | Dog type Animal = Cat | Dog
const ( const (
cat = Cat{name: 'cat'} cat = Cat{
dog = Dog{name: 'dog'} name: 'cat'
}
dog = Dog{
name: 'dog'
}
) )
fn test_shadow() { fn test_shadow() {
@ -15,7 +24,7 @@ fn test_shadow() {
Cat { Cat {
assert animal.name == cat.name assert animal.name == cat.name
} }
else{ else {
assert false assert false
} }
} }
@ -28,7 +37,7 @@ fn test_as() {
Dog { Dog {
assert animal.name == dog.name assert animal.name == dog.name
} }
else{ else {
assert false assert false
} }
} }

View File

@ -5,10 +5,24 @@ mut:
// this must return a reference, or else you'll get a C error // this must return a reference, or else you'll get a C error
// TODO: add a proper checker check for that case // TODO: add a proper checker check for that case
fn new(x int) &Test { return &Test{ x } } fn new(x int) &Test {
fn (mut t Test) inc() &Test { t.val++ return t } return &Test{x}
fn (mut t Test) add(x int) &Test { t.val += x return t } }
fn (mut t Test) div(x int) &Test { t.val /= x return t }
fn (mut t Test) inc() &Test {
t.val++
return t
}
fn (mut t Test) add(x int) &Test {
t.val += x
return t
}
fn (mut t Test) div(x int) &Test {
t.val /= x
return t
}
fn test_method_call_chains() { fn test_method_call_chains() {
mut x := new(4).inc().inc().inc().inc().add(4).div(2).inc() mut x := new(4).inc().inc().inc().inc().add(4).div(2).inc()

View File

@ -7,11 +7,11 @@ interface Animal {
} }
fn (a Animal) info() string { fn (a Animal) info() string {
return "I'm a ${a.breed} ${typeof(a).name}" return "I'm a $a.breed ${typeof(a).name}"
} }
fn new_animal(breed string) Animal { fn new_animal(breed string) Animal {
return &Cat{ breed } return &Cat{breed}
} }
fn test_methods_on_interfaces() { fn test_methods_on_interfaces() {

View File

@ -3,7 +3,7 @@ import crypto.sha256
import term { white } import term { white }
import crypto.md5 { sum } import crypto.md5 { sum }
import log as l import log as l
import time as t { now, utc, Time } import time as t { Time, utc }
import math import math
import crypto.sha512 import crypto.sha512
import cli { Command } import cli { Command }

View File

@ -3,6 +3,6 @@ import time
fn test_module_type_cast() { fn test_module_type_cast() {
a := time.Duration(5) a := time.Duration(5)
b := time.Duration(6) b := time.Duration(6)
//println(a+b) // println(a+b)
assert a+b == 11 assert a + b == 11
} }

View File

@ -1,5 +1,3 @@
/* /*
module acommentedmodule module acommentedmodule
*/ */

View File

@ -1,6 +1,6 @@
module geometry module geometry
const( const (
module_name = 'geometry' module_name = 'geometry'
) )
@ -12,24 +12,24 @@ pub enum Shape {
pub struct Point { pub struct Point {
pub mut: pub mut:
x int x int
y int y int
} }
pub struct Line { pub struct Line {
pub mut: pub mut:
ps []Point ps []Point
} }
pub fn (a Point) +(b Point) Point { pub fn (a Point) + (b Point) Point {
return Point { return Point{
x: a.x + b.x x: a.x + b.x
y: a.y + b.y y: a.y + b.y
} }
} }
pub fn (a Point) str() string { pub fn (a Point) str() string {
return '${a.x} ${a.y}' return '$a.x $a.y'
} }
pub fn point_str(a Point) string { pub fn point_str(a Point) string {

View File

@ -3,14 +3,23 @@ module main
import geometry { Point } import geometry { Point }
fn test_operator_overloading() { fn test_operator_overloading() {
one := Point {x:1, y:2} one := Point{
two := Point {x:5, y:1} x: 1
sum := one + two y: 2
}
two := Point{
x: 5
y: 1
}
sum := one + two
assert sum.x == 6 assert sum.x == 6
assert sum.y == 3 assert sum.y == 3
} }
fn test_str_method() { fn test_str_method() {
one := Point {x:1, y:2} one := Point{
assert '$one' == '1 2' x: 1
y: 2
}
assert '$one' == '1 2'
} }

View File

@ -2,10 +2,10 @@ import simplemodule
// this tests whether the tests can import the same module without any special // this tests whether the tests can import the same module without any special
// custom paths setup on the CLI // custom paths setup on the CLI
fn test_iadd(){ fn test_iadd() {
assert simplemodule.iadd(10, 20) == 30 assert simplemodule.iadd(10, 20) == 30
} }
fn test_imul(){ fn test_imul() {
assert simplemodule.imul(5,8) == 40 assert simplemodule.imul(5, 8) == 40
} }

View File

@ -1,4 +1,5 @@
module xxx module xxx
pub fn f() string { pub fn f() string {
return 'x' return 'x'
} }

View File

@ -1,4 +1,5 @@
module yyy module yyy
pub fn f() string {
pub fn f() string {
return 'y' return 'y'
} }

View File

@ -1,4 +1,5 @@
module zzz module zzz
pub fn f() string { pub fn f() string {
return 'z' return 'z'
} }

View File

@ -49,15 +49,15 @@ fn test_mut_2() {
} }
fn test_mut_3() { fn test_mut_3() {
mut indices := []int{len: 3} mut indices := []int{len: 3}
mut results := []string{} mut results := []string{}
for i, mut v in indices { for i, mut v in indices {
v = i v = i
a := v a := v
println('$i $v $a') println('$i $v $a')
results << '$i $v $a' results << '$i $v $a'
} }
assert results[0] == '0 0 0' assert results[0] == '0 0 0'
assert results[1] == '1 1 1' assert results[1] == '1 1 1'
assert results[2] == '2 2 2' assert results[2] == '2 2 2'
@ -65,42 +65,46 @@ fn test_mut_3() {
struct St { struct St {
mut: mut:
n int n int
} }
fn f(mut x St) { fn f(mut x St) {
mut y := St{n: 2} mut y := St{
a := x n: 2
b := y }
x.n = 3 a := x
y.n = 4 b := y
println('$a.n $b.n') x.n = 3
y.n = 4
println('$a.n $b.n')
assert '$a.n $b.n' == '1 2' assert '$a.n $b.n' == '1 2'
} }
fn test_mut_4() { fn test_mut_4() {
mut x := St{ n: 1 } mut x := St{
f(mut x) n: 1
}
f(mut x)
} }
fn test_mut_5() { fn test_mut_5() {
mut arr1 := []int{len:2} mut arr1 := []int{len: 2}
mut arr2 := []int{len:2} mut arr2 := []int{len: 2}
mut results := []string{} mut results := []string{}
for i, mut v in arr1 { for i, mut v in arr1 {
for ii, mut vv in arr2 { for ii, mut vv in arr2 {
v = i v = i
a := v a := v
println('$i $v $a') println('$i $v $a')
results << '$i $v $a' results << '$i $v $a'
vv = ii vv = ii
aa := vv aa := vv
println('$ii $vv $aa') println('$ii $vv $aa')
results << "$ii $vv $aa" results << '$ii $vv $aa'
} }
} }
assert results[0] == '0 0 0' assert results[0] == '0 0 0'
assert results[1] == '0 0 0' assert results[1] == '0 0 0'
@ -114,21 +118,21 @@ fn test_mut_5() {
fn test_mut_6() { fn test_mut_6() {
mut results := []int{} mut results := []int{}
mut arr := []int{len:3} mut arr := []int{len: 3}
for _, mut v in arr { for _, mut v in arr {
v = v + 1 v = v + 1
println(v) println(v)
results << v results << v
} }
assert results[0] == 1 assert results[0] == 1
assert results[1] == 1 assert results[1] == 1
assert results[2] == 1 assert results[2] == 1
} }
fn test_mut_7() { fn test_mut_7() {
mut arr := []int{len:3} mut arr := []int{len: 3}
mut results := []int{} mut results := []int{}
for _, mut v in arr { for _, mut v in arr {
v = v + 1 // v: 1 v = v + 1 // v: 1
mut vv := v // vv: 1, v: 1 mut vv := v // vv: 1, v: 1
vv = vv + v // vv: 2, v: 1 vv = vv + v // vv: 2, v: 1
@ -136,7 +140,7 @@ fn test_mut_7() {
println(vv) println(vv)
results << v results << v
results << vv results << vv
} }
assert results[0] == 1 assert results[0] == 1
assert results[1] == 2 assert results[1] == 2
assert results[2] == 1 assert results[2] == 1
@ -146,37 +150,40 @@ fn test_mut_7() {
} }
fn test_mut_8() { fn test_mut_8() {
mut indices := []int{len: 1} mut indices := []int{len: 1}
for i, mut v in indices { for i, mut v in indices {
v = i v = i
mut b := v mut b := v
println(typeof(i).name) println(typeof(i).name)
println(typeof(v).name) println(typeof(v).name)
println(typeof(b).name) println(typeof(b).name)
u := [v, 5, 6] u := [v, 5, 6]
println(typeof(u).name) println(typeof(u).name)
println(u) println(u)
assert typeof(b).name == 'int' assert typeof(b).name == 'int'
assert typeof(u).name == '[]int' assert typeof(u).name == '[]int'
assert u == [0, 5, 6] assert u == [0, 5, 6]
} }
} }
fn test_mut_9() { fn test_mut_9() {
mut arr := [0,0,0] mut arr := [0, 0, 0]
mut results := []string{} mut results := []string{}
for _, mut v in arr { for _, mut v in arr {
v = v + 1 // v: 1 v = v + 1 // v: 1
mut vv := v // vv: 1, v: 1 mut vv := v // vv: 1, v: 1
vv = vv + v // vv: 2, v: 1 vv = vv + v // vv: 2, v: 1
foo := {'a': v, 'b': vv} // or use new syntax foo := map{'a': v, 'b': vv}, results are the same foo := map{
'a': v
'b': vv
}
println(v) println(v)
println(vv) println(vv)
println(foo) println(foo)
results << '$v' results << '$v'
results << '$vv' results << '$vv'
results << '$foo' results << '$foo'
} }
assert results[0] == '1' assert results[0] == '1'
assert results[1] == '2' assert results[1] == '2'
assert results[2] == "{'a': 1, 'b': 2}" assert results[2] == "{'a': 1, 'b': 2}"
@ -210,7 +217,7 @@ fn foo1(mut arr [][]int) {
} }
fn test_mut_10() { fn test_mut_10() {
mut arr := [[0,0]] mut arr := [[0, 0]]
foo1(mut arr) foo1(mut arr)
} }
@ -236,7 +243,7 @@ fn foo2(mut arr [][]int) {
} }
fn test_mut_11() { fn test_mut_11() {
mut arr := [[0,0]] mut arr := [[0, 0]]
foo2(mut arr) foo2(mut arr)
} }
@ -251,7 +258,7 @@ fn foo3(mut arr [][]int) {
} }
fn test_mut_12() { fn test_mut_12() {
mut arr := [[0, 0]] mut arr := [[0, 0]]
foo3(mut arr) foo3(mut arr)
} }
@ -270,7 +277,9 @@ fn foo4(mut f Foo) {
} }
fn test_mut_13() { fn test_mut_13() {
mut f := Foo{foo: 1} mut f := Foo{
foo: 1
}
foo4(mut f) foo4(mut f)
} }
@ -286,7 +295,7 @@ fn foo5(mut arr []int) {
} }
fn test_mut_14() { fn test_mut_14() {
mut arr := [1,2,3] mut arr := [1, 2, 3]
foo5(mut arr) foo5(mut arr)
} }
@ -302,7 +311,7 @@ fn foo6(mut arr [3]int) {
} }
fn test_mut_15() { fn test_mut_15() {
mut arr := [1,2,3]! mut arr := [1, 2, 3]!
foo6(mut arr) foo6(mut arr)
} }
@ -318,20 +327,31 @@ fn foo7(mut m map[string]int) {
} }
fn test_mut_16() { fn test_mut_16() {
mut m := map{'one': 100, 'two': 2} mut m := map{
'one': 100
'two': 2
}
foo7(mut m) foo7(mut m)
} }
fn test_mut_17() { fn test_mut_17() {
mut arr := [map{'foo':1}] mut arr := [map{
'foo': 1
}]
for _, mut j in arr { for _, mut j in arr {
mut k := j.clone() mut k := j.clone()
j['foo'] = 0 j['foo'] = 0
unsafe {k['foo'] = 10} unsafe {
k['foo'] = 10
}
println(j) println(j)
println(k) println(k)
assert j == {'foo': 0} assert j == map{
assert k == {'foo': 10} 'foo': 0
}
assert k == map{
'foo': 10
}
} }
} }

View File

@ -62,8 +62,6 @@ fn test_nested_propagation_method() {
mut x := St{ mut x := St{
z: 2.25 z: 2.25
} }
x.aa_propagate() or { x.aa_propagate() or { x.z = 13.0625 }
x.z = 13.0625
}
assert x.z == 13.0625 assert x.z == 13.0625
} }

View File

@ -1,15 +1,19 @@
import math.complex import math.complex
struct Cat { struct Cat {
name string name string
breed string breed string
age int age int
} }
type Feline = Cat type Feline = Cat
fn test_offsetof() { fn test_offsetof() {
cat := Cat{name: 'Cthulhu' breed: 'Great Old One' age: 2147483647} cat := Cat{
name: 'Cthulhu'
breed: 'Great Old One'
age: 2147483647
}
unsafe { unsafe {
assert *(&string(byteptr(&cat) + __offsetof(Cat, name))) == 'Cthulhu' assert *(&string(byteptr(&cat) + __offsetof(Cat, name))) == 'Cthulhu'
assert *(&string(byteptr(&cat) + __offsetof(Cat, breed))) == 'Great Old One' assert *(&string(byteptr(&cat) + __offsetof(Cat, breed))) == 'Great Old One'
@ -26,7 +30,11 @@ fn test_offsetof_struct_from_another_module() {
} }
fn test_offsetof_alias() { fn test_offsetof_alias() {
fel := Feline{name: 'Cthulhu' breed: 'Great Old One' age: 2147483647} fel := Feline{
name: 'Cthulhu'
breed: 'Great Old One'
age: 2147483647
}
unsafe { unsafe {
assert *(&string(byteptr(&fel) + __offsetof(Feline, name))) == 'Cthulhu' assert *(&string(byteptr(&fel) + __offsetof(Feline, name))) == 'Cthulhu'
assert *(&string(byteptr(&fel) + __offsetof(Feline, breed))) == 'Great Old One' assert *(&string(byteptr(&fel) + __offsetof(Feline, breed))) == 'Great Old One'

View File

@ -11,12 +11,16 @@ fn (a Foo) == (b Foo) bool {
} }
fn test_operator_overloading_cmp() { fn test_operator_overloading_cmp() {
a := Foo{i: 38} a := Foo{
b := Foo{i: 38} i: 38
}
b := Foo{
i: 38
}
mut arr := [a, b] mut arr := [a, b]
assert (a > b) == false assert (a > b) == false
assert (a < b) == false assert (a < b) == false
//// /// // //// /// //
assert a >= b assert a >= b
assert a <= b assert a <= b

View File

@ -79,5 +79,5 @@ fn test_operator_overloading_with_string_interpolation() {
ad *= Vec{2, 2} ad *= Vec{2, 2}
assert ad.str() == '{8, 18}' assert ad.str() == '{8, 18}'
ad /= Vec{2, 2} ad /= Vec{2, 2}
assert ad.str() == '{4, 9}' assert ad.str() == '{4, 9}'
} }

View File

@ -41,9 +41,9 @@ fn test_channel_push() {
fn test_thread_wait() { fn test_thread_wait() {
thrs := [ thrs := [
go f(3) go f(3),
go f(-7) go f(-7),
go f(12) go f(12),
] ]
mut res := []int{cap: 3} mut res := []int{cap: 3}
for t in thrs { for t in thrs {
@ -56,4 +56,3 @@ fn test_nested_opt() {
a := f(f(f(-3) or { -7 }) or { 4 }) or { 17 } a := f(f(f(-3) or { -7 }) or { 4 }) or { 17 }
assert a == 4 assert a == 4
} }

View File

@ -36,13 +36,9 @@ fn return_a_string() string {
// //
fn test_optional_int() { fn test_optional_int() {
a := i_0(0) or { a := i_0(0) or { 4999 }
4999
}
assert a == 4999 assert a == 4999
b := i_0(4123) or { b := i_0(4123) or { 4999 }
4999
}
assert b == 4123 assert b == 4123
} }
@ -59,13 +55,9 @@ fn test_optional_bool() {
} }
*/ */
fn test_optional_struct() { fn test_optional_struct() {
sa := struct_0(0) or { sa := struct_0(0) or { Abc{7999} }
Abc{7999}
}
assert sa.x == 7999 assert sa.x == 7999
sb := struct_0(3456) or { sb := struct_0(3456) or { Abc{7999} }
Abc{7999}
}
assert sb.x == 3456 assert sb.x == 3456
} }
@ -75,16 +67,12 @@ fn test_optional_with_statements_before_last_expression() {
Abc{12345} Abc{12345}
} }
assert s.x == 12345 assert s.x == 12345
b := b_0(true) or { b := b_0(true) or { false }
false
}
assert b == true assert b == true
} }
fn test_optional_with_fn_call_as_last_expression() { fn test_optional_with_fn_call_as_last_expression() {
s := string_0(0) or { s := string_0(0) or { return_a_string() }
return_a_string()
}
assert s == 'abcdef' assert s == 'abcdef'
} }
@ -99,46 +87,28 @@ fn test_optional_with_fn_call_last_expr_and_preceding_statements() {
fn test_nested_optional() { fn test_nested_optional() {
a := i_0(1) or { a := i_0(1) or {
b := i_0(0) or { b := i_0(0) or { 3 }
3
}
b b
} }
assert a == 1 assert a == 1
b := i_0(0) or { b := i_0(0) or {
c := i_0(1) or { c := i_0(1) or { 3 }
3
}
c c
} }
assert b == 1 assert b == 1
c := i_0(0) or { c := i_0(0) or {
d := i_0(0) or { d := i_0(0) or { 3 }
3
}
d d
} }
assert c == 3 assert c == 3
} }
fn test_nested_optional_with_opt_fn_call_as_last_value() { fn test_nested_optional_with_opt_fn_call_as_last_value() {
a := i_0(1) or { a := i_0(1) or { i_0(0) or { 3 } }
i_0(0) or {
3
}
}
assert a == 1 assert a == 1
b := i_0(0) or { b := i_0(0) or { i_0(1) or { 3 } }
i_0(1) or {
3
}
}
assert b == 1 assert b == 1
c := i_0(0) or { c := i_0(0) or { i_0(0) or { 3 } }
i_0(0) or {
3
}
}
assert c == 3 assert c == 3
// TODO Enable once optional in boolean expressions are working // TODO Enable once optional in boolean expressions are working
// d := b_0(true) or { // d := b_0(true) or {
@ -156,9 +126,9 @@ fn test_nested_optional_with_opt_fn_call_as_last_value() {
} }
fn remove_suffix1(s string) string { fn remove_suffix1(s string) string {
n := s.len n := s.len
i := s.last_index('.') or { n } i := s.last_index('.') or { n }
return s[0..i] return s[0..i]
} }
fn test_var_inside_or_block() { fn test_var_inside_or_block() {

View File

@ -1,20 +1,19 @@
fn test_error_can_be_converted_to_string() {
fn test_error_can_be_converted_to_string(){
assert 'Option{ error: "an error" }' == error('an error').str() assert 'Option{ error: "an error" }' == error('an error').str()
} }
fn test_error_can_be_assigned_to_a_variable(){ fn test_error_can_be_assigned_to_a_variable() {
f := error('an error') f := error('an error')
assert 'Option{ error: "an error" }' == f.str() assert 'Option{ error: "an error" }' == f.str()
} }
fn test_error_can_be_printed(){ fn test_error_can_be_printed() {
f := error('an error') f := error('an error')
println(f) println(f)
assert true assert true
} }
fn test_error_can_be_interpolated_in_a_string(){ fn test_error_can_be_interpolated_in_a_string() {
f := error('an error') f := error('an error')
s := 'hi $f' s := 'hi $f'
assert s == 'hi Option{ error: "an error" }' assert s == 'hi Option{ error: "an error" }'

View File

@ -269,7 +269,7 @@ fn test_multi_return_opt() {
*/ */
fn test_optional_val_with_empty_or() { fn test_optional_val_with_empty_or() {
ret_none() or { } ret_none() or {}
assert true assert true
} }

View File

@ -1,12 +1,12 @@
import sqlite import sqlite
struct Upper { struct Upper {
id int id int
sub SubStruct sub SubStruct
} }
struct SubStruct { struct SubStruct {
id int id int
name string name string
} }

View File

@ -1,6 +1,6 @@
module main module main
import mod1 import v.tests.project_with_c_code.mod1
fn main() { fn main() {
res := mod1.vadd(1, 2) res := mod1.vadd(1, 2)

View File

@ -1,5 +1,5 @@
import mod1 import v.tests.project_with_c_code.mod1
fn test_using_c_code_in_the_same_module_works(){ fn test_using_c_code_in_the_same_module_works() {
assert 1003 == mod1.vadd(1,2) assert 1003 == mod1.vadd(1, 2)
} }

View File

@ -12,6 +12,6 @@ struct C.MyStruct {
fn C.cadd(int, int) int fn C.cadd(int, int) int
pub fn vadd(a int, b int) int { pub fn vadd(a int, b int) int {
x := C.MyStruct{ 100 } x := C.MyStruct{100}
return 900 + x.UppercaseField + C.cadd(a, b) return 900 + x.UppercaseField + C.cadd(a, b)
} }

View File

@ -1,6 +1,6 @@
module main module main
import modc import v.tests.project_with_c_code_2.modc
// passing array of Vtype to C // passing array of Vtype to C
// Vtype wraps a C type // Vtype wraps a C type

View File

@ -1,4 +1,4 @@
import modc import v.tests.project_with_c_code_2.modc
fn test_using_c_code_in_the_same_module_works() { fn test_using_c_code_in_the_same_module_works() {
x := modc.new_vtype(123) x := modc.new_vtype(123)

Some files were not shown because too many files have changed in this diff Show More