Skip to content

Commit b9ab468

Browse files
committed
revert test case of param_validator
to match the test cases - split the error messages, to get a type in line 418 and 462 - `actualType` will never be undefined which is checked in line 394 so make it undefined in line 430 to pass all test cases
1 parent d5b08c2 commit b9ab468

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

src/core/friendly_errors/param_validator.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ function validateParams(p5, fn, lifecycles) {
415415
}
416416

417417
if (issue.code === 'invalid_type') {
418+
actualType = issue.message.split(', received ')[1]
418419
expectedTypes.add(issue.expected);
419420
}
420421
// The case for constants. Since we don't want to print out the actual
@@ -426,6 +427,7 @@ function validateParams(p5, fn, lifecycles) {
426427
} else if (issue.code === 'custom') {
427428
const match = issue.message.match(/Input not instance of (\w+)/);
428429
if (match) expectedTypes.add(match[1]);
430+
actualType = undefined
429431
}
430432
}
431433
});
@@ -457,7 +459,7 @@ function validateParams(p5, fn, lifecycles) {
457459
break;
458460
}
459461
case 'invalid_type': {
460-
message += buildTypeMismatchMessage(currentError.message, currentError.expected, currentError.path.join('.'));
462+
message += buildTypeMismatchMessage(currentError.message.split(', received ')[1], currentError.expected, currentError.path.join('.'));
461463
break;
462464
}
463465
case 'too_big': {

test/unit/core/param_errors.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ suite('Validate Params', function () {
110110

111111
const invalidInputs = [
112112
{ 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.' },
113-
{ 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 Invalid input: expected number, received undefined in p5.arc().' },
114-
{ name: 'missing required param #4', input: [200, 100, 100, 80, undefined, 0], msg: '🌸 p5.js says: Expected number at the fifth parameter, but received Invalid input: expected number, received undefined in p5.arc().' },
115-
{ 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 Invalid input: expected number, received undefined in p5.arc().' },
116-
{ 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 Invalid input: expected number, received string in p5.arc().' }
113+
{ 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().' },
114+
{ 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().' },
115+
{ 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().' },
116+
{ 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().' }
117117
];
118118

119119
invalidInputs.forEach(({ name, input, msg }) => {
@@ -130,7 +130,7 @@ suite('Validate Params', function () {
130130
console.log(result);
131131
assert.equal(
132132
result.error,
133-
'🌸 p5.js says: Did you mean to put `await` before a loading function? An unexpected Promise was found. Expected Image or Element or Texture or Framebuffer or FramebufferTexture or Renderer or Graphics at the first parameter, but received Input not instance of Image in p5.image().'
133+
'🌸 p5.js says: Did you mean to put `await` before a loading function? An unexpected Promise was found. Expected Image or Element or Texture or Framebuffer or FramebufferTexture or Renderer or Graphics at the first parameter in p5.image().'
134134
);
135135
});
136136
});
@@ -144,13 +144,13 @@ suite('Validate Params', function () {
144144

145145
suite('validateParams: a few edge cases', function () {
146146
const invalidInputs = [
147-
{ 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 Invalid input: expected number, received string in p5.color().' },
148-
{ fn: 'color', name: 'superfluous parameter', input: [[0, 0, 0], 0], msg: '🌸 p5.js says: Expected number at the first parameter, but received Invalid input: expected number, received array in p5.color().' },
149-
{ fn: 'color', name: 'wrong element types', input: [['A', 'B', 'C']], msg: '🌸 p5.js says: Expected number at the first parameter, but received Invalid input: expected number, received array in p5.color().' },
150-
{ 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 Invalid input: expected number, received null in p5.rect().' },
147+
{ 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().' },
148+
{ 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().' },
149+
{ 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().' },
150+
{ 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().' },
151151
{ 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.' },
152-
{ fn: 'line', name: 'null string given', input: [1, 2, 4, 'null'], msg: '🌸 p5.js says: Expected number at the fourth parameter, but received Invalid input: expected number, received string in p5.line().' },
153-
{ fn: 'line', name: 'NaN value given', input: [1, 2, 4, NaN], msg: '🌸 p5.js says: Expected number at the fourth parameter, but received Invalid input: expected number, received NaN in p5.line().' }
152+
{ 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().' },
153+
{ 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().' }
154154
];
155155

156156
invalidInputs.forEach(({ name, input, fn, msg }) => {
@@ -163,12 +163,12 @@ suite('Validate Params', function () {
163163

164164
suite('validateParams: trailing undefined arguments', function () {
165165
const invalidInputs = [
166-
{ fn: 'color', name: 'missing params #1, #2', input: [12, undefined, undefined], msg: '🌸 p5.js says: Expected number at the second parameter, but received Invalid input: expected number, received undefined in p5.color().' },
166+
{ 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().' },
167167
// Even though the undefined arguments are technically allowed for
168168
// optional parameters, it is more likely that the user wanted to call
169169
// the function with meaningful arguments.
170170
{ 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.' },
171-
{ fn: 'circle', name: 'missing compulsory parameter #2', input: [5, 5, undefined], msg: '🌸 p5.js says: Expected number at the third parameter, but received Invalid input: expected number, received undefined in p5.circle().' }
171+
{ 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().' }
172172
];
173173

174174
invalidInputs.forEach(({ fn, name, input, msg }) => {
@@ -193,9 +193,9 @@ suite('Validate Params', function () {
193193
});
194194

195195
const invalidInputs = [
196-
{ name: 'optional parameter, incorrect type', input: [65, 100, 100, 'a'], msg: '🌸 p5.js says: Expected number at the fourth parameter, but received Invalid input: expected number, received string in p5.color().' },
197-
{ name: 'extra parameter', input: [[65, 100, 100], 100], msg: '🌸 p5.js says: Expected number at the first parameter, but received Invalid input: expected number, received array in p5.color().' },
198-
{ name: 'incorrect element type', input: ['A', 'B', 'C'], msg: '🌸 p5.js says: Expected number at the first parameter, but received Invalid input: expected number, received string in p5.color().' },
196+
{ 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().' },
197+
{ name: 'extra parameter', input: [[65, 100, 100], 100], msg: '🌸 p5.js says: Expected number at the first parameter, but received array in p5.color().' },
198+
{ name: 'incorrect element type', input: ['A', 'B', 'C'], msg: '🌸 p5.js says: Expected number at the first parameter, but received string in p5.color().' },
199199
{ 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.' }
200200
];
201201

@@ -223,7 +223,7 @@ suite('Validate Params', function () {
223223

224224
test(`set() with Boolean (invalid)`, function () {
225225
const result = mockP5Prototype.validate('p5.set', [0, 0, true]);
226-
assert.equal(result.error, '🌸 p5.js says: Expected number or array or object at the third parameter, but received Invalid input: expected number, received boolean in p5.set().');
226+
assert.equal(result.error, '🌸 p5.js says: Expected number or array or object at the third parameter, but received boolean in p5.set().');
227227
});
228228
});
229229

@@ -278,4 +278,4 @@ suite('Validate Params', function () {
278278
assert.isFalse(result.success);
279279
});
280280
});
281-
});
281+
});

0 commit comments

Comments
 (0)