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 pub_semver.test.utils; | |
6 | |
7 import 'package:unittest/unittest.dart'; | |
8 | |
9 import 'package:pub_semver/pub_semver.dart'; | |
10 | |
11 /// Some stock example versions to use in tests. | |
12 final v114 = new Version.parse('1.1.4'); | |
13 final v123 = new Version.parse('1.2.3'); | |
14 final v124 = new Version.parse('1.2.4'); | |
15 final v130 = new Version.parse('1.3.0'); | |
16 final v140 = new Version.parse('1.4.0'); | |
17 final v200 = new Version.parse('2.0.0'); | |
18 final v201 = new Version.parse('2.0.1'); | |
19 final v234 = new Version.parse('2.3.4'); | |
20 final v250 = new Version.parse('2.5.0'); | |
21 final v300 = new Version.parse('3.0.0'); | |
22 | |
23 /// A [Matcher] that tests if a [VersionConstraint] allows or does not allow a | |
24 /// given list of [Version]s. | |
25 class _VersionConstraintMatcher implements Matcher { | |
26 final List<Version> _expected; | |
27 final bool _allow; | |
28 | |
29 _VersionConstraintMatcher(this._expected, this._allow); | |
30 | |
31 bool matches(item, Map matchState) => (item is VersionConstraint) && | |
32 _expected.every((version) => item.allows(version) == _allow); | |
33 | |
34 Description describe(Description description) => | |
35 description.add(' ${_allow ? "allows" : "does not allow"} versions'); | |
nweiz
2014/09/25 22:52:02
Shouldn't this include _expected?
Bob Nystrom
2014/09/26 19:41:07
describeMismatch() more or less covers this, but d
| |
36 | |
37 Description describeMismatch(item, Description mismatchDescription, | |
38 Map matchState, bool verbose) { | |
39 if (item is! VersionConstraint) { | |
40 mismatchDescription.add('was not a VersionConstraint'); | |
41 return mismatchDescription; | |
42 } | |
43 | |
44 var first = true; | |
45 for (var version in _expected) { | |
46 if (item.allows(version) != _allow) { | |
47 if (first) { | |
48 if (_allow) { | |
49 mismatchDescription.addDescriptionOf(item).add('did not allow '); | |
50 } else { | |
51 mismatchDescription.addDescriptionOf(item).add('allowed '); | |
52 } | |
53 } else { | |
54 mismatchDescription.add(' and '); | |
55 } | |
56 first = false; | |
57 | |
58 mismatchDescription.add(version.toString()); | |
59 } | |
60 } | |
61 | |
62 return mismatchDescription; | |
63 } | |
64 } | |
65 | |
66 /// Gets a [Matcher] that validates that a [VersionConstraint] allows all | |
67 /// given versions. | |
68 Matcher allows(Version v1, [Version v2, Version v3, Version v4, | |
69 Version v5, Version v6, Version v7, Version v8]) { | |
70 var versions = _makeVersionList(v1, v2, v3, v4, v5, v6, v7, v8); | |
71 return new _VersionConstraintMatcher(versions, true); | |
72 } | |
73 | |
74 /// Gets a [Matcher] that validates that a [VersionConstraint] allows none of | |
75 /// the given versions. | |
76 Matcher doesNotAllow(Version v1, [Version v2, Version v3, Version v4, | |
77 Version v5, Version v6, Version v7, Version v8]) { | |
78 var versions = _makeVersionList(v1, v2, v3, v4, v5, v6, v7, v8); | |
79 return new _VersionConstraintMatcher(versions, false); | |
80 } | |
81 | |
82 /// Validates that [function] throws an [ArgumentError] when invoked. | |
83 throwsArgError(function) { | |
nweiz
2014/09/25 22:52:02
This doesn't seem worth a helper function.
Bob Nystrom
2014/09/26 19:41:07
Done.
| |
84 expect(function, throwsArgumentError); | |
85 } | |
86 | |
87 List<Version> _makeVersionList(Version v1, [Version v2, Version v3, Version v4, | |
88 Version v5, Version v6, Version v7, Version v8]) { | |
89 var versions = [v1]; | |
90 if (v2 != null) versions.add(v2); | |
91 if (v3 != null) versions.add(v3); | |
92 if (v4 != null) versions.add(v4); | |
93 if (v5 != null) versions.add(v5); | |
94 if (v6 != null) versions.add(v6); | |
95 if (v7 != null) versions.add(v7); | |
96 if (v8 != null) versions.add(v8); | |
97 return versions; | |
98 } | |
OLD | NEW |