Skip to content

Commit 97e2c8a

Browse files
committed
Add new tests for validateParameters + fix typos
1 parent 7a3d45a commit 97e2c8a

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

src/core/error_helpers.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ if (typeof IS_MINIFIED !== 'undefined') {
313313
// - seen: true
314314
// seen: true signifies that this argument was also seen as the last
315315
// argument in a call. Now in the second run of the sketch, it would traverse
316-
// the existing tree and see get seen: true, i.e this sequence was seen
316+
// the existing tree and see seen: true, i.e this sequence was seen
317317
// before and so scoring can be skipped. This also prevents logging multiple
318318
// validation messages for the same thing.
319319

@@ -771,6 +771,11 @@ if (typeof IS_MINIFIED !== 'undefined') {
771771
}
772772
};
773773

774+
// allowing access to argumentTree for testing
775+
p5._getValidateParamsArgTree = function getValidateParamsArgTree() {
776+
return argumentTree;
777+
};
778+
774779
/**
775780
* Validates parameters
776781
* param {String} func the name of the function

test/unit/core/error_helpers.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,76 @@ suite('Error Helpers', function() {
123123
}
124124
);
125125

126+
suite('validateParameters: argument tree', function() {
127+
// should not throw a validation error for the same kind of wrong args
128+
// more than once. This prevents repetetive validation logs for a
129+
// function that is called in a loop or draw()
130+
testUnMinified(
131+
'no repeated validation error for the same wrong arguments',
132+
function() {
133+
assert.validationError(function() {
134+
myp5.color();
135+
});
136+
137+
assert.doesNotThrow(
138+
function() {
139+
myp5.color(); // Same type of wrong arguments as above
140+
},
141+
p5.ValidationError,
142+
'got unwanted ValidationError'
143+
);
144+
}
145+
);
146+
147+
testUnMinified(
148+
'should throw validation errors for different wrong args',
149+
function() {
150+
assert.validationError(function() {
151+
myp5.color();
152+
});
153+
154+
assert.validationError(function() {
155+
myp5.color(false);
156+
});
157+
}
158+
);
159+
160+
testUnMinified('arg tree is built properly', function() {
161+
let myArgTree = p5._getValidateParamsArgTree();
162+
myp5.random();
163+
myp5.random(50);
164+
myp5.random([50, 70, 10]);
165+
assert.strictEqual(
166+
myArgTree.random.seen,
167+
true,
168+
'tree built correctly for random()'
169+
);
170+
assert.strictEqual(
171+
myArgTree.random.number.seen,
172+
true,
173+
'tree built correctly for random(min: Number)'
174+
);
175+
assert.strictEqual(
176+
myArgTree.random.as.number.number.number.seen,
177+
true,
178+
'tree built correctly for random(choices: Array)'
179+
);
180+
181+
let c = myp5.color(10);
182+
myp5.alpha(c);
183+
assert.strictEqual(
184+
myArgTree.color.number.seen,
185+
true,
186+
'tree built correctly for color(gray: Number)'
187+
);
188+
assert.strictEqual(
189+
myArgTree.alpha.Color.seen,
190+
true,
191+
'tree built correctly for alpha(color: p5.Color)'
192+
);
193+
});
194+
});
195+
126196
suite('validateParameters: multi-format', function() {
127197
test('color(): no friendly-err-msg', function() {
128198
assert.doesNotThrow(

0 commit comments

Comments
 (0)