Permalink
Browse files

Throwing errors examples

  • Loading branch information...
Nicholas
Nicholas committed Oct 28, 2010
1 parent 61cc972 commit 823474738dbf7dea92ee2b2ad542a2c8c9c30263
Showing with 447 additions and 0 deletions.
  1. +206 −0 examples/throwing-errors-example.htm
  2. +241 −0 examples/throwing-errors.htm
@@ -0,0 +1,206 @@
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Throwing Errors Example</title>
<link type="text/css" rel="stylesheet" href="http://yui.yahooapis.com/3.2.0/build/cssfonts/fonts-min.css" />
<script type="text/javascript" src="http://yui.yahooapis.com/3.2.0/build/yui/yui-min.js"></script>
<script type="text/javascript" src="../javascript/build/yuitest/yuitest-min.js"></script>
<style type="text/css">
.yui3-skin-sam .yui3-console-entry-pass .yui3-console-entry-cat {
background-color: green;
color: #fff;
}
.yui3-skin-sam .yui3-console-entry-fail .yui3-console-entry-cat {
background-color: red;
color: #fff;
}
.yui3-skin-sam .yui3-console-entry-ignore .yui3-console-entry-cat {
background-color: #666;
}
</style>
<body class="yui3-skin-sam yui-skin-sam">
<h1>Throwing Errors Example</h1>
<p>This example shows how to use the <code>Assert.throwsError()</code> method to detect when certain functions will throw errors. This functionality replaces the <code>_should.error</code> options in previous YUI Test versions.</p>
<div id="testLogger"></div>
<script type="text/javascript">
YUI().use("console", function (Y) {
var throwErrorsTestCase = new YUITest.TestCase({
//the name of the test case - if not provided, one is automatically generated
name: "Throwing Errors Tests",
testGenericError : function() {
/*
* You can specify a constructor for an error. All native errors
* inherit from Error, so this effectively tests for all native
* error types.
*/
YUITest.Assert.throwsError(Error, function(){
throw new Error("Generic error");
});
},
testStringError : function() {
/*
* You can specify an error message, in which case the test passes only
* if the error thrown matches the given message.
*/
YUITest.Assert.throwsError("I'm a specific error message.", function(){
throw new Error("I'm a specific error message.");
});
},
testStringError2 : function() {
//throw a specific error message - this will fail because the message isn't expected
YUITest.Assert.throwsError("I'm a specific error message.", function(){
throw new Error("I'm a specific error message, but a wrong one.");
});
},
testObjectError : function() {
/*
* You can also specify an error object, in which case the test passes only
* if the error thrown is on the same type and has the same message.
*/
YUITest.Assert.throwsError(new TypeError("Number expected."), function(){
throw new TypeError("Number expected.");
});
},
testObjectError2 : function() {
//throw a specific error and message - this will fail because the type doesn't match
YUITest.Assert.throwsError(new Error("Number expected."), function(){
throw new TypeError("Number expected.");
});
},
testObjectError3 : function() {
//throw a specific error and message - this will fail because the message doesn't match
YUITest.Assert.throwsError(new TypeError("Number expected."), function(){
throw new TypeError("String expected.");
});
}
});
//create the console
var r = new Y.Console({
newestOnTop : false,
style: 'block' // to anchor in the example content
});
r.render('#testLogger');
var TestRunner = YUITest.TestRunner;
TestRunner.add(throwErrorsTestCase);
//function to handle events generated by the testrunner
function logEvent(event){
//data variables
var message = "",
messageType = "";
switch(event.type){
case TestRunner.BEGIN_EVENT:
message = "Testing began at " + (new Date()).toString() + ".";
messageType = "info";
break;
case TestRunner.COMPLETE_EVENT:
message = Y.substitute("Testing completed at " +
(new Date()).toString() + ".\n" +
"Passed:{passed} Failed:{failed} " +
"Total:{total} ({ignored} ignored)",
event.results);
messageType = "info";
break;
case TestRunner.TEST_FAIL_EVENT:
message = event.testName + ": failed.\n" + event.error.getMessage();
messageType = "fail";
break;
case TestRunner.TEST_IGNORE_EVENT:
message = event.testName + ": ignored.";
messageType = "ignore";
break;
case TestRunner.TEST_PASS_EVENT:
message = event.testName + ": passed.";
messageType = "pass";
break;
case TestRunner.TEST_SUITE_BEGIN_EVENT:
message = "Test suite \"" + event.testSuite.name + "\" started.";
messageType = "info";
break;
case TestRunner.TEST_SUITE_COMPLETE_EVENT:
message = Y.substitute("Test suite \"" +
event.testSuite.name + "\" completed" + ".\n" +
"Passed:{passed} Failed:{failed} " +
"Total:{total} ({ignored} ignored)",
event.results);
messageType = "info";
break;
case TestRunner.TEST_CASE_BEGIN_EVENT:
message = "Test case \"" + event.testCase.name + "\" started.";
messageType = "info";
break;
case TestRunner.TEST_CASE_COMPLETE_EVENT:
message = Y.substitute("Test case \"" +
event.testCase.name + "\" completed.\n" +
"Passed:{passed} Failed:{failed} " +
"Total:{total} ({ignored} ignored)",
event.results);
messageType = "info";
break;
default:
message = "Unexpected event " + event.type;
message = "info";
}
//only log if required
Y.log(message, messageType, "TestRunner");
}
//listen for events to publish to the logger
TestRunner.attach(TestRunner.BEGIN_EVENT, logEvent);
TestRunner.attach(TestRunner.COMPLETE_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_CASE_BEGIN_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_CASE_COMPLETE_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_SUITE_BEGIN_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_SUITE_COMPLETE_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_PASS_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_FAIL_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_IGNORE_EVENT, logEvent);
//run the tests
TestRunner.run();
});
</script>
</body>
</html>
Oops, something went wrong.

0 comments on commit 8234747

Please sign in to comment.