|
| 1 | +import { jsPreprocess } from './jsPreprocess'; |
| 2 | + |
| 3 | +describe('jsPreprocess', () => { |
| 4 | + describe('// noprotect', () => { |
| 5 | + it('returns code unchanged when // noprotect is present', () => { |
| 6 | + const code = '// noprotect\nfor (let i = 0; i < 10; i++) {}'; |
| 7 | + expect(jsPreprocess(code)).toBe(code); |
| 8 | + }); |
| 9 | + }); |
| 10 | + |
| 11 | + describe('regular loop protection', () => { |
| 12 | + it('adds loop protection to a for loop', () => { |
| 13 | + const code = 'for (let i = 0; i < 10; i++) {}'; |
| 14 | + const result = jsPreprocess(code); |
| 15 | + expect(result).toContain('window.loopProtect.hit'); |
| 16 | + expect(result).toContain('Date.now()'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('adds loop protection to a while loop', () => { |
| 20 | + const code = 'while (true) {}'; |
| 21 | + const result = jsPreprocess(code); |
| 22 | + expect(result).toContain('window.loopProtect.hit'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('adds loop protection to a do-while loop', () => { |
| 26 | + const code = 'do {} while (true)'; |
| 27 | + const result = jsPreprocess(code); |
| 28 | + expect(result).toContain('window.loopProtect.hit'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('returns original code if transform fails', () => { |
| 32 | + const code = 'this is not valid javascript @@@@'; |
| 33 | + expect(jsPreprocess(code)).toBe(code); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('shader strings', () => { |
| 38 | + it('does not modify for loops inside template literal shader strings', () => { |
| 39 | + const code = ` |
| 40 | + const shader = \` |
| 41 | + for (int i = 0; i < 20; i++) { |
| 42 | + total += texture(tex, uv); |
| 43 | + } |
| 44 | + \`; |
| 45 | + `; |
| 46 | + const result = jsPreprocess(code); |
| 47 | + expect(result).not.toContain('window.loopProtect.hit'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('does not modify for loops inside single-quoted strings', () => { |
| 51 | + const code = "const glsl = 'for (int i = 0; i < 10; i++) {}';"; |
| 52 | + const result = jsPreprocess(code); |
| 53 | + expect(result).not.toContain('window.loopProtect.hit'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('does not modify GLSL inside buildFilterShader object argument', () => { |
| 57 | + const code = ` |
| 58 | + blur = buildFilterShader({ |
| 59 | + 'vec4 getColor': \`(FilterInputs inputs) { |
| 60 | + for (int i = 0; i < 20; i++) { |
| 61 | + total += getTexture(canvasContent, inputs.texCoord); |
| 62 | + } |
| 63 | + }\` |
| 64 | + }); |
| 65 | + `; |
| 66 | + const result = jsPreprocess(code); |
| 67 | + expect(result).not.toContain('window.loopProtect.hit'); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('p5.strands shader functions', () => { |
| 72 | + it('skips loop protection for named function passed to buildFilterShader', () => { |
| 73 | + const code = ` |
| 74 | + blur = buildFilterShader(doBlur); |
| 75 | + function doBlur() { |
| 76 | + for (let i = 0; i < 20; i++) {} |
| 77 | + } |
| 78 | + `; |
| 79 | + const result = jsPreprocess(code); |
| 80 | + expect(result).not.toContain('window.loopProtect.hit'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('skips loop protection for arrow function variable passed to buildFilterShader', () => { |
| 84 | + const code = ` |
| 85 | + const doBlur = () => { |
| 86 | + for (let i = 0; i < 20; i++) {} |
| 87 | + }; |
| 88 | + blur = buildFilterShader(doBlur); |
| 89 | + `; |
| 90 | + const result = jsPreprocess(code); |
| 91 | + expect(result).not.toContain('window.loopProtect.hit'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('skips loop protection for inline function passed to buildFilterShader', () => { |
| 95 | + const code = ` |
| 96 | + blur = buildFilterShader(function() { |
| 97 | + for (let i = 0; i < 20; i++) {} |
| 98 | + }); |
| 99 | + `; |
| 100 | + const result = jsPreprocess(code); |
| 101 | + expect(result).not.toContain('window.loopProtect.hit'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('skips loop protection for function passed to base*Shader().modify()', () => { |
| 105 | + const code = ` |
| 106 | + blur = baseFilterShader().modify(doBlur); |
| 107 | + function doBlur() { |
| 108 | + for (let i = 0; i < 20; i++) {} |
| 109 | + } |
| 110 | + `; |
| 111 | + const result = jsPreprocess(code); |
| 112 | + expect(result).not.toContain('window.loopProtect.hit'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('still protects regular loops outside shader functions', () => { |
| 116 | + const code = ` |
| 117 | + blur = buildFilterShader(doBlur); |
| 118 | + function doBlur() { |
| 119 | + for (let i = 0; i < 20; i++) {} |
| 120 | + } |
| 121 | + function draw() { |
| 122 | + for (let j = 0; j < 100; j++) {} |
| 123 | + } |
| 124 | + `; |
| 125 | + const result = jsPreprocess(code); |
| 126 | + expect(result).toContain('window.loopProtect.hit'); |
| 127 | + }); |
| 128 | + }); |
| 129 | +}); |
0 commit comments