vq / vlib / v / tests / casts / cast_option_to_interface_test.v
121 lines · 105 sloc · 2.0 KB · 62ec38ab47e4ad3471c9f419eab5f4fbe3ead1f7
Raw
1@[heap]
2struct Package {
3 str string
4}
5
6interface Parser {
7 main &Package
8}
9
10struct ParserV1 {
11mut:
12 main &Package
13}
14
15fn new_0_parser() ?ParserV1 {
16 return ParserV1{
17 main: &Package{
18 str: 'test'
19 }
20 }
21}
22
23fn new_parser() ?Parser {
24 return Parser(new_0_parser()?)
25}
26
27struct Engine {
28 parser Parser
29}
30
31fn test_cast_option_to_interface() {
32 parser := new_parser()?
33 assert parser.main.str == 'test'
34 eprintln(voidptr(parser.main))
35 e := Engine{
36 parser: parser
37 }
38 assert e.parser.main.str == 'test'
39 eprintln(voidptr(e.parser.main))
40}
41
42interface Issue27340Value {
43}
44
45struct Issue27340Cat {
46 state int
47}
48
49fn maybe_issue_27340_cat() ?Issue27340Cat {
50 return Issue27340Cat{
51 state: 1
52 }
53}
54
55fn test_option_none_guard_interface_cast() {
56 x := maybe_issue_27340_cat()
57 if x == none {
58 assert false
59 return
60 }
61 v := Issue27340Value(x)
62 assert v is Issue27340Cat
63 cat := v as Issue27340Cat
64 assert cat.state == 1
65}
66
67fn issue_27340_value_state(v Issue27340Value) int {
68 assert v is Issue27340Cat
69 cat := v as Issue27340Cat
70 return cat.state
71}
72
73fn test_option_none_guard_implicit_interface_arg() {
74 x := maybe_issue_27340_cat()
75 if x == none {
76 assert false
77 return
78 }
79 assert issue_27340_value_state(x) == 1
80}
81
82fn test_option_none_guard_interface_array_append() {
83 x := maybe_issue_27340_cat()
84 if x == none {
85 assert false
86 return
87 }
88 mut values := []Issue27340Value{}
89 values << x
90 assert issue_27340_value_state(values[0]) == 1
91}
92
93fn test_option_positive_none_guard_interface_cast() {
94 x := maybe_issue_27340_cat()
95 if x != none {
96 v := Issue27340Value(x)
97 assert issue_27340_value_state(v) == 1
98 return
99 }
100 assert false
101}
102
103fn test_option_positive_none_guard_implicit_interface_arg() {
104 x := maybe_issue_27340_cat()
105 if x != none {
106 assert issue_27340_value_state(x) == 1
107 return
108 }
109 assert false
110}
111
112fn test_option_positive_none_guard_interface_array_append() {
113 x := maybe_issue_27340_cat()
114 if x != none {
115 mut values := []Issue27340Value{}
116 values << x
117 assert issue_27340_value_state(values[0]) == 1
118 return
119 }
120 assert false
121}
122