-
Notifications
You must be signed in to change notification settings - Fork 3
/
default_keybindings_loader.test.js
395 lines (394 loc) · 18.3 KB
/
default_keybindings_loader.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
'use strict';
const assert = require('assert');
const defaultKeybindingsLoader = require('../../generator/default_keybindings_loader.js');
describe('default_keybindings_loader', () => {
describe('combineBaseKeybingings', () => {
const combineBaseKeybingings = defaultKeybindingsLoader.combineBaseKeybingings;
it('should create a combined keybindings which consists of given set of keybindings with added context', () => {
const input = [
{
keybindings: [
{ key: 'ctrl+a', command: 'command1' },
{ key: 'ctrl+b', command: 'command2', when: 'context1' }
],
context: 'isWindows'
},
{
keybindings: [
{ key: 'ctrl+c', command: 'command3' },
{ key: 'ctrl+d', command: 'command4', when: 'context2 || context3' }
],
context: 'isLinux'
}
];
const expected = [
{ key: 'ctrl+a', command: 'command1', when: 'isWindows' },
{ key: 'ctrl+b', command: 'command2', when: 'isWindows && context1' },
{ key: 'ctrl+c', command: 'command3', when: 'isLinux' },
{ key: 'ctrl+d', command: 'command4', when: 'isLinux && context2 || isLinux && context3' }
];
assert.deepStrictEqual(combineBaseKeybingings(input), expected);
});
it('should retain keybindings that share a common key but have different commands or when-clause', () => {
const input = [
{
keybindings: [
{ key: 'ctrl+a', command: 'command1' },
{ key: 'ctrl+a', command: 'command2' },
],
context: 'isWindows'
},
{
keybindings: [
{ key: 'ctrl+a', command: 'command3' },
{ key: 'ctrl+a', command: 'command3', when: 'context1' }
],
context: 'isLinux'
}
];
const expected = [
{ key: 'ctrl+a', command: 'command1', when: 'isWindows' },
{ key: 'ctrl+a', command: 'command2', when: 'isWindows' },
{ key: 'ctrl+a', command: 'command3', when: 'isLinux' },
{ key: 'ctrl+a', command: 'command3', when: 'isLinux && context1' }
];
assert.deepStrictEqual(combineBaseKeybingings(input), expected);
});
it('should unify keybindings that share a common definition among all sources [compaction]', () => {
const input = [
{
keybindings: [
{ key: 'ctrl+a', command: 'command1' }, // <= common
{ key: 'ctrl+a', command: 'command2' }
],
context: 'isWindows'
},
{
keybindings: [
{ key: 'ctrl+a', command: 'command1' }, // <= common
{ key: 'ctrl+a', command: 'command1', when: 'context1' }
],
context: 'isLinux'
}
];
const expected = [
{ key: 'ctrl+a', command: 'command1' }, // <= common
{ key: 'ctrl+a', command: 'command2', when: 'isWindows' },
{ key: 'ctrl+a', command: 'command1', when: 'isLinux && context1' }
];
assert.deepStrictEqual(combineBaseKeybingings(input), expected);
});
it('should retain the original order of keybindings in each source', () => {
const input = [
{
keybindings: [
{ key: 'ctrl+a', command: 'command1', when: 'cond1' }, // <= common
{ key: 'ctrl+a', command: 'command2', when: 'cond2' } // should come after common one
],
context: 'isWindows'
},
{
keybindings: [
{ key: 'ctrl+a', command: 'command3', when: 'cond3' }, // should come before common one
{ key: 'ctrl+a', command: 'command1', when: 'cond1' } // <= common
],
context: 'isLinux'
}
];
const expected = [
{ key: 'ctrl+a', command: 'command3', when: 'isLinux && cond3' },
{ key: 'ctrl+a', command: 'command1', when: 'cond1' }, // <= common
{ key: 'ctrl+a', command: 'command2', when: 'isWindows && cond2' }
];
assert.deepStrictEqual(combineBaseKeybingings(input), expected);
});
it('should not unify keybindings that cause conflicts that prevent retaining the proper order', () => {
const input = [
{
keybindings: [
{ key: 'ctrl+a', command: 'command1', when: 'cond1' }, // <= common I
{ key: 'ctrl+a', command: 'command2', when: 'cond2' },
{ key: 'ctrl+a', command: 'command3', when: 'cond3' } // <= common II
],
context: 'isWindows'
},
{
keybindings: [
{ key: 'ctrl+a', command: 'command3', when: 'cond3' }, // <= common II
{ key: 'ctrl+a', command: 'command1', when: 'cond1' } // <= common I
],
context: 'isLinux'
}
];
const expected = [
{ key: 'ctrl+a', command: 'command3', when: 'isLinux && cond3' },
{ key: 'ctrl+a', command: 'command1', when: 'cond1' }, // <= common I
{ key: 'ctrl+a', command: 'command2', when: 'isWindows && cond2' },
{ key: 'ctrl+a', command: 'command3', when: 'isWindows && cond3' }
];
assert.deepStrictEqual(combineBaseKeybingings(input), expected);
});
it('should unify keybindings that share a common definition among a subset of sources [compaction]', () => {
const input = [
{
keybindings: [
{ key: 'ctrl+a', command: 'command1' }, // <= partially common
{ key: 'ctrl+a', command: 'command2' }
],
context: 'isWindows'
},
{
keybindings: [
{ key: 'ctrl+a', command: 'command1' }, // <= partially common
{ key: 'ctrl+a', command: 'command1', when: 'context1' }
],
context: 'isLinux'
},
{
keybindings: [
{ key: 'ctrl+b', command: 'command1', when: 'context1' }
],
context: 'isMac'
}
];
// We use negative form of '!isMac' rather than OR form 'isWindows || isLinux'
// for compaction of package.json.
const expected = [
{ key: 'ctrl+a', command: 'command1', when: '!isMac' }, // <= partially common
{ key: 'ctrl+a', command: 'command2', when: 'isWindows' },
{ key: 'ctrl+a', command: 'command1', when: 'isLinux && context1' },
{ key: 'ctrl+b', command: 'command1', when: 'isMac && context1' }
];
assert.deepStrictEqual(combineBaseKeybingings(input), expected);
});
it('should unify keybindings that share a common definition among 2nd and 3rd source [compaction]', () => {
const input = [
{
keybindings: [
{ key: 'ctrl+a', command: 'command1' }
],
context: 'isWindows'
},
{
keybindings: [
{ key: 'ctrl+a', command: 'command2' } // <= partially common
],
context: 'isLinux'
},
{
keybindings: [
{ key: 'ctrl+a', command: 'command3' },
{ key: 'ctrl+a', command: 'command2' } // <= partially common
],
context: 'isMac'
}
];
const expected = [
{ key: 'ctrl+a', command: 'command3', when: 'isMac' },
{ key: 'ctrl+a', command: 'command2', when: '!isWindows' }, // <= partially common
{ key: 'ctrl+a', command: 'command1', when: 'isWindows' }
];
assert.deepStrictEqual(combineBaseKeybingings(input), expected);
});
it('should drop reduntant "isWindows" if the keystroke contains Win key [compaction]', () => {
const input = [
{
keybindings: [
{ key: 'ctrl+win+a', command: 'command1', when: 'context1' }
],
context: 'isWindows'
},
{
keybindings: [
{ key: 'ctrl+alt+a', command: 'command2', when: 'context2' }
],
context: 'isLinux'
}
];
const expected = [
{ key: 'ctrl+win+a', command: 'command1', when: 'context1' },
{ key: 'ctrl+alt+a', command: 'command2', when: 'isLinux && context2' }
];
assert.deepStrictEqual(combineBaseKeybingings(input), expected);
});
it('should drop reduntant "isLinux" if the keystroke contains Meta key [compaction]', () => {
const input = [
{
keybindings: [
{ key: 'ctrl+a', command: 'command1', when: 'context1' }
],
context: 'isWindows'
},
{
keybindings: [
{ key: 'meta+a', command: 'command2', when: 'context2' }
],
context: 'isLinux'
}
];
const expected = [
{ key: 'ctrl+a', command: 'command1', when: 'isWindows && context1' },
{ key: 'meta+a', command: 'command2', when: 'context2' }
];
assert.deepStrictEqual(combineBaseKeybingings(input), expected);
});
it('should drop reduntant "isMac" if the keystroke contains Command key [compaction]', () => {
const input = [
{
keybindings: [
{ key: 'ctrl+a', command: 'command1', when: 'context1' }
],
context: 'isWindows'
},
{
keybindings: [
{ key: 'cmd+a', command: 'command2', when: 'context2' }
],
context: 'isMac'
}
];
const expected = [
{ key: 'ctrl+a', command: 'command1', when: 'isWindows && context1' },
{ key: 'cmd+a', command: 'command2', when: 'context2' }
];
assert.deepStrictEqual(combineBaseKeybingings(input), expected);
});
it('should unify keybindings that are common for Windows and Linux and also Mac except assigned "key" (using "mac" key) (1) [compaction]', () => {
const input = [
{
keybindings: [
{ key: 'ctrl+a', command: 'command1', when: 'context1' }, // <= common
{ key: 'ctrl+a', command: 'command2', when: 'context2' }
],
context: 'isWindows'
},
{
keybindings: [
{ key: 'ctrl+a', command: 'command1', when: 'context1' }, // <= common
{ key: 'ctrl+a', command: 'command1', when: 'context3' }
],
context: 'isLinux'
},
{
keybindings: [
{ key: 'ctrl+b', command: 'command2' },
{ key: 'ctrl+b', command: 'command1', when: 'context1' } // <= common except 'key'
],
context: 'isMac'
}
];
const expected = [
{ key: 'ctrl+b', command: 'command2', when: 'isMac' },
{ key: 'ctrl+a', command: 'command1', when: 'context1', mac: 'ctrl+b' }, // <= unified with 'mac' key
{ key: 'ctrl+a', command: 'command2', when: 'isWindows && context2' },
{ key: 'ctrl+a', command: 'command1', when: 'isLinux && context3' }
];
assert.deepStrictEqual(combineBaseKeybingings(input), expected);
});
it('should unify keybindings using "mac" key (2) [compaction]', () => {
const input = [
{
keybindings: [
{ key: 'ctrl+a', command: 'command1', when: 'context1' }, // <= common A
{ key: 'ctrl+a', command: 'command2', when: 'context2' },
{ key: 'ctrl+c', command: 'command3', when: 'context4' } // <= common B
],
context: 'isWindows'
},
{
keybindings: [
{ key: 'ctrl+a', command: 'command1', when: 'context1' }, // <= common A
{ key: 'ctrl+a', command: 'command1', when: 'context3' },
{ key: 'ctrl+c', command: 'command3', when: 'context4' } // <= common B
],
context: 'isLinux'
},
{
keybindings: [
{ key: 'ctrl+b', command: 'command2' },
{ key: 'ctrl+b', command: 'command1', when: 'context1' }, // <= common A except 'key'
{ key: 'ctrl+b', command: 'command3', when: 'context4' } // <= common B except 'key'
],
context: 'isMac'
}
];
const expected = [
{ key: 'ctrl+b', command: 'command2', when: 'isMac' },
{ key: 'ctrl+a', command: 'command1', when: 'context1', mac: 'ctrl+b' }, // <= A
{ key: 'ctrl+c', command: 'command3', when: 'context4', mac: 'ctrl+b' }, // <= B
{ key: 'ctrl+a', command: 'command2', when: 'isWindows && context2' },
{ key: 'ctrl+a', command: 'command1', when: 'isLinux && context3' }
];
assert.deepStrictEqual(combineBaseKeybingings(input), expected);
});
it('should unify keybindings using "mac" key (3) (special case that a same command applies to multiple keys) [compaction]', () => {
const input = [
{
keybindings: [
{ key: 'ctrl+a', command: 'command1' }, // <= common A
{ key: 'ctrl+a', command: 'command2', when: 'context2' },
{ key: 'ctrl+c', command: 'command1' } // <= common B
],
context: 'isWindows'
},
{
keybindings: [
{ key: 'ctrl+a', command: 'command1' }, // <= common A
{ key: 'ctrl+a', command: 'command1', when: 'context3' },
{ key: 'ctrl+c', command: 'command1' } // <= common B
],
context: 'isLinux'
},
{
keybindings: [
{ key: 'ctrl+b', command: 'command2' },
{ key: 'ctrl+b', command: 'command1' }, // <= common A except 'key'
{ key: 'ctrl+d', command: 'command1' } // <= common B except 'key'
],
context: 'isMac'
}
];
const expected = [
{ key: 'ctrl+b', command: 'command2', when: 'isMac' },
{ key: 'ctrl+a', command: 'command1', mac: 'ctrl+b' }, // <= A
{ key: 'ctrl+c', command: 'command1', mac: 'ctrl+d' }, // <= B
{ key: 'ctrl+a', command: 'command2', when: 'isWindows && context2' },
{ key: 'ctrl+a', command: 'command1', when: 'isLinux && context3' }
];
assert.deepStrictEqual(combineBaseKeybingings(input), expected);
});
it('should unify keybindings using "mac" key (4) [compaction]', () => {
const input = [
{
keybindings: [
{ key: 'ctrl+delete', command: 'command1', when: 'context1' },
{ key: 'alt+delete', command: 'command3', when: 'context3' }
],
context: 'isWindows'
},
{
keybindings: [
{ key: 'ctrl+delete', command: 'command1', when: 'context1' },
{ key: 'alt+delete', command: 'command3', when: 'context3' }
],
context: 'isLinux'
},
{
keybindings: [
{ key: 'alt+delete', command: 'command2', when: 'context2' },
{ key: 'alt+delete', command: 'command3', when: 'context3' },
{ key: 'alt+delete', command: 'command1', when: 'context1' }
],
context: 'isMac'
}
];
const expected = [
{ key: 'ctrl+delete', command: 'command1', when: '!isMac && context1' },
{ key: 'alt+delete', command: 'command2', when: 'isMac && context2' },
{ key: 'alt+delete', command: 'command3', when: 'context3' },
{ key: 'alt+delete', command: 'command1', when: 'isMac && context1' }
];
assert.deepStrictEqual(combineBaseKeybingings(input), expected);
});
});
});