OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
Jennifer Messerly
2014/05/20 18:07:56
2014?
nweiz
2014/05/20 21:39:48
Done.
| |
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:unittest/unittest.dart'; | |
8 import 'package:yaml/src/deep_equals.dart' as de; | |
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) => | |
18 predicate((actual) => de.deepEquals(actual, expected), "equals $expected"); | |
19 | |
20 /// Constructs a new yaml.YamlMap, optionally from a normal Map. | |
21 Map yamlMap([Map from]) => | |
22 from == null ? new YamlMap() : new YamlMap.from(from); | |
23 | |
24 /// Asserts that a string containing a single YAML document produces a given | |
25 /// value when loaded. | |
26 void expectYamlLoads(expected, String source) { | |
27 var actual = loadYaml(cleanUpLiteral(source)); | |
28 expect(expected, deepEquals(actual)); | |
29 } | |
30 | |
31 /// Asserts that a string containing a stream of YAML documents produces a given | |
32 /// list of values when loaded. | |
33 void expectYamlStreamLoads(List expected, String source) { | |
34 var actual = loadYamlStream(cleanUpLiteral(source)); | |
35 expect(expected, deepEquals(actual)); | |
36 } | |
37 | |
38 /// Asserts that a string containing a single YAML document throws a | |
39 /// [YamlException]. | |
40 void expectYamlFails(String source) { | |
41 expect(() => loadYaml(cleanUpLiteral(source)), throwsYamlException); | |
42 } | |
43 | |
44 /// Removes eight spaces of leading indentation from a multiline string. | |
45 /// | |
46 /// Note that this is very sensitive to how the literals are styled. They should | |
47 /// be: | |
48 /// ''' | |
49 /// Text starts on own line. Lines up with subsequent lines. | |
50 /// Lines are indented exactly 8 characters from the left margin. | |
51 /// Close is on the same line.''' | |
52 /// | |
53 /// This does nothing if text is only a single line. | |
54 String cleanUpLiteral(String text) { | |
55 var lines = text.split('\n'); | |
56 if (lines.length <= 1) return text; | |
57 | |
58 for (var j = 0; j < lines.length; j++) { | |
59 if (lines[j].length > 8) { | |
60 lines[j] = lines[j].substring(8, lines[j].length); | |
61 } else { | |
62 lines[j] = ''; | |
63 } | |
64 } | |
65 | |
66 return lines.join('\n'); | |
67 } | |
68 | |
69 /// Indents each line of [text] so that, when passed to [cleanUpLiteral], it wil l | |
Jennifer Messerly
2014/05/20 18:07:56
long line
nweiz
2014/05/20 21:39:48
Done.
| |
70 /// produce output identical to [text]. | |
71 /// | |
72 /// This is useful for literals that need to include newlines but can't be | |
73 /// conveniently represented as multi-line strings. | |
74 String indentLiteral(String text) { | |
75 var lines = text.split('\n'); | |
76 if (lines.length <= 1) return text; | |
77 | |
78 for (var i = 0; i < lines.length; i++) { | |
79 lines[i] = " ${lines[i]}"; | |
80 } | |
81 | |
82 return lines.join("\n"); | |
83 } | |
OLD | NEW |