forked from stackgl/glsl-min-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.js
159 lines (131 loc) · 3.54 KB
/
basic.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const tap = require('tap');
const minify = require('../');
const tokenizer = require('glsl-tokenizer/stream');
const parser = require('glsl-parser');
const deparser = require('glsl-deparser');
const fs = require("fs");
const path = require("path");
const through = require("through");
const zeroGLSL = path.resolve(__dirname, "./zero-decimals.glsl");
const commutativeGLSL = path.resolve(__dirname, "./commutative-operators.glsl");
const workingGLSL = path.resolve(__dirname, "./working.glsl");
const vecGLSL = path.resolve(__dirname, "./vec-shorthand.glsl");
tap.test("basic", t => {
let output = "";
const endStream = through((data) => {
output += data;
}, () => {
t.ok(output, "missing output");
t.ok(output.includes("void main"), "main is overriden");
t.matchSnapshot(output, "output");
t.end();
});
fs.createReadStream(workingGLSL)
.pipe(tokenizer())
.pipe(parser())
.pipe(minify())
.pipe(deparser())
.pipe(endStream);
});
tap.test("basic safe words", t => {
let output = "";
const endStream = through((data) => {
output += data;
}, () => {
t.ok(output.includes("void main"), "main is overriden");
t.ok(output.includes("PI"), "PI is overriden");
t.matchSnapshot(output, "output");
t.end();
});
fs.createReadStream(workingGLSL)
.pipe(tokenizer())
.pipe(parser())
.pipe(minify(["main", "PI"]))
.pipe(deparser())
.pipe(endStream);
});
tap.test("basic storage mutation", t => {
let output = "";
const endStream = through((data) => {
output += data;
}, () => {
t.ok(output.includes("void main"), "main is overriden");
t.notOk(output.includes("uniform vec2 resolution"), "resolution uniform is not overriden");
t.matchSnapshot(output, "output");
t.end();
});
fs.createReadStream(workingGLSL)
.pipe(tokenizer())
.pipe(parser())
.pipe(minify(["main"], true))
.pipe(deparser())
.pipe(endStream);
});
tap.test("decimals starting or ending with 0", t => {
let output = "";
const endStream = through((data) => {
output += data;
}, () => {
t.matchSnapshot(output, "output");
t.end();
});
fs.createReadStream(zeroGLSL)
.pipe(tokenizer())
.pipe(parser())
.pipe(minify())
.pipe(deparser())
.pipe(endStream);
});
tap.test("vec shorthand", t => {
const types = ["", "b", "i"]
.map(prefix => [2,3,4].map(size => `${prefix}vec${size}`))
.reduce((a, b) => a.concat(b));
const variableNames = types
.map(type => ["Short", "Long"].map(suffix => `${type}${suffix}`))
.reduce((a, b) => a.concat(b));
const safewords = [
"main",
...variableNames,
];
let output = "";
const endStream = through((data) => {
output += data;
}, () => {
t.matchSnapshot(output, "output");
t.ok(
variableNames
.filter(name => name.endsWith("Long"))
.every(name => output.includes(`${name} = ${name.replace("Long", "")}(.0, 1.`)),
"does not retain differing scalars"
);
t.ok(
variableNames
.filter(name => name.endsWith("Short"))
.every(name => output.includes(`${name} = ${name.replace("Short", "")}(.0)`)),
"does not shorten identical scalars"
);
t.ok(output.includes("ivec4(vec2(.0, 1.), vec2(.0, 1.))"), "does not preserve vectors in initalizer");
t.end();
});
fs.createReadStream(vecGLSL)
.pipe(tokenizer())
.pipe(parser())
.pipe(minify(safewords))
.pipe(deparser())
.pipe(endStream);
});
tap.test("grouping removal test", t => {
let output = "";
const endStream = through((data) => {
output += data;
}, () => {
t.matchSnapshot(output, "output");
t.end();
});
fs.createReadStream(commutativeGLSL)
.pipe(tokenizer())
.pipe(parser())
.pipe(minify())
.pipe(deparser())
.pipe(endStream);
});