OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package parser |
| 6 |
| 7 import ( |
| 8 "mojom/mojom_parser/mojom" |
| 9 "strings" |
| 10 "testing" |
| 11 ) |
| 12 |
| 13 // TestMinVersionErrors test the method MojomStruct.ComputeVersionInfo() which |
| 14 // is invoked by ComputeFinalData. This phase occurs after resolution |
| 15 // and type validation. We test that different types of errors related to |
| 16 // the MinVersion attribute are correctly detected. |
| 17 func TestMinVersionErrors(t *testing.T) { |
| 18 test := singleFileTest{} |
| 19 |
| 20 //////////////////////////////////////////////////////////// |
| 21 // Test Case: Float value for MinVersion |
| 22 //////////////////////////////////////////////////////////// |
| 23 { |
| 24 contents := ` |
| 25 struct Foo{ |
| 26 int32 x; |
| 27 int32 y; |
| 28 |
| 29 [MinVersion = 1.1] |
| 30 array<int32>? z; |
| 31 |
| 32 [MinVersion = 2] |
| 33 array<int32>? w; |
| 34 };` |
| 35 test.addTestCase(contents, []string{ |
| 36 "Invalid MinVersion attribute for field z: 1.1. ", |
| 37 "The value must be a non-negative 32-bit integer value."
}) |
| 38 } |
| 39 |
| 40 //////////////////////////////////////////////////////////// |
| 41 // Test Case: string value for MinVersion |
| 42 //////////////////////////////////////////////////////////// |
| 43 { |
| 44 contents := ` |
| 45 struct Foo{ |
| 46 int32 x; |
| 47 int32 y; |
| 48 |
| 49 [MinVersion = "1"] |
| 50 array<int32>? z; |
| 51 |
| 52 [MinVersion = 2] |
| 53 array<int32>? w; |
| 54 };` |
| 55 test.addTestCase(contents, []string{ |
| 56 "Invalid MinVersion attribute for field z: \"1\". ", |
| 57 "The value must be a non-negative 32-bit integer value."
}) |
| 58 } |
| 59 |
| 60 //////////////////////////////////////////////////////////// |
| 61 // Test Case: MinVersion is negative |
| 62 //////////////////////////////////////////////////////////// |
| 63 { |
| 64 contents := ` |
| 65 struct Foo{ |
| 66 int32 x; |
| 67 int32 y; |
| 68 |
| 69 [MinVersion = -1] |
| 70 array<int32>? z; |
| 71 |
| 72 [MinVersion = 2] |
| 73 array<int32>? w; |
| 74 };` |
| 75 test.addTestCase(contents, []string{ |
| 76 "Invalid MinVersion attribute for field z: -1. ", |
| 77 "The value must be a non-negative 32-bit integer value."
}) |
| 78 } |
| 79 |
| 80 //////////////////////////////////////////////////////////// |
| 81 // Test Case: MinVersion is to big for 32 bits |
| 82 //////////////////////////////////////////////////////////// |
| 83 { |
| 84 contents := ` |
| 85 struct Foo{ |
| 86 int32 x; |
| 87 int32 y; |
| 88 |
| 89 [MinVersion = 1234567890123] |
| 90 array<int32>? z; |
| 91 |
| 92 [MinVersion = 2] |
| 93 array<int32>? w; |
| 94 };` |
| 95 test.addTestCase(contents, []string{ |
| 96 "Invalid MinVersion attribute for field z: 1234567890123
. ", |
| 97 "The value must be a non-negative 32-bit integer value."
}) |
| 98 } |
| 99 |
| 100 //////////////////////////////////////////////////////////// |
| 101 // Test Case: Min Versions must be increasing. |
| 102 //////////////////////////////////////////////////////////// |
| 103 { |
| 104 contents := ` |
| 105 struct Foo{ |
| 106 int32 x; |
| 107 int32 y; |
| 108 |
| 109 [MinVersion = 2] |
| 110 array<int32>? z; |
| 111 |
| 112 [MinVersion = 1] |
| 113 array<int32>? w; |
| 114 };` |
| 115 test.addTestCase(contents, []string{ |
| 116 "Invalid MinVersion attribute for field w: 1. ", |
| 117 "The MinVersion must be non-decreasing as a function of
the ordinal.", |
| 118 " This field's MinVersion must be at least 2."}) |
| 119 } |
| 120 |
| 121 //////////////////////////////////////////////////////////// |
| 122 // Test Case: Min Versions must be increasing: ordinals are used. |
| 123 //////////////////////////////////////////////////////////// |
| 124 { |
| 125 contents := ` |
| 126 struct Foo{ |
| 127 int32 x; |
| 128 int32 y; |
| 129 |
| 130 [MinVersion = 1] |
| 131 array<int32>? z@3; |
| 132 |
| 133 [MinVersion = 2] |
| 134 array<int32>? w@2; |
| 135 };` |
| 136 test.addTestCase(contents, []string{ |
| 137 "Invalid MinVersion attribute for field z: 1. ", |
| 138 "The MinVersion must be non-decreasing as a function of
the ordinal.", |
| 139 " This field's MinVersion must be at least 2."}) |
| 140 } |
| 141 |
| 142 //////////////////////////////////////////////////////////// |
| 143 // Test Case: Non-nullable type used. |
| 144 //////////////////////////////////////////////////////////// |
| 145 { |
| 146 contents := ` |
| 147 struct Foo{ |
| 148 int32 x; |
| 149 int32 y; |
| 150 |
| 151 [MinVersion = 1] |
| 152 array<int32> z; |
| 153 |
| 154 [MinVersion = 2] |
| 155 array<int32>? w; |
| 156 };` |
| 157 test.addTestCase(contents, []string{ |
| 158 "Invalid type for field z: array<int32>.", |
| 159 "Non-nullable fields are only allowed in version 0 of of
a struct.", |
| 160 "This field's MinVersion is 1."}) |
| 161 } |
| 162 |
| 163 //////////////////////////////////////////////////////////// |
| 164 // Execute all of the test cases. |
| 165 //////////////////////////////////////////////////////////// |
| 166 for i, c := range test.cases { |
| 167 // Parse anresolve the mojom input. |
| 168 descriptor := mojom.NewMojomDescriptor() |
| 169 specifiedName := "" |
| 170 if c.importedFrom == nil { |
| 171 specifiedName = c.fileName |
| 172 } |
| 173 parser := MakeParser(c.fileName, specifiedName, c.mojomContents,
descriptor, c.importedFrom) |
| 174 parser.Parse() |
| 175 if !parser.OK() { |
| 176 t.Errorf("Parsing error for %s: %s", c.fileName, parser.
GetError().Error()) |
| 177 continue |
| 178 } |
| 179 err := descriptor.Resolve() |
| 180 if err != nil { |
| 181 t.Errorf("Resolution error for %s: %s", c.fileName, err) |
| 182 continue |
| 183 } |
| 184 |
| 185 err = descriptor.ComputeFinalData() |
| 186 |
| 187 if err == nil { |
| 188 t.Errorf("Data computation unexpectedly succeeded for te
st case %d.", i) |
| 189 continue |
| 190 } |
| 191 |
| 192 got := err.Error() |
| 193 for _, expected := range c.expectedErrors { |
| 194 if !strings.Contains(got, expected) { |
| 195 t.Errorf("%s:\n*****expected to contain:\n%s\n**
**actual\n%s", c.fileName, expected, got) |
| 196 } |
| 197 } |
| 198 |
| 199 } |
| 200 } |
OLD | NEW |