Skip to content

Commit d3e8f33

Browse files
committed
more updates
1 parent 3ea00b5 commit d3e8f33

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

src/core/friendly_errors/sketch_verifier.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ function sketchVerifier(p5, fn) {
4747
const p5Constructors = {};
4848

4949
fn.loadP5Constructors = function () {
50-
console.log("loading...");
5150
// Make a list of all p5 classes to be used for argument validation
5251
// This must be done only when everything has loaded otherwise we get
5352
// an empty array
@@ -58,7 +57,6 @@ function sketchVerifier(p5, fn) {
5857
p5Constructors[key] = p5[key];
5958
}
6059
}
61-
console.log('p5Constructors:', p5Constructors);
6260
}
6361

6462
/**
@@ -188,18 +186,22 @@ function sketchVerifier(p5, fn) {
188186
...userDefinitions.variables,
189187
...userDefinitions.functions
190188
];
191-
console.log(allDefinitions);
192189

190+
// Helper function that generates a friendly error message that contains
191+
// the type of redefinition (constant or function), the name of the
192+
// redefinition, the line number in user's code, and a link to its
193+
// reference on the p5.js website.
193194
function generateFriendlyError(errorType, name, line) {
194195
const url = `https://p5js.org/reference/#/p5/${name}`;
195196
const message = `${errorType} "${name}" on line ${line} is being redeclared and conflicts with a p5.js ${errorType.toLowerCase()}. JavaScript does not declaring a ${errorType.toLowerCase()} more than once. p5.js reference: ${url}.`;
196197
return message;
197198
}
198199

200+
// Helper function that checks if a user definition has already been defined
201+
// in the p5.js library, either as a constant or as a function.
199202
function checkForRedefinition(name, libValue, line, type) {
200-
console.log('name:', name, 'libValue:', libValue, 'line:', line, 'type:', type);
201203
try {
202-
const userValue = eval(name);
204+
const userValue = eval("name");
203205
if (libValue !== userValue) {
204206
let message = generateFriendlyError(type, name, line);
205207
console.log(message);
@@ -212,6 +214,7 @@ function sketchVerifier(p5, fn) {
212214
return false;
213215
}
214216

217+
// Checks for constant redefinitions.
215218
for (let { name, line } of allDefinitions) {
216219
const libDefinition = constants[name];
217220
if (libDefinition !== undefined) {

test/unit/core/sketch_overrides.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import sketchVerifier from '../../../src/core/friendly_errors/sketch_verifier.js
33
suite('Sketch Verifier', function () {
44
const mockP5 = {
55
_validateParameters: vi.fn(),
6-
Render: function () {
7-
return 'mock render';
8-
},
6+
Renderer: function () { },
97
};
8+
mockP5.Renderer.prototype.rect = vi.fn();
109
const mockP5Prototype = {};
1110

1211
beforeAll(function () {
@@ -184,34 +183,53 @@ suite('Sketch Verifier', function () {
184183
});
185184

186185
suite('checkForConstsAndFuncs()', function () {
186+
// Set up for this suite of tests
187+
let consoleSpy;
188+
189+
beforeEach(function () {
190+
consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => { });
191+
});
192+
193+
afterEach(function () {
194+
consoleSpy.mockRestore();
195+
});
196+
187197
test('Detects conflict with p5.js constant', function () {
188198
const userDefinitions = {
189199
variables: [{ name: 'PI', line: 1 }],
190200
functions: []
191201
};
192-
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => { });
193-
194202
const result = mockP5Prototype.checkForConstsAndFuncs(userDefinitions);
195203

196204
expect(result).toBe(true);
197205
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('Constant "PI" on line 1 is being redeclared and conflicts with a p5.js constant'));
198-
199-
consoleSpy.mockRestore();
200206
});
201207

202208
test('Detects conflict with p5.js global function', function () {
203209
const userDefinitions = {
204210
variables: [],
205-
functions: [{ name: 'setup', line: 2 }]
211+
functions: [{ name: 'rect', line: 2 }]
206212
};
207-
//const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => { });
208-
209213
const result = mockP5Prototype.checkForConstsAndFuncs(userDefinitions);
210214

211215
expect(result).toBe(true);
212-
//expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('Function "setup" on line 2 is being redeclared and conflicts with a p5.js function'));
216+
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('Function "rect" on line 2 is being redeclared and conflicts with a p5.js function'));
217+
});
218+
219+
test('allows redefinition of whitelisted functions', function () {
220+
const userDefinitions = {
221+
variables: [],
222+
functions: [
223+
{ name: 'setup', line: 1 },
224+
{ name: 'draw', line: 2 },
225+
{ name: 'preload', line: 3 }
226+
]
227+
};
213228

214-
//consoleSpy.mockRestore();
229+
const result = mockP5Prototype.checkForConstsAndFuncs(userDefinitions);
230+
231+
expect(result).toBe(false);
232+
expect(consoleSpy).not.toHaveBeenCalled();
215233
});
216234

217235
test('Returns false when no conflicts are found', function () {

0 commit comments

Comments
 (0)