forked from Level/memdown
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtape-type-patch.js
42 lines (40 loc) · 1.33 KB
/
tape-type-patch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var test = require('tape')
if (!test.Test.prototype.type) {
test.Test.prototype.type = function(thing, t, message, extra) {
var name = t
if (typeof name === "function") name = name.name || "(anonymous ctor)"
//console.error("name=%s", name)
message = message || "type is "+name
var type = typeof thing
//console.error("type=%s", type)
if (!thing && type === "object") type = "null"
if (type === "object" && t !== "object") {
if (typeof t === "function") {
//console.error("it is a function!")
extra = extra || {}
extra.found = Object.getPrototypeOf(thing).constructor.name
extra.wanted = name
//console.error(thing instanceof t, name)
return this._assert(thing instanceof t, message, extra)
}
//console.error("check prototype chain")
// check against classnames or objects in prototype chain, as well.
// type(new Error("asdf"), "Error")
// type(Object.create(foo), foo)
var p = thing
while (p = Object.getPrototypeOf(p)) {
if (p === t || p.constructor && p.constructor.name === t) {
type = name
break
}
}
}
this._assert(type === name, {
message : message,
operator : 'type',
expected : name,
actual : type,
extra : extra
});
}
}