Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Throwing errors examples

  • Loading branch information...
commit 823474738dbf7dea92ee2b2ad542a2c8c9c30263 1 parent 61cc972
Nicholas authored
Showing with 447 additions and 0 deletions.
  1. +206 −0 examples/throwing-errors-example.htm
  2. +241 −0 examples/throwing-errors.htm
View
206 examples/throwing-errors-example.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>
View
241 examples/throwing-errors.htm
@@ -0,0 +1,241 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+<meta http-equiv="content-type" content="text/html; charset=utf-8">
+<title>Array Tests Example</title>
+
+
+<link type="text/css" rel="stylesheet" href="http://yui.yahooapis.com/3.2.0/build/cssfonts/fonts-min.css" />
+
+<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>
+<p><a href="throwing-errors-example.htm">See the example in action</a></p>
+
+<h2>Creating the test case and first method</h2>
+
+<p>The first method shows simple error detection with a test that simply throws an error:</p>
+<pre><code>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");
+ });
+ },
+
+ ...
+});
+</code></pre>
+<p>The fact that this method throws an error that is of type <code>Error</code> is enough to cause it to pass (the message doesn't matter). The next
+ two methods, <code>testStringError()</code> and <code>testStringError2()</code> are specified as throwing an error with a specific
+ message (&quot;I'm a specific error message.&quot;):</p>
+<pre><code>var throwErrorsTestCase = new YUITest.TestCase({
+
+ //the name of the test case - if not provided, one is automatically generated
+ name: "Throwing Errors Tests",
+
+ ...
+
+ 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.");
+ });
+ },
+
+
+ ...
+});
+</code></pre>
+<p>The <code>testStringError()</code> method will pass when executed because the error message matches up exactly with the one
+ specified using <code>YUITest.Assert.throwsError()</code>. The <code>testStringError2()</code> method, however, will fail because its
+ error message is different from the one specified.</p>
+<p>To be more specific, <code>testObjectError()</code>, <code>testObjectError2()</code>, and <code>testObjectError3()</code>,
+ specified an error type (<code>TypeError</code>) and an error messsage (&quot;Number expected.&quot;):</p>
+<pre><code>var throwErrorsTestCase = new YUITest.TestCase({
+
+ //the name of the test case - if not provided, one is automatically generated
+ name: "Throwing Errors Tests",
+
+ ...
+
+ 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.");
+ });
+ }
+
+});
+</code></pre>
+<p>Of the these three methods, only <code>testObjectError()</code> will pass because it's the only one that throws a <code>TypeError</code>
+ object with the message, &quot;Number expected.&quot; The <code>testObjectError2()</code> method will fail because the type of error
+ being thrown (<code>Error</code>) is different from the expected type (<code>TypeError</code>), as specified using <code>YUITest.Assert.throwsError()</code>.
+ The last method, <code>testObjectError3()</code>, also fails. Though it throws the right type of error, the error message
+ doesn't match the one that was specified.</p>
+
+<h2>Setting up logging</h2>
+
+<p>Since the standalone YUI Test library isn't a graphical library, you'll need to use something to visualize the results. This example uses a YUI 3 <code>Console</code> object. To output the appropriate information into the console, a function is created to handle <code>TestRunner</code> events:</p>
+
+<pre><code>//function to handle events generated by the testrunner
+var TestRunner = YUITest.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);
+</code></pre>
+
+<p>The <code>logEvent()</code> function is used to handle all events. Based on the event, the message and the message type are determined and then a message is logged. This event handler is assigned to the <code>TestRunner</code> events by using the <code>attach()</code> method.</p>
+
+<h2>Running the tests</h2>
+
+<p>With all of the tests defined, the last step is to run them. This initialization is assigned to take place when all of the YUI
+ components have been loaded:</p>
+
+<pre><code>//create the console
+var r = new Y.Console({
+ verbose : true,
+ newestOnTop : false
+});
+
+r.render('#testLogger');
+
+TestRunner.add(throwErrorsTestCase);
+
+//run the tests
+TestRunner.run();
+</code></pre>
+
+<p>Before running the tests, it's necessary to create a <code>Y.Console</code> object to display the results (otherwise the tests would run
+ but you wouldn't see the results). After that, the <code>YUITest.TestRunner</code> is loaded with the <code>YUITest.TestSuite</code> object by calling
+ <code>add()</code> (any number of <code>YUITest.TestCase</code> and <code>YUITest.TestSuite</code> objects can be added to a <code>YUITest.TestRunner</code>,
+ this example only adds one for simplicity). The very last step is to call <code>run()</code>, which begins executing the tests in its
+ queue and displays the results in the <code>Y.Console</code>.</p>
+
+</body>
+</html>
Please sign in to comment.
Something went wrong with that request. Please try again.