cleaned up the new do_match task

This commit is contained in:
Spencer Alger
2014-07-30 11:40:41 -07:00
parent 00a721a112
commit 6d0a15d82e

View File

@ -526,7 +526,9 @@ YamlDoc.prototype = {
var usedRE = false; var usedRE = false;
if (_.isString(match)) { if (_.isString(match)) {
// convert the matcher into a compatible string for building a regexp
maybeRE = match maybeRE = match
// replace comments, but allow the # to be escaped like \#
.replace(reComments_RE, function (match, prevChar) { .replace(reComments_RE, function (match, prevChar) {
if (prevChar === '\\') { if (prevChar === '\\') {
return match; return match;
@ -534,24 +536,32 @@ YamlDoc.prototype = {
return prevChar + '\n'; return prevChar + '\n';
} }
}) })
// remove all whitespace from the expression, all meaningful
// whitespace is represented with \s
.replace(reWhitespace_RE, ''); .replace(reWhitespace_RE, '');
}
if (maybeRE && maybeRE[0] === '/' && maybeRE[maybeRE.length - 1] === '/') { var startsWithSlash = maybeRE[0] === '/';
usedRE = true; var endsWithSlash = maybeRE[maybeRE.length - 1] === '/';
// replace anymore than one space with a single space
match = new RegExp(maybeRE.substr(1, maybeRE.length - 2));
}
var val; if (startsWithSlash && endsWithSlash) {
try { usedRE = true;
if (match instanceof RegExp) { match = new RegExp(maybeRE.substr(1, maybeRE.length - 2));
val = this.get(path) || '';
expect(val).to.match(match, 'path: ' + path);
} else {
val = this.get(path);
expect(val).to.eql(match, 'path: ' + path);
} }
}
var val = this.get(path);
var test = 'eql';
if (match instanceof RegExp) {
test = 'match';
// convert falsy values to an empty string so that regexp doesn't
// cast them to the strings "false", "undefined", etc.
val = val || '';
}
try {
expect(val).to[test](match);
} catch (e) { } catch (e) {
var msg = [ var msg = [
'\nUnable to match', '\nUnable to match',
@ -559,12 +569,22 @@ YamlDoc.prototype = {
'with the path', 'with the path',
inspect(path), inspect(path),
'and value', 'and value',
inspect(val), inspect(val)
'and original matcher',
'|' + origMatch,
''
]; ];
throw new Error(msg.slice(0, usedRE ? void 0 : -3).join('\n'));
if (usedRE) {
msg.push(
'and original matcher',
'|' + origMatch
);
}
msg.push(
'original error',
e.message
);
throw new Error(msg.join('\n'));
} }
}, this); }, this);
}, },