From f13561e200c3565398b395b3c0a06c62b96f24df Mon Sep 17 00:00:00 2001 From: Pierre-Yves Gerardy Date: Sun, 8 Jan 2017 21:06:35 +0100 Subject: [PATCH 1/3] [ospec] Improve the dupe detection code --- ospec/ospec.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/ospec/ospec.js b/ospec/ospec.js index 96a09ebe..d7ea1f2f 100644 --- a/ospec/ospec.js +++ b/ospec/ospec.js @@ -4,9 +4,8 @@ module.exports = new function init() { var spec = {}, subjects = [], results = [], only = null, ctx = spec, start, stack = 0, hasProcess = typeof process === "object" function o(subject, predicate) { - subject = unique(subject, predicate) - ctx[subject] = predicate if (predicate === undefined) return new Assert(subject) + ctx[unique(subject)] = predicate } o.before = hook("__before") o.after = hook("__after") @@ -15,8 +14,7 @@ module.exports = new function init() { o.new = init o.spec = function(subject, predicate) { var parent = ctx - subject = unique(subject, predicate) - ctx = ctx[subject] = {} + ctx = ctx[unique(subject)] = {} predicate() ctx = parent } @@ -77,7 +75,7 @@ module.exports = new function init() { var timeout = 0, delay = 200, s = new Date var isDone = false var body = fn.toString() - var arg = (body.match(/\(([\w_$]+)/) || body.match(/([\w_$]+)\s*=>/) || []).pop() + var arg = (body.match(/\(([\w$]+)/) || body.match(/([\w$]+)\s*=>/) || []).pop() if (body.indexOf(arg) === body.lastIndexOf(arg)) throw new Error("`" + arg + "()` should be called at least once") try { fn(function done() { @@ -112,12 +110,15 @@ module.exports = new function init() { } } } - function unique(subject, predicate) { - if (ctx.hasOwnProperty(subject) && predicate) { - console.warn("A test named `" + subject + "` was already defined") - subject = subject + "*" + var dedupeCounter = 0 + function unique(subject) { + var res = subject + if (ctx.hasOwnProperty(subject)) { + console.warn("A test or a spec named `" + subject + "` was already defined") + while (ctx.hasOwnProperty(res)) res = subject + ' (' + ++dedupeCounter + ')' + dedupeCounter = 0 } - return subject + return res } function hook(name) { return function(predicate) { From a7ea060a6402cca9704ae4cb04342fff1609bc93 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Gerardy Date: Mon, 9 Jan 2017 10:16:53 +0100 Subject: [PATCH 2/3] [ospec] Add `hasOwn` shortcut --- ospec/ospec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ospec/ospec.js b/ospec/ospec.js index d7ea1f2f..eb11629c 100644 --- a/ospec/ospec.js +++ b/ospec/ospec.js @@ -1,7 +1,7 @@ "use strict" module.exports = new function init() { - var spec = {}, subjects = [], results = [], only = null, ctx = spec, start, stack = 0, hasProcess = typeof process === "object" + var spec = {}, subjects = [], results = [], only = null, ctx = spec, start, stack = 0, hasProcess = typeof process === "object", hasOwn = ({}).hasOwnProperty function o(subject, predicate) { if (predicate === undefined) return new Assert(subject) @@ -156,7 +156,7 @@ module.exports = new function init() { var aKeys = Object.getOwnPropertyNames(a), bKeys = Object.getOwnPropertyNames(b) if (aKeys.length !== bKeys.length) return false for (var i = 0; i < aKeys.length; i++) { - if (!b.hasOwnProperty(aKeys[i]) || !deepEqual(a[aKeys[i]], b[aKeys[i]])) return false + if (!hasOwn.call(b, aKeys[i]) || !deepEqual(a[aKeys[i]], b[aKeys[i]])) return false } return true } From 396a7ff4e4b84a6909ab090c9bfe5ab2829eaf9d Mon Sep 17 00:00:00 2001 From: Pierre-Yves Gerardy Date: Mon, 9 Jan 2017 10:17:38 +0100 Subject: [PATCH 3/3] [ospec] Simplify the dupe checker --- ospec/ospec.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/ospec/ospec.js b/ospec/ospec.js index eb11629c..05b64f98 100644 --- a/ospec/ospec.js +++ b/ospec/ospec.js @@ -110,15 +110,12 @@ module.exports = new function init() { } } } - var dedupeCounter = 0 function unique(subject) { - var res = subject - if (ctx.hasOwnProperty(subject)) { + if (hasOwn.call(ctx, subject)) { console.warn("A test or a spec named `" + subject + "` was already defined") - while (ctx.hasOwnProperty(res)) res = subject + ' (' + ++dedupeCounter + ')' - dedupeCounter = 0 + while (hasOwn.call(ctx, subject)) subject += '*' } - return res + return subject } function hook(name) { return function(predicate) {