cgen: fix struct with map field str() error

pull/4748/head
yuyi 2020-05-06 14:08:48 +08:00 committed by GitHub
parent 24aff9e511
commit c653977c15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 5 deletions

View File

@ -3271,7 +3271,7 @@ fn (mut g Gen) gen_str_for_struct(info table.Struct, styp, str_fn_name string) {
} else if sym.kind == .struct_ {
g.auto_str_funcs.write('indents, ')
g.auto_str_funcs.write('${field_styp_fn_name}( it->${field.name}${second_str_param} ) ')
} else if sym.kind in [.array, .array_fixed] {
} else if sym.kind in [.array, .array_fixed, .map] {
g.auto_str_funcs.write('indents, ')
g.auto_str_funcs.write('${field_styp_fn_name}( it->${field.name}) ')
} else {

View File

@ -10,12 +10,12 @@ struct Man {
fn test_default_struct_string_interpolation() {
superman := Man{'Superman', 30, ['flying', 'fighting evil', 'being nice']}
s := '$superman'
assert s.contains('Man {')
assert s.starts_with('Man {')
assert s.contains("name: 'Superman'")
assert s.contains('age: 30')
assert s.contains('interests: [')
assert s.contains("'being nice'")
assert s.contains('}')
assert s.ends_with('}')
// println(s)
}
@ -29,7 +29,24 @@ fn test_fixed_array_struct_string_interpolation() {
x := 2.32
ctx.vb = [1.1, x, 3.3, 4.4, 5.0, 6.0, 7.0, 8.9]!!
s := '$ctx'
assert s.contains('Context {')
assert s.starts_with('Context {')
assert s.contains('vb: [1.1, 2.32, 3.3, 4.4, 5, 6, 7, 8.9]')
assert s.contains('}')
assert s.ends_with('}')
}
struct Info {
name string
dict map[string]int
}
fn test_struct_map_field_string_interpolation() {
info := Info{
name: 'test'
dict: {'a': 1, 'b': 2}
}
s := '$info'
assert s.starts_with('Info {')
assert s.contains("name: 'test'")
assert s.contains("dict: {'a': 1, 'b': 2}")
assert s.ends_with('}')
}