Skip to content
Browse files

Rewrote functional tests to use vows as well

  • Loading branch information...
1 parent bf30236 commit 694aaa17f78901f93ade8713f43131855ce0f8b8 @ixti ixti committed Dec 17, 2011
View
10 Makefile
@@ -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
3 test/functional-test.js
@@ -0,0 +1,3 @@
+require(__dirname + '/helper')
+ .suite('Functional', __dirname + '/functional', /^functional-.+?\.js$/)
+ .export(module);
View
46 test/functional/functional-errors.js
@@ -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
+////////////////////////////////////////////////////////////////////////////////
View
33 test/functional/functional-resolver.js
@@ -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
+////////////////////////////////////////////////////////////////////////////////
View
78 test/functional/functional-structure.js
@@ -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
+////////////////////////////////////////////////////////////////////////////////
View
81 test/functional/functional-tokens.js
@@ -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 test/functional/run.js
@@ -1 +0,0 @@
-require(__dirname + '/../test-helper').run(__dirname, /^test-.+\.js$/);
View
13 test/functional/test-canonical.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
-////////////////////////////////////////////////////////////////////////////////
-
View
45 test/functional/test-errors.js
@@ -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
-////////////////////////////////////////////////////////////////////////////////
View
28 test/functional/test-resolver.js
@@ -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
-////////////////////////////////////////////////////////////////////////////////
View
73 test/functional/test-structure.js
@@ -1,73 +0,0 @@
-var test = module.exports = {},
- fs = require('fs'),
- assert = require('assert'),
- jsyaml = require(__dirname + '/../../lib/js-yaml'),
- helper = require(__dirname + '/../test-helper'),
- _loader = require(__dirname + '/../../lib/js-yaml/loader'),
- _events = require(__dirname + '/../../lib/js-yaml/events');
-
-
-var convertStructure = 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 '?';
- }
-};
-
-
-test.unittest = ['.data', '.structure'];
-test.execute = function test_structure(data_filename, structure_filename) {
- var nodes1 = [],
- nodes2 = JSON.parse(fs.readFileSync(structure_filename, 'utf8')),
- loader = new _loader.SafeLoader(fs.readFileSync(data_filename, 'utf8'));
-
- while (loader.checkEvent()) {
- if (loader.checkEvent(_events.StreamStartEvent, _events.StreamEndEvent,
- _events.DocumentStartEvent, _events.DocumentEndEvent)) {
- loader.getEvent();
- } else {
- nodes1.push(convertStructure(loader));
- }
- }
-
- if (1 === nodes1.length) {
- nodes1 = nodes1.shift();
- }
-
- assert.deepEqual(nodes2, nodes1);
-};
-
-
-////////////////////////////////////////////////////////////////////////////////
-// vim:ts=2:sw=2
-////////////////////////////////////////////////////////////////////////////////
View
76 test/functional/test-tokens.js
@@ -1,76 +0,0 @@
-var test = 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'),
- _tokens = require(__dirname + '/../../lib/js-yaml/tokens');
-
-// 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 $$.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, ':');
-
-
-test.unittest = ['.data', '.tokens'];
-test.execute = function test_tokens(data_filename, tokens_filename) {
- var tokens1 = [], tokens2 = [];
-
- fs.readFileSync(tokens_filename, 'utf8').split(/[ \n]/).forEach(function (t) {
- if (!!t) { tokens2.push(t); }
- });
-
- jsyaml.scan(fs.readFileSync(data_filename, 'utf8'), function (token) {
- if ($$.isInstanceOf(token, _tokens.StreamStartToken) || $$.isInstanceOf(token, _tokens.StreamEndToken)) {
- return;
- }
-
- tokens1.push(REPLACES.get(token.constructor));
- });
-
- assert.equal(tokens1.length, tokens2.length);
-
- tokens1.forEach(function (token1, i) {
- assert.equal(token1, tokens2[i]);
- });
-};
-
-
-////////////////////////////////////////////////////////////////////////////////
-// vim:ts=2:sw=2
-////////////////////////////////////////////////////////////////////////////////
View
46 test/helper.js
@@ -2,6 +2,7 @@ var Vows = require('vows');
var Assert = require('assert');
var Path = require('path');
var Fs = require('fs');
+var Common = require('../lib/js-yaml/common');
var Helper = module.exports = {};
@@ -34,3 +35,48 @@ Helper.issue = function issue(desc) {
return batch;
};
+
+
+function findTestFilenames(dataDir) {
+ var filenames = {};
+
+ Fs.readdirSync(dataDir).forEach(function (file) {
+ var ext = Path.extname(file),
+ base = Path.basename(file, ext);
+
+ if (undefined === filenames[base]) {
+ filenames[base] = [];
+ }
+
+ filenames[base].push(ext);
+ });
+
+ return filenames;
+}
+
+
+Helper.functional = function functional(desc) {
+ var batch = {};
+
+ Common.each(findTestFilenames(desc.dirname), function (exts, base) {
+ var filenames = [], name;
+
+ desc.files.forEach(function (ext) {
+ if (0 <= exts.indexOf(ext)) {
+ filenames.push(Path.join(desc.dirname, base + ext));
+ }
+ });
+
+ if (filenames.length === desc.files.length) {
+ name = filenames.map(function (f) {
+ return Path.basename(f);
+ }).join(', ');
+
+ batch[name] = function () {
+ desc.test.apply(desc.test, filenames);
+ };
+ }
+ });
+
+ return batch;
+};
View
147 test/test-helper.js
@@ -1,147 +0,0 @@
-// stdlib
-var fs = require('fs'),
- path = require('path');
-
-
-// internal
-var $$ = require(__dirname + '/../lib/js-yaml/common');
-
-
-// current module
-var helper = module.exports = {};
-
-
-// colorizes given text string
-var colorize = function colorize(color, text) {
- return '\033[' + color + 'm' + text + '\033[39m';
-};
-
-
-var warning = function (message) { console.log(colorize(33, 'WARN') + ' ' + message); };
-var success = function (message) { console.log(colorize(32, 'PASS') + ' ' + message); };
-var failure = function (message) { console.log(colorize(31, 'FAIL') + ' ' + message); };
-var generic = function (message) { console.log(colorize(31, '!!!!') + ' ' + message); };
-var verbose = function (message) { console.log(colorize(90, '>>>>') + ' ' + message); };
-
-
-var MAX_WIDTH = 70; // max width is 75 - 5 (code prefix with space)
-
-
-var stats = {pass: 0, fail: 0, warn: 0, error: 0};
-
-
-// wraps func into try-catch block
-var execute = function execute(test, args, message) {
- try {
- test.execute.apply(test, args);
-
- // issue was not marked as fixed yet, but it pass test
- if (false === test.fixed) {
- warning(message);
- stats.warn += 1;
- return;
- }
-
- success(message);
- stats.pass += 1;
- } catch (err) {
- if ('AssertionError' === err.name) {
- failure(message);
- stats.fail += 1;
- } else {
- generic(message);
- stats.error += 1;
- }
-
- verbose(err);
- }
-};
-
-
-var shrink = function shrink(message, maxLength) {
- return (message + '').slice(0, maxLength - 3) + '...';
-};
-
-
-var findTestFilenames = function findTestFilenames(dataDir) {
- var filenames = {};
-
- fs.readdirSync(dataDir).forEach(function (file) {
- var ext = path.extname(file),
- base = path.basename(file, ext);
-
- if (undefined === filenames[base]) {
- filenames[base] = [];
- }
-
- filenames[base].push(ext);
- });
-
- return filenames;
-};
-
-
-// runs single test
-var runTest = function runTest(root, file, test) {
- var message = file + ' // ' + (test.title || test.execute.name);
-
- // execute simple test
- if (!test.unittest) {
- execute(test, [], shrink(message, MAX_WIDTH));
- return;
- }
-
- // run unit tests for all found desired files
- $$.each(findTestFilenames(root + '/data'), function (exts, base) {
- var filenames = [], msgPrefix, msgSuffix;
-
- $$.each(test.unittest, function (ext) {
- if (0 <= exts.indexOf(ext)) {
- filenames.push(root + '/data/' + base + ext);
- }
- });
-
- if (filenames.length === test.unittest.length) {
- msgSuffix = ' (' + base + ')';
- msgPrefix = shrink(message, MAX_WIDTH - msgSuffix.length);
- execute(test, filenames, msgPrefix + msgSuffix);
- }
- });
-};
-
-
-// run tests
-helper.run = function run(root, regexp) {
- var total = 0;
-
- fs.readdirSync(root).forEach(function (file) {
-
- if (!regexp.test(file)) {
- // skip non-test files
- return;
- }
-
- var tests = require(root + '/' + file);
-
- if (!Array.isArray(tests)) {
- tests = [tests];
- }
-
- tests.forEach(function (test) {
- runTest(root, file, test);
- });
- });
-
- total += stats.pass + stats.warn + stats.fail + stats.error;
- console.log(colorize(90, '----' + ' (' + total + ') ' +
- ' P:' + stats.pass + ' W:' + stats.warn +
- ' F:' + stats.fail + ' E:' + stats.error));
-
- // set correct error code on exit
- process.exit((stats.warn + stats.fail + stats.error) > 0 ? 1 : 0);
-};
-
-
-////////////////////////////////////////////////////////////////////////////////
-// vim:ts=2:sw=2
-////////////////////////////////////////////////////////////////////////////////

0 comments on commit 694aaa1

Please sign in to comment.
Something went wrong with that request. Please try again.