Skip to content

Commit 82a92f8

Browse files
committed
Add some unit tests
1 parent d50d0cb commit 82a92f8

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

src/webgl/p5.Shader.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ p5.Shader = class {
327327
* </div>
328328
*/
329329
modify(hooks) {
330+
p5._validateParameters('p5.Shader.modify', arguments);
330331
const newHooks = {
331332
vertex: {},
332333
fragment: {}
@@ -345,7 +346,7 @@ p5.Shader = class {
345346
newHooks.fragment[key] = hooks[key];
346347
} else {
347348
console.error(
348-
`We weren't able to find a hook matching the name ${key}. Try calling .inspect() on the shader you are trying to modify to make sure you're using the right name.`
349+
`We weren't able to find a hook matching the name ${key}. Try calling .inspectHooks() on the shader you are trying to modify to make sure you're using the right name.`
349350
);
350351
}
351352
}

test/unit/webgl/p5.Shader.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,5 +308,83 @@ suite('p5.Shader', function() {
308308
myp5.shader(s);
309309
assert.isFalse(s.isStrokeShader());
310310
});
311+
312+
suite('Hooks', function() {
313+
let myShader;
314+
315+
setup(function() {
316+
myShader = myp5.createShader(
317+
`
318+
precision highp float;
319+
320+
attribute vec3 aPosition;
321+
attribute vec2 aTexCoord;
322+
attribute vec4 aVertexColor;
323+
324+
uniform mat4 uModelViewMatrix;
325+
uniform mat4 uProjectionMatrix;
326+
327+
varying vec2 vTexCoord;
328+
varying vec4 vVertexColor;
329+
330+
void main() {
331+
// Apply the camera transform
332+
vec4 viewModelPosition =
333+
uModelViewMatrix *
334+
vec4(aPosition, 1.0);
335+
336+
// Tell WebGL where the vertex goes
337+
gl_Position =
338+
uProjectionMatrix *
339+
viewModelPosition;
340+
341+
// Pass along data to the fragment shader
342+
vTexCoord = aTexCoord;
343+
vVertexColor = aVertexColor;
344+
}
345+
`,
346+
`
347+
precision highp float;
348+
349+
varying vec2 vTexCoord;
350+
varying vec4 vVertexColor;
351+
352+
void main() {
353+
// Tell WebGL what color to make the pixel
354+
gl_FragColor = HOOK_getVertexColor(vVertexColor);
355+
}
356+
`,
357+
{
358+
fragment: {
359+
'vec4 getVertexColor': '(vec4 color) { return color; }'
360+
}
361+
}
362+
);
363+
});
364+
365+
test('available hooks show up in inspectHooks()', function() {
366+
const logs = [];
367+
const myLog = (...data) => logs.push(data.join(', '));
368+
const oldLog = console.log;
369+
console.log = myLog;
370+
myShader.inspectHooks();
371+
console.log = oldLog;
372+
expect(logs.join('\n')).to.match(/vec4 getVertexColor/);
373+
});
374+
375+
test('unfilled hooks do not have an AUGMENTED_HOOK define', function() {
376+
const modified = myShader.modify({});
377+
expect(modified.fragSrc()).not.to.match(/#define AUGMENTED_HOOK_getVertexColor/);
378+
});
379+
380+
test('filled hooks do have an AUGMENTED_HOOK define', function() {
381+
const modified = myShader.modify({
382+
'vec4 getVertexColor': `(vec4 c) {
383+
return vec4(1., 0., 0., 1.);
384+
}`
385+
});
386+
expect(modified.fragSrc()).to.match(/#define AUGMENTED_HOOK_getVertexColor/);
387+
});
388+
});
311389
});
312390
});

0 commit comments

Comments
 (0)