Chromium Code Reviews| Index: test/utils.dart |
| diff --git a/test/utils.dart b/test/utils.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d65d8aa419253124af00e617e3e8775980d6eff8 |
| --- /dev/null |
| +++ b/test/utils.dart |
| @@ -0,0 +1,98 @@ |
| +// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +library pub_semver.test.utils; |
| + |
| +import 'package:unittest/unittest.dart'; |
| + |
| +import 'package:pub_semver/pub_semver.dart'; |
| + |
| +/// Some stock example versions to use in tests. |
| +final v114 = new Version.parse('1.1.4'); |
| +final v123 = new Version.parse('1.2.3'); |
| +final v124 = new Version.parse('1.2.4'); |
| +final v130 = new Version.parse('1.3.0'); |
| +final v140 = new Version.parse('1.4.0'); |
| +final v200 = new Version.parse('2.0.0'); |
| +final v201 = new Version.parse('2.0.1'); |
| +final v234 = new Version.parse('2.3.4'); |
| +final v250 = new Version.parse('2.5.0'); |
| +final v300 = new Version.parse('3.0.0'); |
| + |
| +/// A [Matcher] that tests if a [VersionConstraint] allows or does not allow a |
| +/// given list of [Version]s. |
| +class _VersionConstraintMatcher implements Matcher { |
| + final List<Version> _expected; |
| + final bool _allow; |
| + |
| + _VersionConstraintMatcher(this._expected, this._allow); |
| + |
| + bool matches(item, Map matchState) => (item is VersionConstraint) && |
| + _expected.every((version) => item.allows(version) == _allow); |
| + |
| + Description describe(Description description) => |
| + 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
|
| + |
| + Description describeMismatch(item, Description mismatchDescription, |
| + Map matchState, bool verbose) { |
| + if (item is! VersionConstraint) { |
| + mismatchDescription.add('was not a VersionConstraint'); |
| + return mismatchDescription; |
| + } |
| + |
| + var first = true; |
| + for (var version in _expected) { |
| + if (item.allows(version) != _allow) { |
| + if (first) { |
| + if (_allow) { |
| + mismatchDescription.addDescriptionOf(item).add('did not allow '); |
| + } else { |
| + mismatchDescription.addDescriptionOf(item).add('allowed '); |
| + } |
| + } else { |
| + mismatchDescription.add(' and '); |
| + } |
| + first = false; |
| + |
| + mismatchDescription.add(version.toString()); |
| + } |
| + } |
| + |
| + return mismatchDescription; |
| + } |
| +} |
| + |
| +/// Gets a [Matcher] that validates that a [VersionConstraint] allows all |
| +/// given versions. |
| +Matcher allows(Version v1, [Version v2, Version v3, Version v4, |
| + Version v5, Version v6, Version v7, Version v8]) { |
| + var versions = _makeVersionList(v1, v2, v3, v4, v5, v6, v7, v8); |
| + return new _VersionConstraintMatcher(versions, true); |
| +} |
| + |
| +/// Gets a [Matcher] that validates that a [VersionConstraint] allows none of |
| +/// the given versions. |
| +Matcher doesNotAllow(Version v1, [Version v2, Version v3, Version v4, |
| + Version v5, Version v6, Version v7, Version v8]) { |
| + var versions = _makeVersionList(v1, v2, v3, v4, v5, v6, v7, v8); |
| + return new _VersionConstraintMatcher(versions, false); |
| +} |
| + |
| +/// Validates that [function] throws an [ArgumentError] when invoked. |
| +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.
|
| + expect(function, throwsArgumentError); |
| +} |
| + |
| +List<Version> _makeVersionList(Version v1, [Version v2, Version v3, Version v4, |
| + Version v5, Version v6, Version v7, Version v8]) { |
| + var versions = [v1]; |
| + if (v2 != null) versions.add(v2); |
| + if (v3 != null) versions.add(v3); |
| + if (v4 != null) versions.add(v4); |
| + if (v5 != null) versions.add(v5); |
| + if (v6 != null) versions.add(v6); |
| + if (v7 != null) versions.add(v7); |
| + if (v8 != null) versions.add(v8); |
| + return versions; |
| +} |