From ca3bfef584bb2ae3a5da36beaeab1aa2918c28c9 Mon Sep 17 00:00:00 2001 From: "Dario Cangialosi, Coder" Date: Mon, 20 Aug 2018 00:26:11 +0200 Subject: [PATCH 1/8] Update package.json --- package.json | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index e270f5e..735239a 100644 --- a/package.json +++ b/package.json @@ -12,11 +12,9 @@ "author": "TJ Holowaychuk ", "contributors": [ "TonyHe ", - "ForbesLindesay" + "ForbesLindesay", + "Dario Cangialosi" ], - "dependencies": { - "callsite": "1.0.0" - }, "repository": { "type": "git", "url": "https://github.com/visionmedia/better-assert.git" From e3d4c87142bcae31e99b245eec0b6a24cd83eff4 Mon Sep 17 00:00:00 2001 From: "Dario Cangialosi, Coder" Date: Mon, 20 Aug 2018 00:28:07 +0200 Subject: [PATCH 2/8] Update index.js --- index.js | 42 +++++------------------------------------- 1 file changed, 5 insertions(+), 37 deletions(-) diff --git a/index.js b/index.js index fd1c9b7..62fb957 100644 --- a/index.js +++ b/index.js @@ -1,38 +1,6 @@ -/** - * Module dependencies. - */ - -var AssertionError = require('assert').AssertionError - , callsite = require('callsite') - , fs = require('fs') - -/** - * Expose `assert`. - */ - -module.exports = process.env.NO_ASSERT - ? function(){} - : assert; - -/** - * Assert the given `expr`. - */ - -function assert(expr) { - if (expr) return; - - var stack = callsite(); - var call = stack[1]; - var file = call.getFileName(); - var lineno = call.getLineNumber(); - var src = fs.readFileSync(file, 'utf8'); - var line = src.split('\n')[lineno-1]; - var src = line.match(/assert\((.*)\)/)[1]; - - var err = new AssertionError({ - message: src, - stackStartFunction: stack[0].getFunction() - }); - - throw err; +module.exports = function assert(test, +message=''){ + if(test!==true) + throw new Error('Assertion is false: '+message) } From 4d99c8b5cb7c677a1e3949fc638d03e292c7dc1a Mon Sep 17 00:00:00 2001 From: "Dario Cangialosi, Coder" Date: Mon, 20 Aug 2018 00:29:01 +0200 Subject: [PATCH 3/8] Delete Makefile --- Makefile | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 Makefile diff --git a/Makefile b/Makefile deleted file mode 100644 index 36a3ed7..0000000 --- a/Makefile +++ /dev/null @@ -1,5 +0,0 @@ - -test: - @echo "populate me" - -.PHONY: test \ No newline at end of file From 662a18b4841c5094197fe6d6ae24999ee0404d97 Mon Sep 17 00:00:00 2001 From: "Dario Cangialosi, Coder" Date: Mon, 20 Aug 2018 00:30:07 +0200 Subject: [PATCH 4/8] Update example.js --- example.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/example.js b/example.js index 688c29e..a4b3413 100644 --- a/example.js +++ b/example.js @@ -5,6 +5,6 @@ test(); function test() { var user = { name: 'tobi' }; - assert('tobi' == user.name); - assert('number' == typeof user.age); -} \ No newline at end of file + assert('tobi' == user.name, "'tobi' == user.name"); + assert('number' == typeof user.age, "'number' == typeof user.age"); +} From 747c96b2f893c52580ef15525080d12bea261fb4 Mon Sep 17 00:00:00 2001 From: "Dario Cangialosi, Coder" Date: Mon, 20 Aug 2018 00:38:24 +0200 Subject: [PATCH 5/8] adjusted tests accordingly --- example.js | 16 +++++++++++----- package-lock.json | 5 +++++ 2 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 package-lock.json diff --git a/example.js b/example.js index a4b3413..e5fe565 100644 --- a/example.js +++ b/example.js @@ -1,10 +1,16 @@ -var assert = require('./'); +var assert = require('./') -test(); +test() function test() { - var user = { name: 'tobi' }; - assert('tobi' == user.name, "'tobi' == user.name"); - assert('number' == typeof user.age, "'number' == typeof user.age"); + var user = { name: 'tobi' + // , age: 18 + } + + //assert(false) + assert('tobi' == user.name, "'tobi' == user.name") + assert('number' == typeof user.age, "'number' == typeof user.age") + + console.log('test ended.') } diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..76b741f --- /dev/null +++ b/package-lock.json @@ -0,0 +1,5 @@ +{ + "name": "better-assert", + "version": "1.0.3", + "lockfileVersion": 1 +} From 99e16ca2563995e735f9087ae570f233113d5f37 Mon Sep 17 00:00:00 2001 From: "Dario Cangialosi, Coder" Date: Mon, 20 Aug 2018 01:06:19 +0200 Subject: [PATCH 6/8] new way, with ()=> syntax. replace 'assert(' with 'assert(()=>' to upgrade. --- example.js | 8 ++++---- index.js | 14 +++++++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/example.js b/example.js index e5fe565..6a1a4ca 100644 --- a/example.js +++ b/example.js @@ -5,12 +5,12 @@ test() function test() { var user = { name: 'tobi' - // , age: 18 + // , age: 18 } - //assert(false) - assert('tobi' == user.name, "'tobi' == user.name") - assert('number' == typeof user.age, "'number' == typeof user.age") + //assert(()=>false) + assert(()=>'tobi' == user.name) + assert(()=>'number' == typeof user.age) console.log('test ended.') } diff --git a/index.js b/index.js index 62fb957..98a6964 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,18 @@ -module.exports = function assert(test, +module.exports = assert2 + +function assert2(test, message=''){ if(test!==true) throw new Error('Assertion is false: '+message) } + +module.exports = assert + +function assert(test){ + if(test()!==true) + throw new Error('Assertion false: '+test.toString().split('=>')[1]) +} + +var name='rich' +assert(()=>'rich'==name) From a190965fa229a23eef7e914b339a378da670ee13 Mon Sep 17 00:00:00 2001 From: "Dario Cangialosi, Coder" Date: Mon, 20 Aug 2018 01:50:07 +0200 Subject: [PATCH 7/8] 'debug' param for optional 'instead' debug infos. reveal() utility function to be used as 'debug' param argument. --- example.js | 12 ++++++++---- index.js | 4 ++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/example.js b/example.js index 6a1a4ca..3b5b68f 100644 --- a/example.js +++ b/example.js @@ -3,14 +3,18 @@ var assert = require('./') test() +function reveal(what, iswhat){ + return eval(`()=>'${what}=='+${JSON.stringify(iswhat)}`) +} + function test() { var user = { name: 'tobi' - // , age: 18 + //, age: 18 // comment-out this line to make an assert test fail } - //assert(()=>false) - assert(()=>'tobi' == user.name) - assert(()=>'number' == typeof user.age) + //assert( ()=>false ) + assert( ()=>user.name=='tobi', reveal('user.name',user.name) ) + assert( ()=>typeof user.age=='number', reveal('typeof user.age',typeof user.age) ) console.log('test ended.') } diff --git a/index.js b/index.js index 98a6964..2bf60df 100644 --- a/index.js +++ b/index.js @@ -9,9 +9,9 @@ message='')[1]) + throw new Error('Assertion {'+test.toString().split('=>')[1]+'} is false!'+(debug==null?'':(' instead '+debug()))) } var name='rich' From 4395598ef82781b12768e07432861bfc994d52ea Mon Sep 17 00:00:00 2001 From: "Dario Cangialosi, Coder" Date: Mon, 20 Aug 2018 10:14:05 +0200 Subject: [PATCH 8/8] better code --- example.js | 10 +++------- index.js | 29 ++++++++++++++++------------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/example.js b/example.js index 3b5b68f..5184bb8 100644 --- a/example.js +++ b/example.js @@ -1,20 +1,16 @@ -var assert = require('./') +var {assert, is} = require('./') test() -function reveal(what, iswhat){ - return eval(`()=>'${what}=='+${JSON.stringify(iswhat)}`) -} - function test() { var user = { name: 'tobi' //, age: 18 // comment-out this line to make an assert test fail } //assert( ()=>false ) - assert( ()=>user.name=='tobi', reveal('user.name',user.name) ) - assert( ()=>typeof user.age=='number', reveal('typeof user.age',typeof user.age) ) + assert( ()=>user.name=='tobi', is('user.name',user.name) ) + assert( ()=>typeof user.age=='number', is('typeof user.age',typeof user.age) ) console.log('test ended.') } diff --git a/index.js b/index.js index 2bf60df..de887c3 100644 --- a/index.js +++ b/index.js @@ -1,18 +1,21 @@ -module.exports = assert2 -function assert2(test, -message=''){ - if(test!==true) - throw new Error('Assertion is false: '+message) -} +module.exports = { assert, is } -module.exports = assert +function assert(test_function,debug_facts=null){ + if(typeof debug_facts=='function') debug_facts=debug_facts() + if(test_function()!==true){ + throw new Error('Assertion {'+ + test_function.toString().split('=>')[1]+ + '} is false!'+ + (debug_facts==null?'':(' in fact ... '+debug_facts))) + } +} -function assert(test,debug=null){ - if(test()!==true) - throw new Error('Assertion {'+test.toString().split('=>')[1]+'} is false!'+(debug==null?'':(' instead '+debug()))) +function is(what, iswhat){ + var repr=iswhat.toString() + if(typeof repr=='undefined') repr=JSON.stringify(iswhat) + return what+' is '+repr } -var name='rich' -assert(()=>'rich'==name) +var name='richard' +assert(()=>name=='richard','name is '+name)