From fd8bb2c95cdb17a1805fd9b8c80d307ac008e011 Mon Sep 17 00:00:00 2001 From: Joe Conigliaro Date: Fri, 27 Mar 2020 20:47:24 +1100 Subject: [PATCH] tests: small array/struct test notes & tmp fix --- vlib/builtin/array_test.v | 22 ++++++++++++++++------ vlib/compiler/tests/struct_test.v | 16 ++++++++-------- vlib/v/checker/checker.v | 3 ++- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/vlib/builtin/array_test.v b/vlib/builtin/array_test.v index f03c71c670..98fef99ee8 100644 --- a/vlib/builtin/array_test.v +++ b/vlib/builtin/array_test.v @@ -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() { diff --git a/vlib/compiler/tests/struct_test.v b/vlib/compiler/tests/struct_test.v index a6080f7914..854d4c227a 100644 --- a/vlib/compiler/tests/struct_test.v +++ b/vlib/compiler/tests/struct_test.v @@ -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 diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 5f4028d226..274b91f773 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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 }