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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
// Using assert instead of Must.js for safety because Must.js uses kindof.
var assert = require("assert")
var kindof = require("..")
describe("kindof", function() {
// Allow using Boolean, Number, String as constructors in tests.
/* jshint -W053 */
it("must return \"undefined\" for undefined", function() {
assert.strictEqual(kindof(undefined), "undefined")
})
it("must return \"null\" for null", function() {
assert.strictEqual(kindof(null), "null")
})
describe("given Boolean", function() {
it("must return \"boolean\" for true", function() {
assert.strictEqual(kindof(true), "boolean")
})
it("must return \"boolean\" for false", function() {
assert.strictEqual(kindof(false), "boolean")
})
it("must return \"object\" for new Boolean(true)", function() {
assert.strictEqual(kindof(new Boolean(true)), "object")
})
it("must return \"object\" for new Boolean(false)", function() {
assert.strictEqual(kindof(new Boolean(false)), "object")
})
describe("given another context", function() {
context("must return \"object\" for boolean object", function(window) {
assert.strictEqual(kindof(new window.Boolean), "object")
})
})
})
describe("given Number", function() {
it("must return \"number\" for number primitive", function() {
assert.strictEqual(kindof(42), "number")
})
it("must return \"number\" for zero number primitive", function() {
assert.strictEqual(kindof(0), "number")
})
it("must return \"object\" for number object", function() {
assert.strictEqual(kindof(new Number(42)), "object")
})
it("must return \"object\" for zero number object", function() {
assert.strictEqual(kindof(new Number(0)), "object")
})
it("must return \"number\" for NaN", function() {
assert.strictEqual(kindof(NaN), "number")
})
it("must return \"number\" for Infinity", function() {
assert.strictEqual(kindof(Infinity), "number")
})
describe("given another context", function() {
context("must return \"object\" for number object", function(window) {
assert.strictEqual(kindof(new window.Number), "object")
})
})
})
describe("given String", function() {
it("must return \"string\" for string primitive", function() {
assert.strictEqual(kindof("Hello"), "string")
})
it("must return \"string\" for empty string primitive", function() {
assert.strictEqual(kindof(""), "string")
})
it("must return \"object\" for string object", function() {
assert.strictEqual(kindof(new String("Hello")), "object")
})
it("must return \"object\" for empty string object", function() {
assert.strictEqual(kindof(new String), "object")
})
describe("given another context", function() {
context("must return \"object\" for string object", function(window) {
assert.strictEqual(kindof(new window.String), "object")
})
})
})
if (typeof Symbol == "function") describe("given Symbol", function() {
it("must return \"symbol\" for an anonymous symbol", function() {
assert.strictEqual(kindof(Symbol()), "symbol")
})
it("must return \"symbol\" for a named symbol", function() {
assert.strictEqual(kindof(Symbol("forEach")), "symbol")
})
it("must return \"symbol\" for an existing symbol", function() {
assert.strictEqual(kindof(Symbol.iterator), "symbol")
})
})
describe("given RegExp", function() {
it("must return \"regexp\"", function() {
assert.strictEqual(kindof(/./), "regexp")
})
describe("given another context", function() {
context("must return \"regexp\"", function(window) {
assert.strictEqual(kindof(new window.RegExp), "regexp")
})
})
})
describe("given Date", function() {
it("must return \"date\"", function() {
assert.strictEqual(kindof(new Date), "date")
})
describe("given another context", function() {
context("must return \"date\"", function(window) {
assert.strictEqual(kindof(new window.Date), "date")
})
})
})
describe("given Array", function() {
it("must return \"array\"", function() {
assert.strictEqual(kindof([]), "array")
})
describe("given another context", function() {
context("must return \"array\"", function(window) {
assert.strictEqual(kindof(new window.Array), "array")
})
})
})
describe("given Function", function() {
it("must return \"function\"", function() {
assert.strictEqual(kindof(function() {}), "function")
})
describe("given another context", function() {
context("must return \"function\"", function(window) {
assert.strictEqual(kindof(new window.Function), "function")
})
})
})
describe("given Error", function() {
it("must return \"object\"", function() {
assert.strictEqual(kindof(new Error), "object")
})
})
describe("given Arguments", function() {
it("must return \"object\"", function() {
assert.strictEqual(kindof(arguments), "object")
})
})
describe("given Math", function() {
it("must return \"object\"", function() {
assert.strictEqual(kindof(Math), "object")
})
})
describe("given JSON", function() {
it("must return \"object\"", function() {
assert.strictEqual(kindof(JSON), "object")
})
})
describe("given Object", function() {
it("must return \"object\"", function() {
assert.strictEqual(kindof({}), "object")
})
})
describe("given custom instance", function() {
it("must return \"object\"", function() {
function Foo() {}
assert.strictEqual(kindof(new Foo), "object")
})
})
function context(title, fn) {
if (typeof window == "undefined") return it.skip(title)
it(title, function() {
var frame = document.createElement("iframe")
document.body.appendChild(frame)
try { fn(frame.contentWindow) }
finally { document.body.removeChild(frame) }
})
}
})
|