power-assert, mechanism and philosophy
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

power-assert, mechanism and philosophy

  • 0 views
Uploaded on

power-assert, mechanism and philosophy

power-assert, mechanism and philosophy

Takuto Wada (a.k.a id:t-wada or @t_wada)
Nov 15, 2014 @ Nodefest Tokyo 2014

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment

Views

Total Views
0
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
0
Comments
0
Likes
3

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. power-assert mechanism and philosophy Takuto Wada (a.k.a id:t-wada or @t_wada) Nov 15, 2014 @ Nodefest Tokyo 2014
  • 2. Takuto Wada id: t-wada @t_wada github: twada
  • 3. agenda •what is power-assert? •mechanism •design philosophy
  • 4. state of JavaScript testing testing framework remote test runner environment assertion library mocking library Plenty of alternatives...
  • 5. 33 Methods!? too much to learn...
  • 6. Something is wrong with arms race among assertion libraries... What if the `assert` is informative enough?
  • 7. power-assert https://github.com/twada/power-assert
  • 8. 1. npm install --save-dev power-assert intelli-espower-loader 2. add "directories" to package.json "directories": { "test": "test" }, 3. change var assert = require('assert'); to var assert = require('power-assert'); 4. mocha --require intelli-espower-loader Just 4 steps
  • 9. assert(typeof item.id === 'strong') | | | | | | | false | | "foo" | Item{id:"foo",name:"bar"} "string" --- [string] 'strong' +++ [string] typeof item.id @@ -1,6 +1,6 @@ str -o +i ng You get this work unobtrusively, fail noisily
  • 10. assert(actual) assert.deepEqual(actual,expected) assert.notDeepEqual(actual,expected) You don t need to memorize assertion APIs any more. Just write simple assert
  • 11. arms race among assertion APIs enrich test vocabulary...resulted in overkill power-assert is a Renaissance of code No more assertion war
  • 12. So, why don t you run with tests?
  • 13. agenda •what is power-assert? •mechanism •design philosophy
  • 14. var assert = require('power-assert'); function Item(id, name) { this.id = id; this.name = name; } describe('Item', function () { it('has string id', function () { var item = new Item(1000, 'bar'); assert(assert._expr(assert._capt(assert._capt(typeof assert._capt(assert._capt(item, 'arguments/0/left/argument/object').id, 'arguments/0/left/argument'), 'arguments/0/left') === 'string', 'arguments/0'), { content: 'assert(typeof item.id === 'string')', filepath: 'test/item_test.js', line: 11 })); }); }); var assert = require('power-assert'); function Item (id, name) { this.id = id; this.name = name; } describe('Item', function () { it('has string id', function () { var item = new Item(1000, 'bar'); assert(typeof item.id === 'string'); }); }); But How? test code transpiled to
  • 15. https://speakerdeck.com/michaelficarra/spidermonkey-parser-api-a-standard-for-structured-js-representations new C(1 + a)
  • 16. https://speakerdeck.com/constellation/escodegen-and-esmangle-using-mozilla-javascript-ast-as-an-ir Demo
  • 17. https://speakerdeck.com/constellation/escodegen-and-esmangle-using-mozilla-javascript-ast-as-an-ir Demo
  • 18. 3 essential AST tools •esprima •escodegen •estraverse
  • 19. espoweresprima code AST escodegen AST AST AST code code code espower-source grunt-espower / gulp-espower / espowerify / espower-loader / espower-coffee / espower-cli
  • 20. agenda •what is power-assert? •mechanism •design philosophy
  • 21. The UNIX Philosophy https://github.com/substack/nodefest-2012
  • 22. Make each program do one thing well https://github.com/substack/nodefest-2012
  • 23. Rule of Composition: Design programs to be connected with other programs. Rule of Modularity: Write simple parts connected by clean interfaces.
  • 24. substack pattern module.exports = function(arg) { // do one thing well };
  • 25. First version (4) •power-assert •empower •espower •grunt-espower Currently (15) • power-assert • empower • power-assert-formatter • espower • espower-source • type-name • stringifier • espurify • escallmatch • grunt-espower • gulp-espower • espowerify • espower-loader • espower-coffee • espower-cli modularize
  • 26. Your sensibilities about simplicity being equal to ease of use are wrong. Easy is not simple Simple: objective Easy: relative
  • 27. Easy •power-assert •grunt-espower •gulp-espower •espowerify •espower-loader •espower-cli •espower-coffee Simple •espower •espower-source •empower separate `simple` from `easy`
  • 28. wrap-up •No more assertion war •Use JS to modify JS •Separate simple from easy •Make things small and composable
  • 29. power-assert https://github.com/twada/power-assert Thank you!