Permalink
Browse files

Allow t.similar(str, regexp) to work as expected

  • Loading branch information...
1 parent 017c0c1 commit 4e9e519e8723b020bf0098f4914e10c15ed9433c @isaacs isaacs committed Feb 23, 2012
Showing with 23 additions and 8 deletions.
  1. +23 −8 lib/tap-assert.js
View
@@ -220,15 +220,32 @@ syns.inequivalent = ["notEquivalent"
,"isNotEquivalent"
,"isInequivalent"]
-function similar (a, b, message, extra) {
+function similar (a, b, message, extra, flip) {
if (extra && extra.skip) return assert.skip(message, extra)
// test that a has all the fields in b
message = message || "should be similar"
- var isObj = assert(a, message, extra);
- if(!isObj.ok)
- return isObj;
- return equivalent(selectFields(a, b), b, message, extra)
+ if (typeof a === "string" &&
+ (Object.prototype.toString.call(b) === "[object RegExp]")) {
+ extra = extra || {}
+ extra.pattern = b
+ extra.string = a
+ var ok = a.match(b)
+ extra.match = ok
+ if (flip) ok = !ok
+ return assert.ok(ok, message, extra)
+ }
+
+ var isObj = assert(a && typeof a === "object", message, extra)
+ if (!isObj.ok) {
+ // not an object
+ if (a === b) isObj.ok = true
+ if (flip) isObj.ok = !isObj.ok
+ return isObj
+ }
+
+ var eq = flip ? inequivalent : equivalent
+ return eq(selectFields(a, b), b, message, extra)
}
assert.similar = similar
syns.similar = ["isSimilar"
@@ -239,10 +256,8 @@ syns.similar = ["isSimilar"
function dissimilar (a, b, message, extra) {
if (extra && extra.skip) return assert.skip(message, extra)
- // test that a has all the fields in b
message = message || "should be dissimilar"
- a = a && selectFields(a, b); // Always send selectFields() usable params.
- return inequivalent(a, b, message, extra)
+ return similar(a, b, message, extra, true)
}
assert.dissimilar = dissimilar
syns.dissimilar = ["unsimilar"

0 comments on commit 4e9e519

Please sign in to comment.