tests: small array/struct test notes & tmp fix

pull/4131/head
Joe Conigliaro 2020-03-27 20:47:24 +11:00
parent ed42b864c1
commit fd8bb2c95c
3 changed files with 26 additions and 15 deletions

View File

@ -339,6 +339,19 @@ mut:
b []Test2
}
// TODO: default array/struct str methods
pub fn (ta []Test2) str() string {
mut s := '['
for i, t in ta {
s += t.str()
if i < ta.len-1 {
s += ', '
}
}
s += ']'
return s
}
pub fn (t Test2) str() string {
return '{$t.one $t.two}'
}
@ -348,9 +361,6 @@ pub fn (t Test) str() string {
}
fn test_struct_print() {
println('QTODO')
/*
mut a := Test{
a: 'Test'
b: []
@ -361,10 +371,10 @@ fn test_struct_print() {
}
a.b << b
a.b << b
assert a.str() == '{Test [{1 2}, {1 2}] }'
assert b.str() == '{1 2}'
println('QTODO: v2 struct .str() methods')
// assert a.str() == '{Test [{1 2}, {1 2}] }'
// assert b.str() == '{1 2}'
assert a.b.str() == '[{1 2}, {1 2}]'
*/
}
fn test_single_element() {

View File

@ -1,19 +1,19 @@
struct AA {
struct Aa {
mut:
val int
nums []int
}
struct BB {
struct Bb {
mut:
a AA
a Aa
}
struct CC {
struct Cu {
mut:
b BB
b Bb
nums []int
aarr []AA
aarr []Aa
num int
}
@ -64,7 +64,7 @@ fn test_empty_struct() {
}
fn test_struct_levels() {
mut c := CC{}
mut c := Cu{}
println(c.nums.len)
assert c.nums.len == 0
c.nums << 3
@ -82,7 +82,7 @@ fn test_struct_levels() {
assert c.b.a.nums[1] == 2
c.b.a.nums[0] = 7
assert c.b.a.nums[0] == 7
c.aarr << AA{
c.aarr << Aa{
val: 8
}
assert c.aarr.len == 1

View File

@ -281,6 +281,7 @@ pub fn (c mut Checker) method_call_expr(method_call_expr mut ast.MethodCallExpr)
name := method_call_expr.name
c.stmts(method_call_expr.or_block.stmts)
// println('method call $name $method_call_expr.pos.line_nr')
// TODO: remove this for actual methods, use only for compiler magic
if typ_sym.kind == .array && name in ['filter', 'clone', 'repeat', 'reverse', 'map', 'slice'] {
if name in ['filter', 'map'] {
array_info := typ_sym.info as table.Array
@ -344,7 +345,7 @@ pub fn (c mut Checker) method_call_expr(method_call_expr mut ast.MethodCallExpr)
return table.string_type
}
if typ_sym.kind == .array && name == 'str' {
// method_call_expr.receiver_type = table.new_type(c.table.type_idxs['ar_string'])
method_call_expr.receiver_type = typ
method_call_expr.return_type = table.string_type
return table.string_type
}