forked from dollarshaveclub/es-check
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutils.test.js
165 lines (142 loc) · 5.45 KB
/
utils.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// test/checkFunctions.spec.js
const assert = require('assert');
const {
checkVarKindMatch,
checkCalleeMatch,
checkOperatorMatch,
checkDefault,
checkMap,
} = require('./utils');
describe('Check Functions', function () {
describe('checkVarKindMatch', function () {
it('should return false if astInfo.kind is not provided', function () {
const node = { kind: 'const' };
const astInfo = {}; // no "kind"
assert.strictEqual(checkVarKindMatch(node, astInfo), false);
});
it('should return true if node.kind equals astInfo.kind', function () {
const node = { kind: 'const' };
const astInfo = { kind: 'const' };
assert.strictEqual(checkVarKindMatch(node, astInfo), true);
});
it('should return false if node.kind does not match astInfo.kind', function () {
const node = { kind: 'let' };
const astInfo = { kind: 'const' };
assert.strictEqual(checkVarKindMatch(node, astInfo), false);
});
});
describe('checkCalleeMatch', function () {
it('should return false if astInfo.callee is not provided', function () {
const node = {
callee: { type: 'Identifier', name: 'Promise' },
};
const astInfo = {}; // no "callee"
assert.strictEqual(checkCalleeMatch(node, astInfo), false);
});
it('should return false if node.callee is missing', function () {
const node = {};
const astInfo = { callee: 'Promise' };
assert.strictEqual(checkCalleeMatch(node, astInfo), false);
});
it('should return false if node.callee.type !== "Identifier"', function () {
const node = {
callee: { type: 'MemberExpression', name: 'Promise' },
};
const astInfo = { callee: 'Promise' };
assert.strictEqual(checkCalleeMatch(node, astInfo), false);
});
it('should return false if the callee name does not match astInfo.callee', function () {
const node = {
callee: { type: 'Identifier', name: 'WeakRef' },
};
const astInfo = { callee: 'Promise' };
assert.strictEqual(checkCalleeMatch(node, astInfo), false);
});
it('should return true if callee name matches astInfo.callee', function () {
const node = {
callee: { type: 'Identifier', name: 'Promise' },
};
const astInfo = { callee: 'Promise' };
assert.strictEqual(checkCalleeMatch(node, astInfo), true);
});
});
describe('checkOperatorMatch', function () {
it('should return false if astInfo.operator is not provided', function () {
const node = { operator: '??' };
const astInfo = {}; // no "operator"
assert.strictEqual(checkOperatorMatch(node, astInfo), false);
});
it('should return false if node.operator does not match astInfo.operator', function () {
const node = { operator: '**' };
const astInfo = { operator: '??' };
assert.strictEqual(checkOperatorMatch(node, astInfo), false);
});
it('should return true if node.operator matches astInfo.operator', function () {
const node = { operator: '??' };
const astInfo = { operator: '??' };
assert.strictEqual(checkOperatorMatch(node, astInfo), true);
});
});
describe('checkDefault', function () {
it('should always return true', function () {
assert.strictEqual(checkDefault(), true);
});
});
describe('checkMap usage examples', function () {
it('checkMap.VariableDeclaration should call checkVarKindMatch internally and return correct boolean', function () {
const node = { kind: 'const' };
const astInfo = { kind: 'const' };
const result = checkMap.VariableDeclaration(node, astInfo);
assert.strictEqual(result, true);
const nodeMismatch = { kind: 'let' };
const result2 = checkMap.VariableDeclaration(nodeMismatch, astInfo);
assert.strictEqual(result2, false);
});
it('checkMap.LogicalExpression should call checkOperatorMatch internally', function () {
const node = { operator: '??' };
const astInfo = { operator: '??' };
const result = checkMap.LogicalExpression(node, astInfo);
assert.strictEqual(result, true);
});
it('checkMap.ArrowFunctionExpression should call checkDefault internally', function () {
const result = checkMap.ArrowFunctionExpression();
assert.strictEqual(result, true);
});
it('checkMap.default should return false if node type is unsupported', function () {
const result = checkMap.default();
assert.strictEqual(result, false);
});
});
describe('checkCallExpression', () => {
const { checkMap } = require('./utils');
it('correctly distinguishes between global calls and member expressions', () => {
const symbolNode = {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'Symbol'
}
};
const getElementNode = {
type: 'CallExpression',
callee: {
type: 'MemberExpression',
object: {
type: 'Identifier',
name: 'window'
},
property: {
type: 'Identifier',
name: 'getElementById'
}
}
};
assert.strictEqual(checkMap.CallExpression(symbolNode, { callee: 'Symbol' }), true);
assert.strictEqual(checkMap.CallExpression(getElementNode, { callee: 'getElementById' }), false);
assert.strictEqual(checkMap.CallExpression(getElementNode, {
object: 'window',
property: 'getElementById'
}), true);
});
});
});