| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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.utils; | 5 library yaml.test.utils; |
| 6 | 6 |
| 7 import 'package:unittest/unittest.dart'; | 7 import 'package:unittest/unittest.dart'; |
| 8 import 'package:yaml/src/deep_equals.dart' as de; | 8 import 'package:yaml/src/equality.dart' as equality; |
| 9 import 'package:yaml/yaml.dart'; | 9 import 'package:yaml/yaml.dart'; |
| 10 | 10 |
| 11 /// A matcher that validates that a closure or Future throws a [YamlException]. | 11 /// A matcher that validates that a closure or Future throws a [YamlException]. |
| 12 final Matcher throwsYamlException = throwsA(new isInstanceOf<YamlException>()); | 12 final Matcher throwsYamlException = throwsA(new isInstanceOf<YamlException>()); |
| 13 | 13 |
| 14 /// Returns a matcher that asserts that the value equals [expected]. | 14 /// Returns a matcher that asserts that the value equals [expected]. |
| 15 /// | 15 /// |
| 16 /// This handles recursive loops and considers `NaN` to equal itself. | 16 /// This handles recursive loops and considers `NaN` to equal itself. |
| 17 Matcher deepEquals(expected) => | 17 Matcher deepEquals(expected) => predicate((actual) => |
| 18 predicate((actual) => de.deepEquals(actual, expected), "equals $expected"); | 18 equality.deepEquals(actual, expected), "equals $expected"); |
| 19 | 19 |
| 20 /// Constructs a new yaml.YamlMap, optionally from a normal Map. | 20 /// Constructs a new yaml.YamlMap, optionally from a normal Map. |
| 21 Map yamlMap([Map from]) => | 21 Map deepEqualsMap([Map from]) { |
| 22 from == null ? new YamlMap() : new YamlMap.from(from); | 22 var map = equality.deepEqualsMap(); |
| 23 if (from != null) map.addAll(from); |
| 24 return map; |
| 25 } |
| 23 | 26 |
| 24 /// Asserts that a string containing a single YAML document produces a given | 27 /// Asserts that a string containing a single YAML document produces a given |
| 25 /// value when loaded. | 28 /// value when loaded. |
| 26 void expectYamlLoads(expected, String source) { | 29 void expectYamlLoads(expected, String source) { |
| 27 var actual = loadYaml(cleanUpLiteral(source)); | 30 var actual = loadYaml(cleanUpLiteral(source)); |
| 28 expect(expected, deepEquals(actual)); | 31 expect(actual, deepEquals(expected)); |
| 29 } | 32 } |
| 30 | 33 |
| 31 /// Asserts that a string containing a stream of YAML documents produces a given | 34 /// Asserts that a string containing a stream of YAML documents produces a given |
| 32 /// list of values when loaded. | 35 /// list of values when loaded. |
| 33 void expectYamlStreamLoads(List expected, String source) { | 36 void expectYamlStreamLoads(List expected, String source) { |
| 34 var actual = loadYamlStream(cleanUpLiteral(source)); | 37 var actual = loadYamlStream(cleanUpLiteral(source)); |
| 35 expect(expected, deepEquals(actual)); | 38 expect(actual, deepEquals(expected)); |
| 36 } | 39 } |
| 37 | 40 |
| 38 /// Asserts that a string containing a single YAML document throws a | 41 /// Asserts that a string containing a single YAML document throws a |
| 39 /// [YamlException]. | 42 /// [YamlException]. |
| 40 void expectYamlFails(String source) { | 43 void expectYamlFails(String source) { |
| 41 expect(() => loadYaml(cleanUpLiteral(source)), throwsYamlException); | 44 expect(() => loadYaml(cleanUpLiteral(source)), throwsYamlException); |
| 42 } | 45 } |
| 43 | 46 |
| 44 /// Removes eight spaces of leading indentation from a multiline string. | 47 /// Removes eight spaces of leading indentation from a multiline string. |
| 45 /// | 48 /// |
| (...skipping 28 matching lines...) Expand all Loading... |
| 74 String indentLiteral(String text) { | 77 String indentLiteral(String text) { |
| 75 var lines = text.split('\n'); | 78 var lines = text.split('\n'); |
| 76 if (lines.length <= 1) return text; | 79 if (lines.length <= 1) return text; |
| 77 | 80 |
| 78 for (var i = 0; i < lines.length; i++) { | 81 for (var i = 0; i < lines.length; i++) { |
| 79 lines[i] = " ${lines[i]}"; | 82 lines[i] = " ${lines[i]}"; |
| 80 } | 83 } |
| 81 | 84 |
| 82 return lines.join("\n"); | 85 return lines.join("\n"); |
| 83 } | 86 } |
| OLD | NEW |