| 1 | import flag |
| 2 | import v.tests.field_publicity |
| 3 | |
| 4 | struct Foo { |
| 5 | x int |
| 6 | y int = 5 |
| 7 | } |
| 8 | |
| 9 | fn (f Foo) foo() {} |
| 10 | |
| 11 | struct Bar { |
| 12 | Foo |
| 13 | } |
| 14 | |
| 15 | fn test_embed() { |
| 16 | b := Bar{} |
| 17 | assert b.x == 0 |
| 18 | b.foo() |
| 19 | } |
| 20 | |
| 21 | fn test_embed_direct_access() { |
| 22 | b := Bar{ |
| 23 | Foo: Foo{} |
| 24 | } |
| 25 | assert b.Foo.y == 5 |
| 26 | } |
| 27 | |
| 28 | fn test_default_value() { |
| 29 | b := Bar{ |
| 30 | Foo: Foo{} |
| 31 | } |
| 32 | assert b.y == 5 |
| 33 | } |
| 34 | |
| 35 | fn test_default_value_without_init() { |
| 36 | b := Bar{} |
| 37 | assert b.y == 5 |
| 38 | } |
| 39 | |
| 40 | fn test_initialize() { |
| 41 | b := Bar{ |
| 42 | x: 1 |
| 43 | y: 2 |
| 44 | } |
| 45 | assert b.x == 1 |
| 46 | assert b.y == 2 |
| 47 | } |
| 48 | |
| 49 | struct Bar3 { |
| 50 | Foo |
| 51 | y string = 'test' |
| 52 | } |
| 53 | |
| 54 | fn test_overwrite_field() { |
| 55 | b := Bar3{} |
| 56 | assert b.y == 'test' |
| 57 | } |
| 58 | |
| 59 | struct TestEmbedFromModule { |
| 60 | flag.Flag |
| 61 | } |
| 62 | |
| 63 | struct BarGeneric[T] { |
| 64 | pub: |
| 65 | foo T |
| 66 | } |
| 67 | |
| 68 | struct BarGenericContainer { |
| 69 | BarGeneric[int] |
| 70 | } |
| 71 | |
| 72 | fn test_generic_embed() { |
| 73 | b := BarGenericContainer{} |
| 74 | assert b.BarGeneric.foo == 0 |
| 75 | assert b.foo == 0 |
| 76 | } |
| 77 | |
| 78 | struct Upper { |
| 79 | mut: |
| 80 | x int |
| 81 | } |
| 82 | |
| 83 | struct UpperHolder { |
| 84 | Upper |
| 85 | } |
| 86 | |
| 87 | fn test_assign() { |
| 88 | mut h := UpperHolder{} |
| 89 | h.x = 5 |
| 90 | assert h.x == 5 |
| 91 | } |
| 92 | |
| 93 | fn test_embed_is_public() { |
| 94 | a := field_publicity.App{} |
| 95 | assert a.Context.name == '' |
| 96 | } |
| 97 | |
| 98 | struct Eggs { |
| 99 | name string |
| 100 | } |
| 101 | |
| 102 | fn (f &Eggs) test(x int) int { |
| 103 | return x |
| 104 | } |
| 105 | |
| 106 | struct Breakfast { |
| 107 | Eggs |
| 108 | } |
| 109 | |
| 110 | fn (b &Breakfast) name() string { |
| 111 | return b.name |
| 112 | } |
| 113 | |
| 114 | fn test_embed_method_receiver_ptr() { |
| 115 | b := Breakfast{} |
| 116 | assert b.test(5) == 5 |
| 117 | } |
| 118 | |
| 119 | fn test_embed_field_receiver_ptr() { |
| 120 | b := Breakfast{} |
| 121 | assert b.name() == '' |
| 122 | } |
| 123 | |
| 124 | fn test_embed_mutable() { |
| 125 | mut a := field_publicity.App{} |
| 126 | a.Context = field_publicity.Context{} |
| 127 | } |
| 128 | |
| 129 | struct Context { |
| 130 | static_files string |
| 131 | } |
| 132 | |
| 133 | fn (c Context) test() bool { |
| 134 | return true |
| 135 | } |
| 136 | |
| 137 | struct App { |
| 138 | Context |
| 139 | } |
| 140 | |
| 141 | fn embed_field_access_generic[T](mut app T) { |
| 142 | app.Context = Context{ |
| 143 | static_files: app.static_files |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | fn test_embed_field_access_generic() { |
| 148 | mut app := App{} |
| 149 | embed_field_access_generic(mut app) |
| 150 | } |
| 151 | |
| 152 | fn embed_method_generic[T](app T) bool { |
| 153 | return app.test() |
| 154 | } |
| 155 | |
| 156 | fn test_embed_method_generic() { |
| 157 | mut app := App{} |
| 158 | assert embed_method_generic(app) |
| 159 | } |
| 160 | |
| 161 | type Piece = King | Queen |
| 162 | |
| 163 | struct Pos { |
| 164 | x u8 |
| 165 | y u8 |
| 166 | } |
| 167 | |
| 168 | enum TeamEnum { |
| 169 | black |
| 170 | white |
| 171 | } |
| 172 | |
| 173 | struct PieceCommonFields { |
| 174 | pos Pos |
| 175 | team TeamEnum |
| 176 | } |
| 177 | |
| 178 | fn (p PieceCommonFields) get_pos() Pos { |
| 179 | return p.pos |
| 180 | } |
| 181 | |
| 182 | struct King { |
| 183 | PieceCommonFields |
| 184 | } |
| 185 | |
| 186 | struct Queen { |
| 187 | PieceCommonFields |
| 188 | } |
| 189 | |
| 190 | fn (piece Piece) pos() Pos { |
| 191 | mut pos := Pos{} |
| 192 | match piece { |
| 193 | King, Queen { pos = piece.pos } |
| 194 | } |
| 195 | |
| 196 | return pos |
| 197 | } |
| 198 | |
| 199 | fn (piece Piece) get_pos() Pos { |
| 200 | mut pos := Pos{} |
| 201 | match piece { |
| 202 | King, Queen { pos = piece.get_pos() } |
| 203 | } |
| 204 | |
| 205 | return pos |
| 206 | } |
| 207 | |
| 208 | fn test_match_aggregate_field() { |
| 209 | piece := Piece(King{ |
| 210 | pos: Pos{1, 8} |
| 211 | team: .black |
| 212 | }) |
| 213 | pos := piece.pos() |
| 214 | assert pos.x == 1 |
| 215 | assert pos.y == 8 |
| 216 | } |
| 217 | |
| 218 | fn test_match_aggregate_method() { |
| 219 | piece := Piece(King{ |
| 220 | pos: Pos{1, 8} |
| 221 | team: .black |
| 222 | }) |
| 223 | pos := piece.get_pos() |
| 224 | assert pos.x == 1 |
| 225 | assert pos.y == 8 |
| 226 | } |
| 227 | |