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 import 'package:unittest/unittest.dart'; |
| 6 import 'package:unittest/src/backend/platform_selector/ast.dart'; |
| 7 import 'package:unittest/src/backend/platform_selector/parser.dart'; |
| 8 |
| 9 /// A matcher that asserts that a value is a [ConditionalNode]. |
| 10 Matcher _isConditionalNode = |
| 11 new isInstanceOf<ConditionalNode>("ConditionalNode"); |
| 12 |
| 13 /// A matcher that asserts that a value is an [OrNode]. |
| 14 Matcher _isOrNode = new isInstanceOf<OrNode>("OrNode"); |
| 15 |
| 16 /// A matcher that asserts that a value is an [AndNode]. |
| 17 Matcher _isAndNode = new isInstanceOf<AndNode>("AndNode"); |
| 18 |
| 19 /// A matcher that asserts that a value is a [NotNode]. |
| 20 Matcher _isNotNode = new isInstanceOf<NotNode>("NotNode"); |
| 21 |
| 22 void main() { |
| 23 group("parses a conditional expression", () { |
| 24 test("with identifiers", () { |
| 25 var node = _parse(" a ? b : c "); |
| 26 expect(node.toString(), equals("a ? b : c")); |
| 27 |
| 28 expect(node.span.text, equals("a ? b : c")); |
| 29 expect(node.span.start.offset, equals(2)); |
| 30 expect(node.span.end.offset, equals(11)); |
| 31 }); |
| 32 |
| 33 test("with nested ors", () { |
| 34 // Should parse as "(a || b) ? (c || d) : (e || f)". |
| 35 // Should not parse as "a || (b ? (c || d) : (e || f))". |
| 36 // Should not parse as "((a || b) ? (c || d) : e) || f". |
| 37 // Should not parse as "a || (b ? (c || d) : e) || f". |
| 38 _expectToString("a || b ? c || d : e || f", |
| 39 "a || b ? c || d : e || f"); |
| 40 }); |
| 41 |
| 42 test("with a conditional expression as branch 1", () { |
| 43 // Should parse as "a ? (b ? c : d) : e". |
| 44 var node = _parse("a ? b ? c : d : e"); |
| 45 expect(node, _isConditionalNode); |
| 46 expect(node.condition, _isVar("a")); |
| 47 expect(node.whenFalse, _isVar("e")); |
| 48 |
| 49 expect(node.whenTrue, _isConditionalNode); |
| 50 expect(node.whenTrue.condition, _isVar("b")); |
| 51 expect(node.whenTrue.whenTrue, _isVar("c")); |
| 52 expect(node.whenTrue.whenFalse, _isVar("d")); |
| 53 }); |
| 54 |
| 55 test("with a conditional expression as branch 2", () { |
| 56 // Should parse as "a ? b : (c ? d : e)". |
| 57 // Should not parse as "(a ? b : c) ? d : e". |
| 58 var node = _parse("a ? b : c ? d : e"); |
| 59 expect(node, _isConditionalNode); |
| 60 expect(node.condition, _isVar("a")); |
| 61 expect(node.whenTrue, _isVar("b")); |
| 62 |
| 63 expect(node.whenFalse, _isConditionalNode); |
| 64 expect(node.whenFalse.condition, _isVar("c")); |
| 65 expect(node.whenFalse.whenTrue, _isVar("d")); |
| 66 expect(node.whenFalse.whenFalse, _isVar("e")); |
| 67 }); |
| 68 |
| 69 group("which must have", () { |
| 70 test("an expression after the ?", () { |
| 71 expect(() => _parse("a ?"), throwsFormatException); |
| 72 expect(() => _parse("a ? && b"), throwsFormatException); |
| 73 }); |
| 74 |
| 75 test("a :", () { |
| 76 expect(() => _parse("a ? b"), throwsFormatException); |
| 77 expect(() => _parse("a ? b && c"), throwsFormatException); |
| 78 }); |
| 79 |
| 80 test("an expression after the :", () { |
| 81 expect(() => _parse("a ? b :"), throwsFormatException); |
| 82 expect(() => _parse("a ? b : && c"), throwsFormatException); |
| 83 }); |
| 84 }); |
| 85 }); |
| 86 |
| 87 group("parses an or expression", () { |
| 88 test("with identifiers", () { |
| 89 var node = _parse(" a || b "); |
| 90 expect(node, _isOrNode); |
| 91 expect(node.left, _isVar("a")); |
| 92 expect(node.right, _isVar("b")); |
| 93 |
| 94 expect(node.span.text, equals("a || b")); |
| 95 expect(node.span.start.offset, equals(2)); |
| 96 expect(node.span.end.offset, equals(8)); |
| 97 }); |
| 98 |
| 99 test("with nested ands", () { |
| 100 // Should parse as "(a && b) || (c && d)". |
| 101 // Should not parse as "a && (b || c) && d". |
| 102 var node = _parse("a && b || c && d"); |
| 103 expect(node, _isOrNode); |
| 104 |
| 105 expect(node.left, _isAndNode); |
| 106 expect(node.left.left, _isVar("a")); |
| 107 expect(node.left.right, _isVar("b")); |
| 108 |
| 109 expect(node.right, _isAndNode); |
| 110 expect(node.right.left, _isVar("c")); |
| 111 expect(node.right.right, _isVar("d")); |
| 112 }); |
| 113 |
| 114 test("with trailing ors", () { |
| 115 // Should parse as "a || (b || (c || d))", although it doesn't affect the |
| 116 // semantics. |
| 117 var node = _parse("a || b || c || d"); |
| 118 |
| 119 for (var variable in ["a", "b", "c"]) { |
| 120 expect(node, _isOrNode); |
| 121 expect(node.left, _isVar(variable)); |
| 122 node = node.right; |
| 123 } |
| 124 expect(node, _isVar("d")); |
| 125 }); |
| 126 |
| 127 test("which must have an expression after the ||", () { |
| 128 expect(() => _parse("a ||"), throwsFormatException); |
| 129 expect(() => _parse("a || && b"), throwsFormatException); |
| 130 }); |
| 131 }); |
| 132 |
| 133 group("parses an and expression", () { |
| 134 test("with identifiers", () { |
| 135 var node = _parse(" a && b "); |
| 136 expect(node, _isAndNode); |
| 137 expect(node.left, _isVar("a")); |
| 138 expect(node.right, _isVar("b")); |
| 139 |
| 140 expect(node.span.text, equals("a && b")); |
| 141 expect(node.span.start.offset, equals(2)); |
| 142 expect(node.span.end.offset, equals(8)); |
| 143 }); |
| 144 |
| 145 test("with nested nots", () { |
| 146 // Should parse as "(!a) && (!b)", obviously. |
| 147 // Should not parse as "!(a && (!b))". |
| 148 var node = _parse("!a && !b"); |
| 149 expect(node, _isAndNode); |
| 150 |
| 151 expect(node.left, _isNotNode); |
| 152 expect(node.left.child, _isVar("a")); |
| 153 |
| 154 expect(node.right, _isNotNode); |
| 155 expect(node.right.child, _isVar("b")); |
| 156 }); |
| 157 |
| 158 test("with trailing ands", () { |
| 159 // Should parse as "a && (b && (c && d))", although it doesn't affect the |
| 160 // semantics since . |
| 161 var node = _parse("a && b && c && d"); |
| 162 |
| 163 for (var variable in ["a", "b", "c"]) { |
| 164 expect(node, _isAndNode); |
| 165 expect(node.left, _isVar(variable)); |
| 166 node = node.right; |
| 167 } |
| 168 expect(node, _isVar("d")); |
| 169 }); |
| 170 |
| 171 test("which must have an expression after the &&", () { |
| 172 expect(() => _parse("a &&"), throwsFormatException); |
| 173 expect(() => _parse("a && && b"), throwsFormatException); |
| 174 }); |
| 175 }); |
| 176 |
| 177 group("parses a not expression", () { |
| 178 test("with an identifier", () { |
| 179 var node = _parse(" ! a "); |
| 180 expect(node, _isNotNode); |
| 181 expect(node.child, _isVar("a")); |
| 182 |
| 183 expect(node.span.text, equals("! a")); |
| 184 expect(node.span.start.offset, equals(2)); |
| 185 expect(node.span.end.offset, equals(5)); |
| 186 }); |
| 187 |
| 188 test("with a parenthesized expression", () { |
| 189 var node = _parse("!(a || b)"); |
| 190 expect(node, _isNotNode); |
| 191 |
| 192 expect(node.child, _isOrNode); |
| 193 expect(node.child.left, _isVar("a")); |
| 194 expect(node.child.right, _isVar("b")); |
| 195 }); |
| 196 |
| 197 test("with a nested not", () { |
| 198 var node = _parse("!!a"); |
| 199 expect(node, _isNotNode); |
| 200 expect(node.child, _isNotNode); |
| 201 expect(node.child.child, _isVar("a")); |
| 202 }); |
| 203 |
| 204 test("which must have an expression after the !", () { |
| 205 expect(() => _parse("!"), throwsFormatException); |
| 206 expect(() => _parse("! && a"), throwsFormatException); |
| 207 }); |
| 208 }); |
| 209 |
| 210 group("parses a parenthesized expression", () { |
| 211 test("with an identifier", () { |
| 212 var node = _parse("(a)"); |
| 213 expect(node, _isVar("a")); |
| 214 }); |
| 215 |
| 216 test("controls precedence", () { |
| 217 // Without parentheses, this would parse as "(a || b) ? c : d". |
| 218 var node = _parse("a || (b ? c : d)"); |
| 219 |
| 220 expect(node, _isOrNode); |
| 221 expect(node.left, _isVar("a")); |
| 222 |
| 223 expect(node.right, _isConditionalNode); |
| 224 expect(node.right.condition, _isVar("b")); |
| 225 expect(node.right.whenTrue, _isVar("c")); |
| 226 expect(node.right.whenFalse, _isVar("d")); |
| 227 }); |
| 228 |
| 229 group("which must have", () { |
| 230 test("an expression within the ()", () { |
| 231 expect(() => _parse("()"), throwsFormatException); |
| 232 expect(() => _parse("( && a )"), throwsFormatException); |
| 233 }); |
| 234 |
| 235 test("a matching )", () { |
| 236 expect(() => _parse("( a"), throwsFormatException); |
| 237 }); |
| 238 }); |
| 239 }); |
| 240 |
| 241 group("disallows", () { |
| 242 test("an empty selector", () { |
| 243 expect(() => _parse(""), throwsFormatException); |
| 244 }); |
| 245 |
| 246 test("too many expressions", () { |
| 247 expect(() => _parse("a b"), throwsFormatException); |
| 248 }); |
| 249 }); |
| 250 } |
| 251 |
| 252 /// Parses [selector] and returns its root node. |
| 253 Node _parse(String selector) => new Parser(selector).parse(); |
| 254 |
| 255 /// A matcher that asserts that a value is a [VariableNode] with the given |
| 256 /// [name]. |
| 257 Matcher _isVar(String name) => predicate( |
| 258 (value) => value is VariableNode && value.name == name, |
| 259 'is a variable named "$name"'); |
| 260 |
| 261 void _expectToString(String selector, [String result]) { |
| 262 if (result == null) result = selector; |
| 263 expect(_toString(selector), equals(result), |
| 264 reason: 'Expected toString of "$selector" to be "$result".'); |
| 265 } |
| 266 |
| 267 String _toString(String selector) => new Parser(selector).parse().toString(); |
OLD | NEW |