sku-pg
Version:
Skulpt is a Javascript implementation of Python 2.x. Python that runs in your browser!
139 lines (122 loc) • 3.21 kB
HTML
<html>
<!--
Copyright 2007 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.enhanceError</title>
<script src="../base.js"></script>
<script>
goog.require('goog.debug');
goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>
var THROW_STRING = 1;
var THROW_NPE = 2;
var THROW_ERROR = 3;
var THROW_ENHANCED_ERROR = 4;
var THROW_ENHANCED_STRING = 5;
if (typeof debug == 'undefined') {
function debug(str) {
if (window.console) window.console.log(str);
}
}
function testEnhanceError() {
// Tests are like this:
// [test num, expect something in the stack, expect an extra message]
var tests = [
[THROW_STRING],
[THROW_NPE],
[THROW_ERROR],
[THROW_ENHANCED_ERROR, 'throwEnhancedError', 'an enhanced error'],
[THROW_ENHANCED_STRING, 'throwEnhancedString']
];
for (var i = 0; i < tests.length; ++i) {
var test = tests[i];
var testNum = test[0];
var testInStack = test[1];
var testExtraMessage = test[2] || null;
try {
foo(testNum);
} catch (e) {
debug(goog.debug.expose(e));
var s = e.stack.split('\n');
for (var j = 0; j < s.length; ++j) {
debug(s[j]);
}
// 'baz' is always in the stack
assertTrue('stack should contain "baz"',
e.stack.indexOf('baz') != -1);
if (testInStack) {
assertTrue('stack should contain "' + testInStack + '"',
e.stack.indexOf(testInStack) != -1);
}
if (testExtraMessage) {
// 2 messages
assertTrue('message0 should contain "' + testExtraMessage + '"',
e.message0.indexOf(testExtraMessage) != -1);
assertTrue('message1 should contain "message from baz"',
e.message1.indexOf('message from baz') != -1);
} else {
// 1 message
assertTrue('message0 should contain "message from baz"',
e.message0.indexOf('message from baz') != -1);
}
continue;
}
fail('expected to catch an exception');
}
}
function foo(testNum) {
bar(testNum);
}
function bar(testNum) {
baz(testNum);
}
function baz(testNum) {
try {
switch (testNum) {
case THROW_STRING:
throwString();
break;
case THROW_NPE:
throwNpe();
break;
case THROW_ERROR:
throwError();
break;
case THROW_ENHANCED_ERROR:
throwEnhancedError();
break;
case THROW_ENHANCED_STRING:
throwEnhancedString();
break;
}
} catch (e) {
throw goog.debug.enhanceError(e, 'message from baz');
}
}
function throwString() {
throw 'a string';
}
function throwNpe() {
var nada = null;
nada.noSuchFunction();
}
function throwError() {
throw Error('an error');
}
function throwEnhancedError() {
throw goog.debug.enhanceError(Error('dang!'), 'an enhanced error');
}
function throwEnhancedString() {
throw goog.debug.enhanceError('oh nos!');
}
</script>
</body>
</html>