[ospec] Change the reserved test name token from '__' to '\x01'

This commit is contained in:
Pierre-Yves Gérardy 2018-05-27 10:29:06 +02:00 committed by Pierre-Yves Gérardy
parent 0574d193a0
commit 33180370ea
2 changed files with 16 additions and 21 deletions

View file

@ -8,13 +8,6 @@ else window.o = m()
var ospecFileName = getStackName(ensureStackTrace(new Error), /[\/\\](.*?):\d+:\d+/), timeoutStackName var ospecFileName = getStackName(ensureStackTrace(new Error), /[\/\\](.*?):\d+:\d+/), timeoutStackName
var globalTimeout = noTimeoutRightNow var globalTimeout = noTimeoutRightNow
var currentTestError = null var currentTestError = null
var reservedNames = {
__before: true,
__beforeEach: true,
__after: true,
__afterEach: true,
__defaultTimeout: true
}
if (name != null) spec[name] = ctx = {} if (name != null) spec[name] = ctx = {}
function o(subject, predicate) { function o(subject, predicate) {
@ -24,20 +17,19 @@ else window.o = m()
} else { } else {
if (isRunning()) throw new Error("Test definitions and hooks shouldn't be nested. To group tests use `o.spec()`") if (isRunning()) throw new Error("Test definitions and hooks shouldn't be nested. To group tests use `o.spec()`")
subject = String(subject) subject = String(subject)
if (hasOwn.call(reservedNames, subject)) throw new Error("'" + subject + "' is a reserved test name") if (subject.charCodeAt(0) === 1) throw new Error("test names starting with '\\x01' are reserved for internal use")
if (subject.slice(0, 2) === "__") console.warn("test names starting with '__' are reserved for internal use\n" + o.cleanStackTrace(ensureStackTrace(new Error)))
ctx[unique(subject)] = new Task(predicate, ensureStackTrace(new Error)) ctx[unique(subject)] = new Task(predicate, ensureStackTrace(new Error))
} }
} }
o.before = hook("__before") o.before = hook("\x01before")
o.after = hook("__after") o.after = hook("\x01after")
o.beforeEach = hook("__beforeEach") o.beforeEach = hook("\x01beforeEach")
o.afterEach = hook("__afterEach") o.afterEach = hook("\x01afterEach")
o.defaultTimeout = function (t) { o.defaultTimeout = function (t) {
if (isRunning()) throw new Error("o.defaultTimeout() can only be called before o.run()") if (isRunning()) throw new Error("o.defaultTimeout() can only be called before o.run()")
if (hasOwn.call(ctx, "__defaultTimeout")) throw new Error("A default timeout has already been defined in this context") if (hasOwn.call(ctx, "\x01defaultTimeout")) throw new Error("A default timeout has already been defined in this context")
if (typeof t !== "number") throw new Error("o.defaultTimeout() expects a number as argument") if (typeof t !== "number") throw new Error("o.defaultTimeout() expects a number as argument")
ctx.__defaultTimeout = t ctx["\x01defaultTimeout"] = t
} }
o.new = init o.new = init
o.spec = function(subject, predicate) { o.spec = function(subject, predicate) {
@ -105,11 +97,11 @@ else window.o = m()
}, null), 200 /*default timeout delay*/) }, null), 200 /*default timeout delay*/)
function test(spec, pre, post, finalize, defaultDelay) { function test(spec, pre, post, finalize, defaultDelay) {
if (hasOwn.call(spec, "__defaultTimeout")) defaultDelay = spec.__defaultTimeout if (hasOwn.call(spec, "\x01defaultTimeout")) defaultDelay = spec["\x01defaultTimeout"]
pre = [].concat(pre, spec["__beforeEach"] || []) pre = [].concat(pre, spec["\x01beforeEach"] || [])
post = [].concat(spec["__afterEach"] || [], post) post = [].concat(spec["\x01afterEach"] || [], post)
series([].concat(spec["__before"] || [], Object.keys(spec).reduce(function(tasks, key) { series([].concat(spec["\x01before"] || [], Object.keys(spec).reduce(function(tasks, key) {
if (!hasOwn.call(reservedNames, key)) { if (key.charCodeAt(0) !== 1) {
tasks.push(new Task(function(done, timeout) { tasks.push(new Task(function(done, timeout) {
timeout(Infinity) timeout(Infinity)
if (only !== null && spec[key].fn !== only && spec[key] instanceof Task) return done() if (only !== null && spec[key].fn !== only && spec[key] instanceof Task) return done()
@ -126,7 +118,7 @@ else window.o = m()
}, null)) }, null))
} }
return tasks return tasks
}, []), spec["__after"] || [], finalize), defaultDelay) }, []), spec["\x01after"] || [], finalize), defaultDelay)
} }
function series(tasks, defaultDelay) { function series(tasks, defaultDelay) {

View file

@ -127,6 +127,7 @@ o.spec("reporting", function() {
o.spec("ospec", function() { o.spec("ospec", function() {
o.spec("sync", function() { o.spec("sync", function() {
var a = 0, b = 0, illegalAssertionThrows = false var a = 0, b = 0, illegalAssertionThrows = false
var reservedTestNameTrows = false
o.before(function() {a = 1}) o.before(function() {a = 1})
o.after(function() {a = 0}) o.after(function() {a = 0})
@ -135,6 +136,7 @@ o.spec("ospec", function() {
o.afterEach(function() {b = 0}) o.afterEach(function() {b = 0})
try {o("illegal assertion")} catch (e) {illegalAssertionThrows = true} try {o("illegal assertion")} catch (e) {illegalAssertionThrows = true}
try {o("\x01reserved test name", function(){})} catch (e) {reservedTestNameTrows = true}
o("assertions", function() { o("assertions", function() {
var nestedTestDeclarationThrows = false var nestedTestDeclarationThrows = false
@ -142,6 +144,7 @@ o.spec("ospec", function() {
o(illegalAssertionThrows).equals(true) o(illegalAssertionThrows).equals(true)
o(nestedTestDeclarationThrows).equals(true) o(nestedTestDeclarationThrows).equals(true)
o(reservedTestNameTrows).equals(true)
var spy = o.spy() var spy = o.spy()
spy(a) spy(a)