Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Throwing errors examples

  • Loading branch information...
commit 823474738dbf7dea92ee2b2ad542a2c8c9c30263 1 parent 61cc972
Nicholas authored
206 examples/throwing-errors-example.htm
... ... @@ -0,0 +1,206 @@
  1 +<!DOCTYPE HTML>
  2 +<html>
  3 +<head>
  4 +<meta http-equiv="content-type" content="text/html; charset=utf-8">
  5 +<title>Throwing Errors Example</title>
  6 +
  7 +
  8 +<link type="text/css" rel="stylesheet" href="http://yui.yahooapis.com/3.2.0/build/cssfonts/fonts-min.css" />
  9 +<script type="text/javascript" src="http://yui.yahooapis.com/3.2.0/build/yui/yui-min.js"></script>
  10 +<script type="text/javascript" src="../javascript/build/yuitest/yuitest-min.js"></script>
  11 +<style type="text/css">
  12 +.yui3-skin-sam .yui3-console-entry-pass .yui3-console-entry-cat {
  13 + background-color: green;
  14 + color: #fff;
  15 +}
  16 +
  17 +.yui3-skin-sam .yui3-console-entry-fail .yui3-console-entry-cat {
  18 + background-color: red;
  19 + color: #fff;
  20 +}
  21 +
  22 +.yui3-skin-sam .yui3-console-entry-ignore .yui3-console-entry-cat {
  23 + background-color: #666;
  24 +}
  25 +
  26 +</style>
  27 +<body class="yui3-skin-sam yui-skin-sam">
  28 +
  29 +<h1>Throwing Errors Example</h1>
  30 +
  31 +<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>
  32 +<div id="testLogger"></div>
  33 +<script type="text/javascript">
  34 +YUI().use("console", function (Y) {
  35 +
  36 + var throwErrorsTestCase = new YUITest.TestCase({
  37 +
  38 + //the name of the test case - if not provided, one is automatically generated
  39 + name: "Throwing Errors Tests",
  40 +
  41 + testGenericError : function() {
  42 +
  43 + /*
  44 + * You can specify a constructor for an error. All native errors
  45 + * inherit from Error, so this effectively tests for all native
  46 + * error types.
  47 + */
  48 + YUITest.Assert.throwsError(Error, function(){
  49 + throw new Error("Generic error");
  50 + });
  51 + },
  52 +
  53 + testStringError : function() {
  54 +
  55 + /*
  56 + * You can specify an error message, in which case the test passes only
  57 + * if the error thrown matches the given message.
  58 + */
  59 + YUITest.Assert.throwsError("I'm a specific error message.", function(){
  60 + throw new Error("I'm a specific error message.");
  61 + });
  62 + },
  63 +
  64 + testStringError2 : function() {
  65 +
  66 + //throw a specific error message - this will fail because the message isn't expected
  67 + YUITest.Assert.throwsError("I'm a specific error message.", function(){
  68 + throw new Error("I'm a specific error message, but a wrong one.");
  69 + });
  70 + },
  71 +
  72 + testObjectError : function() {
  73 +
  74 + /*
  75 + * You can also specify an error object, in which case the test passes only
  76 + * if the error thrown is on the same type and has the same message.
  77 + */
  78 + YUITest.Assert.throwsError(new TypeError("Number expected."), function(){
  79 + throw new TypeError("Number expected.");
  80 + });
  81 + },
  82 +
  83 + testObjectError2 : function() {
  84 +
  85 + //throw a specific error and message - this will fail because the type doesn't match
  86 + YUITest.Assert.throwsError(new Error("Number expected."), function(){
  87 + throw new TypeError("Number expected.");
  88 + });
  89 + },
  90 +
  91 + testObjectError3 : function() {
  92 +
  93 + //throw a specific error and message - this will fail because the message doesn't match
  94 + YUITest.Assert.throwsError(new TypeError("Number expected."), function(){
  95 + throw new TypeError("String expected.");
  96 + });
  97 + }
  98 +
  99 + });
  100 +
  101 + //create the console
  102 + var r = new Y.Console({
  103 + newestOnTop : false,
  104 + style: 'block' // to anchor in the example content
  105 + });
  106 +
  107 + r.render('#testLogger');
  108 +
  109 + var TestRunner = YUITest.TestRunner;
  110 +
  111 + TestRunner.add(throwErrorsTestCase);
  112 +
  113 +
  114 + //function to handle events generated by the testrunner
  115 + function logEvent(event){
  116 +
  117 + //data variables
  118 + var message = "",
  119 + messageType = "";
  120 +
  121 + switch(event.type){
  122 + case TestRunner.BEGIN_EVENT:
  123 + message = "Testing began at " + (new Date()).toString() + ".";
  124 + messageType = "info";
  125 + break;
  126 +
  127 + case TestRunner.COMPLETE_EVENT:
  128 + message = Y.substitute("Testing completed at " +
  129 + (new Date()).toString() + ".\n" +
  130 + "Passed:{passed} Failed:{failed} " +
  131 + "Total:{total} ({ignored} ignored)",
  132 + event.results);
  133 + messageType = "info";
  134 + break;
  135 +
  136 + case TestRunner.TEST_FAIL_EVENT:
  137 + message = event.testName + ": failed.\n" + event.error.getMessage();
  138 + messageType = "fail";
  139 + break;
  140 +
  141 + case TestRunner.TEST_IGNORE_EVENT:
  142 + message = event.testName + ": ignored.";
  143 + messageType = "ignore";
  144 + break;
  145 +
  146 + case TestRunner.TEST_PASS_EVENT:
  147 + message = event.testName + ": passed.";
  148 + messageType = "pass";
  149 + break;
  150 +
  151 + case TestRunner.TEST_SUITE_BEGIN_EVENT:
  152 + message = "Test suite \"" + event.testSuite.name + "\" started.";
  153 + messageType = "info";
  154 + break;
  155 +
  156 + case TestRunner.TEST_SUITE_COMPLETE_EVENT:
  157 + message = Y.substitute("Test suite \"" +
  158 + event.testSuite.name + "\" completed" + ".\n" +
  159 + "Passed:{passed} Failed:{failed} " +
  160 + "Total:{total} ({ignored} ignored)",
  161 + event.results);
  162 + messageType = "info";
  163 + break;
  164 +
  165 + case TestRunner.TEST_CASE_BEGIN_EVENT:
  166 + message = "Test case \"" + event.testCase.name + "\" started.";
  167 + messageType = "info";
  168 + break;
  169 +
  170 + case TestRunner.TEST_CASE_COMPLETE_EVENT:
  171 + message = Y.substitute("Test case \"" +
  172 + event.testCase.name + "\" completed.\n" +
  173 + "Passed:{passed} Failed:{failed} " +
  174 + "Total:{total} ({ignored} ignored)",
  175 + event.results);
  176 + messageType = "info";
  177 + break;
  178 + default:
  179 + message = "Unexpected event " + event.type;
  180 + message = "info";
  181 + }
  182 +
  183 + //only log if required
  184 + Y.log(message, messageType, "TestRunner");
  185 + }
  186 +
  187 + //listen for events to publish to the logger
  188 + TestRunner.attach(TestRunner.BEGIN_EVENT, logEvent);
  189 + TestRunner.attach(TestRunner.COMPLETE_EVENT, logEvent);
  190 + TestRunner.attach(TestRunner.TEST_CASE_BEGIN_EVENT, logEvent);
  191 + TestRunner.attach(TestRunner.TEST_CASE_COMPLETE_EVENT, logEvent);
  192 + TestRunner.attach(TestRunner.TEST_SUITE_BEGIN_EVENT, logEvent);
  193 + TestRunner.attach(TestRunner.TEST_SUITE_COMPLETE_EVENT, logEvent);
  194 + TestRunner.attach(TestRunner.TEST_PASS_EVENT, logEvent);
  195 + TestRunner.attach(TestRunner.TEST_FAIL_EVENT, logEvent);
  196 + TestRunner.attach(TestRunner.TEST_IGNORE_EVENT, logEvent);
  197 +
  198 +
  199 + //run the tests
  200 + TestRunner.run();
  201 +
  202 +});
  203 +</script>
  204 +
  205 +</body>
  206 +</html>
241 examples/throwing-errors.htm
... ... @@ -0,0 +1,241 @@
  1 +<!DOCTYPE HTML>
  2 +<html>
  3 +<head>
  4 +<meta http-equiv="content-type" content="text/html; charset=utf-8">
  5 +<title>Array Tests Example</title>
  6 +
  7 +
  8 +<link type="text/css" rel="stylesheet" href="http://yui.yahooapis.com/3.2.0/build/cssfonts/fonts-min.css" />
  9 +
  10 +<body class="yui3-skin-sam yui-skin-sam">
  11 +
  12 +<h1>Throwing Errors Example</h1>
  13 +
  14 +<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>
  15 +<p><a href="throwing-errors-example.htm">See the example in action</a></p>
  16 +
  17 +<h2>Creating the test case and first method</h2>
  18 +
  19 +<p>The first method shows simple error detection with a test that simply throws an error:</p>
  20 +<pre><code>var throwErrorsTestCase = new YUITest.TestCase({
  21 +
  22 + //the name of the test case - if not provided, one is automatically generated
  23 + name: "Throwing Errors Tests",
  24 +
  25 + ...
  26 +
  27 + testGenericError : function() {
  28 +
  29 + /*
  30 + * You can specify a constructor for an error. All native errors
  31 + * inherit from Error, so this effectively tests for all native
  32 + * error types.
  33 + */
  34 + YUITest.Assert.throwsError(Error, function(){
  35 + throw new Error("Generic error");
  36 + });
  37 + },
  38 +
  39 + ...
  40 +});
  41 +</code></pre>
  42 +<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
  43 + two methods, <code>testStringError()</code> and <code>testStringError2()</code> are specified as throwing an error with a specific
  44 + message (&quot;I'm a specific error message.&quot;):</p>
  45 +<pre><code>var throwErrorsTestCase = new YUITest.TestCase({
  46 +
  47 + //the name of the test case - if not provided, one is automatically generated
  48 + name: "Throwing Errors Tests",
  49 +
  50 + ...
  51 +
  52 + testStringError : function() {
  53 +
  54 + /*
  55 + * You can specify an error message, in which case the test passes only
  56 + * if the error thrown matches the given message.
  57 + */
  58 + YUITest.Assert.throwsError("I'm a specific error message.", function(){
  59 + throw new Error("I'm a specific error message.");
  60 + });
  61 + },
  62 +
  63 + testStringError2 : function() {
  64 +
  65 + //throw a specific error message - this will fail because the message isn't expected
  66 + YUITest.Assert.throwsError("I'm a specific error message.", function(){
  67 + throw new Error("I'm a specific error message, but a wrong one.");
  68 + });
  69 + },
  70 +
  71 +
  72 + ...
  73 +});
  74 +</code></pre>
  75 +<p>The <code>testStringError()</code> method will pass when executed because the error message matches up exactly with the one
  76 + specified using <code>YUITest.Assert.throwsError()</code>. The <code>testStringError2()</code> method, however, will fail because its
  77 + error message is different from the one specified.</p>
  78 +<p>To be more specific, <code>testObjectError()</code>, <code>testObjectError2()</code>, and <code>testObjectError3()</code>,
  79 + specified an error type (<code>TypeError</code>) and an error messsage (&quot;Number expected.&quot;):</p>
  80 +<pre><code>var throwErrorsTestCase = new YUITest.TestCase({
  81 +
  82 + //the name of the test case - if not provided, one is automatically generated
  83 + name: "Throwing Errors Tests",
  84 +
  85 + ...
  86 +
  87 + testObjectError : function() {
  88 +
  89 + /*
  90 + * You can also specify an error object, in which case the test passes only
  91 + * if the error thrown is on the same type and has the same message.
  92 + */
  93 + YUITest.Assert.throwsError(new TypeError("Number expected."), function(){
  94 + throw new TypeError("Number expected.");
  95 + });
  96 + },
  97 +
  98 + testObjectError2 : function() {
  99 +
  100 + //throw a specific error and message - this will fail because the type doesn't match
  101 + YUITest.Assert.throwsError(new Error("Number expected."), function(){
  102 + throw new TypeError("Number expected.");
  103 + });
  104 + },
  105 +
  106 + testObjectError3 : function() {
  107 +
  108 + //throw a specific error and message - this will fail because the message doesn't match
  109 + YUITest.Assert.throwsError(new TypeError("Number expected."), function(){
  110 + throw new TypeError("String expected.");
  111 + });
  112 + }
  113 +
  114 +});
  115 +</code></pre>
  116 +<p>Of the these three methods, only <code>testObjectError()</code> will pass because it's the only one that throws a <code>TypeError</code>
  117 + object with the message, &quot;Number expected.&quot; The <code>testObjectError2()</code> method will fail because the type of error
  118 + being thrown (<code>Error</code>) is different from the expected type (<code>TypeError</code>), as specified using <code>YUITest.Assert.throwsError()</code>.
  119 + The last method, <code>testObjectError3()</code>, also fails. Though it throws the right type of error, the error message
  120 + doesn't match the one that was specified.</p>
  121 +
  122 +<h2>Setting up logging</h2>
  123 +
  124 +<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>
  125 +
  126 +<pre><code>//function to handle events generated by the testrunner
  127 +var TestRunner = YUITest.TestRunner;
  128 +
  129 +function logEvent(event){
  130 +
  131 + //data variables
  132 + var message = "",
  133 + messageType = "";
  134 +
  135 + switch(event.type){
  136 + case TestRunner.BEGIN_EVENT:
  137 + message = "Testing began at " + (new Date()).toString() + ".";
  138 + messageType = "info";
  139 + break;
  140 +
  141 + case TestRunner.COMPLETE_EVENT:
  142 + message = Y.substitute("Testing completed at " +
  143 + (new Date()).toString() + ".\n" +
  144 + "Passed:{passed} Failed:{failed} " +
  145 + "Total:{total} ({ignored} ignored)",
  146 + event.results);
  147 + messageType = "info";
  148 + break;
  149 +
  150 + case TestRunner.TEST_FAIL_EVENT:
  151 + message = event.testName + ": failed.\n" + event.error.getMessage();
  152 + messageType = "fail";
  153 + break;
  154 +
  155 + case TestRunner.TEST_IGNORE_EVENT:
  156 + message = event.testName + ": ignored.";
  157 + messageType = "ignore";
  158 + break;
  159 +
  160 + case TestRunner.TEST_PASS_EVENT:
  161 + message = event.testName + ": passed.";
  162 + messageType = "pass";
  163 + break;
  164 +
  165 + case TestRunner.TEST_SUITE_BEGIN_EVENT:
  166 + message = "Test suite \"" + event.testSuite.name + "\" started.";
  167 + messageType = "info";
  168 + break;
  169 +
  170 + case TestRunner.TEST_SUITE_COMPLETE_EVENT:
  171 + message = Y.substitute("Test suite \"" +
  172 + event.testSuite.name + "\" completed" + ".\n" +
  173 + "Passed:{passed} Failed:{failed} " +
  174 + "Total:{total} ({ignored} ignored)",
  175 + event.results);
  176 + messageType = "info";
  177 + break;
  178 +
  179 + case TestRunner.TEST_CASE_BEGIN_EVENT:
  180 + message = "Test case \"" + event.testCase.name + "\" started.";
  181 + messageType = "info";
  182 + break;
  183 +
  184 + case TestRunner.TEST_CASE_COMPLETE_EVENT:
  185 + message = Y.substitute("Test case \"" +
  186 + event.testCase.name + "\" completed.\n" +
  187 + "Passed:{passed} Failed:{failed} " +
  188 + "Total:{total} ({ignored} ignored)",
  189 + event.results);
  190 + messageType = "info";
  191 + break;
  192 + default:
  193 + message = "Unexpected event " + event.type;
  194 + message = "info";
  195 + }
  196 +
  197 + //only log if required
  198 + Y.log(message, messageType, "TestRunner");
  199 +}
  200 +
  201 +//listen for events to publish to the logger
  202 +TestRunner.attach(TestRunner.BEGIN_EVENT, logEvent);
  203 +TestRunner.attach(TestRunner.COMPLETE_EVENT, logEvent);
  204 +TestRunner.attach(TestRunner.TEST_CASE_BEGIN_EVENT, logEvent);
  205 +TestRunner.attach(TestRunner.TEST_CASE_COMPLETE_EVENT, logEvent);
  206 +TestRunner.attach(TestRunner.TEST_SUITE_BEGIN_EVENT, logEvent);
  207 +TestRunner.attach(TestRunner.TEST_SUITE_COMPLETE_EVENT, logEvent);
  208 +TestRunner.attach(TestRunner.TEST_PASS_EVENT, logEvent);
  209 +TestRunner.attach(TestRunner.TEST_FAIL_EVENT, logEvent);
  210 +TestRunner.attach(TestRunner.TEST_IGNORE_EVENT, logEvent);
  211 +</code></pre>
  212 +
  213 +<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>
  214 +
  215 +<h2>Running the tests</h2>
  216 +
  217 +<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
  218 + components have been loaded:</p>
  219 +
  220 +<pre><code>//create the console
  221 +var r = new Y.Console({
  222 + verbose : true,
  223 + newestOnTop : false
  224 +});
  225 +
  226 +r.render('#testLogger');
  227 +
  228 +TestRunner.add(throwErrorsTestCase);
  229 +
  230 +//run the tests
  231 +TestRunner.run();
  232 +</code></pre>
  233 +
  234 +<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
  235 + 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
  236 + <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>,
  237 + this example only adds one for simplicity). The very last step is to call <code>run()</code>, which begins executing the tests in its
  238 + queue and displays the results in the <code>Y.Console</code>.</p>
  239 +
  240 +</body>
  241 +</html>

0 comments on commit 8234747

Please sign in to comment.
Something went wrong with that request. Please try again.