Node.js ç¨ã®ãã¹ãã£ã³ã°ãã¬ã¼ã ã¯ã¼ã¯ Vows ãã® 2
æ¨æ¥ã®ç¶ãã§ãï¼ä»åã¯ããã¡ãã£ã¨ã ãæ¬æ ¼çã« Vows ã使ã£ã¦ã¿ã¾ãï¼
ã¨ã¯ããï¼ã©ãã«ã TDD ã身ã«ã¤ããªã人ãªã®ã§ï¼ãã£ã¡æ¹é¢ã¯å¤§ç®ã«è¦ã¦ãã ããï¼
ãµã³ãã«ã¨ã㦠read-line ã¢ã¸ã¥ã¼ã«ãä½æãã¾ãï¼
Node.js ã®å ´åï¼ãã¡ã¤ã«ããã§ããï¼ãããã¯ã¼ã¯ããã§ããï¼åºæ¬çã«ãã¼ã¿å
¥å㯠ReadableStream
ããã³ã¼ã«ããã¯ããã¾ãï¼
ã³ã¼ã«ããã¯é¢æ°ã®å½¢å¼ã¯
function(data)
ã§ï¼ã³ã¼ã«ããã¯é¢æ°ã«æ¸¡ããããã¼ã¿ã¯å¤§æµã¯ãããã¡ãªã³ã°ãããåä½ã§ã¾ã¨ãã¦ãã£ã¦ãã¾ãï¼ããã¯å¤æ°ã®è¡ãå«ãã§ãããã¨ãï¼è¡ã®éä¸ã§ã¶ã£ãåãã¦ãããã¨ãããã¾ãï¼ã§ãã§ãï¼è¡åä½ã§å¦çãããã±ã¼ã¹ã£ã¦ããããã¾ãããï¼HTTP ãªããã 㨠Node.js ã®æ¨æºã©ã¤ãã©ãªããããã解éãã¦ããã¾ããï¼ä¾ãã°ã¡ãã»ã¼ã¸ããã£ã CSV ã ã£ããã¨ãããããã£ã±ãè¡åä½ã§å¦çããããªãã¾ãï¼
ãããªãã㧠(ã©ããªããã§ï¼)ï¼ReadableStream
ããã®ãã¼ã¿ã¤ãã³ããè¡åä½ã«ãã¦ãããã¢ã¸ã¥ã¼ã«ãä½ãã¾ãï¼
ã¡ãªã¿ã«ï¼Node.js æ¬ä½ã«ã readline ã¨ããã¢ã¸ã¥ã¼ã«ãããã¾ããï¼ãã㯠REPL (Read-Eval-Print-Loop) ã®ããã« tty ããã¤ã¹ãã 1 è¡èªã¿è¾¼ãããã®ãã®ã§ï¼ä»»æã®ã¹ããªã¼ã ããè¡åä½ã«ã³ã¼ã«ããã¯ãã¦ããããã®ã§ã¯ãªãããã§ãï¼
ã¾ãã¯ä¸èº«ã®ãªããã¹ã read-line-test.js
ãç¨æãã¾ãï¼
+var vows = require('vows'), assert = require('assert'); + +vows.describe('read-line').addBatch( { +}).export(module);
軽ã id:t-wada ããã®ããããï¼
å®è¡ãã¾ãï¼
$ vows read-line-test.js undefined » (0.000s)
ä½ããªãã¦ãæããã¾ããï¼
ã§ã¯ãããã«ã³ã³ããã¹ãã追å ãã¾ãï¼
var vows = require('vows'), assert = require('assert'); vows.describe('read-line').addBatch( { + 'ãã¹ãããã' : { + } }).export(module);
æå³ãã¼ã£ï¼ã§ãæ°ã«ããªãã§å®è¡ï¼ï¼
$ vows read-line-test.js undefined » (0.000s)
ã³ã³ããã¹ãããã£ã¦ã vow ããªãã¨ä½ãåºãªãã®ãï¼
ãã¦ï¼ããããã¡ãã£ã¨ã¾ããã«èãã¾ããããï¼
TDD ã身ã«ã¤ãã¦ãªããã®ã¨ãã¦ã¯ï¼ã¾ã read-line ã® API ããèãã¦ãã¾ãããã§ããï¼ããã¯ãã£ã¨ææ
¢ãã¦ãã¹ãããèãã¦ã¿ã¾ãï¼ã£ã¦ããããã®ã¨ã³ããªã®ä¸»å½¹ã¯ Vows ã ãããï¼
ãã£ã¨ï¼read-line ã¯ä½è¡ãã¾ã¨ãã¦ã³ã¼ã«ããã¯ããã¦ãããã¼ã¿ãè¡åä½ã«ã³ã¼ã«ããã¯ãã¦ããããã®ãªã®ã§ï¼
function('abc\ndef')
ãªãã¦å¼ã³åºããï¼
function('abc')
function('def')
ã£ã¦å
·åã«ãªã£ã¦ã»ããããã§ããï¼ã¾ãã¯æåã®å¼ã³åºãã«æ³¨ç®ããã¨ï¼'abc\ndef'
ã 'abc'
ã«ãªã£ã¦ã»ãããããªã®ã§ï¼ããããã®ã¾ã¾ãã¹ãã«æ¸ãã¦ã¿ã¾ãï¼
vows.describe('read-line').addBatch( { 'ãã¹ãããã' : { + topic : 'abc\ndef', + 'æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { + assert.equal(topic, 'abc'); + } } }).export(module);
ã¾ããã«èãã¦ããããï¼ï¼ï¼ ãªãã¦æ°ã«ããªãã§å®è¡ãã¾ãï¼
$ vows read-line-test.js ✗ ãã¹ãããã ✗ æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ãããã㨠» expected 'abc', got 'abc\ndef' (==) // read-line-test.js:7 ✗ Broken » 1 broken (0.004s)
ã¾ãå½ç¶ã§ããï¼
ã¨ããããï¼read-line ã¢ã¸ã¥ã¼ã«ã«ã³ã¼ã«ããã¯é¢æ°ãããã¨ä»®å®ãã¦ï¼ããã¤ãç´æ¥å¼ã³åºãã¨ããããå§ãã¦ã¿ã¾ãããï¼
'ãã¹ãããã' : { - topic : 'abc\ndef', + topic : function() { + return read_line.callback('abc\ndef'); + }, 'æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic, 'abc'); }
å®è¡ãã¾ãï¼
$ vows read-line-test.js node.js:63 throw e; ^ ReferenceError: read_line is not defined
ãããããã ï¼
ãããªãã㧠(ã©ããªããã§ï¼)ï¼read-line.js ãä½æãã¾ãï¼
+exports.callback = function(data) { + return 'abc'; +};
ããã¨ãããï¼ï¼ï¼ æ°ããããã©æ°ã«ããªãï¼
ã¢ã¸ã¥ã¼ã«ãã§ããã®ã§ï¼ãã¹ãå´ã§èªã¿è¾¼ã¿ã¾ãï¼
var vows = require('vows'), assert = require('assert'); +var read_line = require('./read-line'); vows.describe('read-line').addBatch( { 'ãã¹ãããã' : {
å®è¡ãã¾ãï¼
$ vows read-line-test.js · ✓ OK » 1 honored (0.002s)
ããï¼
ã§ã¯ãã¹ãã±ã¼ã¹ãå¢ããã¦ã¿ã¾ãããï¼
ã¨ããããï¼ãããã¯ãå¢ããã¦ã¿ã¾ãï¼ãããã¯ã¯ã³ã³ããã¹ãã«ä¸ã¤ãªã®ã§ï¼ã³ã³ããã¹ããå¢ããã¾ãï¼
var read_line = require('./read-line'); vows.describe('read-line').addBatch( { - 'ãã¹ãããã' : { + 'éä¸ã«æ¹è¡ãå«ãæååã®å ´å' : { topic : function() { return read_line.callback('abc\ndef'); }, 'æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic, 'abc'); } + }, + 'æ¹è¡ã§å§ã¾ãæååã®å ´å' : { + topic : function() { + return read_line.callback('\nabc'); + }, + '空æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { + assert.equal(topic, ''); + } } }).export(module);
çµæã¯åãã£ã¦ããã©å®è¡ãã¾ãï¼
$ vows read-line-test.js ·✗ æ¹è¡ã§å§ã¾ãæååã®å ´å ✗ 空æååãã³ã¼ã«ããã¯ãããã㨠» expected '', got 'abc' (==) // read-line-test.js:18 ✗ Broken » 1 honored ∙ 1 broken (0.006s)
å½ç¶ã§ãï¼
ã¡ãã£ã¨ã ãã¾ããã«å®è£ ãã¾ãï¼
exports.callback = function(data) { - return 'abc'; + if (data.match(/\r?\n/)) { + return RegExp.leftContext; + } };
ã©ããã¾ãããªãã ãï¼ï¼ï¼ ã§ãæ°ã«ããªãï¼æ°ã«ãã¦ã¯ãããªãã®ã ï¼
æ°ã«ããªãã§å®è¡ãã¾ãï¼
$ vows read-line-test.js ·· ✓ OK » 2 honored (0.002s)
ããï¼
次ã¸é²ãåã«ãã¹ãã³ã¼ããè¦ç´ãã¦ã¿ã¾ãï¼
äºã¤ã®ã³ã³ããã¹ãã®ãããã¯ã¯ãããã 1 è¡ãããªãã¨ã¯ããï¼ç°ãªããã¼ã¿ã«å¯¾ãã¦åãäºããã£ã¦ããã ãã§å
容ã¨ãã¦ã¯éè¤ãã¦ã¾ãï¼ã¾ãã¯ãããåãé¤ãã¦ã¿ã¾ãããï¼
Vows ã®ãã¹ãã³ã¼ã㯠JavaScript ã®ã½ã¼ã¹ãã¡ã¤ã«ã«ãããªãã®ã§ï¼æ®éã«é¢æ°ãå®ç¾©ãã¦å¼ã³åºããã¨ãã§ãã¾ãï¼ã£ã¦ãããï¼Vows ã®ãã¹ããè¦ã¦ãããããæ¹ãæ®éããï¼
ã§ããï¼ãã㯠Vows ã®ã³ã³ããã¹ãï¼ãããã¯ãå
±éãªã³ã¼ãã®ç½®ãå ´æã¨ãã¦å©ç¨ãã¦ã¿ã¾ãï¼
åæ¥ã®æ¥è¨ã§æ¸ããããã«ï¼ãããã¯ãé¢æ°ã¨ãã¦è¨è¿°ããå ´åã¯ãããè©ä¾¡ããçµæããããã¯ã¨ãªãã¾ãï¼ã¨ãããã¨ã¯ï¼è¦ªã³ã³ããã¹ãã®ãããã¯ããé¢æ°ãè¿ãé¢æ°ãã¨ãããã¨ã§ï¼è¿ããé¢æ°ãåã³ã³ããã¹ãã®ãããã¯ã«æ¸¡ããã¾ãï¼
å
ã®äºã¤ã®ã³ã³ããã¹ãã«å
±éãªè¦ªã³ã³ããã¹ããç¨æãã¦ï¼ãã®ãããã¯ã¨ãã¦å
±éãªé¢æ°ãè¿ãããã«ãã¾ãããï¼ãã®é¢æ°ã¯å¼æ°ã¨ã㦠read-line ã¢ã¸ã¥ã¼ã«ã®ã³ã¼ã«ããã¯ãå¼ã³åºããã¼ã¿ãåãåãï¼ãã®æ»ãå¤ãè¿ãã¾ãï¼
ãããªãã¾ã (ã¤ã³ãã³ãããããã®ã§ diff ã§ã¯ãªãã¾ããã¨æ²è¼)ï¼
vows.describe('read-line').addBatch( { 'ã³ã¼ã«ããã¯ãç´æ¥å¼ã³åºãå ´å' : { topic : function() { return function(data) { return read_line.callback(data); }; }, 'éä¸ã«æ¹è¡ãå«ãæååã®å ´å' : { topic : function(topic) { return topic('abc\ndef'); }, 'æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic, 'abc'); } }, 'æ¹è¡ã§å§ã¾ãæååã®å ´å' : { topic : function(topic) { return topic('\nabc'); }, '空æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic, ''); } } } }).export(module);
親ã³ã³ããã¹ãã®ãããã¯ãè¿ãé¢æ°ãæç²ããã¨ï¼ãããªã£ã¦ã¾ãï¼
return function(data) { return read_line.callback(data); };
ãã®é¢æ°ãåã³ã³ããã¹ãã«ãããã¯ã¨ãã¦æ¸¡ããã¾ãï¼
äºã¤ã®åã³ã³ããã¹ãã®ãããã¯ã¯ï¼æ¸¡ããã親ã³ã³ããã¹ãã®ããã㯠(ä¸è¨ã®é¢æ°ã§ã) ãããããã®å¼æ°ã渡ãã¦å¼ã³åºãï¼ãã®çµæãåãã³ã³ããã¹ãã® vow ã«ãããã¯ã¨ãã¦æ¸¡ããã¾ãï¼
å®è¡ãã¾ãï¼
$ vows read-line-test.js ·· ✓ OK » 2 honored (0.001s)
ãã¦ï¼æ¨æ¥ã¯æ¸ãã¾ããã§ãããï¼vows
ã³ãã³ãã¯å¼æ° --spec
ãæå®ããã¨ã³ã³ããã¹ãããããã¯ï¼vow ã®èª¬æã綺éºã«åºåãã¦ããã¾ãï¼
$ vows read-line-test.js --spec ♢ read-line ã³ã¼ã«ããã¯ãç´æ¥å¼ã³åºãå ´å éä¸ã«æ¹è¡ãå«ãæååã®å ´å ✓ æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ãããã㨠ã³ã¼ã«ããã¯ãç´æ¥å¼ã³åºãå ´å æ¹è¡ã§å§ã¾ãæååã®å ´å ✓ 空æååãã³ã¼ã«ããã¯ãããã㨠✓ OK » 2 honored (0.002s)
ããã§ããï¼
RSpec ãªããã¨éã£ã¦æååã§æ¸ãã¦ãã ããªã®ã§ï¼DRY ã§ããªããã°ä¿¡ææ§ã確ããããã¹ããªãããã§ããï¼è±èªè¦æãªèªåã«ã¯ããã§ããããªï¼
ã§ãã§ãï¼ããå ´å ãå ´åãã£ã¦å¤ãªã®ã§ã¡ãã£ã¨ä¿®æ£ãã¾ãï¼
var read_line = require('./read-line'); vows.describe('read-line').addBatch( { - 'ã³ã¼ã«ããã¯ãç´æ¥å¼ã³åºãå ´å' : { + 'ã³ã¼ã«ããã¯ãç´æ¥å¼ã³åºãã¦' : { topic : function() { return function(data) { return read_line.callback(data); }; }, - 'éä¸ã«æ¹è¡ãå«ãæååã®å ´å' : { + 'ãã¼ã¿ãéä¸ã«æ¹è¡ãå«ãæååã®å ´å' : { topic : function(topic) { return topic('abc\ndef'); }, @@ -16,7 +16,7 @@ vows.describe('read-line').addBatch( { assert.equal(topic, 'abc'); } }, - 'æ¹è¡ã§å§ã¾ãæååã®å ´å' : { + 'ãã¼ã¿ãæ¹è¡ã§å§ã¾ãæååã®å ´å' : { topic : function(topic) { return topic('\nabc'); },
å®è¡ãã¾ãï¼
$ vows read-line-test.js --spec ♢ read-line ã³ã¼ã«ããã¯ãç´æ¥å¼ã³åºã㦠ãã¼ã¿ãéä¸ã«æ¹è¡ãå«ãæååã®å ´å ✓ æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ãããã㨠ã³ã¼ã«ããã¯ãç´æ¥å¼ã³åºã㦠ãã¼ã¿ãæ¹è¡ã§å§ã¾ãæååã®å ´å ✓ 空æååãã³ã¼ã«ããã¯ãããã㨠✓ OK » 2 honored (0.003s)
ããããï¼
ããã§ã¯ read-line ã®å®è£
ãããã¡ãã£ã¨ã ããã·ãªãã®ã«ãã¾ãããï¼
ããããï¼read-line ã¯èªåãã³ã¼ã«ããã¯ããããæ¬æ¥ã®ã³ã¼ã«ããã¯é¢æ°ãå¼ã³åºãã¹ãã§ãã£ã¦ï¼æ»ãå¤ãè¿ãã¦æ¬²ããããã§ã¯ããã¾ããï¼éåæ㪠Node.js ããã API ã«ãªã£ã¦ããªãã®ã§ãï¼
ããã§ï¼read-line ãéåæãªã³ã¼ã«ããã¯ãåãåã£ã¦å¼ã³åºãããã«ãã¦ã¿ã¾ãããï¼
ãã®ããã®ãã¹ããã©ããããï¼
æ¨æ¥ãæ¸ããããã«ï¼Vows ã¯éåæãªãã¹ãã®ããã®æ©è½ãç¨æãã¦ããã¦ãã¾ãï¼ã¾ãã¯ç°¡å㪠this.callback
ã使ã£ã¦ã¿ã¾ãããï¼
vows.describe('read-line').addBatch( { 'ã³ã¼ã«ããã¯ãç´æ¥å¼ã³åºãã¦' : { topic : function() { - return function(data) { - return read_line.callback(data); + return function(data, callback) { + read_line.callback(data, callback); }; }, 'ãã¼ã¿ãéä¸ã«æ¹è¡ãå«ãæååã®å ´å' : { topic : function(topic) { - return topic('abc\ndef'); + return topic('abc\ndef', this.callback); }, 'æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic, 'abc'); @@ -18,7 +18,7 @@ vows.describe('read-line').addBatch( { }, 'ãã¼ã¿ãæ¹è¡ã§å§ã¾ãæååã®å ´å' : { topic : function(topic) { - return topic('\nabc'); + return topic('\nabc', this.callback); }, '空æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic, '');
ã¾ãã¯äºã¤ããåã³ã³ããã¹ãã®ãããã¯ã§
topic : function(topic) { return topic('abc\ndef', this.callback); },
㨠this.callback
ã親ã³ã³ããã¹ãã®ããã㯠(ãè¿ãé¢æ°) ã«æ¸¡ãã¦ãã¾ãï¼å¾ã§ read-line ããã® this.callback
ãã³ã¼ã«ããã¯ããã¨ï¼vow ãå¼ã³åºãããããã§ããï¼
ããã¦è¦ªã³ã³ããã¹ãã®ãããã¯ã¯ï¼
topic : function() { return function(data, callback) { read_line.callback(data, callback); }; },
ãã¯ãå¤ãè¿ããªããªãã¾ããï¼this.callback
ã使ãå ´åã®ãç´æã§ãï¼ãã㦠this.callback
ã read-line ã«æ¸¡ãã¦ãã¾ãï¼
read-line ã¯ã¾ã ã³ã¼ã«ããã¯ãåãåãããã«ãªã£ã¦ãªããã©ï¼ä¸å¿å®è¡ãã¾ãï¼
$ vows read-line-test.js ✗ Errored » read-line: ã³ã¼ã«ããã¯ãç´æ¥å¼ã³åºã㦠ãã¼ã¿ãéä¸ã«æ¹è¡ãå«ãæååã®å ´å ∙ not fired! ✗ Errored » read-line: ã³ã¼ã«ããã¯ãç´æ¥å¼ã³åºã㦠ãã¼ã¿ãæ¹è¡ã§å§ã¾ãæååã®å ´å ∙ not fired! ✗ Errored » 1 errored ∙ 2 dropped
ã ãã this.callback
ãå¼ã³åºãã¦ããªãã®ã§ï¼ã¤ãã³ããçºçããªãã£ãã¨æããã¾ããï¼
ããã read-line å´ãä¿®æ£ãã¦ï¼ã³ã¼ã«ããã¯ãåãåã£ã¦å¼ã³åºãããã«ãã¾ãããï¼
-exports.callback = function(data) { +exports.callback = function(data, callback) { if (data.match(/\r?\n/)) { - return RegExp.leftContext; + callback(RegExp.leftContext); } };
å®è¡ãã¾ãï¼
$ vows read-line-test.js ✗✗ ã³ã¼ã«ããã¯ãç´æ¥å¼ã³åºã㦠ãã¼ã¿ãéä¸ã«æ¹è¡ãå«ãæååã®å ´å ✗ æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ãããã㨠» An unexpected error was caught: abc ã³ã¼ã«ããã¯ãç´æ¥å¼ã³åºã㦠ãã¼ã¿ãæ¹è¡ã§å§ã¾ãæååã®å ´å ✗ 空æååãã³ã¼ã«ããã¯ãããã㨠» expected '', got undefined (==) // read-line-test.js:24 ✗ Errored » 1 broken ∙ 1 errored (0.006s)
ããã
ï¼ï¼ï¼
ããã§ããï¼æ¨æ¥ãæ¸ããããã«ï¼this.callback
ã¯ãã¡ã¤ã«ã¢ã¯ã»ã¹ãªã©ã¨åã
function(err, arg1, ...)
ã¨ããå½¢å¼ãæ³å®ãã¦ãã¦ï¼ç¬¬ 1 å¼æ°ã null
ã¾ã㯠undefined
ã§ãªãã¨ã¨ã©ã¼ãçºçããã¨è¦ãªããã¡ããã®ã§ããï¼ã£ã¦ãããï¼ç©ºæååãæåæ±ããªã®ãï¼ãã®ãã㧠2 çªç®ã® vow ã¯å¼ã³åºããã¦ã¾ããï¼æ¸¡ãããã®ã¯ undefined
ãï¼ï¼ï¼
ãããããªãã®ã§ this.callback
ãç´æ¥ read-line ã«æ¸¡ãã®ã§ã¯ãªãï¼ä¸ã¤é¢æ°ãæã¿ã¾ãï¼
'ã³ã¼ã«ããã¯ãç´æ¥å¼ã³åºãã¦' : { topic : function() { return function(data, callback) { - read_line.callback(data, callback); + read_line.callback(data, function(data) { + callback(null, data); + }); }; }, 'ãã¼ã¿ãéä¸ã«æ¹è¡ãå«ãæååã®å ´å' : {
å®è¡ãã¾ãï¼
$ vows read-line-test.js ·· ✓ OK » 2 honored (0.002s)
ããï¼ããï¼
ããã¾ã§ã®ãã¹ãã³ã¼ãå ¨ä½ãããã§æ²è¼ãã¦ããã¾ãï¼
var vows = require('vows'), assert = require('assert'); var read_line = require('./read-line'); vows.describe('read-line').addBatch( { 'ã³ã¼ã«ããã¯ãç´æ¥å¼ã³åºãã¦' : { topic : function() { return function(data, callback) { read_line.callback(data, function(data) { callback(null, data); }); }; }, 'ãã¼ã¿ãéä¸ã«æ¹è¡ãå«ãæååã®å ´å' : { topic : function(topic) { return topic('abc\ndef', this.callback); }, 'æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic, 'abc'); } }, 'ãã¼ã¿ãæ¹è¡ã§å§ã¾ãæååã®å ´å' : { topic : function(topic) { return topic('\nabc', this.callback); }, '空æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic, ''); } } } }).export(module);
ãã¦ï¼ä¸ã¤ç®ã®ãã¹ãã§ã¯ 'abc\ndef'
ã¨ãã 2 è¡åã®ãã¼ã¿ã渡ãã¦ããã®ã«ï¼ã³ã¼ã«ããã¯ãããã®ã¯ 1 åã ãã§ãï¼ããã¯ã¡ãã㨠2 åå¼ã³åºãã¦æ¬²ããã§ãããï¼
ããããã¹ãããã«ã¯ï¼ï¼ï¼ ã³ã¼ã«ããã¯ããããããã« this.callback
ããã®ã§ã¯ãªãï¼ã³ã¼ã«ããã¯ããããã¼ã¿ã貯ãã¦ããã¦ï¼ã¾ã¨ã㦠vow ã«æ¸¡ãã®ãããããã§ãï¼
ããã¾ãããï¼
'ã³ã¼ã«ããã¯ãç´æ¥å¼ã³åºãã¦' : { topic : function() { return function(data, callback) { + var lines = []; read_line.callback(data, function(data) { - callback(null, data); + lines.push(data); }); + callback(null, lines); }; }, 'ãã¼ã¿ãéä¸ã«æ¹è¡ãå«ãæååã®å ´å' : {
ããã§è¡åä½ã«åå²ããããã¼ã¿ãé
åã«ããè¾¼ãã§ãã¹ããããã¨ãã§ããã¯ãï¼
ã¾ã vow ãç´ãã¦ãªããã©å®è¡ï¼ï¼
$ vows read-line-test.js ·· ✓ OK » 2 honored (0.002s)
ããï¼ äºæ³å¤ã®æåï¼ãªãï¼
ãããã㦠JavaScript ã§ã¯ [ 'abc' ]
㨠'abc'
ã¯åå¤ãªã®ï¼
assert.equal(['abc'], 'abc');
ã£ã¦ãã¦ã¿ããæ¬å½ã«åå¤ã ã£ãï¼ç¥ããªãã£ããï¼ä¿ºã® JavaScript åï¼ããã§ï¼ï¼ï¼
æ°ãåãç´ã㦠vow ãä¿®æ£ãã¾ãï¼é
åã®ãã¹ãã«ã¯ assert.deepEqual()
ã使ããã®ã§ããï¼ããã¯ã¡ãã£ã¨é å¼µã£ã¦ãä¸ã¤ã®ãããã¯ã«è¤æ°ã® vowãã§ãã£ã¦ã¿ã¾ãï¼
topic : function(topic) { return topic('abc\ndef', this.callback); }, - 'æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { - assert.equal(topic, 'abc'); + '2åã³ã¼ã«ããã¯ããããã¨' : function(topic) { + assert.length(topic, 2); + }, + 'æåã«æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { + assert.equal(topic[0], 'abc'); + }, + '次ã«æ¹è¡ã®å¾ã®æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { + assert.equal(topic[1], 'def'); } }, 'ãã¼ã¿ãæ¹è¡ã§å§ã¾ãæååã®å ´å' : { topic : function(topic) { return topic('\nabc', this.callback); }, - '空æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { - assert.equal(topic, ''); + '2åã³ã¼ã«ããã¯ããããã¨' : function(topic) { + assert.length(topic, 2); + }, + 'æåã«ç©ºè¡ãã³ã¼ã«ããã¯ããããã¨' : function(topic) { + assert.equal(topic[0], ''); + }, + '次ã«æ¹è¡ã®å¾ã®æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { + assert.equal(topic[1], 'abc'); } } }
assert.length()
ã¯æ¨æºã® assert ã¢ã¸ã¥ã¼ã«ã Vows ãæ¡å¼µãã¦è¿½å ãã¦ããã¦ãããã®ã§ãï¼
å®è¡ãã¾ãï¼
$ vows read-line-test.js ✗·✗✗·✗ ã³ã¼ã«ããã¯ãç´æ¥å¼ã³åºã㦠ãã¼ã¿ãéä¸ã«æ¹è¡ãå«ãæååã®å ´å ✗ 2åã³ã¼ã«ããã¯ãããã㨠» expected [ 'abc' ] to have 2 element(s) // read-line-test.js:20 ✗ 次ã«æ¹è¡ã®å¾ã®æååãã³ã¼ã«ããã¯ãããã㨠» expected 'def', got undefined (==) // read-line-test.js:26 ã³ã¼ã«ããã¯ãç´æ¥å¼ã³åºã㦠ãã¼ã¿ãæ¹è¡ã§å§ã¾ãæååã®å ´å ✗ 2åã³ã¼ã«ããã¯ãããã㨠» expected [ '' ] to have 2 element(s) // read-line-test.js:34 ✗ 次ã«æ¹è¡ã®å¾ã®æååãã³ã¼ã«ããã¯ãããã㨠» expected 'abc', got undefined (==) // read-line-test.js:40 ✗ Broken » 2 honored ∙ 4 broken (0.008s)
ä»åº¦ã¯ããæãã§ãã¹ãã失æãã¾ããâª
ãããªãã㧠(ã©ããªããã§ï¼)ï¼read-line ãä¿®æ£ãã¦è¡ã®æ°ã ãã³ã¼ã«ããã¯ããããã«ãã¾ãï¼
exports.callback = function(data, callback) { - if (data.match(/\r?\n/)) { + while (data.match(/\r?\n/)) { callback(RegExp.leftContext); + data = RegExp.rightContext; } + callback(data); };
ãã¹ããå®è¡ãã¾ãï¼
$ vows read-line-test.js ······ ✓ OK » 6 honored (0.003s)
ãããï¼
--spec
ãã¦ã¿ã¾ãï¼
$ vows read-line-test.js --spec ♢ read-line ã³ã¼ã«ããã¯ãç´æ¥å¼ã³åºã㦠ãã¼ã¿ãéä¸ã«æ¹è¡ãå«ãæååã®å ´å ✓ 2åã³ã¼ã«ããã¯ãããã㨠✓ æåã«æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ãããã㨠✓ 次ã«æ¹è¡ã®å¾ã®æååãã³ã¼ã«ããã¯ãããã㨠ã³ã¼ã«ããã¯ãç´æ¥å¼ã³åºã㦠ãã¼ã¿ãæ¹è¡ã§å§ã¾ãæååã®å ´å ✓ 2åã³ã¼ã«ããã¯ãããã㨠✓ æåã«ç©ºè¡ãã³ã¼ã«ããã¯ãããã㨠✓ 次ã«æ¹è¡ã®å¾ã®æååãã³ã¼ã«ããã¯ãããã㨠✓ OK » 6 honored (0.003s)
ãï¼æ¨æ¥ Vows ãé¸ãã çç±ã«ã¡ãã£ã¨ã ãã«ãã³ããã£ã¦æ¸ããæããåºã¦ããã§ããï¼
å§ãã®æ¹ã§
function('abc\ndef')
ãªãã¦å¼ã³åºããï¼
function('abc')
function('def')
ã£ã¦å
·åã«ãªã£ã¦æ¬²ããã¨æ¸ãã¾ãããï¼æ¬å½ã¯ããã§ã¯ãã¡ã§ãï¼
'def'
ã®å¾ãã«ã¯æ¹è¡ããªãã®ã§ï¼ããã¯å³åº§ã«ã³ã¼ã«ããã¯ã«æ¸¡ãããã®ã§ã¯ãªãï¼æ¬¡ã®ã¤ãã³ãã®ãã¼ã¿ã¨ãã£ã¤ãã¦ã³ã¼ã«ããã¯ãããã¹ãã§ãï¼ãã®ãã¼ã¿ã«æ®ããããã°ï¼ãããã¾ããã®æ¬¡ã®ã¤ãã³ãã®ãã¼ã¿ã¨ãã£ã¤ãã¦ï¼ï¼ï¼ 以ä¸ç¹°ãè¿ãï¼
ããã¦ã¹ããªã¼ã ã (ã¯ãã¼ãºã EOF ãªã©ã§) çµäºãããï¼æ®ã£ã¦ãããã¼ã¿ãã³ã¼ã«ããã¯ãããã¹ãã§ãï¼
ã©ããã£ã¦ãã¹ããæ¸ãã¾ãããï¼
'data'
ã¤ãã³ããè¤æ°åçæãã¦ï¼æå¾ã« 'end'
ã¤ãã³ããçæããï¼å½ç©ã® ReadableStream
ãããã¨ããããã§ãï¼
ããã§ï¼Node.js ã®ã¤ãã³ãã½ã¼ã¹ã®å
ç· ãï¼EventEmitter
ã使ãã¾ãããï¼
å®ã®ã¨ããï¼ReadableStream
ãå«ããå
¨ã¦ã®ã¹ããªã¼ã 㯠EventEmitter
ãªã®ã§ãï¼
å
·ä½çã«ã¯ï¼EventEmitter
ã¨ã³ã¼ã«ããã¯é¢æ°ã read-line ã«æ¸¡ãã¨ï¼read-line ã®äºã¤ã®ã³ã¼ã«ããã¯é¢æ°ã EventEmitter
ã«ç»é²ããã¦ï¼'data'
ã¤ãã³ããçºçãããã¨ã«è¡åä½ã§åºåãããæååãå¼æ°ã¨ãã¦å¼ã³åºãå´ã®ã³ã¼ã«ããã¯é¢æ°ãå¼ã°ããï¼æå¾ã« 'end'
ã¤ãã³ããçºçããã¨ï¼æ®ã£ã¦ããæååãå¼æ°ã¨ãã¦å¼ã³åºãå´ã®ã³ã¼ã«ããã¯é¢æ°ãå¼ã°ããï¼
ãããªæãï¼
ããã§ã¯è¦ªã³ã³ããã¹ãã®ãããã¯ãã©ããªããèãã¦ã¿ã¾ãããï¼
ã¾ã㯠EventEmitter
ã使ãã®ã§ï¼
var events = require('events');
ãå¿
è¦ã§ãï¼æ¬¡ã« EventEmitter
ãçæãã¾ãï¼
var emitter = new (events.EventEmitter);
ããã¦ã¤ãã³ãã½ã¼ã¹ã§ãã EventEmitter
ã¨ï¼è¡åä½ã®ãã¼ã¿ãåãåãã³ã¼ã«ããã¯ã read-line ã«æ¸¡ãã¾ãï¼
ããå æ¸ï¼read-line å´ã®é¢æ°ã®åå (API) ãã¡ããã¨èããªãã¨ããã¾ãããï¼'data'
ã¤ãã³ãã«å¯¾ãã¦è¡åä½ã«ã³ã¼ã«ããã¯ãã¦ãããé¢æ°ãç»é²ããã¨ããæ°æã¡ãªã®ã§ï¼onDataByLine()
ã¨ã§ããã¦ããã¾ãããï¼
read_line.onDataByLine(emitter, function(line) { lines.push(line); });
ãªããï¼ã¢ã³ã¹ã³åºåãã¨ãã£ã¡ã«è¨æ³ãæ··ãã£ã¡ãã£ããã©æ°ã«ããªãï¼
ãã®ã³ã¼ã«ããã¯ã®å¼æ°ã¯ãã£ãã¾ã§ data
ã¨ããååã§ãããï¼è¡åä½ã§ãããã¨ã強調ã㦠line
ã«ååãå¤æ´ãã¾ããï¼æåãããããã¦ããã°ããã£ãï¼
ãã㦠'data'
ã¤ãã³ããçæãã¾ãï¼
emitter.emit('data', data);
æå¾ã« 'end'
ã¤ãã³ããçæãã¾ãï¼
emitter.emit('end');
ããã§å®éã® ReadableStream
ã使ã£ãå ´åã¨åãããã« read-line ãåãããã¨ãã§ãããã§ãï¼
ã¾ã¨ããã¨ãããªãã¾ããï¼
-var vows = require('vows'), assert = require('assert'); +var vows = require('vows'), assert = require('assert'), events = require('events'); var read_line = require('./read-line'); vows.describe('read-line').addBatch( { @@ -6,9 +6,12 @@ vows.describe('read-line').addBatch( { topic : function() { return function(data, callback) { var lines = []; - read_line.callback(data, function(data) { - lines.push(data); + var emitter = new (events.EventEmitter); + read_line.onDataByLine(emitter, function(line) { + lines.push(line); }); + emitter.emit('data', data); + emitter.emit('end'); callback(null, lines); }; },
æ¡å¤å¤ãã£ã¦ãªããããªæ°ããã¡ããã¾ããï¼ã§ãï¼read_line.onDataByLine()
ã¯ä¸å¿ç®çã® API ã«ãªã£ãã¯ãï¼
ã§ã¯å®è¡ãã¾ãï¼
$ vows read-line-test.js node.js:63 throw e; ^ TypeError: Object #has no method 'onDataByLine'
ãããªããã§ï¼read-line å´ã® callback
ã onDataByLine()
ã«å¤æ´ã㦠ReadableStream
ã«å¯¾ãã¦ã¤ãã³ããã³ãã©ãç»é²ããããã«ãã¾ãï¼
-exports.callback = function(data, callback) { - while (data.match(/\r?\n/)) { - callback(RegExp.leftContext); - data = RegExp.rightContext; - } - callback(data); +exports.onDataByLine = function(stream, callback) { + var line = ''; + stream.on('data', function(data) { + line += data; + while (line.match(/\r?\n/)) { + callback(RegExp.leftContext); + line = RegExp.rightContext; + } + }); + stream.on('end', function() { + callback(line); + }); };
line
ã¨ããå¤æ°ãåã®ãã¼ã¿ã®æ®ããä¿æãã¦ããã®ã§ï¼'data'
ã®ã¤ãã³ããã³ãã©ã§ã¯ããã¨æ°ãã«åä¿¡ãããã¼ã¿ããã£ã¤ãã¦ããï¼æ¹è¡ã¾ã§ã®é¨åæååãå¼æ°ã¨ãã¦ã³ã¼ã«ããã¯ãå¼ã³åºãã¦ãã¾ãï¼'end'
ã®ã¤ãã³ããã³ãã©ã§ã¯ãã® line
ãå¼æ°ã¨ãã¦ã³ã¼ã«ããã¯ãå¼ã³åºãã¦ãã¾ãï¼
ã§ã¯å®è¡ãã¾ãï¼
$ vows read-line-test.js ······ ✓ OK » 6 honored (0.003s)
ãããããï¼ï¼
ã§ãã§ãï¼ãªãã ãå¤ã§ãï¼
'ã³ã¼ã«ããã¯ãç´æ¥å¼ã³åºãã¦' : {
ãã read-line ã®ã³ã¼ã«ããã¯ãç´æ¥å¼ãã§ãªããï¼
topic : function() { return function(data, callback) { var lines = []; var emitter = new (events.EventEmitter); readine.onDataByLine(emitter, function(line) { lines.push(line); }); emitter.emit('data', data); emitter.emit('end'); callback(null, lines); }; },
ããããï¼ãã¤ã®éã«ã this.callback
ãå¼ã³åºãå¿
è¦ããªããªã£ã¦ã¾ãï¼ã ã£ã¦ read-line ã«æ¸¡ãã³ã¼ã«ããã¯é¢æ°ãå¼ã°ããã vow ãå¼ã°ããããã«ãããã¦å°å
¥ããã®ã«ï¼ä»ãã³ã¼ã«ããã¯é¢æ°ã®ä¸ã§ä½¿ã£ã¦ã¾ãããããï¼ãã¤ã®éã«ï¼ï¼ï¼ï¼
ãããï¼lines
ãå°å
¥ãã¦ãããï¼æ°ã¥ããªãã£ããï¼å¿ããæ¥ããï¼ããããã¾ãåãã¦ãã®ã¯ï¼EventEmitter
ã® emit()
ãéåæãªã¤ãã³ããçæãã¦ããã®ã§ã¯ãªãï¼ãã®å ´ã§ã³ã¼ã«ããã¯ãåæçã«å¼ã³åºãã¦ããããã§ããï¼
ãããªãã㧠(ã©ããªããã§ï¼)ï¼ã³ã¡ã³ããä¿®æ£ã㦠this.callback
ãå¼ã³åºã代ããã«ç´æ¥é
åãè¿ãããã«ãã¾ãï¼
var read_line = require('./read-line'); vows.describe('read-line').addBatch( { - 'ã³ã¼ã«ããã¯ãç´æ¥å¼ã³åºãã¦' : { + 'å½ã®EventEmitterã使ã£ã¦' : { topic : function() { - return function(data, callback) { + return function(data) { var lines = []; var emitter = new (events.EventEmitter); read_line.onDataByLine(emitter, function(line) { @@ -12,12 +12,12 @@ vows.describe('read-line').addBatch( { }); emitter.emit('data', data); emitter.emit('end'); - callback(null, lines); + return lines; }; }, 'ãã¼ã¿ãéä¸ã«æ¹è¡ãå«ãæååã®å ´å' : { topic : function(topic) { - return topic('abc\ndef', this.callback); + return topic('abc\ndef'); }, '2åã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.length(topic, 2); @@ -31,7 +31,7 @@ vows.describe('read-line').addBatch( { }, 'ãã¼ã¿ãæ¹è¡ã§å§ã¾ãæååã®å ´å' : { topic : function(topic) { - return topic('\nabc', this.callback); + return topic('\nabc'); }, '2åã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.length(topic, 2);
å®è¡ãã¾ãï¼
$ vows read-line-test.js ······ ✓ OK » 6 honored (0.003s)
--spec
ããã¦ã¿ã¾ãï¼
$ vows read-line-test.js --spec ♢ read-line å½ã®EventEmitterã使ã£ã¦ ãã¼ã¿ãéä¸ã«æ¹è¡ãå«ãæååã®å ´å ✓ 2åã³ã¼ã«ããã¯ãããã㨠✓ æåã«æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ãããã㨠✓ 次ã«æ¹è¡ã®å¾ã®æååãã³ã¼ã«ããã¯ãããã㨠å½ã®EventEmitterã使ã£ã¦ ãã¼ã¿ãæ¹è¡ã§å§ã¾ãæååã®å ´å ✓ 2åã³ã¼ã«ããã¯ãããã㨠✓ æåã«ç©ºè¡ãã³ã¼ã«ããã¯ãããã㨠✓ 次ã«æ¹è¡ã®å¾ã®æååãã³ã¼ã«ããã¯ãããã㨠✓ OK » 6 honored (0.003s)
ããï¼ããï¼
次ã¸é²ãåã«ï¼ãã¹ããå¢ããã¦ã¿ã¾ã (éãå¤ãã®ã§ vow 丸ãã¨æ²è¼)ï¼
'ãã¼ã¿ãundefinedã®å ´å' : { topic : function(topic) { return topic(undefined); }, '1åãã³ã¼ã«ããã¯ãããªããã¨' : function(topic) { assert.length(topic, 0); } }, 'ãã¼ã¿ãnullã®å ´å' : { topic : function(topic) { return topic(null); }, '1åãã³ã¼ã«ããã¯ãããªããã¨' : function(topic) { assert.length(topic, 0); } }, 'ãã¼ã¿ã空æååã®å ´å' : { topic : function(topic) { return topic(''); }, '1åãã³ã¼ã«ããã¯ãããªããã¨' : function(topic) { assert.length(topic, 0); } }, 'ãã¼ã¿ãæ¹è¡ãå«ã¾ãªãæååã®å ´å' : { topic : function(topic) { return topic('abcdef'); }, '1åã ãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.length(topic, 1); }, 'ãã¼ã¿å ¨ä½ãæååã¨ãã¦ã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic[0], 'abcdef'); } }, 'ãã¼ã¿ãæ¹è¡ã§å§ã¾ãæååã®å ´å' : { topic : function(topic) { return topic('\nabc'); }, '2åã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.length(topic, 2); }, 'æåã«ç©ºè¡ãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic[0], ''); }, '次ã«æ¹è¡ã®å¾ã®æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic[1], 'abc'); } }, 'ãã¼ã¿ãæ¹è¡ã§çµäºããæååã®å ´å' : { topic : function(topic) { return topic('abcdef\n'); }, '1åã ãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.length(topic, 1); }, 'æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic[0], 'abcdef'); } }, 'ãã¼ã¿ãéä¸ã«æ¹è¡ãå«ãæååã®å ´å' : { topic : function(topic) { return topic('abc\ndef'); }, '2åã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.length(topic, 2); }, 'æåã«æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic[0], 'abc'); }, '次ã«æ¹è¡ã®å¾ã®æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic[1], 'def'); } }, 'ãã¼ã¿ãéä¸ã«æ¹è¡ãäºã¤å«ãæååã®å ´å' : { topic : function(topic) { return topic('abc\ndef\nghi'); }, '3åã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.length(topic, 3); }, 'æåã«ä¸çªç®ã®æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic[0], 'abc'); }, '次ã«ä¸çªç®ã®æ¹è¡ã¨äºçªç®ã®æ¹è¡ã®éã®æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic[1], 'def'); }, 'æå¾ã«äºçªç®ã®æ¹è¡ããå¾ã®æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic[2], 'ghi'); } }
å®è¡ãã¾ãï¼
$ vows read-line-test.js ✗✗✗·····✗········ å½ã®EventEmitterã使ã£ã¦ ãã¼ã¿ãundefinedã®å ´å ✗ 1åãã³ã¼ã«ããã¯ãããªãã㨠» expected [ 'undefined' ] to have 0 element(s) // read-line-test.js:23 å½ã®EventEmitterã使ã£ã¦ ãã¼ã¿ãnullã®å ´å ✗ 1åãã³ã¼ã«ããã¯ãããªãã㨠» expected [ 'null' ] to have 0 element(s) // read-line-test.js:31 å½ã®EventEmitterã使ã£ã¦ ãã¼ã¿ã空æååã®å ´å ✗ 1åãã³ã¼ã«ããã¯ãããªãã㨠» expected [ '' ] to have 0 element(s) // read-line-test.js:39 å½ã®EventEmitterã使ã£ã¦ ãã¼ã¿ãæ¹è¡ã§çµäºããæååã®å ´å ✗ 1åã ãã³ã¼ã«ããã¯ãããã㨠» expected [ 'abcdef', '' ] to have 1 element(s) // read-line-test.js:72 ✗ Broken » 13 honored ∙ 4 broken (0.009s)
ãã¯ãã£ï¼ï¼ï¼
ããã§ããï¼read-line ãä½ããã§ãã¯ãã¦ã¾ãããããï¼ã¬ã¼ããå
¥ãã¾ãï¼
exports.onDataByLine = function(stream, callback) { var line = ''; stream.on('data', function(data) { - line += data; - while (line.match(/\r?\n/)) { - callback(RegExp.leftContext); - line = RegExp.rightContext; + if (data) { + line += data; + while (line.match(/\r?\n/)) { + callback(RegExp.leftContext); + line = RegExp.rightContext; + } } }); stream.on('end', function() { - callback(line); + if (line) { + callback(line); + } }); };
å®è¡ãã¾ãï¼
$ vows read-line-test.js ················· ✓ OK » 17 honored (0.004s)
--spec
ãã¾ãï¼
$ vows read-line-test.js --spec ♢ read-line å½ã®EventEmitterã使ã£ã¦ ãã¼ã¿ãundefinedã®å ´å ✓ 1åãã³ã¼ã«ããã¯ãããªãã㨠å½ã®EventEmitterã使ã£ã¦ ãã¼ã¿ãnullã®å ´å ✓ 1åãã³ã¼ã«ããã¯ãããªãã㨠å½ã®EventEmitterã使ã£ã¦ ãã¼ã¿ã空æååã®å ´å ✓ 1åãã³ã¼ã«ããã¯ãããªãã㨠å½ã®EventEmitterã使ã£ã¦ ãã¼ã¿ãæ¹è¡ãå«ã¾ãªãæååã®å ´å ✓ 1åã ãã³ã¼ã«ããã¯ãããã㨠✓ ãã¼ã¿å ¨ä½ãæååã¨ãã¦ã³ã¼ã«ããã¯ãããã㨠å½ã®EventEmitterã使ã£ã¦ ãã¼ã¿ãæ¹è¡ã§å§ã¾ãæååã®å ´å ✓ 2åã³ã¼ã«ããã¯ãããã㨠✓ æåã«ç©ºè¡ãã³ã¼ã«ããã¯ãããã㨠✓ 次ã«æ¹è¡ã®å¾ã®æååãã³ã¼ã«ããã¯ãããã㨠å½ã®EventEmitterã使ã£ã¦ ãã¼ã¿ãæ¹è¡ã§çµäºããæååã®å ´å ✓ 1åã ãã³ã¼ã«ããã¯ãããã㨠✓ æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ãããã㨠å½ã®EventEmitterã使ã£ã¦ ãã¼ã¿ãéä¸ã«æ¹è¡ãå«ãæååã®å ´å ✓ 2åã³ã¼ã«ããã¯ãããã㨠✓ æåã«æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ãããã㨠✓ 次ã«æ¹è¡ã®å¾ã®æååãã³ã¼ã«ããã¯ãããã㨠å½ã®EventEmitterã使ã£ã¦ ãã¼ã¿ãéä¸ã«æ¹è¡ãäºã¤å«ãæååã®å ´å ✓ 3åã³ã¼ã«ããã¯ãããã㨠✓ æåã«ä¸çªç®ã®æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ãããã㨠✓ 次ã«ä¸çªç®ã®æ¹è¡ã¨äºçªç®ã®æ¹è¡ã®éã®æååãã³ã¼ã«ããã¯ãããã㨠✓ æå¾ã«äºçªç®ã®æ¹è¡ããå¾ã®æååãã³ã¼ã«ããã¯ãããã㨠✓ OK » 17 honored (0.004s)
ããããï¼ãªããã§ãã¦ããæãâª
ãã¦ï¼ããã¾ã§ã¯ 'data'
ã¤ãã³ãã 1 åããçºçãã¦ãã¾ããï¼æ¬¡ã¯è¤æ°åçºçããã¾ãããï¼ããã«ã¯ï¼åã³ã³ããã¹ãã®ãããã¯ãã親ã³ã³ããã¹ãã®ããã㯠(ãè¿ãé¢æ°) ã«æ¸¡ãå¼æ°ãé
åã«ããã°è¯ãããã§ãï¼
topic : function(topic) { return topic( [ 'abc', 'def' ]); }
ã¿ãããªï¼
ããã¦è¦ªã®ããã㯠(ãè¿ãé¢æ°) ã¯ï¼å¼æ°ãé
åã ã£ããç¹°ãè¿ã 'data'
ã¤ãã³ããçæãã¾ãï¼
ãããªãã¾ãã (éãå¤ãã®ã§ diff ã§ã¯ãªãæç²ãæ²è¼)ï¼
vows.describe('read-line').addBatch( { 'å½ã®EventEmitterã使ã£ã¦' : { topic : function() { return function(data) { var lines = []; var emitter = new (events.EventEmitter); read_line.onDataByLine(emitter, function(line) { lines.push(line); }); if (!Array.isArray(data)) { emitter.emit('data', data); } else { for ( var i = 0; i < data.length; ++i) { emitter.emit('data', data[i]); } } emitter.emit('end'); return lines; }; }, '1åã ããã¼ã¿åä¿¡ã¤ãã³ããçºçã' : { // ãã£ãã¾ã§ã®ãã¹ãã±ã¼ã¹ãªã®ã§çç¥ }, '2åãã¼ã¿åä¿¡ã¤ãã³ããçºçã' : { 'ã©ã®ãã¼ã¿ã«ãæ¹è¡ãå«ã¾ããªãå ´å' : { topic : function(topic) { return topic( [ 'abc', 'def' ]); }, '1åã ãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.length(topic, 1); }, '1çªç®ã®ãã¼ã¿ã¨2çªç®ã®ãã¼ã¿ãé£çµããæååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic[0], 'abcdef'); } }, 'æåã®ã¤ãã³ãã«ã ãæ¹è¡ãå«ã¾ããå ´å' : { topic : function(topic) { return topic( [ 'abc\ndef', 'ghi' ]); }, '2åã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.length(topic, 2); }, 'æåã«1çªç®ã®ãã¼ã¿ã®æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic[0], 'abc'); }, '次ã«1çªç®ã®ãã¼ã¿ã®æ¹è¡ããå¾ã¨2çªç®ã®ãã¼ã¿ãé£çµããæååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic[1], 'defghi'); } }, '2çªç®ã®ã¤ãã³ãã«ã ãæ¹è¡ãå«ã¾ããå ´å' : { topic : function(topic) { return topic( [ 'abc', 'def\nghi' ]); }, '2åã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.length(topic, 2); }, 'æåã«1çªç®ã®ãã¼ã¿ã¨2çªç®ã®ãã¼ã¿ã®æ¹è¡ããåãé£çµããæååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic[0], 'abcdef'); }, '次ã«2çªç®ã®ãã¼ã¿ã®æ¹è¡ããå¾ã®æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic[1], 'ghi'); } }, '1çªç®ã¨2çªç®ã®ä¸¡æ¹ã®ã¤ãã³ãã«æ¹è¡ãå«ã¾ããå ´å' : { topic : function(topic) { return topic( [ 'abc\ndef', 'ghi\njkl' ]); }, '3åã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.length(topic, 3); }, 'æåã«1çªç®ã®ãã¼ã¿ã®æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic[0], 'abc'); }, '次ã«1çªç®ã®ãã¼ã¿ã®æ¹è¡ããå¾ã¨ï¼2çªç®ã®ãã¼ã¿ã®æ¹è¡ããåãé£çµããæååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic[1], 'defghi'); }, 'æå¾ã«2çªç®ã®ãã¼ã¿ã®æ¹è¡ããå¾ã®æååãã³ã¼ã«ããã¯ããããã¨' : function(topic) { assert.equal(topic[2], 'jkl'); } } } } }).export(module);
å®è¡ãã¾ãï¼
$ vows read-line-test.js ····························· ✓ OK » 29 honored (0.012s)
--spec
ãã¾ãï¼
$ vows read-line-test.js --spec ♢ read-line å½ã®EventEmitterã使ã£ã¦ 1åã ããã¼ã¿åä¿¡ã¤ãã³ããçºçã ãã¼ã¿ãundefinedã®å ´å ✓ 1åãã³ã¼ã«ããã¯ãããªãã㨠å½ã®EventEmitterã使ã£ã¦ 1åã ããã¼ã¿åä¿¡ã¤ãã³ããçºçã ãã¼ã¿ãnullã®å ´å ✓ 1åãã³ã¼ã«ããã¯ãããªãã㨠å½ã®EventEmitterã使ã£ã¦ 1åã ããã¼ã¿åä¿¡ã¤ãã³ããçºçã ãã¼ã¿ã空æååã®å ´å ✓ 1åãã³ã¼ã«ããã¯ãããªãã㨠å½ã®EventEmitterã使ã£ã¦ 1åã ããã¼ã¿åä¿¡ã¤ãã³ããçºçã ãã¼ã¿ãæ¹è¡ãå«ã¾ãªãæååã®å ´å ✓ 1åã ãã³ã¼ã«ããã¯ãããã㨠✓ ãã¼ã¿å ¨ä½ãæååã¨ãã¦ã³ã¼ã«ããã¯ãããã㨠å½ã®EventEmitterã使ã£ã¦ 1åã ããã¼ã¿åä¿¡ã¤ãã³ããçºçã ãã¼ã¿ãæ¹è¡ã§å§ã¾ãæååã®å ´å ✓ 2åã³ã¼ã«ããã¯ãããã㨠✓ æåã«ç©ºè¡ãã³ã¼ã«ããã¯ãããã㨠✓ 次ã«æ¹è¡ã®å¾ã®æååãã³ã¼ã«ããã¯ãããã㨠å½ã®EventEmitterã使ã£ã¦ 1åã ããã¼ã¿åä¿¡ã¤ãã³ããçºçã ãã¼ã¿ãæ¹è¡ã§çµäºããæååã®å ´å ✓ 1åã ãã³ã¼ã«ããã¯ãããã㨠✓ æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ãããã㨠å½ã®EventEmitterã使ã£ã¦ 1åã ããã¼ã¿åä¿¡ã¤ãã³ããçºçã ãã¼ã¿ãéä¸ã«æ¹è¡ãå«ãæååã®å ´å ✓ 2åã³ã¼ã«ããã¯ãããã㨠✓ æåã«æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ãããã㨠✓ 次ã«æ¹è¡ã®å¾ã®æååãã³ã¼ã«ããã¯ãããã㨠å½ã®EventEmitterã使ã£ã¦ 1åã ããã¼ã¿åä¿¡ã¤ãã³ããçºçã ãã¼ã¿ãéä¸ã«æ¹è¡ãäºã¤å«ãæååã®å ´å ✓ 3åã³ã¼ã«ããã¯ãããã㨠✓ æåã«ä¸çªç®ã®æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ãããã㨠✓ 次ã«ä¸çªç®ã®æ¹è¡ã¨äºçªç®ã®æ¹è¡ã®éã®æååãã³ã¼ã«ããã¯ãããã㨠✓ æå¾ã«äºçªç®ã®æ¹è¡ããå¾ã®æååãã³ã¼ã«ããã¯ãããã㨠å½ã®EventEmitterã使ã£ã¦ è¤æ°åãã¼ã¿åä¿¡ã¤ãã³ããçºçã ã©ã®ãã¼ã¿ã«ãæ¹è¡ãå«ã¾ããªãå ´å ✓ 1åã ãã³ã¼ã«ããã¯ãããã㨠✓ ä¸çªç®ã®ãã¼ã¿ã¨äºçªç®ã®ãã¼ã¿ãé£çµããæååãã³ã¼ã«ããã¯ãããã㨠å½ã®EventEmitterã使ã£ã¦ è¤æ°åãã¼ã¿åä¿¡ã¤ãã³ããçºçã æåã®ã¤ãã³ãã«ã ãæ¹è¡ãå«ã¾ããå ´å ✓ 2åã³ã¼ã«ããã¯ãããã㨠✓ æåã«ä¸çªç®ã®ãã¼ã¿ã®æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ãããã㨠✓ 次ã«ä¸çªç®ã®ãã¼ã¿ã®æ¹è¡ããå¾ã¨äºçªç®ã®ãã¼ã¿ãé£çµããæååãã³ã¼ã«ããã¯ãããã㨠å½ã®EventEmitterã使ã£ã¦ è¤æ°åãã¼ã¿åä¿¡ã¤ãã³ããçºçã 2çªç®ã®ã¤ãã³ãã«ã ãæ¹è¡ãå«ã¾ããå ´å ✓ 2åã³ã¼ã«ããã¯ãããã㨠✓ æåã«ä¸çªç®ã®ãã¼ã¿ã¨äºçªç®ã®ãã¼ã¿ã®æ¹è¡ããåãé£çµããæååãã³ã¼ã«ããã¯ãããã㨠✓ 次ã«äºçªç®ã®ãã¼ã¿ã®æ¹è¡ããå¾ã®æååãã³ã¼ã«ããã¯ãããã㨠å½ã®EventEmitterã使ã£ã¦ è¤æ°åãã¼ã¿åä¿¡ã¤ãã³ããçºçã 1çªç®ã¨2çªç®ã®ä¸¡æ¹ã®ã¤ãã³ãã«æ¹è¡ãå«ã¾ããå ´å ✓ 3åã³ã¼ã«ããã¯ãããã㨠✓ æåã«ä¸çªç®ã®ãã¼ã¿ã®æ¹è¡ããåã®æååãã³ã¼ã«ããã¯ãããã㨠✓ 次ã«ä¸çªç®ã®ãã¼ã¿ã®æ¹è¡ããå¾ã¨ï¼äºçªç®ã®ãã¼ã¿ã®æ¹è¡ããåãé£çµããæååãã³ã¼ã«ããã¯ãããã㨠✓ æå¾ã«äºçªç®ã®ãã¼ã¿ã®æ¹è¡ããå¾ã®æååãã³ã¼ã«ããã¯ãããã㨠✓ OK » 29 honored (0.005s)
ãªããæºè¶³æãå¾ãããï¼
ããã¾ã§ã§æ¬æ¥ã®ã¨ã³ããªã® 6ã7 å²ã ã£ãã®ã§ããï¼é·ãããã®ãä½åº¦ãµãããããã¦ãéä¸ã§åãã¦ãã¾ãã®ã§ (è¦ç¬)ï¼ãã®ç¶ãã¯ããã® 3ãã¨ãã¾ãï¼ææ¥ã®ã¨ã³ããªã«ããæ¹ãç¡é£ããªï¼
ãµããããã§ããªããªããã¬ãã¥ã¼ã®æ®µéã§æãã¦ãï¼ã¯ã¦ãª
ç¶ãã®å
容ã¯ï¼æ¬ç©ã®ãã¡ã¤ã«ã使ã£ããã¹ãã®è¿½å ã§ãï¼