aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/debug/error_test.html
blob: 4efa0fd2d839607c239071cdbd03e60cba36f714 (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
92
93
94
95
96
97
98
99
100
101
<!DOCTYPE html>
<html>
<!--
Copyright 2009 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>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.debug.Error</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.debug.Error');
  goog.require('goog.testing.ExpectedFailures');
  goog.require('goog.testing.jsunit');
  goog.require('goog.userAgent');
  goog.require('goog.userAgent.product');
</script>
</head>
<body>
<script>
var expectedFailures = new goog.testing.ExpectedFailures();

function tearDown() {
  expectedFailures.handleTearDown();
}

function testError() {
  function xxxxx() {
    yyyyy();
  }
  function yyyyy() {
    zzzzz();
  }
  function zzzzz() {
    throw new goog.debug.Error('testing');
  }

  var stack = null, message = null;
  try {
    xxxxx();
  } catch (e) {
    message = e.message;
    stack = e.stack.split('\n');
  }

  assertEquals('Message property should be set', 'testing', message);

  expectedFailures.expectFailureFor(
      goog.userAgent.IE ||
      goog.userAgent.product.SAFARI || (
          goog.userAgent.product.CHROME &&
          !goog.userAgent.isVersion(532)),
      'error.stack is not widely supported');

  try {
    if (goog.userAgent.product.FIREFOX &&
        goog.userAgent.isVersion('2.0')) {
      // Firefox 4 and greater does not have the first line that says
      // 'Error'. So we insert a dummy line to simplify the test.
      stack.splice(0, 0, 'Error');
    }

   if (Error.captureStackTrace) {
      // captureStackTrace removes extra junk.
      assertContains('1st line of stack should have "Error"', 'Error', stack[0]);
      assertContains('2nd line of stack should have "zzzzz"', 'zzzzz', stack[1]);
      assertContains('3rd line of stack should have "yyyyy"', 'yyyyy', stack[2]);
      assertContains('4th line of stack should have "xxxxx"', 'xxxxx', stack[3]);
   } else {
      assertContains('1st line of stack should have "Error"', 'Error', stack[0]);
      // 2nd line is slightly different in firefox/chrome
      assertContains('3rd line of stack should have "zzzzz"', 'zzzzz', stack[2]);
      assertContains('4th line of stack should have "yyyyy"', 'yyyyy', stack[3]);
      assertContains('5th line of stack should have "xxxxx"', 'xxxxx', stack[4]);
    }
  } catch (e) {
    expectedFailures.handleException(e);
  }
}

function testInheriting() {
  function MyError() {
    goog.debug.Error.call(this);
  }
  goog.inherits(MyError, goog.debug.Error);
  MyError.prototype.message = 'My custom error';

  var message = null;
  try {
    throw new MyError();
  } catch (e) {
    message = e.message;
  }
  assertEquals('My custom error', message);
}

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