Permalink
Browse files

Rewrote functional tests to use vows as well

  • Loading branch information...
ixti committed Dec 17, 2011
1 parent bf30236 commit 694aaa17f78901f93ade8713f43131855ce0f8b8
View
@@ -25,22 +25,14 @@ lint:
# (white) -> tolerate messy whitespace
jslint --node --nomen --bitwise --white --indent=2 ${JS_FILES}
-test: lint test-functional
+test: lint
@if test ! `which vows` ; then \
echo "You need 'vows' installed in order to run tests." >&2 ; \
echo " $ make dev-deps" >&2 ; \
exit 128 ; \
fi
NODE_ENV=test vows --spec
-test-functional:
- echo
- echo "## FUNCTIONAL ##################################################################"
- echo "################################################################################"
- echo
- node ./test/functional/run.js
- echo
-
dev-deps:
@if test ! `which npm` ; then \
echo "You need 'npm' installed." >&2 ; \
View
@@ -0,0 +1,3 @@
+require(__dirname + '/helper')
+ .suite('Functional', __dirname + '/functional', /^functional-.+?\.js$/)
+ .export(module);
@@ -0,0 +1,46 @@
+var Assert = require('assert');
+var Fs = require('fs');
+var JsYaml = require('../../lib/js-yaml');
+var YAMLError = require('../../lib/js-yaml/errors').YAMLError;
+var Helper = require('../helper');
+
+
+module.exports = {
+ "Test errors loading all documents from file resource": Helper.functional({
+ dirname: __dirname + '/data',
+ files: ['.loader-error'],
+ test: function (errorFilename) {
+ Assert.throws(function () {
+ var fd = Fs.openSync(errorFilename, 'r');
+ JsYaml.loadAll(fd, function () {});
+ Fs.closeSync(fd);
+ }, YAMLError);
+ }
+ }),
+
+ "Test errors loading all documents from the string": Helper.functional({
+ dirname: __dirname + '/data',
+ files: ['.loader-error'],
+ test: function (errorFilename) {
+ Assert.throws(function () {
+ var str = Fs.readFileSync(errorFilename, 'utf8');
+ JsYaml.loadAll(str, function () {});
+ }, YAMLError);
+ }
+ }),
+
+ "Test errors loading single documents from the string": Helper.functional({
+ dirname: __dirname + '/data',
+ files: ['.single-loader-error'],
+ test: function (errorFilename) {
+ Assert.throws(function () {
+ JsYaml.load(Fs.readFileSync(errorFilename, 'utf8'));
+ }, YAMLError);
+ }
+ })
+};
+
+
+////////////////////////////////////////////////////////////////////////////////
+// vim:ts=2:sw=2
+////////////////////////////////////////////////////////////////////////////////
@@ -0,0 +1,33 @@
+var Assert = require('assert');
+var Fs = require('fs');
+var JsYaml = require('../../lib/js-yaml');
+var Common = require('../../lib/js-yaml/common');
+var Nodes = require('../../lib/js-yaml/nodes');
+var Helper = require('../helper');
+
+
+module.exports = {
+ "Test implicit resolver": Helper.functional({
+ dirname: __dirname + '/data',
+ files: ['.data', '.detect'],
+ test: function (dataFilename, detectFilename) {
+ var correctTag, node;
+
+ node = JsYaml.compose(Fs.readFileSync(dataFilename, 'utf8'));
+ correctTag = Fs.readFileSync(detectFilename, 'utf8')
+ .replace(/^[ \s]+|[ \s]+$/g, '');
+
+ Assert.equal(Common.isInstanceOf(node, Nodes.SequenceNode), true);
+
+ Common.each(node.value, function (scalar) {
+ Assert.equal(Common.isInstanceOf(scalar, Nodes.ScalarNode), true);
+ Assert.equal(scalar.tag, correctTag);
+ });
+ }
+ })
+};
+
+
+////////////////////////////////////////////////////////////////////////////////
+// vim:ts=2:sw=2
+////////////////////////////////////////////////////////////////////////////////
@@ -0,0 +1,78 @@
+var Assert = require('assert');
+var Fs = require('fs');
+var JsYaml = require('../../lib/js-yaml');
+var Loader = require('../../lib/js-yaml/loader');
+var Events = require('../../lib/js-yaml/events');
+var Helper = require('../helper');
+
+
+function convertStructure(loader) {
+ var event, sequence, mapping, key, value;
+
+ if (loader.checkEvent(Events.ScalarEvent)) {
+ event = loader.getEvent();
+ return (!!event.tag || !!event.anchor || !!event.value);
+ } else if (loader.checkEvent(Events.SequenceStartEvent)) {
+ sequence = [];
+
+ loader.getEvent();
+ while (!loader.checkEvent(Events.SequenceEndEvent)) {
+ sequence.push(convertStructure(loader));
+ }
+ loader.getEvent();
+
+ return sequence;
+ } else if (loader.checkEvent(Events.MappingStartEvent)) {
+ mapping = [];
+
+ loader.getEvent();
+ while (!loader.checkEvent(Events.MappingEndEvent)) {
+ key = convertStructure(loader);
+ value = convertStructure(loader);
+ mapping.push([key, value]);
+ }
+ loader.getEvent();
+
+ return mapping;
+ } else if (loader.checkEvent(Events.AliasEvent)) {
+ loader.getEvent();
+ return '*';
+ } else {
+ loader.getEvent();
+ return '?';
+ }
+}
+
+
+module.exports = {
+ "Test tokens": Helper.functional({
+ dirname: __dirname + '/data',
+ files: ['.data', '.structure'],
+ test: function (dataFile, structureFile) {
+ var result = [], expected, loader;
+
+ expected = JSON.parse(Fs.readFileSync(structureFile, 'utf8'));
+ loader = new Loader.SafeLoader(Fs.readFileSync(dataFile, 'utf8'));
+
+ while (loader.checkEvent()) {
+ if (loader.checkEvent(Events.StreamStartEvent, Events.StreamEndEvent,
+ Events.DocumentStartEvent, Events.DocumentEndEvent)) {
+ loader.getEvent();
+ } else {
+ result.push(convertStructure(loader));
+ }
+ }
+
+ if (1 === result.length) {
+ result = result.shift();
+ }
+
+ Assert.deepEqual(result, expected);
+ }
+ })
+};
+
+
+////////////////////////////////////////////////////////////////////////////////
+// vim:ts=2:sw=2
+////////////////////////////////////////////////////////////////////////////////
@@ -0,0 +1,81 @@
+var Assert = require('assert');
+var Fs = require('fs');
+var JsYaml = require('../../lib/js-yaml');
+var Common = require('../../lib/js-yaml/common');
+var Tokens = require('../../lib/js-yaml/tokens');
+var Helper = require('../helper');
+
+// Tokens mnemonic:
+// directive: %
+// document_start: ---
+// document_end: ...
+// alias: *
+// anchor: &
+// tag: !
+// scalar _
+// block_sequence_start: [[
+// block_mapping_start: {{
+// block_end: ]}
+// flow_sequence_start: [
+// flow_sequence_end: ]
+// flow_mapping_start: {
+// flow_mapping_end: }
+// entry: ,
+// key: ?
+// value: :
+
+var REPLACES = new Common.Hash();
+
+REPLACES.store(Tokens.DirectiveToken, '%');
+REPLACES.store(Tokens.DocumentStartToken, '---');
+REPLACES.store(Tokens.DocumentEndToken, '...');
+REPLACES.store(Tokens.AliasToken, '*');
+REPLACES.store(Tokens.AnchorToken, '&');
+REPLACES.store(Tokens.TagToken, '!');
+REPLACES.store(Tokens.ScalarToken, '_');
+REPLACES.store(Tokens.BlockSequenceStartToken, '[[');
+REPLACES.store(Tokens.BlockMappingStartToken, '{{');
+REPLACES.store(Tokens.BlockEndToken, ']}');
+REPLACES.store(Tokens.FlowSequenceStartToken, '[');
+REPLACES.store(Tokens.FlowSequenceEndToken, ']');
+REPLACES.store(Tokens.FlowMappingStartToken, '{');
+REPLACES.store(Tokens.FlowMappingEndToken, '}');
+REPLACES.store(Tokens.BlockEntryToken, ',');
+REPLACES.store(Tokens.FlowEntryToken, ',');
+REPLACES.store(Tokens.KeyToken, '?');
+REPLACES.store(Tokens.ValueToken, ':');
+
+
+module.exports = {
+ "Test tokens": Helper.functional({
+ dirname: __dirname + '/data',
+ files: ['.data', '.tokens'],
+ test: function (dataFile, tokensFile) {
+ var result = [], expected = [];
+
+ Fs.readFileSync(tokensFile, 'utf8').split(/[ \n]/).forEach(function (t) {
+ if (!!t) { expected.push(t); }
+ });
+
+ JsYaml.scan(Fs.readFileSync(dataFile, 'utf8'), function (token) {
+ if (Common.isInstanceOf(token, Tokens.StreamStartToken)
+ || Common.isInstanceOf(token, Tokens.StreamEndToken)) {
+ return;
+ }
+
+ result.push(REPLACES.get(token.constructor));
+ });
+
+ Assert.equal(result.length, expected.length);
+
+ result.forEach(function (token, i) {
+ Assert.equal(expected[i], token);
+ });
+ }
+ })
+};
+
+
+////////////////////////////////////////////////////////////////////////////////
+// vim:ts=2:sw=2
+////////////////////////////////////////////////////////////////////////////////
View
@@ -1 +0,0 @@
-require(__dirname + '/../test-helper').run(__dirname, /^test-.+\.js$/);
@@ -1,13 +0,0 @@
-var suite = module.exports = [],
- fs = require('fs'),
- assert = require('assert'),
- jsyaml = require(__dirname + '/../../lib/js-yaml'),
- helper = require(__dirname + '/../test-helper'),
- $$ = require(__dirname + '/../../lib/js-yaml/common'),
- _errors = require(__dirname + '/../../lib/js-yaml/errors');
-
-
-////////////////////////////////////////////////////////////////////////////////
-// vim:ts=2:sw=2
-////////////////////////////////////////////////////////////////////////////////
-
@@ -1,45 +0,0 @@
-var suite = module.exports = [],
- fs = require('fs'),
- assert = require('assert'),
- jsyaml = require(__dirname + '/../../lib/js-yaml'),
- helper = require(__dirname + '/../test-helper'),
- $$ = require(__dirname + '/../../lib/js-yaml/common'),
- _errors = require(__dirname + '/../../lib/js-yaml/errors');
-
-
-suite.push({
- unittest: ['.loader-error'],
- execute: function test_loader_error(error_filename) {
- assert.throws(function () {
- var fd = fs.openSync(error_filename, 'r');
- jsyaml.loadAll(fd, function (doc) {});
- fs.closeSync(fd);
- }, _errors.YAMLError);
- }
-});
-
-
-suite.push({
- unittest: ['.loader-error'],
- execute: function test_loader_error_string(error_filename) {
- assert.throws(function () {
- var str = fs.readFileSync(error_filename, 'utf8');
- jsyaml.loadAll(str, function (doc) {});
- }, _errors.YAMLError);
- }
-});
-
-
-suite.push({
- unittest: ['.single-loader-error'],
- execute: function test_loader_error_single(error_filename) {
- assert.throws(function () {
- jsyaml.load(fs.readFileSync(error_filename, 'utf8'));
- }, _errors.YAMLError);
- }
-});
-
-
-////////////////////////////////////////////////////////////////////////////////
-// vim:ts=2:sw=2
-////////////////////////////////////////////////////////////////////////////////
@@ -1,28 +0,0 @@
-var suite = module.exports = [],
- fs = require('fs'),
- assert = require('assert'),
- jsyaml = require(__dirname + '/../../lib/js-yaml'),
- helper = require(__dirname + '/../test-helper'),
- $$ = require(__dirname + '/../../lib/js-yaml/common'),
- _nodes = require(__dirname + '/../../lib/js-yaml/nodes');
-
-
-suite.push({
- unittest: ['.data', '.detect'],
- execute: function test_implicit_resolver(data_filename, detect_filename) {
- var correctTag = fs.readFileSync(detect_filename, 'utf8').replace(/^[ \s]+|[ \s]+$/g, ''),
- node = jsyaml.compose(fs.readFileSync(data_filename, 'utf8'));
-
- assert.equal($$.isInstanceOf(node, _nodes.SequenceNode), true);
-
- $$.each(node.value, function (scalar) {
- assert.equal($$.isInstanceOf(scalar, _nodes.ScalarNode), true);
- assert.equal(scalar.tag, correctTag);
- });
- }
-});
-
-
-////////////////////////////////////////////////////////////////////////////////
-// vim:ts=2:sw=2
-////////////////////////////////////////////////////////////////////////////////
Oops, something went wrong.

0 comments on commit 694aaa1

Please sign in to comment.