UNPKG

3.98 kBJavaScriptView Raw
1import transformSource from "./transformSource";
2
3test("parse correctly burn.glsl", () => {
4 const glsl = `
5// author: gre
6// license: MIT
7uniform vec3 color; // = vec3(0.9, 0.4, 0.2)
8vec4 transition (vec2 uv) {
9 return mix(
10 getFromColor(uv) + vec4(progress*color, 1.0),
11 getToColor(uv) + vec4((1.0-progress)*color, 1.0),
12 progress
13 );
14}
15 `;
16 expect(transformSource("burn.glsl", glsl)).toMatchSnapshot();
17});
18
19test("not providing a default creates a warning", () => {
20 const glsl = `
21// Author: gre
22// License: MIT
23uniform vec3 color;
24vec4 transition (vec2 uv) {
25 return mix(
26 getFromColor(uv) + vec4(progress*color, 1.0),
27 getToColor(uv) + vec4((1.0-progress)*color, 1.0),
28 progress
29 );
30}
31 `;
32 const res = transformSource("burn-2.glsl", glsl);
33 expect(res.data).toMatchSnapshot();
34 expect(res.errors.filter(e => e.type === "error")).toEqual([]);
35 expect(res.errors.filter(e => e.type === "warn").length).toBe(1);
36});
37
38test("other example", () => {
39 const res = transformSource(
40 "test.glsl",
41 `
42 // author: gre
43 // License: MIT
44 uniform vec3 color /* = vec3(0.9, 0.4, 0.2) */;
45 uniform float foo; /* = 42. */
46 uniform sampler2D yo;
47 vec4 transition (/* weird comment in the middle */vec2 uv) {
48 return mix(
49 getFromColor(uv) + vec4(progress*color, 1.0),
50 getToColor(uv) + vec4((1.0-progress)*color, 1.0),
51 progress);
52 }
53 `
54 );
55 expect(res).toMatchSnapshot();
56 expect(res.errors).toEqual([]);
57});
58
59test("must not override existing things", () => {
60 const res = transformSource(
61 "test.glsl",
62 `
63uniform float from; /* = 42. */
64uniform float to; /* = 42. */
65vec4 transition (vec2 uv) {}
66`
67 );
68 expect(res).toMatchSnapshot();
69 expect(res.errors.length).toBeGreaterThan(0);
70});
71
72test("must define the transition function", () => {
73 const res = transformSource(
74 "test.glsl",
75 `
76
77`
78 );
79 expect(res).toMatchSnapshot();
80 expect(res.errors.length).toBeGreaterThan(0);
81});
82
83test("must define metas", () => {
84 const res = transformSource(
85 "test.glsl",
86 `
87 vec4 transition (vec2 uv) {
88 return vec4(0.0);
89 }
90`
91 );
92 expect(res).toMatchSnapshot();
93 expect(res.errors.length).toBeGreaterThan(0);
94});
95
96test("simple transition function is fine", () => {
97 const res = transformSource(
98 "test.glsl",
99 `
100 // Author: gre
101 // License: MIT
102 vec4 transition (vec2 uv) {
103 return vec4(0.0);
104 }
105 `
106 );
107 expect(res).toMatchSnapshot();
108 expect(res.errors).toEqual([]);
109});
110
111test("must define uv params", () => {
112 const res = transformSource(
113 "test.glsl",
114 `
115 // Author: gre
116 // License: MIT
117 vec4 transition () {
118 return vec4(0.0);
119 }
120 `
121 );
122 expect(res).toMatchSnapshot();
123 expect(res.errors.length).toBeGreaterThan(0);
124});
125
126test("renaming param is fine", () => {
127 const res = transformSource(
128 "test.glsl",
129 `
130 // Author: gre
131 // License: MIT
132 vec4 transition (vec2 p) {
133 return vec4(p, 1.0, 1.0);
134 }
135 `
136 );
137 expect(res).toMatchSnapshot();
138 expect(res.errors).toEqual([]);
139});
140
141test("must define the correct signature", () => {
142 const res = transformSource(
143 "test.glsl",
144 `
145 // Author: gre
146 // License: MIT
147 void transition (vec2 uv) {}`
148 );
149 expect(res).toMatchSnapshot();
150 expect(res.errors.length).toBeGreaterThan(0);
151});
152
153test("crazy defaults", () => {
154 const res = transformSource(
155 "crazyyyy.glsl",
156 `
157 // Author: Gaetan
158 // License: LGPL
159 uniform int yo; // = 1
160 uniform vec4 yo2; // = vec4(4.4)
161 uniform vec2 yo3 /* = vec2(1.1, 2.2) */;
162 uniform float a,b; // = 42.2
163 uniform float c /* = 3.3*/, d /* = 5.5 */;
164 vec4 transition (vec2 uv) { return vec4(0.); }`
165 );
166 expect(res.errors).toEqual([]);
167 expect(res.data.defaultParams).toEqual({
168 yo: 1,
169 yo2: [4.4, 4.4, 4.4, 4.4],
170 yo3: [1.1, 2.2],
171 a: 42.2,
172 b: 42.2,
173 c: 3.3,
174 d: 5.5,
175 });
176 expect(res.data.author).toEqual("Gaetan");
177 expect(res.data.license).toEqual("LGPL");
178});