stringmatcher_test.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. goog.provide('goog.labs.testing.stringMatcherTest');
  15. goog.setTestOnly('goog.labs.testing.stringMatcherTest');
  16. goog.require('goog.labs.testing.MatcherError');
  17. /** @suppress {extraRequire} */
  18. goog.require('goog.labs.testing.StringContainsInOrderMatcher');
  19. goog.require('goog.labs.testing.assertThat');
  20. goog.require('goog.testing.jsunit');
  21. function testAnyString() {
  22. goog.labs.testing.assertThat('foo', anyString(), 'typeof "foo" == "string"');
  23. assertMatcherError(function() {
  24. goog.labs.testing.assertThat(1, anyString());
  25. }, 'typeof 1 == "string"');
  26. }
  27. function testContainsString() {
  28. goog.labs.testing.assertThat(
  29. 'hello', containsString('ell'), 'hello contains ell');
  30. assertMatcherError(function() {
  31. goog.labs.testing.assertThat('hello', containsString('world!'));
  32. }, 'containsString should throw exception when it fails');
  33. }
  34. function testEndsWith() {
  35. goog.labs.testing.assertThat('hello', endsWith('llo'), 'hello ends with llo');
  36. assertMatcherError(function() {
  37. goog.labs.testing.assertThat('minutes', endsWith('midnight'));
  38. }, 'endsWith should throw exception when it fails');
  39. }
  40. function testEqualToIgnoringWhitespace() {
  41. goog.labs.testing.assertThat(
  42. ' h\n EL L\tO', equalToIgnoringWhitespace('h el l o'),
  43. '" h EL L\tO " is equal to "h el l o"');
  44. assertMatcherError(function() {
  45. goog.labs.testing.assertThat('hybrid', equalToIgnoringWhitespace('theory'));
  46. }, 'equalToIgnoringWhitespace should throw exception when it fails');
  47. }
  48. function testEquals() {
  49. goog.labs.testing.assertThat('hello', equals('hello'), 'hello equals hello');
  50. assertMatcherError(function() {
  51. goog.labs.testing.assertThat('thousand', equals('suns'));
  52. }, 'equals should throw exception when it fails');
  53. }
  54. function testStartsWith() {
  55. goog.labs.testing.assertThat(
  56. 'hello', startsWith('hel'), 'hello starts with hel');
  57. assertMatcherError(function() {
  58. goog.labs.testing.assertThat('linkin', startsWith('park'));
  59. }, 'startsWith should throw exception when it fails');
  60. }
  61. function testStringContainsInOrder() {
  62. goog.labs.testing.assertThat(
  63. 'hello', stringContainsInOrder(['h', 'el', 'el', 'l', 'o']),
  64. 'hello contains in order: [h, el, l, o]');
  65. assertMatcherError(function() {
  66. goog.labs.testing.assertThat(
  67. 'hybrid', stringContainsInOrder(['hy', 'brid', 'theory']));
  68. }, 'stringContainsInOrder should throw exception when it fails');
  69. }
  70. function testMatchesRegex() {
  71. goog.labs.testing.assertThat('foobar', matchesRegex(/foobar/));
  72. goog.labs.testing.assertThat('foobar', matchesRegex(/oobar/));
  73. assertMatcherError(function() {
  74. goog.labs.testing.assertThat('foo', matchesRegex(/^foobar$/));
  75. }, 'matchesRegex should throw exception when it fails');
  76. }
  77. function assertMatcherError(callable, errorString) {
  78. var e = assertThrows(errorString || 'callable throws exception', callable);
  79. assertTrue(e instanceof goog.labs.testing.MatcherError);
  80. }