[ospec] Improve the dupe detection code
This commit is contained in:
parent
1d8021500a
commit
f13561e200
1 changed files with 11 additions and 10 deletions
|
|
@ -4,9 +4,8 @@ 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"
|
||||||
|
|
||||||
function o(subject, predicate) {
|
function o(subject, predicate) {
|
||||||
subject = unique(subject, predicate)
|
|
||||||
ctx[subject] = predicate
|
|
||||||
if (predicate === undefined) return new Assert(subject)
|
if (predicate === undefined) return new Assert(subject)
|
||||||
|
ctx[unique(subject)] = predicate
|
||||||
}
|
}
|
||||||
o.before = hook("__before")
|
o.before = hook("__before")
|
||||||
o.after = hook("__after")
|
o.after = hook("__after")
|
||||||
|
|
@ -15,8 +14,7 @@ module.exports = new function init() {
|
||||||
o.new = init
|
o.new = init
|
||||||
o.spec = function(subject, predicate) {
|
o.spec = function(subject, predicate) {
|
||||||
var parent = ctx
|
var parent = ctx
|
||||||
subject = unique(subject, predicate)
|
ctx = ctx[unique(subject)] = {}
|
||||||
ctx = ctx[subject] = {}
|
|
||||||
predicate()
|
predicate()
|
||||||
ctx = parent
|
ctx = parent
|
||||||
}
|
}
|
||||||
|
|
@ -77,7 +75,7 @@ module.exports = new function init() {
|
||||||
var timeout = 0, delay = 200, s = new Date
|
var timeout = 0, delay = 200, s = new Date
|
||||||
var isDone = false
|
var isDone = false
|
||||||
var body = fn.toString()
|
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")
|
if (body.indexOf(arg) === body.lastIndexOf(arg)) throw new Error("`" + arg + "()` should be called at least once")
|
||||||
try {
|
try {
|
||||||
fn(function done() {
|
fn(function done() {
|
||||||
|
|
@ -112,12 +110,15 @@ module.exports = new function init() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function unique(subject, predicate) {
|
var dedupeCounter = 0
|
||||||
if (ctx.hasOwnProperty(subject) && predicate) {
|
function unique(subject) {
|
||||||
console.warn("A test named `" + subject + "` was already defined")
|
var res = subject
|
||||||
subject = 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) {
|
function hook(name) {
|
||||||
return function(predicate) {
|
return function(predicate) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue