Skip to content

Commit

Permalink
Renovated tests, fixed function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
chbrown committed Jan 10, 2013
1 parent 1742862 commit b9f679c
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 264 deletions.
50 changes: 26 additions & 24 deletions lib/rendering.js
Expand Up @@ -66,13 +66,7 @@ function stringEval(context, expression) {

var parts = expression.split('.');
for (var i = 0, part; (part = parts[i]) !== undefined; i++) {
var next = context[part];
if (typeof(next) === 'function') {
context = next();
}
else {
context = next;
}
var context = context[part];
if (context === undefined || context === null) {
return undefined;
}
Expand All @@ -98,34 +92,38 @@ function expressionEval(context, expression) {
else if (op === '>=') return left >= right;
else throw new Error("Unrecognized operator: " + op);
}
else if (m = expression.match(/^\S\(\S*\)$/)) {
// xxx: this TOTALLY needs fixing.
else if (m = expression.match(/^(\S+)\((\S*)\)$/)) {
// functions.
// open parens indicates function application,
// the entire following conditional is for functional application.
var open_parens_index = new_node.val.indexOf('(');
// close_parens_index should be append_to_top.length - 1
var close_parens_index = new_node.val.lastIndexOf(')');

// String.substring takes two indices (not a length)
var argument_part = new_node.val.substring(open_parens_index + 1, close_parens_index);
// var argument_part = new_node.val.substring(open_parens_index + 1, close_parens_index);
// trim whitespace/quotes since arguments can only be strings or ints anyway. At least, for now.
// (todo: let them embed commas in strings)
var argument_array = argument_part.split(',').map(function(s) {
var trimmed_val = s.replace(/^\s+|\s+$/g, '');
var type = 'variable';
if (trimmed_val.match(/^['"].*['"]$/)) {
type = 'static';
trimmed_val = trimmed_val.slice(1, -1);
console.log('\nm:', m[1], '(', m[2], ')', context);
var func = stringEval(context, m[1]) || eval(m[1]);
console.log('func:', func);
var args = m[2].split(',').map(function(raw) {
var val = raw.replace(/^\s+|\s+$/g, '');
console.log('arg:', val);
if (val.match(/^('|").*\1$/)) { // string literal
val = val.slice(1, -1);
}
else if (val.match(/^-?\d+$/)) { // integer literal
val = parseInt(val, 10);
}
else if (trimmed_val.match(/^-?\d+\.?\d*$/)) {
// it's a number! but it's okay for it to stay a string, here.
type = 'static';
else if (val.match(/^-?\d+\.\d+$/)) { // float literal
val = parseFloat(val);
}
return {t: type, val: trimmed_val};
else if (val.match(/^\w+$/)) { // variable reference
val = stringEval(context, val);
}
return val;
});

new_node.val = new_node.val.slice(0, open_parens_index);
new_node['arguments'] = argument_array;
return func.apply(null, args);
}
else {
return stringEval(context, expression);
Expand Down Expand Up @@ -251,6 +249,10 @@ Renderer.prototype.renderTokens = function(tokens, context, callback) {
else {
(function bump() {
var value = expressionEval(context, node.val);
if (typeof(value) === 'function') {
value = value();
}

if (value === undefined && renderer.asap) {
// this.asap can be true if we want to wait,
// and false if we merely want to ignore the missing contxt variables.
Expand Down
72 changes: 0 additions & 72 deletions tests/extended_spec.yaml

This file was deleted.

64 changes: 0 additions & 64 deletions tests/lib.js

This file was deleted.

68 changes: 68 additions & 0 deletions tests/local_spec.js
@@ -0,0 +1,68 @@
var fs = require('fs'),
yaml = require('js-yaml');
amulet = require('../lib/rendering'),
async = require('async'),
argv = require('optimist').default({'ignore-whitespace': true, extended: false}).argv;

if (argv.extended) {
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
};
String.prototype.titleize = function() {
var result = [];
var parts = this.split(" ");
for (var ii in parts) {
result.push(capitalize(parts[ii]));
}
return result.join(" ");
};
String.prototype.humanize = function() {
return titleize(this.replace(/_/g, ' '));
};
String.prototype.equals = function(test) {
return this.valueOf() === test;
};
}

var successes = 0, failures = 0;

function runTest(test, callback) {
var context = {};
process.stdout.write(' Spec: ' + test.description + ' ');
amulet.parseTemplate(test.description, test.template);

try {
context = eval('(' + test.context + ')');
}
catch (e) {
console.error('Reading context failed', e, test.context);
}

amulet.renderString(test.description, context, function(err, output) {
var matches = output == test.output;
if (argv['ignore-whitespace'] && !matches)
matches = output.replace(/\s+/g, '') == test.output.replace(/\s+/g, '');

if (matches) {
successes++;
process.stdout.write('[Success]\n');
}
else {
failures++;
process.stdout.write('[Failed]\n Expected:\n' + test.output + '\n Actual:\n' + output + '\n');
}

callback(err);
});
}

// runSpec('extended_spec.yaml');
var full_spec = yaml.load(fs.readFileSync('local_spec.yaml', 'utf8'));
var tests = full_spec.tests;
if (argv.extended) {
tests.push.apply(tests, full_spec.extended_tests);
}
async.forEachSeries(tests, runTest, function(callback) {
var success_rate = successes / (successes + failures);
console.log((success_rate * 100 | 0) + '% success');
});

0 comments on commit b9f679c

Please sign in to comment.