Skip to content

Commit 07f906a

Browse files
authored
test suite for js/js-export/constraints.js (#4458)
* test suite for js/js-export/constraints.js * Exporting modules for test * updating sample string to be action from C
1 parent fd43d08 commit 07f906a

File tree

2 files changed

+320
-0
lines changed

2 files changed

+320
-0
lines changed
Lines changed: 317 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,317 @@
1+
global.JSInterface = { _methodArgConstraints: {} };
2+
require("../constraints.js");
3+
4+
describe('JSInterface._methodArgConstraints', () => {
5+
const validateConstraints = (methodName, args) => {
6+
const constraints = JSInterface._methodArgConstraints[methodName];
7+
if (!constraints) {
8+
throw new Error(`Method ${methodName} not found in constraints`);
9+
}
10+
11+
args.forEach((arg, index) => {
12+
const constraint = constraints[index];
13+
if (!constraint) {
14+
throw new Error(`Constraint for argument ${index} not found`);
15+
}
16+
17+
if (typeof arg !== constraint.type) {
18+
throw new Error(`Argument ${index} is not of type ${constraint.type}`);
19+
}
20+
21+
if (constraint.constraints) {
22+
if (constraint.constraints.min !== undefined && arg < constraint.constraints.min) {
23+
throw new Error(`Argument ${index} is less than the minimum value ${constraint.constraints.min}`);
24+
}
25+
if (constraint.constraints.max !== undefined && arg > constraint.constraints.max) {
26+
throw new Error(`Argument ${index} is greater than the maximum value ${constraint.constraints.max}`);
27+
}
28+
if (constraint.constraints.integer !== undefined && constraint.constraints.integer && !Number.isInteger(arg)) {
29+
throw new Error(`Argument ${index} is not an integer`);
30+
}
31+
if (constraint.constraints.async !== undefined && constraint.constraints.async && typeof arg !== 'function') {
32+
throw new Error(`Argument ${index} is not an async function`);
33+
}
34+
if (constraint.constraints.type === 'solfegeorletter' && typeof arg !== 'string') {
35+
throw new Error(`Argument ${index} is not a valid solfege or letter`);
36+
}
37+
if (constraint.constraints.type === 'accidental' && typeof arg !== 'string') {
38+
throw new Error(`Argument ${index} is not a valid accidental`);
39+
}
40+
if (constraint.constraints.type === 'synth' && typeof arg !== 'string') {
41+
throw new Error(`Argument ${index} is not a valid synth`);
42+
}
43+
if (constraint.constraints.type === 'drum' && typeof arg !== 'string') {
44+
throw new Error(`Argument ${index} is not a valid drum`);
45+
}
46+
if (constraint.constraints.type === 'noise' && typeof arg !== 'string') {
47+
throw new Error(`Argument ${index} is not a valid noise`);
48+
}
49+
if (constraint.constraints.type === 'letterkey' && typeof arg !== 'string') {
50+
throw new Error(`Argument ${index} is not a valid letter key`);
51+
}
52+
if (constraint.constraints.type === 'oneof' && !constraint.constraints.values.includes(arg)) {
53+
throw new Error(`Argument ${index} is not one of the allowed values`);
54+
}
55+
}
56+
});
57+
};
58+
59+
it('should validate setMeter arguments', () => {
60+
expect(() => validateConstraints('setMeter', [8, 500])).not.toThrow();
61+
expect(() => validateConstraints('setMeter', [0, 500])).toThrow();
62+
expect(() => validateConstraints('setMeter', [17, 500])).toThrow();
63+
expect(() => validateConstraints('setMeter', [8, -1])).toThrow();
64+
});
65+
66+
it('should validate PICKUP arguments', () => {
67+
expect(() => validateConstraints('PICKUP', [500])).not.toThrow();
68+
expect(() => validateConstraints('PICKUP', [-1])).toThrow();
69+
expect(() => validateConstraints('PICKUP', [1001])).toThrow();
70+
});
71+
72+
it('should validate setBPM arguments', () => {
73+
expect(() => validateConstraints('setBPM', [120, 5])).not.toThrow();
74+
expect(() => validateConstraints('setBPM', [39, 5])).toThrow();
75+
expect(() => validateConstraints('setBPM', [209, 5])).toThrow();
76+
expect(() => validateConstraints('setBPM', [120, -1])).toThrow();
77+
});
78+
79+
it('should validate setMasterBPM arguments', () => {
80+
expect(() => validateConstraints('setMasterBPM', [120, 5])).not.toThrow();
81+
expect(() => validateConstraints('setMasterBPM', [39, 5])).toThrow();
82+
expect(() => validateConstraints('setMasterBPM', [209, 5])).toThrow();
83+
expect(() => validateConstraints('setMasterBPM', [120, -1])).toThrow();
84+
});
85+
86+
it('should validate onEveryNoteDo arguments', () => {
87+
expect(() => validateConstraints('onEveryNoteDo', ["action"])).not.toThrow();
88+
expect(() => validateConstraints('onEveryNoteDo', [123])).toThrow();
89+
});
90+
91+
it('should validate onEveryBeatDo arguments', () => {
92+
expect(() => validateConstraints('onEveryBeatDo', ["action"])).not.toThrow();
93+
expect(() => validateConstraints('onEveryBeatDo', [123])).toThrow();
94+
});
95+
96+
it('should validate onStrongBeatDo arguments', () => {
97+
expect(() => validateConstraints('onStrongBeatDo', [8, "action"])).not.toThrow();
98+
expect(() => validateConstraints('onStrongBeatDo', [0, "action"])).toThrow();
99+
expect(() => validateConstraints('onStrongBeatDo', [17, "action"])).toThrow();
100+
expect(() => validateConstraints('onStrongBeatDo', [8, 123])).toThrow();
101+
});
102+
103+
it('should validate onWeakBeatDo arguments', () => {
104+
expect(() => validateConstraints('onWeakBeatDo', ["action"])).not.toThrow();
105+
expect(() => validateConstraints('onWeakBeatDo', [123])).toThrow();
106+
});
107+
108+
it('should validate getNotesPlayed arguments', () => {
109+
expect(() => validateConstraints('getNotesPlayed', [500])).not.toThrow();
110+
expect(() => validateConstraints('getNotesPlayed', [-1])).toThrow();
111+
expect(() => validateConstraints('getNotesPlayed', [1001])).toThrow();
112+
});
113+
114+
it('should validate playPitch arguments', () => {
115+
expect(() => validateConstraints('playPitch', ["action", 4])).not.toThrow();
116+
expect(() => validateConstraints('playPitch', [123, 4])).toThrow();
117+
expect(() => validateConstraints('playPitch', ["action", 0])).toThrow();
118+
});
119+
120+
it('should validate stepPitch arguments', () => {
121+
expect(() => validateConstraints('stepPitch', [3])).not.toThrow();
122+
expect(() => validateConstraints('stepPitch', [-8])).toThrow();
123+
expect(() => validateConstraints('stepPitch', [8])).toThrow();
124+
});
125+
126+
it('should validate playNthModalPitch arguments', () => {
127+
expect(() => validateConstraints('playNthModalPitch', [3, 4])).not.toThrow();
128+
expect(() => validateConstraints('playNthModalPitch', [-8, 4])).toThrow();
129+
expect(() => validateConstraints('playNthModalPitch', [8, 4])).toThrow();
130+
expect(() => validateConstraints('playNthModalPitch', [3, 0])).toThrow();
131+
});
132+
133+
it('should validate playPitchNumber arguments', () => {
134+
expect(() => validateConstraints('playPitchNumber', [5])).not.toThrow();
135+
expect(() => validateConstraints('playPitchNumber', [-4])).toThrow();
136+
expect(() => validateConstraints('playPitchNumber', [13])).toThrow();
137+
});
138+
139+
it('should validate playHertz arguments', () => {
140+
expect(() => validateConstraints('playHertz', [440])).not.toThrow();
141+
expect(() => validateConstraints('playHertz', [19])).toThrow();
142+
expect(() => validateConstraints('playHertz', [20001])).toThrow();
143+
});
144+
145+
it('should validate setRegister arguments', () => {
146+
expect(() => validateConstraints('setRegister', [2])).not.toThrow();
147+
expect(() => validateConstraints('setRegister', [-4])).toThrow();
148+
expect(() => validateConstraints('setRegister', [4])).toThrow();
149+
});
150+
151+
it('should validate invert arguments', () => {
152+
expect(() => validateConstraints('invert', ['C', 4, 'even'])).not.toThrow();
153+
expect(() => validateConstraints('invert', [123, 4, 'even'])).toThrow();
154+
expect(() => validateConstraints('invert', ['C', 0, 'even'])).toThrow();
155+
expect(() => validateConstraints('invert', ['C', 4, 'invalid'])).toThrow();
156+
});
157+
158+
it('should validate setPitchNumberOffset arguments', () => {
159+
expect(() => validateConstraints('setPitchNumberOffset', ['C', 4])).not.toThrow();
160+
expect(() => validateConstraints('setPitchNumberOffset', [123, 4])).toThrow();
161+
expect(() => validateConstraints('setPitchNumberOffset', ['C', 0])).toThrow();
162+
});
163+
164+
it('should validate numToPitch arguments', () => {
165+
expect(() => validateConstraints('numToPitch', [50])).not.toThrow();
166+
expect(() => validateConstraints('numToPitch', [-1])).toThrow();
167+
expect(() => validateConstraints('numToPitch', [101])).toThrow();
168+
});
169+
170+
it('should validate numToOctave arguments', () => {
171+
expect(() => validateConstraints('numToOctave', [50])).not.toThrow();
172+
expect(() => validateConstraints('numToOctave', [-1])).toThrow();
173+
expect(() => validateConstraints('numToOctave', [101])).toThrow();
174+
});
175+
176+
it('should validate setKey arguments', () => {
177+
expect(() => validateConstraints('setKey', ['C', 'major'])).not.toThrow();
178+
expect(() => validateConstraints('setKey', [123, 'major'])).toThrow();
179+
expect(() => validateConstraints('setKey', ['C', 'invalid'])).toThrow();
180+
});
181+
182+
it('should validate MOVABLEDO arguments', () => {
183+
expect(() => validateConstraints('MOVABLEDO', [true])).not.toThrow();
184+
expect(() => validateConstraints('MOVABLEDO', [false])).not.toThrow();
185+
expect(() => validateConstraints('MOVABLEDO', [123])).toThrow();
186+
});
187+
188+
it('should validate PANNING arguments', () => {
189+
expect(() => validateConstraints('PANNING', [50])).not.toThrow();
190+
expect(() => validateConstraints('PANNING', [-101])).toThrow();
191+
expect(() => validateConstraints('PANNING', [101])).toThrow();
192+
});
193+
194+
it('should validate MASTERVOLUME arguments', () => {
195+
expect(() => validateConstraints('MASTERVOLUME', [50])).not.toThrow();
196+
expect(() => validateConstraints('MASTERVOLUME', [-1])).toThrow();
197+
expect(() => validateConstraints('MASTERVOLUME', [101])).toThrow();
198+
});
199+
200+
it('should validate setSynthVolume arguments', () => {
201+
expect(() => validateConstraints('setSynthVolume', ['synth', 50])).not.toThrow();
202+
expect(() => validateConstraints('setSynthVolume', [123, 50])).toThrow();
203+
expect(() => validateConstraints('setSynthVolume', ['synth', -1])).toThrow();
204+
});
205+
206+
it('should validate getSynthVolume arguments', () => {
207+
expect(() => validateConstraints('getSynthVolume', ['synth'])).not.toThrow();
208+
expect(() => validateConstraints('getSynthVolume', [123])).toThrow();
209+
});
210+
211+
it('should validate playDrum arguments', () => {
212+
expect(() => validateConstraints('playDrum', ['drum'])).not.toThrow();
213+
expect(() => validateConstraints('playDrum', [123])).toThrow();
214+
});
215+
216+
it('should validate playNoise arguments', () => {
217+
expect(() => validateConstraints('playNoise', ['noise'])).not.toThrow();
218+
expect(() => validateConstraints('playNoise', [123])).toThrow();
219+
});
220+
221+
it('should validate goForward arguments', () => {
222+
expect(() => validateConstraints('goForward', [50000])).not.toThrow();
223+
expect(() => validateConstraints('goForward', [-100001])).toThrow();
224+
expect(() => validateConstraints('goForward', [100001])).toThrow();
225+
});
226+
227+
it('should validate goBackward arguments', () => {
228+
expect(() => validateConstraints('goBackward', [50000])).not.toThrow();
229+
expect(() => validateConstraints('goBackward', [-100001])).toThrow();
230+
expect(() => validateConstraints('goBackward', [100001])).toThrow();
231+
});
232+
233+
it('should validate turnRight arguments', () => {
234+
expect(() => validateConstraints('turnRight', [180])).not.toThrow();
235+
expect(() => validateConstraints('turnRight', [-361])).toThrow();
236+
expect(() => validateConstraints('turnRight', [361])).toThrow();
237+
});
238+
239+
it('should validate turnLeft arguments', () => {
240+
expect(() => validateConstraints('turnLeft', [180])).not.toThrow();
241+
expect(() => validateConstraints('turnLeft', [-361])).toThrow();
242+
expect(() => validateConstraints('turnLeft', [361])).toThrow();
243+
});
244+
245+
it('should validate setXY arguments', () => {
246+
expect(() => validateConstraints('setXY', [50000, 50000])).not.toThrow();
247+
expect(() => validateConstraints('setXY', [-100001, 50000])).toThrow();
248+
expect(() => validateConstraints('setXY', [100001, 50000])).toThrow();
249+
});
250+
251+
it('should validate setHeading arguments', () => {
252+
expect(() => validateConstraints('setHeading', [180])).not.toThrow();
253+
expect(() => validateConstraints('setHeading', [-361])).toThrow();
254+
expect(() => validateConstraints('setHeading', [361])).toThrow();
255+
});
256+
257+
it('should validate drawArc arguments', () => {
258+
expect(() => validateConstraints('drawArc', [180, 50000])).not.toThrow();
259+
expect(() => validateConstraints('drawArc', [-361, 50000])).toThrow();
260+
expect(() => validateConstraints('drawArc', [361, 50000])).toThrow();
261+
expect(() => validateConstraints('drawArc', [180, -1])).toThrow();
262+
});
263+
264+
it('should validate drawBezier arguments', () => {
265+
expect(() => validateConstraints('drawBezier', [50000, 50000])).not.toThrow();
266+
expect(() => validateConstraints('drawBezier', [-100001, 50000])).toThrow();
267+
expect(() => validateConstraints('drawBezier', [100001, 50000])).toThrow();
268+
});
269+
270+
it('should validate setBezierControlPoint1 arguments', () => {
271+
expect(() => validateConstraints('setBezierControlPoint1', [50000, 50000])).not.toThrow();
272+
expect(() => validateConstraints('setBezierControlPoint1', [-100001, 50000])).toThrow();
273+
expect(() => validateConstraints('setBezierControlPoint1', [100001, 50000])).toThrow();
274+
});
275+
276+
it('should validate scrollXY arguments', () => {
277+
expect(() => validateConstraints('scrollXY', [50000, 50000])).not.toThrow();
278+
expect(() => validateConstraints('scrollXY', [-100001, 50000])).toThrow();
279+
expect(() => validateConstraints('scrollXY', [100001, 50000])).toThrow();
280+
});
281+
282+
it('should validate setColor arguments', () => {
283+
expect(() => validateConstraints('setColor', [50])).not.toThrow();
284+
expect(() => validateConstraints('setColor', [-1])).toThrow();
285+
expect(() => validateConstraints('setColor', [101])).toThrow();
286+
});
287+
288+
it('should validate setGrey arguments', () => {
289+
expect(() => validateConstraints('setGrey', [50])).not.toThrow();
290+
expect(() => validateConstraints('setGrey', [-1])).toThrow();
291+
expect(() => validateConstraints('setGrey', [101])).toThrow();
292+
});
293+
294+
it('should validate setShade arguments', () => {
295+
expect(() => validateConstraints('setShade', [50])).not.toThrow();
296+
expect(() => validateConstraints('setShade', [-1])).toThrow();
297+
expect(() => validateConstraints('setShade', [101])).toThrow();
298+
});
299+
300+
it('should validate setHue arguments', () => {
301+
expect(() => validateConstraints('setHue', [50])).not.toThrow();
302+
expect(() => validateConstraints('setHue', [-1])).toThrow();
303+
expect(() => validateConstraints('setHue', [101])).toThrow();
304+
});
305+
306+
it('should validate setTranslucency arguments', () => {
307+
expect(() => validateConstraints('setTranslucency', [50])).not.toThrow();
308+
expect(() => validateConstraints('setTranslucency', [-1])).toThrow();
309+
expect(() => validateConstraints('setTranslucency', [101])).toThrow();
310+
});
311+
312+
it('should validate setPensize arguments', () => {
313+
expect(() => validateConstraints('setPensize', [50])).not.toThrow();
314+
expect(() => validateConstraints('setPensize', [-1])).toThrow();
315+
expect(() => validateConstraints('setPensize', [101])).toThrow();
316+
});
317+
});

js/js-export/constraints.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,3 +1191,6 @@ JSInterface._methodArgConstraints = {
11911191
]
11921192
]
11931193
};
1194+
if (typeof module !== 'undefined' && module.exports) {
1195+
module.exports = { JSInterface };
1196+
}

0 commit comments

Comments
 (0)