Chromium Code Reviews| Index: tests/utils/unittest_test.dart |
| =================================================================== |
| --- tests/utils/unittest_test.dart (revision 0) |
| +++ tests/utils/unittest_test.dart (revision 0) |
| @@ -0,0 +1,369 @@ |
| +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
|
Bob Nystrom
2012/05/30 23:23:51
Yay unit tests for unittest! This is awesome.
|
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +#library('unittestTest'); |
|
Bob Nystrom
2012/05/30 23:23:51
unittest_test
|
| + |
| +#import('../../lib/unittest/unittest.dart'); |
| + |
| + |
| +foo() {} |
|
Bob Nystrom
2012/05/30 23:23:51
"doesNotThrow"
|
| +bar() { throw 'X'; } |
|
Bob Nystrom
2012/05/30 23:23:51
"doesThrow"
|
| + |
| +int errorCount; |
| +String errorString; |
| +var myhandler; |
|
Bob Nystrom
2012/05/30 23:23:51
myHandler
Or, ideally, a more meaningful name.
|
| + |
| +class MyFailureHandler implements IFailureHandler { |
| + DefaultErrorFormatter f = null; |
| + |
| + void fail(String reason) { |
| + ++errorCount; |
| + errorString = reason; |
| + } |
| + void failMatch(actual, IMatcher matcher, String reason) { |
| + if (f == null) |
| + f = new DefaultErrorFormatter(); |
|
Bob Nystrom
2012/05/30 23:23:51
Put all on one line or use {}.
|
| + fail(f.format(actual, matcher, reason)); |
| + } |
| +} |
| + |
| +void shouldFail(var value, IMatcher matcher, String reason) { |
| + errorCount = 0; |
| + errorString = ''; |
| + configureExpectHandler(myhandler); |
| + expect(value, matcher); |
| + configureExpectHandler(null); |
| + expect(errorCount, equals(1)); |
| + expect(errorString, equalToIgnoringWhitespace(reason)); |
| +} |
| + |
| +void shouldPass(var value, IMatcher matcher) { |
| + errorCount = 0; |
| + errorString = ''; |
| + configureExpectHandler(myhandler); |
| + expect(value, matcher); |
| + configureExpectHandler(null); |
| + expect(errorCount, equals(0)); |
| +} |
| + |
| +void main() { |
| + |
| + myhandler = new MyFailureHandler(); |
| + |
| + // Core matchers |
|
Bob Nystrom
2012/05/30 23:23:51
Instead of comments, use group() and test(). That
gram
2012/06/01 22:21:59
Done.
|
| + |
| + // isTrue |
| + shouldPass(true, isTrue()); |
| + shouldFail(false, isTrue(), "Expected: true but: was <false>"); |
| + |
| + // isFalse |
| + shouldPass(false, isFalse()); |
| + shouldFail(true, isFalse(), "Expected: not true but: was <true>"); |
| + |
| + // isNull |
| + shouldPass(null, isNull()); |
| + shouldFail(false, isNull(), "Expected: null but: was <false>"); |
| + |
| + // isNotNull |
| + shouldPass(false, isNotNull()); |
| + shouldFail(null, isNotNull(), "Expected: not null but: was <null>"); |
| + |
| + var a = new Map(); |
| + var b = new Map(); |
| + |
| + // same |
| + shouldPass(a, same(a)); |
| + shouldFail(b, same(a), "Expected: same instance as <{}> but: was <{}>"); |
| + |
| + // equals - only for scalars |
| + shouldPass(a, equals(a)); |
| + shouldFail(a, equals(b), "Expected: <{}> but: was <{}>"); |
| + |
| + // anything |
| + shouldPass(a, anything()); |
| + shouldFail(a, isNot(anything()), "Expected: not anything but: was <{}>"); |
| + |
| + // throwsException |
| + shouldFail(foo, throwsException(), |
| + "Expected: throws an exception but: no exception"); |
|
Bob Nystrom
2012/05/30 23:23:51
Indent another 2, here and elsewhere.
gram
2012/06/01 22:21:59
Done.
|
| + shouldPass(bar, throwsException()); |
| + |
| + // throwsExceptionWhich |
| + shouldPass(bar, throwsExceptionWhich(hasString('X'))); |
| + shouldFail(bar, throwsExceptionWhich(hasString('Y')), |
| + "Expected: throws an exception which needs to match with toString() " |
| + "value 'Y' but: exception does not match with toString() value 'Y'"); |
| + |
| + // returnsNormally |
| + shouldPass(foo, returnsNormally()); |
| + shouldFail(bar, returnsNormally(), |
| + "Expected: return normally but: threw exception"); |
| + |
| + // isInstanceOf |
| + shouldFail(0, isInstanceOf(new Type<String>('String')), |
| + "Expected: an instance of String but: was <0>"); |
| + shouldPass('cow', isInstanceOf(new Type<String>('String'))); |
| + |
| + // hasLength |
| + var c = new List(); |
| + shouldPass(c, hasLength()); |
| + shouldPass(a, hasLength()); |
| + shouldPass('a', hasLength()); |
| + shouldFail(0, hasLength(), |
| + "Expected: an object with length of anything " |
| + "but: was <0> has no length property"); |
| + |
| + c.add(0); |
| + shouldPass(c, hasLength(1)); |
| + shouldFail(c, hasLength(2), |
| + "Expected: an object with length of <2> " |
| + "but: was <[0]> with length of <1>"); |
| + |
| + c.add(0); |
| + shouldFail(c, hasLength(1), |
| + "Expected: an object with length of <1> " |
| + "but: was <[0, 0]> with length of <2>"); |
| + shouldPass(c, hasLength(2)); |
| + |
| + // hasString |
| + shouldPass(10, hasString('10')); |
| + shouldFail(10, hasString('11'), |
| + "Expected: with toString() value '11' but: was <10>"); |
| + |
| + // Numeric Matchers |
| + |
| + //greaterThan |
| + shouldPass(10, greaterThan(9)); |
| + shouldFail(9, greaterThan(10), |
| + "Expected: a value greater than <10> but: was <9>"); |
| + |
| + // greaterThanOrEqualTo |
| + shouldPass(10, greaterThanOrEqualTo(10)); |
| + shouldFail(9, greaterThanOrEqualTo(10), |
| + "Expected: a value greater than or equal to <10> but: was <9>"); |
| + |
| + // lessThan |
| + shouldFail(10, lessThan(9), "Expected: a value less than <9> but: was <10>"); |
| + shouldPass(9, lessThan(10)); |
| + |
| + // lessThanOrEqualTo |
| + shouldPass(10, lessThanOrEqualTo(10)); |
| + shouldFail(11, lessThanOrEqualTo(10), |
| + "Expected: a value less than or equal to <10> but: was <11>"); |
| + |
| + // isZero |
| + shouldPass(0, isZero()); |
| + shouldFail(1, isZero(), "Expected: a value equal to <0> but: was <1>"); |
| + |
| + // isNonZero |
| + shouldFail(0, isNonZero(), "Expected: a value not equal to <0> but: was <0>"); |
| + shouldPass(1, isNonZero()); |
| + |
| + // isPositive |
| + shouldFail(-1, isPositive(), "Expected: a value positive <0> but: was <-1>"); |
| + shouldPass(0, isPositive()); |
| + shouldPass(1, isPositive()); |
| + |
| + // isNegative |
| + shouldPass(-1, isNegative()); |
| + shouldFail(0, isNegative(), "Expected: a value negative <0> but: was <0>"); |
| + |
| + // closeTo |
| + shouldPass(0, closeTo(0, 1)); |
| + shouldPass(-1, closeTo(0, 1)); |
| + shouldPass(1, closeTo(0, 1)); |
| + shouldFail(1.001, closeTo(0, 1), |
| + "Expected: a numeric value within <1> of <0> " |
| + "but: <1.001> differed by <1.001>"); |
| + shouldFail(-1.001, closeTo(0, 1), |
| + "Expected: a numeric value within <1> of <0> " |
| + "but: <-1.001> differed by <1.001>"); |
| + |
| + // inInclusiveRange |
| + shouldFail(-1, inInclusiveRange(0,2), |
| + "Expected: be in range from 0 (inclusive) to 2 (inclusive) but: was <-1>"); |
| + shouldPass(0, inInclusiveRange(0,2)); |
| + shouldPass(1, inInclusiveRange(0,2)); |
| + shouldPass(2, inInclusiveRange(0,2)); |
| + shouldFail(3, inInclusiveRange(0,2), |
| + "Expected: be in range from 0 (inclusive) to 2 (inclusive) but: was <3>"); |
| + |
| + // inExclusiveRange |
| + shouldFail(0, inExclusiveRange(0,2), |
| + "Expected: be in range from 0 (exclusive) to 2 (exclusive) but: was <0>"); |
| + shouldPass(1, inExclusiveRange(0,2)); |
| + shouldFail(2, inExclusiveRange(0,2), |
| + "Expected: be in range from 0 (exclusive) to 2 (exclusive) but: was <2>"); |
| + |
| + // inOpenClosedRange |
| + shouldFail(0, inOpenClosedRange(0,2), |
| + "Expected: be in range from 0 (exclusive) to 2 (inclusive) but: was <0>"); |
| + shouldPass(1, inOpenClosedRange(0,2)); |
| + shouldPass(2, inOpenClosedRange(0,2)); |
| + |
| + // inClosedOpenRange |
| + shouldPass(0, inClosedOpenRange(0,2)); |
| + shouldPass(1, inClosedOpenRange(0,2)); |
| + shouldFail(2, inClosedOpenRange(0,2), |
| + "Expected: be in range from 0 (inclusive) to 2 (exclusive) but: was <2>"); |
| + |
| + // String Matchers |
| + |
| + // isEmptyString |
| + shouldPass('', isEmptyString()); |
| + shouldFail(null, isEmptyString(), |
| + "Expected: empty string but: <null> not a string"); |
| + shouldFail(0, isEmptyString(), |
| + "Expected: empty string but: <0> not a string"); |
| + shouldFail([], isEmptyString(), |
| + "Expected: empty string but: <[]> not a string"); |
| + shouldFail({}, isEmptyString(), |
| + "Expected: empty string but: <{}> not a string"); |
| + shouldFail('a', isEmptyString(), "Expected: empty string but: was 'a'"); |
| + |
| + // isInterpolated |
| + shouldPass(c, isInterpolated('[0, 0]')); |
| + shouldFail(c, isInterpolated('[0, 1]'), |
| + "Expected: '[0, 1]' interpolated but: was <[0, 0]>"); |
| + |
| + // equalToIgnoringCase |
| + shouldPass('hello', equalToIgnoringCase('HELLO')); |
| + shouldFail('hi', equalToIgnoringCase('HELLO'), |
| + "Expected: 'HELLO' ignoring case but: was 'hi'"); |
| + |
| + // equalToIgnoringWhitespace |
| + shouldPass(' hello world ', equalToIgnoringWhitespace('hello world')); |
| + shouldFail(' helloworld ', equalToIgnoringWhitespace('hello world'), |
| + "Expected: 'hello world' ignoring whitespace but: was 'helloworld'"); |
| + |
| + // startsWith |
| + shouldPass('hello', startsWith('')); |
| + shouldPass('hello', startsWith('hell')); |
| + shouldPass('hello', startsWith('hello')); |
| + shouldFail('hello', startsWith('hello '), |
| + "Expected: a string starting with 'hello ' but: was 'hello'"); |
| + |
| + // endsWith |
| + shouldPass('hello', endsWith('')); |
| + shouldPass('hello', endsWith('lo')); |
| + shouldPass('hello', endsWith('hello')); |
| + shouldFail('hello', endsWith(' hello'), |
| + "Expected: a string ending with ' hello' but: was 'hello'"); |
| + |
| + // containsString |
| + shouldPass('hello', containsString('')); |
| + shouldPass('hello', containsString('h')); |
| + shouldPass('hello', containsString('o')); |
| + shouldPass('hello', containsString('hell')); |
| + shouldPass('hello', containsString('hello')); |
| + shouldFail('hello', containsString(' '), |
| + "Expected: a string containing ' ' but: was 'hello'"); |
| + |
| + // stringContainsInOrder |
| + shouldPass('goodbye cruel world', stringContainsInOrder([''])); |
| + shouldPass('goodbye cruel world', stringContainsInOrder(['goodbye'])); |
| + shouldPass('goodbye cruel world', stringContainsInOrder(['cruel'])); |
| + shouldPass('goodbye cruel world', stringContainsInOrder(['world'])); |
| + shouldPass('goodbye cruel world', |
| + stringContainsInOrder(['good', 'bye', 'world'])); |
| + shouldPass('goodbye cruel world', stringContainsInOrder(['goodbye', 'cruel'])); |
|
Bob Nystrom
2012/05/30 23:23:51
Long line.
gram
2012/06/01 22:21:59
Done.
|
| + shouldPass('goodbye cruel world', stringContainsInOrder(['cruel', 'world'])); |
| + shouldPass('goodbye cruel world', |
| + stringContainsInOrder(['goodbye', 'cruel', 'world'])); |
| + shouldFail('goodbye cruel world', |
| + stringContainsInOrder(['goo', 'cruel', 'bye']), |
| + "Expected: a string containing 'goo', 'cruel', 'bye' in order " |
| + "but: was 'goodbye cruel world'"); |
| + |
| + // matches |
| + shouldPass('c0d', matches('[a-z][0-9][a-z]')); |
| + shouldPass('c0d', matches(new RegExp('[a-z][0-9][a-z]'))); |
| + shouldFail('cOd', matches('[a-z][0-9][a-z]'), |
| + "Expected: match '[a-z][0-9][a-z]' but: was 'cOd'"); |
| + |
| + // Collection Matchers |
| + |
| + // emptyCollection |
| + shouldPass([], emptyCollection()); |
| + shouldFail([1], emptyCollection(), "Expected: be empty but: was <[1]>"); |
| + |
| + var d = [ 1, 2 ]; |
|
Bob Nystrom
2012/05/30 23:23:51
No spaces after [ or before ]
gram
2012/06/01 22:21:59
Done.
|
| + |
| + // contains |
| + shouldPass(d, contains(1)); |
| + shouldFail(d, contains(0), "Expected: contains <0> but: was <[1, 2]>"); |
| + |
| + var e = [1, 1, 1]; |
| + |
| + // everyElement |
| + shouldFail(d, everyElement(1), |
| + "Expected: every element <1> but: was <[1, 2]>"); |
| + shouldPass(e, everyElement(1)); |
| + |
| + // someElement |
| + shouldPass(d, someElement(2)); |
| + shouldFail(e, someElement(2), |
| + "Expected: some element <2> but: was <[1, 1, 1]>"); |
| + |
| + // orderedEquals |
| + shouldPass(d, orderedEquals([1, 2])); |
| + shouldFail(d, orderedEquals([2, 1]), |
| + "Expected: equals <[2, 1]> ordered but: was <[1, 2]>"); |
| + |
| + // unorderedEquals |
| + shouldPass(d, unorderedEquals([2, 1])); |
| + shouldFail(d, unorderedEquals([1]), |
| + "Expected: equals <{1}> unordered but: was <[1, 2]>"); |
| + shouldFail(d, unorderedEquals([3, 2, 1]), |
| + "Expected: equals <{1, 2, 3}> unordered but: was <[1, 2]>"); |
| + |
| + // Map Matchers |
| + |
| + // emptyMap |
| + |
| + shouldPass({}, emptyMap()); |
| + shouldPass(a, emptyMap()); |
| + a['foo'] = 'bar'; |
| + shouldFail(a, emptyMap(), "Expected: empty but: was <{foo: bar}>"); |
| + |
| + // mapContainsKey |
| + shouldPass(a, mapContainsKey('foo')); |
| + shouldFail(b, mapContainsKey('foo'), |
| + "Expected: contains key 'foo' but: was <{}>"); |
| + shouldFail(10, mapContainsKey('foo'), |
| + "Expected: contains key 'foo' but: <10> not a map"); |
| + |
| + // mapContainsValue |
| + shouldPass(a, mapContainsValue('bar')); |
| + shouldFail(a, mapContainsValue('ba'), |
| + "Expected: contains value 'ba' but: was <{foo: bar}>"); |
| + |
| + // mapContains |
| + shouldPass(a, mapContains('foo', 'bar')); |
| + shouldFail(a, mapContains('foo', 'ba'), |
| + "Expected: contains pair 'foo' => 'ba' " |
| + "but: contains key 'foo' but with value "); |
| + shouldFail(a, mapContains('fo', 'bar'), |
| + "Expected: contains pair 'fo' => 'bar' " |
| + "but: <{foo: bar}>' doesn't contain key ''fo'"); |
| + |
| + // mapLength |
| + shouldPass(a, mapLength(1)); |
| + shouldFail(b, mapLength(1), "Expected: map length <1> but: was <{}>"); |
| + |
| + // Operator Matchers |
| + |
| + // anyOf |
| + shouldFail(0, anyOf([equals(1), equals(2)]), |
| + "Expected: (<1> or <2>) but: was <0>"); |
| + shouldPass(1, anyOf([equals(1), equals(2)])); |
| + |
| + // allOf |
| + shouldPass(1, allOf([lessThan(10), greaterThan(0)])); |
| + shouldFail(-1, allOf([lessThan(10), greaterThan(0)]), |
| + "Expected: (a value less than <10> and a value greater than <0>) " |
| + "but: a value greater than <0> was <-1>"); |
| +} |
| + |
| + |