/
inspect_test.js
141 lines (120 loc) · 3.58 KB
/
inspect_test.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
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
import { HAS_NATIVE_SYMBOL, inspect } from '..';
import { moduleFor, AbstractTestCase as TestCase } from 'internal-test-helpers';
moduleFor(
'Ember.inspect',
class extends TestCase {
['@test strings'](assert) {
assert.equal(inspect('foo'), '"foo"');
}
['@test numbers'](assert) {
assert.equal(inspect(2.6), '2.6');
}
['@test null'](assert) {
assert.equal(inspect(null), 'null');
}
['@test undefined'](assert) {
assert.equal(inspect(undefined), 'undefined');
}
['@test true'](assert) {
assert.equal(inspect(true), 'true');
}
['@test false'](assert) {
assert.equal(inspect(false), 'false');
}
['@test object'](assert) {
assert.equal(inspect({}), '{ }');
assert.equal(inspect({ foo: 'bar' }), '{ foo: "bar" }');
let obj = {
foo() {
return this;
},
};
assert.equal(inspect(obj), `{ foo: [Function:foo] }`);
}
['@test objects without a prototype'](assert) {
let prototypelessObj = Object.create(null);
prototypelessObj.a = 1;
prototypelessObj.b = [Object.create(null)];
assert.equal(inspect({ foo: prototypelessObj }), '{ foo: { a: 1, b: [ { } ] } }');
}
['@test array'](assert) {
assert.equal(inspect([1, 2, 3]), '[ 1, 2, 3 ]');
}
['@test array list limit'](assert) {
let a = [];
for (let i = 0; i < 120; i++) {
a.push(1);
}
assert.equal(inspect(a), `[ ${a.slice(0, 100).join(', ')}, ... 20 more items ]`);
}
['@test object list limit'](assert) {
let obj = {};
let pairs = [];
for (let i = 0; i < 120; i++) {
obj['key' + i] = i;
pairs.push(`key${i}: ${i}`);
}
assert.equal(inspect(obj), `{ ${pairs.slice(0, 100).join(', ')}, ... 20 more keys }`);
}
['@test depth limit'](assert) {
assert.equal(
inspect([[[['here', { a: 1 }, [1]]]]]),
'[ [ [ [ "here", [Object], [Array] ] ] ] ]'
);
}
['@test odd key'](assert) {
assert.equal(
inspect({
[`Hello world!
How are you?`]: 1,
}),
'{ "Hello world!\\nHow are you?": 1 }'
);
}
['@test node call'](assert) {
let obj = { a: 1 };
obj.inspect = inspect;
let depth = 2;
let options = {};
assert.equal(obj.inspect(depth, options), obj);
}
['@test cycle'](assert) {
let obj = {};
obj.a = obj;
let arr = [obj];
arr.push(arr);
assert.equal(inspect(arr), '[ { a: [Circular] }, [Circular] ]');
}
['@test custom toString'](assert) {
class Component {
static toString() {
return '@ember/component';
}
toString() {
return `<${this.constructor}:ember234>`;
}
}
assert.equal(
inspect([new Component(), Component]),
'[ <@ember/component:ember234>, @ember/component ]'
);
}
['@test regexp'](assert) {
assert.equal(inspect(/regexp/), '/regexp/');
}
['@test date'](assert) {
let inspected = inspect(new Date('Sat Apr 30 2011 13:24:11'));
assert.ok(inspected.match(/Sat Apr 30/), 'The inspected date has its date');
assert.ok(inspected.match(/2011/), 'The inspected date has its year');
assert.ok(inspected.match(/13:24:11/), 'The inspected date has its time');
}
['@test inspect outputs the toString() representation of Symbols'](assert) {
if (HAS_NATIVE_SYMBOL) {
let symbol = Symbol('test');
assert.equal(inspect(symbol), 'Symbol(test)');
} else {
assert.expect(0);
}
}
}
);