Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/friendly_errors/param_validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function validateParams(p5, fn, lifecycles) {
'Boolean': z.boolean(),
'Function': z.function(),
'Integer': z.number().int(),
'Number': z.number(),
'Number': z.union([z.number(), z.literal(Infinity), z.literal(-Infinity)]),
'Object': z.object({}),
'String': z.string()
};
Expand Down
32 changes: 16 additions & 16 deletions test/unit/core/param_errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ suite('Validate Params', function () {

const invalidInputs = [
{ name: 'missing required arc parameters #4, #5', input: [200, 100, 100, 80], msg: '🌸 p5.js says: Expected at least 6 arguments, but received fewer in p5.arc(). For more information, see https://p5js.org/reference/p5/arc.' },
{ name: 'missing required param #0', input: [undefined, 100, 100, 80, 0, Math.PI, constants.PIE, 30], msg: '🌸 p5.js says: Expected number at the first parameter, but received undefined in p5.arc().' },
{ name: 'missing required param #4', input: [200, 100, 100, 80, undefined, 0], msg: '🌸 p5.js says: Expected number at the fifth parameter, but received undefined in p5.arc().' },
{ name: 'missing optional param #5', input: [200, 100, 100, 80, 0, undefined, Math.PI], msg: '🌸 p5.js says: Expected number at the sixth parameter, but received undefined in p5.arc().' },
{ name: 'wrong param type at #0', input: ['a', 100, 100, 80, 0, Math.PI, constants.PIE, 30], msg: '🌸 p5.js says: Expected number at the first parameter, but received string in p5.arc().' }
{ name: 'missing required param #0', input: [undefined, 100, 100, 80, 0, Math.PI, constants.PIE, 30], msg: '🌸 p5.js says: Expected number or constant (please refer to documentation for allowed values) at the first parameter in p5.arc().' },
{ name: 'missing required param #4', input: [200, 100, 100, 80, undefined, 0], msg: '🌸 p5.js says: Expected number or constant (please refer to documentation for allowed values) at the fifth parameter in p5.arc().' },
{ name: 'missing optional param #5', input: [200, 100, 100, 80, 0, undefined, Math.PI], msg: '🌸 p5.js says: Expected number or constant (please refer to documentation for allowed values) at the sixth parameter in p5.arc().' },
{ name: 'wrong param type at #0', input: ['a', 100, 100, 80, 0, Math.PI, constants.PIE, 30], msg: '🌸 p5.js says: Expected number or constant (please refer to documentation for allowed values) at the first parameter, but received a in p5.arc().' }
];

invalidInputs.forEach(({ name, input, msg }) => {
Expand Down Expand Up @@ -145,13 +145,13 @@ suite('Validate Params', function () {

suite('validateParams: a few edge cases', function () {
const invalidInputs = [
{ fn: 'color', name: 'wrong type for optional parameter', input: [0, 0, 0, 'A'], msg: '🌸 p5.js says: Expected number at the fourth parameter, but received string in p5.color().' },
{ fn: 'color', name: 'superfluous parameter', input: [[0, 0, 0], 0], msg: '🌸 p5.js says: Expected number at the first parameter, but received array in p5.color().' },
{ fn: 'color', name: 'wrong element types', input: [['A', 'B', 'C']], msg: '🌸 p5.js says: Expected number at the first parameter, but received array in p5.color().' },
{ fn: 'rect', name: 'null, non-trailing, optional parameter', input: [0, 0, 0, 0, null, 0, 0, 0], msg: '🌸 p5.js says: Expected number at the fifth parameter, but received null in p5.rect().' },
{ fn: 'color', name: 'wrong type for optional parameter', input: [0, 0, 0, 'A'], msg: '🌸 p5.js says: Expected number or constant (please refer to documentation for allowed values) at the fourth parameter, but received A in p5.color().' },
{ fn: 'color', name: 'superfluous parameter', input: [[0, 0, 0], 0], msg: '🌸 p5.js says: Expected number or constant (please refer to documentation for allowed values) at the first parameter, but received 0,0,0 in p5.color().' },
{ fn: 'color', name: 'wrong element types', input: [['A', 'B', 'C']], msg: '🌸 p5.js says: Expected number or constant (please refer to documentation for allowed values) at the first parameter, but received A,B,C in p5.color().' },
{ fn: 'rect', name: 'null, non-trailing, optional parameter', input: [0, 0, 0, 0, null, 0, 0, 0], msg: '🌸 p5.js says: Expected number or constant (please refer to documentation for allowed values) at the fifth parameter in p5.rect().' },
{ fn: 'color', name: 'too many args + wrong types too', input: ['A', 'A', 0, 0, 0, 0, 0, 0, 0, 0], msg: '🌸 p5.js says: Expected at most 4 arguments, but received more in p5.color(). For more information, see https://p5js.org/reference/p5/color.' },
{ fn: 'line', name: 'null string given', input: [1, 2, 4, 'null'], msg: '🌸 p5.js says: Expected number at the fourth parameter, but received string in p5.line().' },
{ fn: 'line', name: 'NaN value given', input: [1, 2, 4, NaN], msg: '🌸 p5.js says: Expected number at the fourth parameter, but received NaN in p5.line().' }
{ fn: 'line', name: 'null string given', input: [1, 2, 4, 'null'], msg: '🌸 p5.js says: Expected number or constant (please refer to documentation for allowed values) at the fourth parameter, but received null in p5.line().' },
{ fn: 'line', name: 'NaN value given', input: [1, 2, 4, NaN], msg: '🌸 p5.js says: Expected number or constant (please refer to documentation for allowed values) at the fourth parameter in p5.line().' }
];

invalidInputs.forEach(({ name, input, fn, msg }) => {
Expand All @@ -164,12 +164,12 @@ suite('Validate Params', function () {

suite('validateParams: trailing undefined arguments', function () {
const invalidInputs = [
{ fn: 'color', name: 'missing params #1, #2', input: [12, undefined, undefined], msg: '🌸 p5.js says: Expected number at the second parameter, but received undefined in p5.color().' },
{ fn: 'color', name: 'missing params #1, #2', input: [12, undefined, undefined], msg: '🌸 p5.js says: Expected number or constant (please refer to documentation for allowed values) at the second parameter in p5.color().' },
// Even though the undefined arguments are technically allowed for
// optional parameters, it is more likely that the user wanted to call
// the function with meaningful arguments.
{ fn: 'random', name: 'missing params #0, #1', input: [undefined, undefined], msg: '🌸 p5.js says: All arguments for p5.random() are undefined. There is likely an error in the code.' },
{ fn: 'circle', name: 'missing compulsory parameter #2', input: [5, 5, undefined], msg: '🌸 p5.js says: Expected number at the third parameter, but received undefined in p5.circle().' }
{ fn: 'circle', name: 'missing compulsory parameter #2', input: [5, 5, undefined], msg: '🌸 p5.js says: Expected number or constant (please refer to documentation for allowed values) at the third parameter in p5.circle().' }
];

invalidInputs.forEach(({ fn, name, input, msg }) => {
Expand All @@ -194,9 +194,9 @@ suite('Validate Params', function () {
});

const invalidInputs = [
{ name: 'optional parameter, incorrect type', input: [65, 100, 100, 'a'], msg: '🌸 p5.js says: Expected number at the fourth parameter, but received string in p5.color().' },
{ name: 'extra parameter', input: [[65, 100, 100], 100], msg: '🌸 p5.js says: Expected number at the first parameter, but received array in p5.color().' },
{ name: 'incorrect element type', input: ['A', 'B', 'C'], msg: '🌸 p5.js says: Expected number at the first parameter, but received string in p5.color().' },
{ name: 'optional parameter, incorrect type', input: [65, 100, 100, 'a'], msg: '🌸 p5.js says: Expected number or constant (please refer to documentation for allowed values) at the fourth parameter, but received a in p5.color().' },
{ name: 'extra parameter', input: [[65, 100, 100], 100], msg: '🌸 p5.js says: Expected number or constant (please refer to documentation for allowed values) at the first parameter, but received 65,100,100 in p5.color().' },
{ name: 'incorrect element type', input: ['A', 'B', 'C'], msg: '🌸 p5.js says: Expected number or constant (please refer to documentation for allowed values) at the first parameter, but received A in p5.color().' },
{ name: 'incorrect parameter count', input: ['A', 'A', 0, 0, 0, 0, 0, 0], msg: '🌸 p5.js says: Expected at most 4 arguments, but received more in p5.color(). For more information, see https://p5js.org/reference/p5/color.' }
];

Expand Down Expand Up @@ -224,7 +224,7 @@ suite('Validate Params', function () {

test('set() with Boolean (invalid)', function () {
const result = mockP5Prototype._validate('p5.set', [0, 0, true]);
assert.equal(result.error, '🌸 p5.js says: Expected number or array or object at the third parameter, but received boolean in p5.set().');
assert.equal(result.error, '🌸 p5.js says: Expected array or object at the third parameter, but received boolean in p5.set().');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there a reason to alter the tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it passes the test cases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The solution to #8104 should not change any tests. Tests are how we ensure that there are no side-effects of bugfixes, so it's important that they pass. To make them pass, the implementation wold need to be updated, such as using the suggestions in #8109

});
});

Expand Down
Loading