| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2015, 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 @TestOn("vm") | |
| 6 | |
| 7 import 'dart:io'; | |
| 8 | |
| 9 import 'package:test/test.dart'; | |
| 10 import 'package:test/src/backend/operating_system.dart'; | |
| 11 import 'package:test/src/backend/platform_selector.dart'; | |
| 12 import 'package:test/src/backend/test_platform.dart'; | |
| 13 | |
| 14 void main() { | |
| 15 test("new PlatformSelector.parse() disallows invalid variables", () { | |
| 16 expect(() => new PlatformSelector.parse("undefined"), | |
| 17 throwsFormatException); | |
| 18 }); | |
| 19 | |
| 20 group("operator:", () { | |
| 21 test("conditional", () { | |
| 22 _expectEval("vm ? vm : browser", true); | |
| 23 _expectEval("vm ? browser : vm", false); | |
| 24 _expectEval("browser ? vm : browser", false); | |
| 25 _expectEval("browser ? browser : vm", true); | |
| 26 }); | |
| 27 | |
| 28 test("or", () { | |
| 29 _expectEval("vm || vm", true); | |
| 30 _expectEval("vm || browser", true); | |
| 31 _expectEval("browser || vm", true); | |
| 32 _expectEval("browser || browser", false); | |
| 33 }); | |
| 34 | |
| 35 test("and", () { | |
| 36 _expectEval("vm && vm", true); | |
| 37 _expectEval("vm && browser", false); | |
| 38 _expectEval("browser && vm", false); | |
| 39 _expectEval("browser && browser", false); | |
| 40 }); | |
| 41 | |
| 42 test("not", () { | |
| 43 _expectEval("!vm", false); | |
| 44 _expectEval("!browser", true); | |
| 45 }); | |
| 46 }); | |
| 47 | |
| 48 group("baseline variable:", () { | |
| 49 test("vm", () { | |
| 50 _expectEval("vm", true, platform: TestPlatform.vm); | |
| 51 _expectEval("vm", false, platform: TestPlatform.chrome); | |
| 52 }); | |
| 53 | |
| 54 test("chrome", () { | |
| 55 _expectEval("chrome", true, platform: TestPlatform.chrome); | |
| 56 _expectEval("chrome", false, platform: TestPlatform.vm); | |
| 57 }); | |
| 58 | |
| 59 test("windows", () { | |
| 60 _expectEval("windows", true, os: OperatingSystem.windows); | |
| 61 _expectEval("windows", false, os: OperatingSystem.linux); | |
| 62 _expectEval("windows", false, os: OperatingSystem.none); | |
| 63 }); | |
| 64 | |
| 65 test("mac-os", () { | |
| 66 _expectEval("mac-os", true, os: OperatingSystem.macOS); | |
| 67 _expectEval("mac-os", false, os: OperatingSystem.linux); | |
| 68 _expectEval("mac-os", false, os: OperatingSystem.none); | |
| 69 }); | |
| 70 | |
| 71 test("linux", () { | |
| 72 _expectEval("linux", true, os: OperatingSystem.linux); | |
| 73 _expectEval("linux", false, os: OperatingSystem.android); | |
| 74 _expectEval("linux", false, os: OperatingSystem.none); | |
| 75 }); | |
| 76 | |
| 77 test("android", () { | |
| 78 _expectEval("android", true, os: OperatingSystem.android); | |
| 79 _expectEval("android", false, os: OperatingSystem.linux); | |
| 80 _expectEval("android", false, os: OperatingSystem.none); | |
| 81 }); | |
| 82 }); | |
| 83 | |
| 84 group("derived variable:", () { | |
| 85 test("dart-vm", () { | |
| 86 _expectEval("dart-vm", true, platform: TestPlatform.vm); | |
| 87 _expectEval("dart-vm", false, platform: TestPlatform.chrome); | |
| 88 }); | |
| 89 | |
| 90 test("browser", () { | |
| 91 _expectEval("browser", true, platform: TestPlatform.chrome); | |
| 92 _expectEval("browser", false, platform: TestPlatform.vm); | |
| 93 }); | |
| 94 | |
| 95 test("js", () { | |
| 96 _expectEval("js", true, platform: TestPlatform.chrome); | |
| 97 _expectEval("js", false, platform: TestPlatform.vm); | |
| 98 }); | |
| 99 | |
| 100 test("blink", () { | |
| 101 _expectEval("blink", true, platform: TestPlatform.chrome); | |
| 102 _expectEval("blink", false, platform: TestPlatform.vm); | |
| 103 }); | |
| 104 | |
| 105 test("posix", () { | |
| 106 _expectEval("posix", false, os: OperatingSystem.windows); | |
| 107 _expectEval("posix", true, os: OperatingSystem.macOS); | |
| 108 _expectEval("posix", true, os: OperatingSystem.linux); | |
| 109 _expectEval("posix", true, os: OperatingSystem.android); | |
| 110 _expectEval("posix", false, os: OperatingSystem.none); | |
| 111 }); | |
| 112 }); | |
| 113 } | |
| 114 | |
| 115 /// Asserts that [expression] evaluates to [result] on [platform] and [os]. | |
| 116 /// | |
| 117 /// [platform] defaults to [TestPlatform.vm]; [os] defaults to the current | |
| 118 /// operating system. | |
| 119 void _expectEval(String expression, bool result, {TestPlatform platform, | |
| 120 OperatingSystem os}) { | |
| 121 | |
| 122 var reason = 'Expected "$expression" to evaluate to $result'; | |
| 123 if (platform != null && os != null) { | |
| 124 reason += ' on $platform and $os.'; | |
| 125 } else if (platform != null || os != null) { | |
| 126 reason += ' on ${platform == null ? os : platform}'; | |
| 127 } | |
| 128 | |
| 129 expect(_eval(expression, platform: platform, os: os), equals(result), | |
| 130 reason: '$reason.'); | |
| 131 } | |
| 132 | |
| 133 /// Returns the result of evaluating [expression] on [platform] and [os]. | |
| 134 /// | |
| 135 /// [platform] defaults to [TestPlatform.vm]; [os] defaults to the current | |
| 136 /// operating system. | |
| 137 bool _eval(String expression, {TestPlatform platform, OperatingSystem os}) { | |
| 138 if (platform == null) platform = TestPlatform.vm; | |
| 139 if (os == null) os = OperatingSystem.findByIoName(Platform.operatingSystem); | |
| 140 var selector = new PlatformSelector.parse(expression); | |
| 141 return selector.evaluate(platform, os: os); | |
| 142 } | |
| OLD | NEW |