aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/labs/testing/assertthat_test.html
blob: a70e9c73bc0a2d1987f65983123a3f6fd63eb074 (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
<!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 - goog.labs.testing.assertThat</title>
<script src="../../base.js"></script>
<script>

goog.require('goog.labs.testing.Matcher');
goog.require('goog.labs.testing.assertThat');
goog.require('goog.testing.recordFunction');
goog.require('goog.testing.jsunit');

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

var successMatchesFn, failureMatchesFn, describeFn, successTestMatcher;
var failureTestMatcher;

function setUp() {
  successMatchesFn = new goog.testing.recordFunction(function() {return true;});
  failureMatchesFn = new goog.testing.recordFunction(function() {return false;});
  describeFn = new goog.testing.recordFunction();

  successTestMatcher = function() {
    return { matches: successMatchesFn, describe: describeFn };
  };
  failureTestMatcher = function() {
    return { matches: failureMatchesFn, describe: describeFn };
  };
}

function testAssertthatAlwaysCallsMatches() {
  var value = 7;
  goog.labs.testing.assertThat(value, successTestMatcher(),
      'matches is called on success');

  assertEquals(1, successMatchesFn.getCallCount());
  var matchesCall = successMatchesFn.popLastCall();
  assertEquals(value, matchesCall.getArgument(0));

  var e = assertThrows(goog.bind(goog.labs.testing.assertThat, null,
      value, failureTestMatcher(), 'matches is called on failure'));

  assertTrue(e instanceof goog.labs.testing.MatcherError);

  assertEquals(1, failureMatchesFn.getCallCount());
}

function testAssertthatCallsDescribeOnFailure() {
  var value = 7;
  var e = assertThrows(goog.bind(goog.labs.testing.assertThat, null,
      value, failureTestMatcher(), 'describe is called on failure'));

  assertTrue(e instanceof goog.labs.testing.MatcherError);

  assertEquals(1, failureMatchesFn.getCallCount());
  assertEquals(1, describeFn.getCallCount());

  var matchesCall = describeFn.popLastCall();
  assertEquals(value, matchesCall.getArgument(0));
}

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