forked from nodeca/js-yaml
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for a listener to capture parse events
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use strict'; | ||
|
||
var assert = require('assert'); | ||
var yaml = require('../../'); | ||
|
||
test('Listener informed on a very simple scalar.', function () { | ||
var history = []; | ||
function l(eventType, state) { | ||
history.push([ eventType, state.position ]); | ||
} | ||
|
||
yaml.load('a_simple_scalar', { listener: l }); | ||
|
||
// 2 open events then 2 close events | ||
assert.equal(history.length, 4); | ||
assert.equal(history[0][0], 'open'); | ||
assert.equal(history[1][0], 'open'); | ||
assert.equal(history[2][0], 'close'); | ||
assert.equal(history[3][0], 'close'); | ||
assert.equal(history[0][1], 0); | ||
assert.equal(history[3][1], 16); | ||
}); | ||
|
||
test('Listener informed on a map with a list.', function () { | ||
var history = []; | ||
function l(eventType, state) { | ||
history.push([ eventType, state.position, state.result ]); | ||
} | ||
|
||
yaml.load('{ a: 1, b: [ 0, xyz ] }', { listener: l }); | ||
|
||
var i = -1; | ||
assert.equal(history[++i][0], 'open'); // doc | ||
assert.equal(history[++i][0], 'open'); // map | ||
|
||
assert.equal(history[++i][0], 'open'); // key | ||
assert.equal(history[++i][0], 'close'); | ||
assert.equal(history[i][2], 'a'); | ||
|
||
assert.equal(history[++i][0], 'open'); // a value | ||
assert.equal(history[++i][0], 'close'); | ||
assert.equal(history[i][2], 1); | ||
|
||
assert.equal(history[++i][0], 'open'); // key | ||
assert.equal(history[++i][0], 'close'); | ||
assert.equal(history[i][2], 'b'); | ||
|
||
assert.equal(history[++i][0], 'open'); // b value (list) | ||
assert.equal(history[++i][0], 'open'); // item in list | ||
assert.equal(history[++i][0], 'close'); | ||
assert.equal(history[i][2], 0); | ||
assert.equal(history[++i][0], 'open'); // item in list | ||
assert.equal(history[++i][0], 'close'); | ||
|
||
assert.equal(history[++i][0], 'close'); // b value (list) end | ||
assert.deepEqual(history[i][2], [ 0, 'xyz' ]); | ||
|
||
assert.equal(history[++i][0], 'close'); // map end | ||
assert.equal(history[++i][0], 'close'); // doc end | ||
|
||
assert.equal(history.length, ++i); | ||
}); |