|
| 1 | +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:expect/expect.dart'; |
| 6 | + |
| 7 | +import 'package:test_runner/src/path.dart'; |
| 8 | +import 'package:test_runner/src/test_file.dart'; |
| 9 | + |
| 10 | +// Note: This test file validates how some of the special markers used by the |
| 11 | +// test runner are parsed. But this test is also run *by* that same test |
| 12 | +// runner, and we don't want it to see the markers inside the string literals |
| 13 | +// here as significant, so we obfuscate them using seemingly-pointless string |
| 14 | +// interpolations here, like `{'//'}`. |
| 15 | + |
| 16 | +void main() { |
| 17 | + testParseDill(); |
| 18 | + testParseVMOptions(); |
| 19 | + testParseOtherOptions(); |
| 20 | + testParseEnvironment(); |
| 21 | + testParsePackages(); |
| 22 | + testParseMultitest(); |
| 23 | + testParseMultiHtmltest(); |
| 24 | + testParseErrorFlags(); |
| 25 | + testName(); |
| 26 | + testMultitest(); |
| 27 | +} |
| 28 | + |
| 29 | +void testParseDill() { |
| 30 | + // Handles ".dill" files. |
| 31 | + var file = parse("", path: "test.dill"); |
| 32 | + Expect.isNotNull(file.vmOptions); |
| 33 | + Expect.equals(1, file.vmOptions.length); |
| 34 | + Expect.listEquals(<String>[], file.vmOptions.first); |
| 35 | + |
| 36 | + Expect.listEquals(<String>[], file.dartOptions); |
| 37 | + Expect.listEquals(<String>[], file.sharedOptions); |
| 38 | + Expect.listEquals(<String>[], file.dart2jsOptions); |
| 39 | + Expect.listEquals(<String>[], file.ddcOptions); |
| 40 | + Expect.listEquals(<String>[], file.otherResources); |
| 41 | + Expect.listEquals(<String>[], file.sharedObjects); |
| 42 | + |
| 43 | + Expect.isNull(file.environment); |
| 44 | + Expect.isNull(file.packages); |
| 45 | + |
| 46 | + Expect.isFalse(file.isMultitest); |
| 47 | + |
| 48 | + Expect.isFalse(file.hasSyntaxError); |
| 49 | + Expect.isFalse(file.hasCompileError); |
| 50 | + Expect.isFalse(file.hasRuntimeError); |
| 51 | + Expect.isFalse(file.hasStaticWarning); |
| 52 | + Expect.isFalse(file.hasCrash); |
| 53 | +} |
| 54 | + |
| 55 | +void testParseVMOptions() { |
| 56 | + expectVMOptions(String source, List<List<String>> expected) { |
| 57 | + var file = parse(source); |
| 58 | + Expect.isNotNull(file.vmOptions); |
| 59 | + Expect.equals(expected.length, file.vmOptions.length); |
| 60 | + for (var i = 0; i < expected.length; i++) { |
| 61 | + Expect.listEquals(expected[i], file.vmOptions[i]); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // No options. |
| 66 | + expectVMOptions("", [[]]); |
| 67 | + |
| 68 | + // Splits words. |
| 69 | + expectVMOptions("${'//'} VMOptions=--verbose --async", [ |
| 70 | + ["--verbose", "--async"] |
| 71 | + ]); |
| 72 | + |
| 73 | + // Allows multiple. |
| 74 | + expectVMOptions(""" |
| 75 | + ${'//'} VMOptions=--first one |
| 76 | + ${'//'} VMOptions=--second two |
| 77 | + """, [ |
| 78 | + ["--first", "one"], |
| 79 | + ["--second", "two"] |
| 80 | + ]); |
| 81 | +} |
| 82 | + |
| 83 | +void testParseOtherOptions() { |
| 84 | + // No options. |
| 85 | + var file = parse(""); |
| 86 | + Expect.listEquals(<String>[], file.dartOptions); |
| 87 | + Expect.listEquals(<String>[], file.sharedOptions); |
| 88 | + Expect.listEquals(<String>[], file.dart2jsOptions); |
| 89 | + Expect.listEquals(<String>[], file.ddcOptions); |
| 90 | + Expect.listEquals(<String>[], file.otherResources); |
| 91 | + Expect.listEquals(<String>[], file.sharedObjects); |
| 92 | + |
| 93 | + // Single options split into words. |
| 94 | + file = parse(""" |
| 95 | + ${'//'} DartOptions=dart options |
| 96 | + ${'//'} SharedOptions=shared options |
| 97 | + ${'//'} dart2jsOptions=dart2js options |
| 98 | + ${'//'} dartdevcOptions=ddc options |
| 99 | + ${'//'} OtherResources=other resources |
| 100 | + ${'//'} SharedObjects=shared objects |
| 101 | + """); |
| 102 | + Expect.listEquals(["dart", "options"], file.dartOptions); |
| 103 | + Expect.listEquals(["shared", "options"], file.sharedOptions); |
| 104 | + Expect.listEquals(["dart2js", "options"], file.dart2jsOptions); |
| 105 | + Expect.listEquals(["ddc", "options"], file.ddcOptions); |
| 106 | + Expect.listEquals(["other", "resources"], file.otherResources); |
| 107 | + Expect.listEquals(["shared", "objects"], file.sharedObjects); |
| 108 | + |
| 109 | + // Disallows multiple lines for some options. |
| 110 | + expectParseThrows(""" |
| 111 | + ${'//'} DartOptions=first |
| 112 | + ${'//'} DartOptions=second |
| 113 | + """); |
| 114 | + expectParseThrows(""" |
| 115 | + ${'//'} SharedOptions=first |
| 116 | + ${'//'} SharedOptions=second |
| 117 | + """); |
| 118 | + expectParseThrows(""" |
| 119 | + ${'//'} dart2jsOptions=first |
| 120 | + ${'//'} dart2jsOptions=second |
| 121 | + """); |
| 122 | + expectParseThrows(""" |
| 123 | + ${'//'} dartdevcOptions=first |
| 124 | + ${'//'} dartdevcOptions=second |
| 125 | + """); |
| 126 | + |
| 127 | + // Merges multiple lines for others. |
| 128 | + file = parse(""" |
| 129 | + ${'//'} OtherResources=other resources |
| 130 | + ${'//'} OtherResources=even more |
| 131 | + ${'//'} SharedObjects=shared objects |
| 132 | + ${'//'} SharedObjects=many more |
| 133 | + """); |
| 134 | + Expect.listEquals( |
| 135 | + ["other", "resources", "even", "more"], file.otherResources); |
| 136 | + Expect.listEquals(["shared", "objects", "many", "more"], file.sharedObjects); |
| 137 | +} |
| 138 | + |
| 139 | +void testParseEnvironment() { |
| 140 | + // No environment. |
| 141 | + var file = parse(""); |
| 142 | + Expect.isNull(file.environment); |
| 143 | + |
| 144 | + // Without values. |
| 145 | + file = parse(""" |
| 146 | + ${'//'} Environment=some value |
| 147 | + ${'//'} Environment=another one |
| 148 | + """); |
| 149 | + Expect.mapEquals({"some value": "", "another one": ""}, file.environment); |
| 150 | + |
| 151 | + // With values. |
| 152 | + file = parse(""" |
| 153 | + ${'//'} Environment=some value=its value |
| 154 | + ${'//'} Environment=another one = also value |
| 155 | + """); |
| 156 | + Expect.mapEquals( |
| 157 | + {"some value": "its value", "another one ": " also value"}, |
| 158 | + file.environment); |
| 159 | +} |
| 160 | + |
| 161 | +void testParsePackages() { |
| 162 | + // No option. |
| 163 | + var file = parse(""); |
| 164 | + Expect.isNull(file.packages); |
| 165 | + |
| 166 | + // Single option is converted to a path. |
| 167 | + file = parse(""" |
| 168 | + ${'//'} Packages=packages thing |
| 169 | + """); |
| 170 | + Expect.isTrue(file.packages.endsWith("/packages thing")); |
| 171 | + |
| 172 | + // "none" is left alone. |
| 173 | + file = parse(""" |
| 174 | + ${'//'} Packages=none |
| 175 | + """); |
| 176 | + Expect.equals("none", file.packages); |
| 177 | + |
| 178 | + // Cannot appear more than once. |
| 179 | + expectParseThrows(""" |
| 180 | + ${'//'} Packages=first |
| 181 | + ${'//'} Packages=second |
| 182 | + """); |
| 183 | +} |
| 184 | + |
| 185 | +void testParseMultitest() { |
| 186 | + // Not present. |
| 187 | + var file = parse(""); |
| 188 | + Expect.isFalse(file.isMultitest); |
| 189 | + |
| 190 | + // Present. |
| 191 | + file = parse(""" |
| 192 | + main() {} ${'//'}# 01: compile-time error |
| 193 | + """); |
| 194 | + Expect.isTrue(file.isMultitest); |
| 195 | +} |
| 196 | + |
| 197 | +void testParseMultiHtmltest() { |
| 198 | + // Not present. |
| 199 | + var file = parse(""); |
| 200 | + Expect.isFalse(file.isMultiHtmlTest); |
| 201 | + Expect.listEquals(<String>[], file.subtestNames); |
| 202 | + |
| 203 | + // Present. |
| 204 | + // Note: the "${''}" is to prevent the test runner running *this* test file |
| 205 | + // from parsing it as a multi-HTML test. |
| 206 | + file = parse(""" |
| 207 | + main() { |
| 208 | + useHtml${''}IndividualConfiguration(); |
| 209 | + group('pixel_manipulation', () { |
| 210 | + }); |
| 211 | + group('arc', () { |
| 212 | + }); |
| 213 | + group('drawImage_image_element', () { |
| 214 | + }); |
| 215 | + } |
| 216 | + """); |
| 217 | + Expect.isTrue(file.isMultiHtmlTest); |
| 218 | + Expect.listEquals(["pixel_manipulation", "arc", "drawImage_image_element"], |
| 219 | + file.subtestNames); |
| 220 | +} |
| 221 | + |
| 222 | +void testParseErrorFlags() { |
| 223 | + // Not present. |
| 224 | + var file = parse(""); |
| 225 | + Expect.isFalse(file.hasSyntaxError); |
| 226 | + Expect.isFalse(file.hasCompileError); |
| 227 | + Expect.isFalse(file.hasRuntimeError); |
| 228 | + Expect.isFalse(file.hasStaticWarning); |
| 229 | + Expect.isFalse(file.hasCrash); |
| 230 | + |
| 231 | + file = parse("@syntax${'-'}error"); |
| 232 | + Expect.isTrue(file.hasSyntaxError); |
| 233 | + Expect.isTrue(file.hasCompileError); // Note: true. |
| 234 | + Expect.isFalse(file.hasRuntimeError); |
| 235 | + Expect.isFalse(file.hasStaticWarning); |
| 236 | + Expect.isFalse(file.hasCrash); |
| 237 | + |
| 238 | + file = parse("@compile${'-'}error"); |
| 239 | + Expect.isFalse(file.hasSyntaxError); |
| 240 | + Expect.isTrue(file.hasCompileError); |
| 241 | + Expect.isFalse(file.hasRuntimeError); |
| 242 | + Expect.isFalse(file.hasStaticWarning); |
| 243 | + Expect.isFalse(file.hasCrash); |
| 244 | + |
| 245 | + file = parse("@runtime${'-'}error"); |
| 246 | + Expect.isFalse(file.hasSyntaxError); |
| 247 | + Expect.isFalse(file.hasCompileError); |
| 248 | + Expect.isTrue(file.hasRuntimeError); |
| 249 | + Expect.isFalse(file.hasStaticWarning); |
| 250 | + Expect.isFalse(file.hasCrash); |
| 251 | + |
| 252 | + file = parse("@static${'-'}warning"); |
| 253 | + Expect.isFalse(file.hasSyntaxError); |
| 254 | + Expect.isFalse(file.hasCompileError); |
| 255 | + Expect.isFalse(file.hasRuntimeError); |
| 256 | + Expect.isTrue(file.hasStaticWarning); |
| 257 | + Expect.isFalse(file.hasCrash); |
| 258 | +} |
| 259 | + |
| 260 | +void testName() { |
| 261 | + // Immediately inside suite. |
| 262 | + var file = TestFile.parse(Path("suite").absolute, |
| 263 | + Path("suite/a_test.dart").absolute.toNativePath(), ""); |
| 264 | + Expect.equals("a_test", file.name); |
| 265 | + |
| 266 | + // Inside subdirectory. |
| 267 | + file = TestFile.parse(Path("suite").absolute, |
| 268 | + Path("suite/a/b/c_test.dart").absolute.toNativePath(), ""); |
| 269 | + Expect.equals("a/b/c_test", file.name); |
| 270 | + |
| 271 | + // Multitest. |
| 272 | + file = file.split(Path("suite/a/b/c_test_00.dart").absolute, "00"); |
| 273 | + Expect.equals("a/b/c_test/00", file.name); |
| 274 | +} |
| 275 | + |
| 276 | +void testMultitest() { |
| 277 | + var file = parse("", path: "origin.dart"); |
| 278 | + Expect.isFalse(file.hasSyntaxError); |
| 279 | + Expect.isFalse(file.hasCompileError); |
| 280 | + Expect.isFalse(file.hasRuntimeError); |
| 281 | + Expect.isFalse(file.hasStaticWarning); |
| 282 | + |
| 283 | + var a = file.split(Path("a.dart").absolute, "a", hasSyntaxError: true); |
| 284 | + Expect.isTrue(a.originPath.toNativePath().endsWith("origin.dart")); |
| 285 | + Expect.isTrue(a.path.toNativePath().endsWith("a.dart")); |
| 286 | + Expect.isTrue(a.hasSyntaxError); |
| 287 | + Expect.isFalse(a.hasCompileError); |
| 288 | + Expect.isFalse(a.hasRuntimeError); |
| 289 | + Expect.isFalse(a.hasStaticWarning); |
| 290 | + |
| 291 | + var b = file.split( |
| 292 | + Path("b.dart").absolute, |
| 293 | + "b", |
| 294 | + hasCompileError: true, |
| 295 | + ); |
| 296 | + Expect.isTrue(b.originPath.toNativePath().endsWith("origin.dart")); |
| 297 | + Expect.isTrue(b.path.toNativePath().endsWith("b.dart")); |
| 298 | + Expect.isFalse(b.hasSyntaxError); |
| 299 | + Expect.isTrue(b.hasCompileError); |
| 300 | + Expect.isFalse(b.hasRuntimeError); |
| 301 | + Expect.isFalse(b.hasStaticWarning); |
| 302 | + |
| 303 | + var c = file.split(Path("c.dart").absolute, "c", hasRuntimeError: true); |
| 304 | + Expect.isTrue(c.originPath.toNativePath().endsWith("origin.dart")); |
| 305 | + Expect.isTrue(c.path.toNativePath().endsWith("c.dart")); |
| 306 | + Expect.isFalse(c.hasSyntaxError); |
| 307 | + Expect.isFalse(c.hasCompileError); |
| 308 | + Expect.isTrue(c.hasRuntimeError); |
| 309 | + Expect.isFalse(c.hasStaticWarning); |
| 310 | + |
| 311 | + var d = file.split(Path("d.dart").absolute, "d", hasStaticWarning: true); |
| 312 | + Expect.isTrue(d.originPath.toNativePath().endsWith("origin.dart")); |
| 313 | + Expect.isTrue(d.path.toNativePath().endsWith("d.dart")); |
| 314 | + Expect.isFalse(d.hasSyntaxError); |
| 315 | + Expect.isFalse(d.hasCompileError); |
| 316 | + Expect.isFalse(d.hasRuntimeError); |
| 317 | + Expect.isTrue(d.hasStaticWarning); |
| 318 | +} |
| 319 | + |
| 320 | +TestFile parse(String source, {String path = "some_test.dart"}) { |
| 321 | + path = Path(path).absolute.toNativePath(); |
| 322 | + var suiteDirectory = Path(path).directoryPath; |
| 323 | + return TestFile.parse(suiteDirectory, path, source); |
| 324 | +} |
| 325 | + |
| 326 | +void expectParseThrows(String source) { |
| 327 | + Expect.throws(() => parse(source)); |
| 328 | +} |
0 commit comments