Shader Builder / add utilities for checking an expression type

Type checking is done either against a literal value (number, string...)
or against the operator in case of an expression.

Sometimes it is not possible to infer only one type, for example
the value 'transparent' could either be a color or a string. This
is covered by the fact that all operators expect exactly one type
for their arguments.
This commit is contained in:
Olivier Guyot
2019-10-22 16:44:38 +02:00
parent f43637cc33
commit b8e8d30df0
2 changed files with 127 additions and 1 deletions

View File

@@ -2,6 +2,9 @@ import {
formatArray,
formatColor,
formatNumber,
isValueTypeColor,
isValueTypeNumber,
isValueTypeString,
parse,
parseLiteralStyle,
ShaderBuilder
@@ -36,6 +39,46 @@ describe('ol.webgl.ShaderBuilder', function() {
});
});
describe('value type checking', function() {
it('correctly recognizes a number value', function() {
expect(isValueTypeNumber(1234)).to.eql(true);
expect(isValueTypeNumber(['time'])).to.eql(true);
expect(isValueTypeNumber(['clamp', ['get', 'attr'], -1, 1])).to.eql(true);
expect(isValueTypeNumber(['interpolate', ['get', 'attr'], 'red', 'green'])).to.eql(false);
expect(isValueTypeNumber('yellow')).to.eql(false);
expect(isValueTypeNumber('#113366')).to.eql(false);
expect(isValueTypeNumber('rgba(252,171,48,0.62)')).to.eql(false);
});
it('correctly recognizes a color value', function() {
expect(isValueTypeColor(1234)).to.eql(false);
expect(isValueTypeColor(['time'])).to.eql(false);
expect(isValueTypeColor(['clamp', ['get', 'attr'], -1, 1])).to.eql(false);
expect(isValueTypeColor(['interpolate', ['get', 'attr'], 'red', 'green'])).to.eql(true);
expect(isValueTypeColor('yellow')).to.eql(true);
expect(isValueTypeColor('#113366')).to.eql(true);
expect(isValueTypeColor('rgba(252,171,48,0.62)')).to.eql(true);
expect(isValueTypeColor('abcd')).to.eql(false);
});
it('correctly recognizes a string value', function() {
expect(isValueTypeString(1234)).to.eql(false);
expect(isValueTypeString(['time'])).to.eql(false);
expect(isValueTypeString(['clamp', ['get', 'attr'], -1, 1])).to.eql(false);
expect(isValueTypeString(['interpolate', ['get', 'attr'], 'red', 'green'])).to.eql(false);
expect(isValueTypeString('yellow')).to.eql(true);
expect(isValueTypeString('#113366')).to.eql(true);
expect(isValueTypeString('rgba(252,171,48,0.62)')).to.eql(true);
expect(isValueTypeString('abcd')).to.eql(true);
});
it('throws on an unsupported type', function(done) {
try {
isValueTypeColor(true);
} catch (e) {
done();
}
done(true);
});
});
describe('getSymbolVertexShader', function() {
it('generates a symbol vertex shader (with varying)', function() {
const builder = new ShaderBuilder();