v4 / vlib / v / tests / interfaces / interface_uninitialized_field_str_test.v
32 lines · 26 sloc · 870 bytes · 78c5946200278130882d01ad7e35b0e23a6c7f9c
Raw
1// Test for bug #17372: Uninitialized interface type field gives runtime error when accessing it
2// Expected behavior: print "nil" instead of causing a runtime crash
3
4interface Boo {}
5
6struct Foo {
7 boo Boo
8}
9
10@[noinit]
11struct Bar {
12 boo Boo
13}
14
15fn test_uninitialized_interface_field_str() {
16 // Printing uninitialized interface field should return "nil", not crash
17 s := '${Foo{}.boo}'
18 assert s == 'nil', 'Expected "nil" for uninitialized interface, got "${s}"'
19}
20
21fn test_uninitialized_interface_field_in_struct_str() {
22 // Struct containing uninitialized interface should also work
23 foo := Foo{}
24 s := '${foo}'
25 assert s.contains('boo: nil'), 'Expected struct str to contain "boo: nil", got "${s}"'
26}
27
28fn test_noinit_struct_with_interface_field() {
29 bar := Bar{}
30 s := '${bar.boo}'
31 assert s == 'nil', 'Expected "nil" for noinit struct interface field, got "${s}"'
32}
33