Created
May 5, 2016 07:08
-
-
Save slugbyte/c5b0e8a2251deff8f664771b307669f8 to your computer and use it in GitHub Desktop.
test output of the node assert modules methods against various data types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var assert = require('assert'); | |
| var logAssert = function(method, expected, result){ | |
| if (typeof(expected) != 'object'){ | |
| var paramString = '' + expected + ' ' + result; | |
| process.stdout.write(' ' + paramString); | |
| } else { | |
| process.stdout.write(' '); | |
| process.stdout.write(JSON.stringify(expected) + ' ' + JSON.stringify(result)); | |
| } | |
| try { | |
| assert[method](expected, result); | |
| console.log(' ---> success'); | |
| } catch (e) { | |
| console.log(' ---> fail'); | |
| } | |
| }; | |
| var assertMethods = [ | |
| 'equal', | |
| 'notEqual', | |
| 'deepEqual', | |
| 'notDeepEqual', | |
| 'strictEqual', | |
| 'notStrictEqual' | |
| ]; | |
| var data = [ | |
| [true, true], | |
| [true, false], | |
| [1, 1], | |
| [1, 0], | |
| ['abc', 'abc'], | |
| ['abc', 'xxx'], | |
| [{}, {}], | |
| [{}, {a: 0}], | |
| [[0], [0]], | |
| [[0], [1]], | |
| [NaN, NaN], | |
| [0, NaN], | |
| [0, null], | |
| [null, null], | |
| [Infinity, Infinity], | |
| [0, Infinity] | |
| ]; | |
| assertMethods.forEach(function(method){ | |
| console.log('assert.' + method); | |
| data.forEach(function(args){ | |
| logAssert(method, args[0], args[1]); | |
| }); | |
| console.log(); | |
| }); | |
| console.log('assert.fail'); | |
| data.forEach(function(param){ | |
| process.stdout.write(' ' + JSON.stringify(param[0]) + ' ' + JSON.stringify(param[1]) + ' null ' + ' =='); | |
| try { | |
| assert.fail(param[0], param[1], null, '=='); | |
| console.log(' ---> success'); | |
| } catch (e) { | |
| console.log(' ---> fail'); | |
| } | |
| }); | |
| console.log('\nassert'); | |
| data = [ | |
| 'string', | |
| 0, | |
| [], | |
| {}, | |
| true, | |
| false, | |
| null, | |
| NaN, | |
| Infinity | |
| ]; | |
| data.forEach(function(param){ | |
| if (typeof(param) == 'object'){ | |
| process.stdout.write(' ' + JSON.stringify(param)); | |
| } else { | |
| process.stdout.write(' ' + param); | |
| } | |
| try { | |
| assert(param); | |
| console.log(' ---> success'); | |
| } catch (e) { | |
| console.log(' ---> fail'); | |
| } | |
| }); | |
| console.log('\nifError'); | |
| data.forEach(function(param){ | |
| if (typeof(param) == 'object'){ | |
| process.stdout.write(' ' + JSON.stringify(param)); | |
| } else { | |
| process.stdout.write(' ' + param); | |
| } | |
| try { | |
| assert.ifError(param); | |
| console.log(' ---> success'); | |
| } catch (e) { | |
| console.log(' ---> fail'); | |
| } | |
| }); | |
| console.log('\nthrows'); | |
| process.stdout.write(' block-that-throws-error'); | |
| try { | |
| assert.throws(assert.fail); | |
| console.log(' ---> success'); | |
| } catch (e) { | |
| console.log(' ---> fail'); | |
| } | |
| process.stdout.write(' block-that-dosnt-throws-error'); | |
| try { | |
| assert.throws(function(){}); | |
| console.log(' ---> success'); | |
| } catch (e) { | |
| console.log(' ---> fail'); | |
| } | |
| console.log('\ndoesNotThow'); | |
| process.stdout.write(' block-that-throws-error'); | |
| try { | |
| assert.doesNotThrow(assert.fail); | |
| console.log(' ---> success'); | |
| } catch (e) { | |
| console.log(' ---> fail'); | |
| } | |
| process.stdout.write(' block-that-dosnt-throws-error'); | |
| try { | |
| assert.doesNotThrow(function(){}); | |
| console.log(' ---> success'); | |
| } catch (e) { | |
| console.log(' ---> fail'); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment