| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package templates | 5 package templates |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "testing" | 8 "testing" |
| 9 | 9 |
| 10 "mojom/generators/go/translator" | 10 "mojom/generators/go/translator" |
| 11 ) | 11 ) |
| 12 | 12 |
| 13 type mockEncodingInfo struct { | 13 type mockEncodingInfo struct { |
| 14 translator.EncodingInfo | 14 translator.EncodingInfo |
| 15 isSimple bool | 15 isSimple bool |
| 16 isPointer bool | 16 isPointer bool |
| 17 isHandle bool | 17 isHandle bool |
| 18 isArray bool | 18 isArray bool |
| 19 isMap bool | 19 isMap bool |
| 20 isNullable bool | 20 isNullable bool |
| 21 isStruct bool | 21 isStruct bool |
| 22 isUnion bool |
| 22 elementEncodingInfo *mockEncodingInfo | 23 elementEncodingInfo *mockEncodingInfo |
| 23 keyEncodingInfo *mockEncodingInfo | 24 keyEncodingInfo *mockEncodingInfo |
| 24 valueEncodingInfo *mockEncodingInfo | 25 valueEncodingInfo *mockEncodingInfo |
| 25 bitSize uint32 | 26 bitSize uint32 |
| 26 writeFunction string | 27 writeFunction string |
| 27 readFunction string | 28 readFunction string |
| 28 identifier string | 29 identifier string |
| 29 goType string | 30 goType string |
| 30 } | 31 } |
| 31 | 32 |
| 32 func (m mockEncodingInfo) IsSimple() bool { return
m.isSimple } | 33 func (m mockEncodingInfo) IsSimple() bool { return
m.isSimple } |
| 33 func (m mockEncodingInfo) IsPointer() bool { return
m.isPointer } | 34 func (m mockEncodingInfo) IsPointer() bool { return
m.isPointer } |
| 34 func (m mockEncodingInfo) IsHandle() bool { return
m.isHandle } | 35 func (m mockEncodingInfo) IsHandle() bool { return
m.isHandle } |
| 35 func (m mockEncodingInfo) IsArray() bool { return
m.isArray } | 36 func (m mockEncodingInfo) IsArray() bool { return
m.isArray } |
| 36 func (m mockEncodingInfo) IsMap() bool { return
m.isMap } | 37 func (m mockEncodingInfo) IsMap() bool { return
m.isMap } |
| 37 func (m mockEncodingInfo) IsNullable() bool { return
m.isNullable } | 38 func (m mockEncodingInfo) IsNullable() bool { return
m.isNullable } |
| 38 func (m mockEncodingInfo) IsStruct() bool { return
m.isStruct } | 39 func (m mockEncodingInfo) IsStruct() bool { return
m.isStruct } |
| 40 func (m mockEncodingInfo) IsUnion() bool { return
m.isUnion } |
| 39 func (m mockEncodingInfo) ElementEncodingInfo() translator.EncodingInfo { return
m.elementEncodingInfo } | 41 func (m mockEncodingInfo) ElementEncodingInfo() translator.EncodingInfo { return
m.elementEncodingInfo } |
| 40 func (m mockEncodingInfo) KeyEncodingInfo() translator.EncodingInfo { return
m.keyEncodingInfo } | 42 func (m mockEncodingInfo) KeyEncodingInfo() translator.EncodingInfo { return
m.keyEncodingInfo } |
| 41 func (m mockEncodingInfo) ValueEncodingInfo() translator.EncodingInfo { return
m.valueEncodingInfo } | 43 func (m mockEncodingInfo) ValueEncodingInfo() translator.EncodingInfo { return
m.valueEncodingInfo } |
| 42 func (m mockEncodingInfo) BitSize() uint32 { return
m.bitSize } | 44 func (m mockEncodingInfo) BitSize() uint32 { return
m.bitSize } |
| 43 func (m mockEncodingInfo) WriteFunction() string { return
m.writeFunction } | 45 func (m mockEncodingInfo) WriteFunction() string { return
m.writeFunction } |
| 44 func (m mockEncodingInfo) ReadFunction() string { return
m.readFunction } | 46 func (m mockEncodingInfo) ReadFunction() string { return
m.readFunction } |
| 45 func (m mockEncodingInfo) Identifier() string { return
m.identifier } | 47 func (m mockEncodingInfo) Identifier() string { return
m.identifier } |
| 46 func (m mockEncodingInfo) GoType() string { return
m.goType } | 48 func (m mockEncodingInfo) GoType() string { return
m.goType } |
| 47 | 49 |
| 48 func TestEncodingSimpleFieldEncoding(t *testing.T) { | 50 func TestEncodingSimpleFieldEncoding(t *testing.T) { |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 | 289 |
| 288 encodingInfo := mockEncodingInfo{ | 290 encodingInfo := mockEncodingInfo{ |
| 289 isPointer: true, | 291 isPointer: true, |
| 290 isStruct: true, | 292 isStruct: true, |
| 291 isNullable: true, | 293 isNullable: true, |
| 292 identifier: "s.FNullableStruct", | 294 identifier: "s.FNullableStruct", |
| 293 } | 295 } |
| 294 | 296 |
| 295 check(t, expected, "FieldEncodingTmpl", encodingInfo) | 297 check(t, expected, "FieldEncodingTmpl", encodingInfo) |
| 296 } | 298 } |
| 299 |
| 300 func TestEncodingUnionFieldEncoding(t *testing.T) { |
| 301 expected := `if s.FUnion == nil { |
| 302 return &bindings.ValidationError{bindings.UnexpectedNullUnion, "unexpect
ed null union"} |
| 303 } else { |
| 304 if err := s.FUnion.Encode(encoder); err != nil { |
| 305 return err |
| 306 } |
| 307 }` |
| 308 |
| 309 encodingInfo := mockEncodingInfo{ |
| 310 isUnion: true, |
| 311 identifier: "s.FUnion", |
| 312 } |
| 313 |
| 314 check(t, expected, "FieldEncodingTmpl", encodingInfo) |
| 315 } |
| 316 |
| 317 func TestEncodingNullableUnionFieldEncoding(t *testing.T) { |
| 318 expected := `if s.FUnion == nil { |
| 319 encoder.WriteNullUnion() |
| 320 } else { |
| 321 if err := s.FUnion.Encode(encoder); err != nil { |
| 322 return err |
| 323 } |
| 324 }` |
| 325 |
| 326 encodingInfo := mockEncodingInfo{ |
| 327 isNullable: true, |
| 328 isUnion: true, |
| 329 identifier: "s.FUnion", |
| 330 } |
| 331 |
| 332 check(t, expected, "FieldEncodingTmpl", encodingInfo) |
| 333 } |
| 334 |
| 335 func TestEncodingNestedUnionFieldEncoding(t *testing.T) { |
| 336 expected := `if err := encoder.WritePointer(); err != nil { |
| 337 return err |
| 338 } |
| 339 encoder.StartNestedUnion() |
| 340 if s.FUnion == nil { |
| 341 return &bindings.ValidationError{bindings.UnexpectedNullUnion, "unexpect
ed null union"} |
| 342 } else { |
| 343 if err := s.FUnion.Encode(encoder); err != nil { |
| 344 return err |
| 345 } |
| 346 }` |
| 347 |
| 348 encodingInfo := mockEncodingInfo{ |
| 349 isPointer: true, |
| 350 isUnion: true, |
| 351 identifier: "s.FUnion", |
| 352 } |
| 353 |
| 354 check(t, expected, "FieldEncodingTmpl", encodingInfo) |
| 355 } |
| 356 |
| 357 func TestEncodingNestedNullableUnionFieldEncoding(t *testing.T) { |
| 358 expected := `if err := encoder.WritePointer(); err != nil { |
| 359 return err |
| 360 } |
| 361 encoder.StartNestedUnion() |
| 362 if s.FUnion == nil { |
| 363 encoder.WriteNullUnion() |
| 364 } else { |
| 365 if err := s.FUnion.Encode(encoder); err != nil { |
| 366 return err |
| 367 } |
| 368 }` |
| 369 |
| 370 encodingInfo := mockEncodingInfo{ |
| 371 isNullable: true, |
| 372 isPointer: true, |
| 373 isUnion: true, |
| 374 identifier: "s.FUnion", |
| 375 } |
| 376 |
| 377 check(t, expected, "FieldEncodingTmpl", encodingInfo) |
| 378 } |
| OLD | NEW |