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.addAll(' ${_allow ? "allows" : "does not allow"} versions ', |
| 36 ', ', '', _expected); |
| 37 return description; |
| 38 } |
| 39 |
| 40 Description describeMismatch(item, Description mismatchDescription, |
| 41 Map matchState, bool verbose) { |
| 42 if (item is! VersionConstraint) { |
| 43 mismatchDescription.add('was not a VersionConstraint'); |
| 44 return mismatchDescription; |
| 45 } |
| 46 |
| 47 var first = true; |
| 48 for (var version in _expected) { |
| 49 if (item.allows(version) != _allow) { |
| 50 if (first) { |
| 51 if (_allow) { |
| 52 mismatchDescription.addDescriptionOf(item).add(' did not allow '); |
| 53 } else { |
| 54 mismatchDescription.addDescriptionOf(item).add(' allowed '); |
| 55 } |
| 56 } else { |
| 57 mismatchDescription.add(' and '); |
| 58 } |
| 59 first = false; |
| 60 |
| 61 mismatchDescription.add(version.toString()); |
| 62 } |
| 63 } |
| 64 |
| 65 return mismatchDescription; |
| 66 } |
| 67 } |
| 68 |
| 69 /// Gets a [Matcher] that validates that a [VersionConstraint] allows all |
| 70 /// given versions. |
| 71 Matcher allows(Version v1, [Version v2, Version v3, Version v4, |
| 72 Version v5, Version v6, Version v7, Version v8]) { |
| 73 var versions = _makeVersionList(v1, v2, v3, v4, v5, v6, v7, v8); |
| 74 return new _VersionConstraintMatcher(versions, true); |
| 75 } |
| 76 |
| 77 /// Gets a [Matcher] that validates that a [VersionConstraint] allows none of |
| 78 /// the given versions. |
| 79 Matcher doesNotAllow(Version v1, [Version v2, Version v3, Version v4, |
| 80 Version v5, Version v6, Version v7, Version v8]) { |
| 81 var versions = _makeVersionList(v1, v2, v3, v4, v5, v6, v7, v8); |
| 82 return new _VersionConstraintMatcher(versions, false); |
| 83 } |
| 84 |
| 85 List<Version> _makeVersionList(Version v1, [Version v2, Version v3, Version v4, |
| 86 Version v5, Version v6, Version v7, Version v8]) { |
| 87 var versions = [v1]; |
| 88 if (v2 != null) versions.add(v2); |
| 89 if (v3 != null) versions.add(v3); |
| 90 if (v4 != null) versions.add(v4); |
| 91 if (v5 != null) versions.add(v5); |
| 92 if (v6 != null) versions.add(v6); |
| 93 if (v7 != null) versions.add(v7); |
| 94 if (v8 != null) versions.add(v8); |
| 95 return versions; |
| 96 } |
OLD | NEW |