Created
May 8, 2011 21:55
-
-
Save foogoof/961719 to your computer and use it in GitHub Desktop.
trying to verify a sequence of messages in one vows.js test
This file contains 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
// vows calls transmute, which registers trap as the listener to all specified events | |
// node sends each message to the trap() callback | |
// FAIL: trap calls this.callback with its arguments | |
// PASS: trap calls this.callback with a property which cached arguments from each invocation of trap | |
// HOWTO pass / fail: toggle the conditional at line 152 | |
// revised sample output: | |
// | |
// PASS | |
// | |
// DEBUG: { event: 'Raw Text', rows: 'hello' } | |
// DEBUG: { trapped_values: [ 'hello' ] } | |
// DEBUG: { remainder: '\u001b[B' } | |
// DEBUG: { event: 'Cursor Down', rows: 1 } | |
// DEBUG: { trapped_values: [ 1 ] } | |
// DEBUG: { received_actual_results: [ 'hello', [ 1 ] ] } | |
// · | |
// ✓ OK » 1 honored (0.006s) | |
// DEBUG: { received_actual_results: [ 'hello', [ 1 ] ] } | |
// · | |
// | |
// FAIL (from node) | |
// | |
// DEBUG: { event: 'Raw Text', rows: 'hello' } | |
// DEBUG: { trapped_values: [ 'hello' ] } | |
// DEBUG: { remainder: '\u001b[B' } | |
// DEBUG: { event: 'Cursor Down', rows: 1 } | |
// DEBUG: { trapped_values: [ 1 ] } | |
// DEBUG: { received_actual_results: [ 'hello' ] } | |
// DEBUG: { actual: [ 'hello' ], expected: [ 'hello', [ 1 ] ] } | |
// ✗ | |
// friendly data data then control | |
// ✗ should be two lines | |
// » expected expression to evaluate to true, but was false // foo.js:155 | |
// ✗ Broken » 1 broken (0.012s) | |
// DEBUG: { received_actual_results: [ 1 ] } | |
// DEBUG: { actual: [ 1 ], expected: [ 'hello', [ 1 ] ] } | |
// ✗ | |
// | |
// FAIL (from vows) | |
// | |
// vows foo.js | |
// DEBUG: { event: 'Raw Text', rows: 'hello' } | |
// DEBUG: { trapped_values: [ 'hello' ] } | |
// DEBUG: { remainder: '\u001b[B' } | |
// DEBUG: { event: 'Cursor Down', rows: 1 } | |
// DEBUG: { trapped_values: [ 1 ] } | |
// DEBUG: { received_actual_results: [ 'hello' ] } | |
// DEBUG: { actual: [ 'hello' ], expected: [ 'hello', [ 1 ] ] } | |
// ✗ | |
// | |
// friendly data data then control | |
// ✗ should be two lines | |
// » expected expression to evaluate to true, but was false // foo.js:169 | |
// | |
// ✗ Broken » 1 broken (0.008s) | |
// | |
// HOWTO run: node recall_this_callback.js | |
// | |
// environment: vows 0.5.8, node 0.4.6 | |
// | |
// PLEASE NOTE: this code is really awkward and crude at this point in its life | |
var vows = require('vows'), | |
assert = require('assert'), | |
_ = require('underscore'), | |
events = require('events'), | |
util = require('util'); | |
var suite = vows.describe('ansi control seq callback test'); | |
var debug_inspect = _.compose(util.debug, util.inspect); | |
//////////////////////////////////////////////////////////////////////////////// | |
var read_all_codes = function(val) { | |
var remainder = val; | |
do { | |
remainder = this.read_code(remainder); | |
if (remainder) debug_inspect({remainder: remainder}); | |
} while (remainder); | |
}; | |
// Code intentionally crude, working slowly towards a general solution | |
var read_code = function(val) { | |
var event = undefined; | |
var rows = undefined; | |
var next_escape = val.indexOf('\x1b'); | |
var remainder = ''; | |
if (-1 === next_escape) { | |
event = 'Raw Text'; | |
rows = val; | |
} | |
else if (0 !== next_escape) { | |
event = 'Raw Text'; | |
rows = val.substring(0, next_escape); | |
remainder = val.slice(next_escape); | |
} | |
else if (0 === next_escape) { | |
if (1 === val.indexOf('[')) { | |
if (val.match(/A$/) ) { | |
event = 'Cursor Up'; | |
} else if (val.match(/B$/)) { | |
event = 'Cursor Down'; | |
} | |
if (val.match(/\[2/)) { | |
rows = 2; | |
} else if (val.match(/\[1/) || val.match(/\[[^\d]/)) { | |
rows = 1; | |
} | |
} else if (1 === val.indexOf(')')) { | |
event = 'setspecg1'; | |
} | |
} | |
debug_inspect({event:event, rows:rows}); | |
if (event) { | |
this.emit(event, rows); | |
} | |
return remainder; | |
}; | |
//////////////////////////////////////////////////////////////////////////////// | |
var Machine = (function() { | |
util.inherits(Machine, events.EventEmitter); | |
function Machine() { } | |
Machine.prototype.read = read_all_codes; | |
Machine.prototype.read_code = read_code; | |
return Machine; | |
})(); | |
//////////////////////////////////////////////////////////////////////////////// | |
var transmute = function(event, value) { | |
return function() { | |
var machine = new Machine(); | |
var trap = function() { | |
var args = _.compact(arguments); | |
debug_inspect({trapped_values: args}); | |
if (false) { | |
this.callback(null, args); | |
} else { | |
if (!this.pool) | |
this.pool = args; | |
else | |
this.pool.push(args); | |
this.callback(null, this.pool); | |
} | |
}; | |
if (!_.isArray(event)) { | |
event = [event]; | |
} | |
var that = this; | |
_.each(event, function(evt) { | |
machine.on(evt, _.bind(trap, that)); | |
}); | |
machine.read(value); | |
}; | |
} | |
var match_params = function(expected) { | |
return function(err, actual) { | |
debug_inspect({received_actual_results:actual}); | |
assert.equal(err, undefined); | |
if (!_.isEqual(actual, expected)) { | |
debug_inspect({'actual': actual, 'expected': expected}); | |
} | |
assert.ok(_.isEqual(actual, expected)); | |
}; | |
}; | |
var flat_data_tests = { | |
'data then control': { | |
topic: transmute(['Raw Text', 'Cursor Down'], 'hello\x1b[B'), | |
'should be two lines': match_params(['hello',[1]]) | |
} | |
}; | |
suite.addBatch({'friendly data': flat_data_tests}); | |
suite.export(module); // required when code from CLI exe |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment