-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest.js
More file actions
132 lines (103 loc) · 2.88 KB
/
Copy pathtest.js
File metadata and controls
132 lines (103 loc) · 2.88 KB
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
'use strict';
var expect = require('expect.js');
var type = require('../src/index.js').type;
describe('单元测试', function () {
this.timeout(1000);
var baseList = [
// undefined
{ a: undefined, b: 'undefined' },
// null
{ a: null, b: 'null' },
// number
{ a: 1, b: 'number' },
{ a: 1.1, b: 'number' },
{ a: Math.pow(2, 64), b: 'number' },
{ a: Number(1), b: 'number' },
{ a: new Number(1), b: 'Number' },
// boolean
{ a: true, b: 'boolean' },
{ a: false, b: 'boolean' },
{ a: Boolean(true), b: 'boolean' },
{ a: new Boolean(true), b: 'Boolean' },
// string
{ a: '123', b: 'string' },
{ a: String(123), b: 'string' },
{ a: new String(123), b: 'String' },
// symbol
{ a: Symbol(), b: 'symbol' },
];
function B() {}
B.prototype.constructor = null;
var refList = [
// array
{ a: [], b: 'array' },
{ a: Array(1), b: 'array' },
{ a: new Array(1), b: 'array' },
// object
{ a: {}, b: 'object' },
{ a: Object({}), b: 'object' },
{ a: new Object(), b: 'object' },
{ a: Object.create({}), b: 'object' },
{ a: Object.create(null), b: 'object' },
// set
{ a: new Set(), b: 'set' },
// weakset
{ a: new WeakSet(), b: 'weakset' },
// map
{ a: new Map(), b: 'map' },
// weakmap
{ a: new WeakMap(), b: 'weakmap' },
// function
{ a: function () {}, b: 'function' },
{ a: () => {}, b: 'function' },
{ a: new Function(), b: 'function' },
// class
{ a: class A {}, b: 'function' },
{ a: new (class A {})(), b: 'A' },
// regexp
{ a: /1/, b: 'regexp' },
{ a: new RegExp(), b: 'regexp' },
// date
{ a: new Date(), b: 'date' },
// math
{ a: Math, b: 'math' },
// promise
{ a: new Promise(function () {}), b: 'promise' },
// unknown
{ a: new B(), b: 'unknown' },
];
describe('非严格模式', function () {
it('基本类型', function () {
for (var i = 0; i < baseList.length; i++) {
var a = baseList[i].a;
var b = baseList[i].b;
expect(type(a)).to.equal(b.toLowerCase());
}
});
it('复杂类型', function () {
for (var i = 0; i < refList.length; i++) {
var a = refList[i].a;
var b = refList[i].b;
expect(type(a)).to.equal(b);
}
});
});
describe('严格模式', function () {
it('基本类型', function () {
for (var i = 0; i < baseList.length; i++) {
var a = baseList[i].a;
var b = baseList[i].b;
expect(type(a, true)).to.equal(b);
}
expect(type(NaN, true)).to.equal('nan');
});
it('复杂类型', function () {
for (var i = 0; i < refList.length; i++) {
var a = refList[i].a;
var b = refList[i].b;
expect(type(a, true)).to.equal(b);
}
expect(type(new Number(NaN), true)).to.equal('NaN');
});
});
});