| 1 |
// Copyright 2012 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.labs.testing.stringMatcherTest'); |
| 16 |
goog.setTestOnly('goog.labs.testing.stringMatcherTest'); |
| 17 |
|
| 18 |
goog.require('goog.labs.testing.MatcherError'); |
| 19 |
/** @suppress {extraRequire} */ |
| 20 |
goog.require('goog.labs.testing.StringContainsInOrderMatcher'); |
| 21 |
goog.require('goog.labs.testing.assertThat'); |
| 22 |
goog.require('goog.testing.jsunit'); |
| 23 |
|
| 24 |
function testAnyString() { |
| 25 |
goog.labs.testing.assertThat('foo', anyString(), 'typeof "foo" == "string"'); |
| 26 |
assertMatcherError(function() { |
| 27 |
goog.labs.testing.assertThat(1, anyString()); |
| 28 |
}, 'typeof 1 == "string"'); |
| 29 |
} |
| 30 |
|
| 31 |
function testContainsString() { |
| 32 |
goog.labs.testing.assertThat( |
| 33 |
'hello', containsString('ell'), 'hello contains ell'); |
| 34 |
|
| 35 |
assertMatcherError(function() { |
| 36 |
goog.labs.testing.assertThat('hello', containsString('world!')); |
| 37 |
}, 'containsString should throw exception when it fails'); |
| 38 |
} |
| 39 |
|
| 40 |
function testEndsWith() { |
| 41 |
goog.labs.testing.assertThat('hello', endsWith('llo'), 'hello ends with llo'); |
| 42 |
|
| 43 |
assertMatcherError(function() { |
| 44 |
goog.labs.testing.assertThat('minutes', endsWith('midnight')); |
| 45 |
}, 'endsWith should throw exception when it fails'); |
| 46 |
} |
| 47 |
|
| 48 |
function testEqualToIgnoringWhitespace() { |
| 49 |
goog.labs.testing.assertThat( |
| 50 |
' h\n EL L\tO', equalToIgnoringWhitespace('h el l o'), |
| 51 |
'" h EL L\tO " is equal to "h el l o"'); |
| 52 |
|
| 53 |
assertMatcherError(function() { |
| 54 |
goog.labs.testing.assertThat('hybrid', equalToIgnoringWhitespace('theory')); |
| 55 |
}, 'equalToIgnoringWhitespace should throw exception when it fails'); |
| 56 |
} |
| 57 |
|
| 58 |
function testEquals() { |
| 59 |
goog.labs.testing.assertThat('hello', equals('hello'), 'hello equals hello'); |
| 60 |
|
| 61 |
assertMatcherError(function() { |
| 62 |
goog.labs.testing.assertThat('thousand', equals('suns')); |
| 63 |
}, 'equals should throw exception when it fails'); |
| 64 |
} |
| 65 |
|
| 66 |
function testStartsWith() { |
| 67 |
goog.labs.testing.assertThat( |
| 68 |
'hello', startsWith('hel'), 'hello starts with hel'); |
| 69 |
|
| 70 |
assertMatcherError(function() { |
| 71 |
goog.labs.testing.assertThat('linkin', startsWith('park')); |
| 72 |
}, 'startsWith should throw exception when it fails'); |
| 73 |
} |
| 74 |
|
| 75 |
function testStringContainsInOrder() { |
| 76 |
goog.labs.testing.assertThat( |
| 77 |
'hello', stringContainsInOrder(['h', 'el', 'el', 'l', 'o']), |
| 78 |
'hello contains in order: [h, el, l, o]'); |
| 79 |
|
| 80 |
assertMatcherError(function() { |
| 81 |
goog.labs.testing.assertThat( |
| 82 |
'hybrid', stringContainsInOrder(['hy', 'brid', 'theory'])); |
| 83 |
}, 'stringContainsInOrder should throw exception when it fails'); |
| 84 |
} |
| 85 |
|
| 86 |
function testMatchesRegex() { |
| 87 |
goog.labs.testing.assertThat('foobar', matchesRegex(/foobar/)); |
| 88 |
goog.labs.testing.assertThat('foobar', matchesRegex(/oobar/)); |
| 89 |
|
| 90 |
assertMatcherError(function() { |
| 91 |
goog.labs.testing.assertThat('foo', matchesRegex(/^foobar$/)); |
| 92 |
}, 'matchesRegex should throw exception when it fails'); |
| 93 |
} |
| 94 |
|
| 95 |
function assertMatcherError(callable, errorString) { |
| 96 |
var e = assertThrows(errorString || 'callable throws exception', callable); |
| 97 |
assertTrue(e instanceof goog.labs.testing.MatcherError); |
| 98 |
} |