OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library yaml.test; | 5 library yaml.test; |
6 | 6 |
7 // TODO(rnystrom): rewrite tests so that they don't need "Expect". | 7 // TODO(rnystrom): rewrite tests so that they don't need "Expect". |
8 import 'package:unittest/unittest.dart'; | 8 import 'package:unittest/unittest.dart'; |
9 import 'package:yaml/yaml.dart'; | 9 import 'package:yaml/yaml.dart'; |
10 | 10 |
11 import 'utils.dart'; | 11 import 'utils.dart'; |
12 | 12 |
13 main() { | 13 main() { |
14 var infinity = double.parse("Infinity"); | 14 var infinity = double.parse("Infinity"); |
15 var nan = double.parse("NaN"); | 15 var nan = double.parse("NaN"); |
16 | 16 |
17 group('YamlMap', () { | |
18 group('accepts as a key', () { | |
19 _expectKeyWorks(keyFn()) { | |
20 var map = yamlMap(); | |
21 map[keyFn()] = 5; | |
22 expect(map.containsKey(keyFn()), isTrue); | |
23 expect(map[keyFn()], 5); | |
24 } | |
25 | |
26 test('null', () => _expectKeyWorks(() => null)); | |
27 test('true', () => _expectKeyWorks(() => true)); | |
28 test('false', () => _expectKeyWorks(() => false)); | |
29 test('a list', () => _expectKeyWorks(() => [1, 2, 3])); | |
30 test('a map', () => _expectKeyWorks(() => {'foo': 'bar'})); | |
31 test('a YAML map', () => _expectKeyWorks(() => yamlMap({'foo': 'bar'}))); | |
32 }); | |
33 | |
34 test('works as a hash key', () { | |
35 var normalMap = new Map(); | |
36 normalMap[yamlMap({'foo': 'bar'})] = 'baz'; | |
37 expect(normalMap.containsKey(yamlMap({'foo': 'bar'})), isTrue); | |
38 expect(normalMap[yamlMap({'foo': 'bar'})], 'baz'); | |
39 }); | |
40 | |
41 test('treats YamlMap keys the same as normal maps', () { | |
42 var map = yamlMap(); | |
43 map[{'a': 'b'}] = 5; | |
44 expect(map[yamlMap({'a': 'b'})], 5); | |
45 }); | |
46 }); | |
47 | |
48 group('has a friendly error message for', () { | 17 group('has a friendly error message for', () { |
49 var tabError = predicate((e) => | 18 var tabError = predicate((e) => |
50 e.toString().contains('tab characters are not allowed as indentation')); | 19 e.toString().contains('tab characters are not allowed as indentation')); |
51 | 20 |
52 test('using a tab as indentation', () { | 21 test('using a tab as indentation', () { |
53 expect(() => loadYaml('foo:\n\tbar'), | 22 expect(() => loadYaml('foo:\n\tbar'), |
54 throwsA(tabError)); | 23 throwsA(tabError)); |
55 }); | 24 }); |
56 | 25 |
57 test('using a tab not as indentation', () { | 26 test('using a tab not as indentation', () { |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 // hr: | 183 // hr: |
215 // - Mark McGwire | 184 // - Mark McGwire |
216 // # Following node labeled SS | 185 // # Following node labeled SS |
217 // - &SS Sammy Sosa | 186 // - &SS Sammy Sosa |
218 // rbi: | 187 // rbi: |
219 // - *SS # Subsequent occurrence | 188 // - *SS # Subsequent occurrence |
220 // - Ken Griffey"""); | 189 // - Ken Griffey"""); |
221 // }); | 190 // }); |
222 | 191 |
223 test('[Example 2.11]', () { | 192 test('[Example 2.11]', () { |
224 var doc = yamlMap(); | 193 var doc = deepEqualsMap(); |
225 doc[["Detroit Tigers", "Chicago cubs"]] = ["2001-07-23"]; | 194 doc[["Detroit Tigers", "Chicago cubs"]] = ["2001-07-23"]; |
226 doc[["New York Yankees", "Atlanta Braves"]] = | 195 doc[["New York Yankees", "Atlanta Braves"]] = |
227 ["2001-07-02", "2001-08-12", "2001-08-14"]; | 196 ["2001-07-02", "2001-08-12", "2001-08-14"]; |
228 expectYamlLoads(doc, | 197 expectYamlLoads(doc, |
229 """ | 198 """ |
230 ? - Detroit Tigers | 199 ? - Detroit Tigers |
231 - Chicago cubs | 200 - Chicago cubs |
232 : | 201 : |
233 - 2001-07-23 | 202 - 2001-07-23 |
234 | 203 |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 }, | 344 }, |
376 """ | 345 """ |
377 canonical: 1.23015e+3 | 346 canonical: 1.23015e+3 |
378 exponential: 12.3015e+02 | 347 exponential: 12.3015e+02 |
379 fixed: 1230.15 | 348 fixed: 1230.15 |
380 negative infinity: -.inf | 349 negative infinity: -.inf |
381 not a number: .NaN"""); | 350 not a number: .NaN"""); |
382 }); | 351 }); |
383 | 352 |
384 test('[Example 2.21]', () { | 353 test('[Example 2.21]', () { |
385 var doc = yamlMap({ | 354 var doc = deepEqualsMap({ |
386 "booleans": [true, false], | 355 "booleans": [true, false], |
387 "string": "012345" | 356 "string": "012345" |
388 }); | 357 }); |
389 doc[null] = null; | 358 doc[null] = null; |
390 expectYamlLoads(doc, | 359 expectYamlLoads(doc, |
391 """ | 360 """ |
392 null: | 361 null: |
393 booleans: [ true, false ] | 362 booleans: [ true, false ] |
394 string: '012345'"""); | 363 string: '012345'"""); |
395 }); | 364 }); |
(...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
893 | 862 |
894 group('6.7: Separation Lines', () { | 863 group('6.7: Separation Lines', () { |
895 test('may not be used within implicit keys', () { | 864 test('may not be used within implicit keys', () { |
896 expectYamlFails( | 865 expectYamlFails( |
897 """ | 866 """ |
898 [1, | 867 [1, |
899 2]: 3"""); | 868 2]: 3"""); |
900 }); | 869 }); |
901 | 870 |
902 test('[Example 6.12]', () { | 871 test('[Example 6.12]', () { |
903 var doc = yamlMap(); | 872 var doc = deepEqualsMap(); |
904 doc[{'first': 'Sammy', 'last': 'Sosa'}] = { | 873 doc[{'first': 'Sammy', 'last': 'Sosa'}] = { |
905 'hr': 65, | 874 'hr': 65, |
906 'avg': 0.278 | 875 'avg': 0.278 |
907 }; | 876 }; |
908 expectYamlLoads(doc, | 877 expectYamlLoads(doc, |
909 """ | 878 """ |
910 { first: Sammy, last: Sosa }: | 879 { first: Sammy, last: Sosa }: |
911 # Statistics: | 880 # Statistics: |
912 hr: # Home runs | 881 hr: # Home runs |
913 65 | 882 65 |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1085 // "": "bar" | 1054 // "": "bar" |
1086 // }, | 1055 // }, |
1087 // """ | 1056 // """ |
1088 // { | 1057 // { |
1089 // foo : !!str, | 1058 // foo : !!str, |
1090 // !!str : bar, | 1059 // !!str : bar, |
1091 // }"""); | 1060 // }"""); |
1092 // }); | 1061 // }); |
1093 | 1062 |
1094 test('[Example 7.3]', () { | 1063 test('[Example 7.3]', () { |
1095 var doc = yamlMap({"foo": null}); | 1064 var doc = deepEqualsMap({"foo": null}); |
1096 doc[null] = "bar"; | 1065 doc[null] = "bar"; |
1097 expectYamlLoads(doc, | 1066 expectYamlLoads(doc, |
1098 """ | 1067 """ |
1099 { | 1068 { |
1100 ? foo :, | 1069 ? foo :, |
1101 : bar, | 1070 : bar, |
1102 }"""); | 1071 }"""); |
1103 }); | 1072 }); |
1104 }); | 1073 }); |
1105 | 1074 |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1232 expectYamlLoads([ | 1201 expectYamlLoads([ |
1233 {"one": "two", "three": "four"}, | 1202 {"one": "two", "three": "four"}, |
1234 {"five": "six", "seven": "eight"}, | 1203 {"five": "six", "seven": "eight"}, |
1235 ], | 1204 ], |
1236 """ | 1205 """ |
1237 - { one : two , three: four , } | 1206 - { one : two , three: four , } |
1238 - {five: six,seven : eight}"""); | 1207 - {five: six,seven : eight}"""); |
1239 }); | 1208 }); |
1240 | 1209 |
1241 test('[Example 7.16]', () { | 1210 test('[Example 7.16]', () { |
1242 var doc = yamlMap({ | 1211 var doc = deepEqualsMap({ |
1243 "explicit": "entry", | 1212 "explicit": "entry", |
1244 "implicit": "entry" | 1213 "implicit": "entry" |
1245 }); | 1214 }); |
1246 doc[null] = null; | 1215 doc[null] = null; |
1247 expectYamlLoads(doc, | 1216 expectYamlLoads(doc, |
1248 """ | 1217 """ |
1249 { | 1218 { |
1250 ? explicit: entry, | 1219 ? explicit: entry, |
1251 implicit: entry, | 1220 implicit: entry, |
1252 ? | 1221 ? |
1253 }"""); | 1222 }"""); |
1254 }); | 1223 }); |
1255 | 1224 |
1256 test('[Example 7.17]', () { | 1225 test('[Example 7.17]', () { |
1257 var doc = yamlMap({ | 1226 var doc = deepEqualsMap({ |
1258 "unquoted": "separate", | 1227 "unquoted": "separate", |
1259 "http://foo.com": null, | 1228 "http://foo.com": null, |
1260 "omitted value": null | 1229 "omitted value": null |
1261 }); | 1230 }); |
1262 doc[null] = "omitted key"; | 1231 doc[null] = "omitted key"; |
1263 expectYamlLoads(doc, | 1232 expectYamlLoads(doc, |
1264 ''' | 1233 ''' |
1265 { | 1234 { |
1266 unquoted : "separate", | 1235 unquoted : "separate", |
1267 http://foo.com, | 1236 http://foo.com, |
(...skipping 27 matching lines...) Expand all Loading... |
1295 test('[Example 7.20]', () { | 1264 test('[Example 7.20]', () { |
1296 expectYamlLoads([{"foo bar": "baz"}], | 1265 expectYamlLoads([{"foo bar": "baz"}], |
1297 """ | 1266 """ |
1298 [ | 1267 [ |
1299 ? foo | 1268 ? foo |
1300 bar : baz | 1269 bar : baz |
1301 ]"""); | 1270 ]"""); |
1302 }); | 1271 }); |
1303 | 1272 |
1304 test('[Example 7.21]', () { | 1273 test('[Example 7.21]', () { |
1305 var el1 = yamlMap(); | 1274 var el1 = deepEqualsMap(); |
1306 el1[null] = "empty key entry"; | 1275 el1[null] = "empty key entry"; |
1307 | 1276 |
1308 var el2 = yamlMap(); | 1277 var el2 = deepEqualsMap(); |
1309 el2[{"JSON": "like"}] = "adjacent"; | 1278 el2[{"JSON": "like"}] = "adjacent"; |
1310 | 1279 |
1311 expectYamlLoads([[{"YAML": "separate"}], [el1], [el2]], | 1280 expectYamlLoads([[{"YAML": "separate"}], [el1], [el2]], |
1312 """ | 1281 """ |
1313 - [ YAML : separate ] | 1282 - [ YAML : separate ] |
1314 - [ : empty key entry ] | 1283 - [ : empty key entry ] |
1315 - [ {JSON: like}:adjacent ]"""); | 1284 - [ {JSON: like}:adjacent ]"""); |
1316 }); | 1285 }); |
1317 | 1286 |
1318 test('[Example 7.22]', () { | 1287 test('[Example 7.22]', () { |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1570 }, | 1539 }, |
1571 """ | 1540 """ |
1572 ? explicit key # Empty value | 1541 ? explicit key # Empty value |
1573 ? | | 1542 ? | |
1574 block key | 1543 block key |
1575 : - one # Explicit compact | 1544 : - one # Explicit compact |
1576 - two # block value"""); | 1545 - two # block value"""); |
1577 }); | 1546 }); |
1578 | 1547 |
1579 test('[Example 8.18]', () { | 1548 test('[Example 8.18]', () { |
1580 var doc = yamlMap({ | 1549 var doc = deepEqualsMap({ |
1581 'plain key': 'in-line value', | 1550 'plain key': 'in-line value', |
1582 "quoted key": ["entry"] | 1551 "quoted key": ["entry"] |
1583 }); | 1552 }); |
1584 doc[null] = null; | 1553 doc[null] = null; |
1585 expectYamlLoads(doc, | 1554 expectYamlLoads(doc, |
1586 ''' | 1555 ''' |
1587 plain key: in-line value | 1556 plain key: in-line value |
1588 : # Both empty | 1557 : # Both empty |
1589 "quoted key": | 1558 "quoted key": |
1590 - entry'''); | 1559 - entry'''); |
1591 }); | 1560 }); |
1592 | 1561 |
1593 test('[Example 8.19]', () { | 1562 test('[Example 8.19]', () { |
1594 var el = yamlMap(); | 1563 var el = deepEqualsMap(); |
1595 el[{'earth': 'blue'}] = {'moon': 'white'}; | 1564 el[{'earth': 'blue'}] = {'moon': 'white'}; |
1596 expectYamlLoads([{'sun': 'yellow'}, el], | 1565 expectYamlLoads([{'sun': 'yellow'}, el], |
1597 """ | 1566 """ |
1598 - sun: yellow | 1567 - sun: yellow |
1599 - ? earth: blue | 1568 - ? earth: blue |
1600 : moon: white"""); | 1569 : moon: white"""); |
1601 }); | 1570 }); |
1602 | 1571 |
1603 // test('[Example 8.20]', () { | 1572 // test('[Example 8.20]', () { |
1604 // expectYamlLoads(["flow in block", "Block scalar\n", {"foo": "bar"}], | 1573 // expectYamlLoads(["flow in block", "Block scalar\n", {"foo": "bar"}], |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1756 // ''' | 1725 // ''' |
1757 // Block style: !!str |- | 1726 // Block style: !!str |- |
1758 // String: just a theory. | 1727 // String: just a theory. |
1759 | 1728 |
1760 // Flow style: !!str "String: just a theory."'''); | 1729 // Flow style: !!str "String: just a theory."'''); |
1761 // }); | 1730 // }); |
1762 }); | 1731 }); |
1763 | 1732 |
1764 group('10.2: JSON Schema', () { | 1733 group('10.2: JSON Schema', () { |
1765 // test('[Example 10.4]', () { | 1734 // test('[Example 10.4]', () { |
1766 // var doc = yamlMap({"key with null value": null}); | 1735 // var doc = deepEqualsMap({"key with null value": null}); |
1767 // doc[null] = "value for null key"; | 1736 // doc[null] = "value for null key"; |
1768 // expectYamlStreamLoads(doc, | 1737 // expectYamlStreamLoads(doc, |
1769 // """ | 1738 // """ |
1770 // !!null null: value for null key | 1739 // !!null null: value for null key |
1771 // key with null value: !!null null"""); | 1740 // key with null value: !!null null"""); |
1772 // }); | 1741 // }); |
1773 | 1742 |
1774 // test('[Example 10.5]', () { | 1743 // test('[Example 10.5]', () { |
1775 // expectYamlStreamLoads({ | 1744 // expectYamlStreamLoads({ |
1776 // "YAML is a superset of JSON": true, | 1745 // "YAML is a superset of JSON": true, |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1843 A null: null | 1812 A null: null |
1844 Also a null: # Empty | 1813 Also a null: # Empty |
1845 Not a null: "" | 1814 Not a null: "" |
1846 Booleans: [ true, True, false, FALSE ] | 1815 Booleans: [ true, True, false, FALSE ] |
1847 Integers: [ 0, 0o7, 0x3A, -19 ] | 1816 Integers: [ 0, 0o7, 0x3A, -19 ] |
1848 Floats: [ 0., -0.0, .5, +12e03, -2E+05 ] | 1817 Floats: [ 0., -0.0, .5, +12e03, -2E+05 ] |
1849 Also floats: [ .inf, -.Inf, +.INF, .NAN ]'''); | 1818 Also floats: [ .inf, -.Inf, +.INF, .NAN ]'''); |
1850 }); | 1819 }); |
1851 }); | 1820 }); |
1852 } | 1821 } |
OLD | NEW |