| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 parser | 5 package parser |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "mojom/mojom_parser/mojom" | 9 "mojom/mojom_parser/mojom" |
| 10 "strings" | 10 "strings" |
| (...skipping 624 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 635 | 635 |
| 636 // TestSingleFileTypeValidationErrors() test the method UserTypeRef.validateAfte
rResolution(). | 636 // TestSingleFileTypeValidationErrors() test the method UserTypeRef.validateAfte
rResolution(). |
| 637 // It is similar to TestSingleFileValueValidationErrors() | 637 // It is similar to TestSingleFileValueValidationErrors() |
| 638 // except that it tests the validation of types phase which occurs before the va
lidation | 638 // except that it tests the validation of types phase which occurs before the va
lidation |
| 639 // of values phase. This phase detects errors that may not be detected during pa
rsing | 639 // of values phase. This phase detects errors that may not be detected during pa
rsing |
| 640 // but that may be detected without resolving value references. | 640 // but that may be detected without resolving value references. |
| 641 func TestSingleFileTypeValidationErrors(t *testing.T) { | 641 func TestSingleFileTypeValidationErrors(t *testing.T) { |
| 642 test := singleFileTest{} | 642 test := singleFileTest{} |
| 643 | 643 |
| 644 //////////////////////////////////////////////////////////// | 644 //////////////////////////////////////////////////////////// |
| 645 // Group 1: Left-hand-side is a struct |
| 646 //////////////////////////////////////////////////////////// |
| 647 |
| 648 //////////////////////////////////////////////////////////// |
| 645 // Test Case: Use struct as constant type | 649 // Test Case: Use struct as constant type |
| 646 //////////////////////////////////////////////////////////// | 650 //////////////////////////////////////////////////////////// |
| 647 { | 651 { |
| 648 contents := ` | 652 contents := ` |
| 649 struct Foo{ | 653 struct Foo{ |
| 650 }; | 654 }; |
| 651 | 655 |
| 652 const Foo MyFoo = 3; | 656 const Foo MyFoo = 3; |
| 653 ` | 657 ` |
| 654 test.addTestCase(contents, []string{ | 658 test.addTestCase(contents, []string{ |
| 655 "The type Foo is not allowed as the type of a declared c
onstant.", | 659 "The type Foo is not allowed as the type of a declared c
onstant.", |
| 656 "Only simple types, strings and enum types may be used"}
) | 660 "Only simple types, strings and enum types may be used"}
) |
| 657 } | 661 } |
| 658 | 662 |
| 659 //////////////////////////////////////////////////////////// | 663 //////////////////////////////////////////////////////////// |
| 660 » // Test Case: Assign struct as map key | 664 » // Test Case: Use struct as map key |
| 661 //////////////////////////////////////////////////////////// | 665 //////////////////////////////////////////////////////////// |
| 662 { | 666 { |
| 663 contents := ` | 667 contents := ` |
| 664 struct Foo{ | 668 struct Foo{ |
| 665 }; | 669 }; |
| 666 | 670 |
| 667 struct Bar{ | 671 struct Bar{ |
| 668 map<Foo, int32> x; | 672 map<Foo, int32> x; |
| 669 }; | 673 }; |
| 670 ` | 674 ` |
| (...skipping 13 matching lines...) Expand all Loading... |
| 684 struct Bar{ | 688 struct Bar{ |
| 685 Foo x = 42; | 689 Foo x = 42; |
| 686 }; | 690 }; |
| 687 ` | 691 ` |
| 688 test.addTestCase(contents, []string{ | 692 test.addTestCase(contents, []string{ |
| 689 "Illegal assignment", | 693 "Illegal assignment", |
| 690 "Field x of type Foo may not be assigned the value 42 of
type int8"}) | 694 "Field x of type Foo may not be assigned the value 42 of
type int8"}) |
| 691 } | 695 } |
| 692 | 696 |
| 693 //////////////////////////////////////////////////////////// | 697 //////////////////////////////////////////////////////////// |
| 698 // Group 2: Left-hand-side is an enum |
| 699 //////////////////////////////////////////////////////////// |
| 700 |
| 701 //////////////////////////////////////////////////////////// |
| 702 // Test Case: Assign an integer to an enum variable in a struct field. |
| 703 //////////////////////////////////////////////////////////// |
| 704 { |
| 705 contents := ` |
| 706 enum Hats { |
| 707 COWBOY, |
| 708 TOP |
| 709 }; |
| 710 |
| 711 struct Bar{ |
| 712 Hats my_hat = 1; |
| 713 }; |
| 714 ` |
| 715 test.addTestCase(contents, []string{ |
| 716 "Illegal assignment", |
| 717 "Field my_hat of type Hats may not be assigned the value
1 of type int8."}) |
| 718 } |
| 719 |
| 720 //////////////////////////////////////////////////////////// |
| 721 // Test Case: Assign an integer to an enum variable in a constant. |
| 722 //////////////////////////////////////////////////////////// |
| 723 { |
| 724 contents := ` |
| 725 enum Hats { |
| 726 COWBOY, |
| 727 TOP |
| 728 }; |
| 729 |
| 730 const Hats MY_HAT = 1; |
| 731 ` |
| 732 test.addTestCase(contents, []string{ |
| 733 "Illegal assignment", |
| 734 "Const MY_HAT of type Hats may not be assigned the value
1 of type int8."}) |
| 735 } |
| 736 |
| 737 //////////////////////////////////////////////////////////// |
| 694 // Execute all of the test cases. | 738 // Execute all of the test cases. |
| 695 //////////////////////////////////////////////////////////// | 739 //////////////////////////////////////////////////////////// |
| 696 for i, c := range test.cases { | 740 for i, c := range test.cases { |
| 697 // Parse and resolve the mojom input. | 741 // Parse and resolve the mojom input. |
| 698 descriptor := mojom.NewMojomDescriptor() | 742 descriptor := mojom.NewMojomDescriptor() |
| 699 parser := MakeParser(c.fileName, c.mojomContents, descriptor, c.
importedFrom) | 743 parser := MakeParser(c.fileName, c.mojomContents, descriptor, c.
importedFrom) |
| 700 parser.Parse() | 744 parser.Parse() |
| 701 if !parser.OK() { | 745 if !parser.OK() { |
| 702 t.Errorf("Parsing error for %s: %s", c.fileName, parser.
GetError().Error()) | 746 t.Errorf("Parsing error for %s: %s", c.fileName, parser.
GetError().Error()) |
| 703 continue | 747 continue |
| 704 } | 748 } |
| 705 err := descriptor.Resolve() | 749 err := descriptor.Resolve() |
| 706 if err == nil { | 750 if err == nil { |
| 707 t.Errorf("Resolution unexpectedly succeeded for test cas
e %d.", i) | 751 t.Errorf("Resolution unexpectedly succeeded for test cas
e %d.", i) |
| 708 continue | 752 continue |
| 709 } | 753 } |
| 710 got := err.Error() | 754 got := err.Error() |
| 711 for _, expected := range c.expectedErrors { | 755 for _, expected := range c.expectedErrors { |
| 712 if !strings.Contains(got, expected) { | 756 if !strings.Contains(got, expected) { |
| 713 t.Errorf("%s:\n*****expected to contain:\n%s\n**
**actual\n%s", c.fileName, expected, got) | 757 t.Errorf("%s:\n*****expected to contain:\n%s\n**
**actual\n%s", c.fileName, expected, got) |
| 714 } | 758 } |
| 715 } | 759 } |
| 716 | 760 |
| 717 } | 761 } |
| 718 } | 762 } |
| OLD | NEW |