OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library yaml.test.utils; |
| 6 |
| 7 import 'package:test/test.dart'; |
| 8 import 'package:yaml/src/equality.dart' as equality; |
| 9 import 'package:yaml/yaml.dart'; |
| 10 |
| 11 /// A matcher that validates that a closure or Future throws a [YamlException]. |
| 12 final Matcher throwsYamlException = throwsA(new isInstanceOf<YamlException>()); |
| 13 |
| 14 /// Returns a matcher that asserts that the value equals [expected]. |
| 15 /// |
| 16 /// This handles recursive loops and considers `NaN` to equal itself. |
| 17 Matcher deepEquals(expected) => predicate((actual) => |
| 18 equality.deepEquals(actual, expected), "equals $expected"); |
| 19 |
| 20 /// Constructs a new yaml.YamlMap, optionally from a normal Map. |
| 21 Map deepEqualsMap([Map from]) { |
| 22 var map = equality.deepEqualsMap(); |
| 23 if (from != null) map.addAll(from); |
| 24 return map; |
| 25 } |
| 26 |
| 27 /// Asserts that a string containing a single YAML document produces a given |
| 28 /// value when loaded. |
| 29 void expectYamlLoads(expected, String source) { |
| 30 var actual = loadYaml(cleanUpLiteral(source)); |
| 31 expect(actual, deepEquals(expected)); |
| 32 } |
| 33 |
| 34 /// Asserts that a string containing a stream of YAML documents produces a given |
| 35 /// list of values when loaded. |
| 36 void expectYamlStreamLoads(List expected, String source) { |
| 37 var actual = loadYamlStream(cleanUpLiteral(source)); |
| 38 expect(actual, deepEquals(expected)); |
| 39 } |
| 40 |
| 41 /// Asserts that a string containing a single YAML document throws a |
| 42 /// [YamlException]. |
| 43 void expectYamlFails(String source) { |
| 44 expect(() => loadYaml(cleanUpLiteral(source)), throwsYamlException); |
| 45 } |
| 46 |
| 47 /// Removes eight spaces of leading indentation from a multiline string. |
| 48 /// |
| 49 /// Note that this is very sensitive to how the literals are styled. They should |
| 50 /// be: |
| 51 /// ''' |
| 52 /// Text starts on own line. Lines up with subsequent lines. |
| 53 /// Lines are indented exactly 8 characters from the left margin. |
| 54 /// Close is on the same line.''' |
| 55 /// |
| 56 /// This does nothing if text is only a single line. |
| 57 String cleanUpLiteral(String text) { |
| 58 var lines = text.split('\n'); |
| 59 if (lines.length <= 1) return text; |
| 60 |
| 61 for (var j = 0; j < lines.length; j++) { |
| 62 if (lines[j].length > 8) { |
| 63 lines[j] = lines[j].substring(8, lines[j].length); |
| 64 } else { |
| 65 lines[j] = ''; |
| 66 } |
| 67 } |
| 68 |
| 69 return lines.join('\n'); |
| 70 } |
| 71 |
| 72 /// Indents each line of [text] so that, when passed to [cleanUpLiteral], it |
| 73 /// will produce output identical to [text]. |
| 74 /// |
| 75 /// This is useful for literals that need to include newlines but can't be |
| 76 /// conveniently represented as multi-line strings. |
| 77 String indentLiteral(String text) { |
| 78 var lines = text.split('\n'); |
| 79 if (lines.length <= 1) return text; |
| 80 |
| 81 for (var i = 0; i < lines.length; i++) { |
| 82 lines[i] = " ${lines[i]}"; |
| 83 } |
| 84 |
| 85 return lines.join("\n"); |
| 86 } |
OLD | NEW |