aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/labs/testing/stringmatcher_test.html
blob: 9ad6dd40159a0cdbd73499837ad90e9e0b26fba7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<!DOCTYPE html>
<html>
<!--
Copyright 2012 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<title>Closure Unit Tests - String matchers</title>
<script src="../../base.js"></script>
<script>

goog.require('goog.labs.testing.ContainsStringMatcher');
goog.require('goog.labs.testing.EndsWithMatcher');
goog.require('goog.labs.testing.EqualsMatcher');
goog.require('goog.labs.testing.EqualToIgnoringWhitespaceMatcher');
goog.require('goog.labs.testing.StartsWithMatcher');
goog.require('goog.labs.testing.StringContainsInOrderMatcher');
goog.require('goog.labs.testing.assertThat');
goog.require('goog.testing.jsunit');

</script>
</head>
<body>
<script>

function testContainsString() {
  goog.labs.testing.assertThat('hello', containsString('ell'),
      'hello contains ell');

  assertMatcherError(function() {
    goog.labs.testing.assertThat('hello', containsString('world!'));
  }, 'containsString should throw exception when it fails');
}

function testEndsWith() {
  goog.labs.testing.assertThat('hello', endsWith('llo'), 'hello ends with llo');

  assertMatcherError(function() {
    goog.labs.testing.assertThat('minutes', endsWith('midnight'));
  }, 'endsWith should throw exception when it fails');
}

function testEqualToIgnoringWhitespace() {
  goog.labs.testing.assertThat('    h\n   EL L\tO',
      equalToIgnoringWhitespace("h el l o"),
      '"   h   EL L\tO   " is equal to "h el l o"');

  assertMatcherError(function() {
    goog.labs.testing.assertThat('hybrid', equalToIgnoringWhitespace('theory'));
  }, 'equalToIgnoringWhitespace should throw exception when it fails');
}

function testEquals() {
  goog.labs.testing.assertThat('hello', equals('hello'),
      'hello equals hello');

  assertMatcherError(function() {
    goog.labs.testing.assertThat('thousand', equals('suns'));
  }, 'equals should throw exception when it fails');
}

function testStartsWith() {
  goog.labs.testing.assertThat('hello', startsWith('hel'),
      'hello starts with hel');

  assertMatcherError(function() {
    goog.labs.testing.assertThat('linkin', startsWith('park'));
  }, 'startsWith should throw exception when it fails');
}

function testStringContainsInOrder() {
  goog.labs.testing.assertThat('hello',
      stringContainsInOrder(['h', 'el', 'el', 'l', 'o']),
      'hello contains in order: [h, el, l, o]');

  assertMatcherError(function() {
    goog.labs.testing.assertThat('hybrid', stringContainsInOrder(['hy', 'brid',
    'theory']));
  }, 'stringContainsInOrder should throw exception when it fails');
}

function assertMatcherError(callable, errorString) {
  var e = assertThrows(errorString || 'callable throws exception', callable);
  assertTrue(e instanceof goog.labs.testing.MatcherError);
}

</script>
</body>
</html>