Skip to content

Commit

Permalink
Fix tests, add missing interpolation due to merge
Browse files Browse the repository at this point in the history
  • Loading branch information
HarelM committed Nov 16, 2024
1 parent 2a4d2c3 commit 121d4b7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 24 deletions.
11 changes: 7 additions & 4 deletions src/expression/definitions/interpolate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import UnitBezier from '@mapbox/unitbezier';

import {array, ArrayType, ColorType, ColorTypeT, NumberType, NumberTypeT, PaddingType, PaddingTypeT, VariableAnchorOffsetCollectionType, VariableAnchorOffsetCollectionTypeT, typeToString, verifyType, ProjectionDefinitionType} from '../types';
import {findStopLessThanOrEqualTo} from '../stops';
import {Color} from '../types/color';
import {interpolateArray, interpolateNumber} from '../../util/interpolate-primitives';
import {Padding} from '../types/padding';
import {VariableAnchorOffsetCollection} from '../types/variable_anchor_offset_collection';
import {ProjectionDefinition} from '../types/projection_definition';

import type {Stops} from '../stops';
import type {Expression} from '../expression';
import type {ParsingContext} from '../parsing_context';
import type {EvaluationContext} from '../evaluation_context';
import type {ProjectionDefinitionTypeT, StringTypeT, Type} from '../types';
import {Color} from '../types/color';
import {interpolateArray, interpolateNumber} from '../../util/interpolate-primitives';
import {Padding} from '../types/padding';
import {VariableAnchorOffsetCollection} from '../types/variable_anchor_offset_collection';

export type InterpolationType = {
name: 'linear';
Expand Down Expand Up @@ -186,6 +187,8 @@ export class Interpolate implements Expression {
return VariableAnchorOffsetCollection.interpolate(outputLower, outputUpper, t);
case 'array':
return interpolateArray(outputLower, outputUpper, t);
case 'projectionDefinition':
return ProjectionDefinition.interpolate(outputLower, outputUpper, t);
}
case 'interpolate-hcl':
return Color.interpolate(outputLower, outputUpper, t, 'hcl');
Expand Down
35 changes: 19 additions & 16 deletions src/expression/expression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,15 @@ describe('projection expression', () => {
})

test('step array', () => {
const response = createExpression(['step', ['zoom'], ['literal', ['vertical-perspective', 'mercator', 0.5]], 10, 'mercator']);
const response = createExpression(['step', ['zoom'], ['literal', ['vertical-perspective', 'mercator', 0.5]], 10, 'mercator'], {
type: 'projectionDefinition',
transition: false,
"property-type": "data-constant",

Check failure on line 651 in src/expression/expression.test.ts

View workflow job for this annotation

GitHub Actions / Code Hygiene

Strings must use singlequote

Check failure on line 651 in src/expression/expression.test.ts

View workflow job for this annotation

GitHub Actions / Code Hygiene

Strings must use singlequote
expression: {
interpolated: true,
parameters: ["zoom"]

Check failure on line 654 in src/expression/expression.test.ts

View workflow job for this annotation

GitHub Actions / Code Hygiene

Strings must use singlequote
}
});

if (response.result === 'success') {
expect(response.value.evaluate({zoom: 5})).toStrictEqual(['vertical-perspective', 'mercator', 0.5]);
Expand All @@ -656,25 +664,20 @@ describe('projection expression', () => {
}
})

test('interpolate color', () => {

const response = createExpression(['interpolate', ['linear'], ['zoom'], 8, 'vertical-perspective', 10, 'mercator']);

if (response.result === 'success') {
expect(response.value.evaluate({zoom: 5})).toBe('vertical-perspective');
expect(response.value.evaluate({zoom: 9})).toBe(['vertical-perspective', 'mercator', 0.5]);
expect(response.value.evaluate({zoom: 11})).toBe('mercator');
} else {
throw new Error('Failed to parse Interpolate expression');
}
})

test('interpolate', () => {
const response = createExpression(['interpolate', ['linear'], ['zoom'], 8, 'vertical-perspective', 10, 'mercator']);
const response = createExpression(['interpolate', ['linear'], ['zoom'], 8, 'vertical-perspective', 10, 'mercator'], {
type: 'projectionDefinition',
transition: false,
"property-type": "data-constant",

Check failure on line 671 in src/expression/expression.test.ts

View workflow job for this annotation

GitHub Actions / Code Hygiene

Strings must use singlequote

Check failure on line 671 in src/expression/expression.test.ts

View workflow job for this annotation

GitHub Actions / Code Hygiene

Strings must use singlequote
expression: {
interpolated: true,
parameters: ["zoom"]

Check failure on line 674 in src/expression/expression.test.ts

View workflow job for this annotation

GitHub Actions / Code Hygiene

Strings must use singlequote
}
});

if (response.result === 'success') {
expect(response.value.evaluate({zoom: 5})).toBe('vertical-perspective');
expect(response.value.evaluate({zoom: 9})).toBe(['vertical-perspective', 'mercator', 0.5]);
expect(response.value.evaluate({zoom: 9})).toEqual({from: 'vertical-perspective', to: 'mercator', transition: 0.5});
expect(response.value.evaluate({zoom: 11})).toBe('mercator');
} else {
throw new Error('Failed to parse Interpolate expression');
Expand Down
6 changes: 5 additions & 1 deletion src/expression/types/projection_definition.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export class ProjectionDefinition {
constructor(public from: string, public to: string, public transition: number){
readonly from: string;
readonly to: string;
readonly transition: number;

constructor(from: string, to: string, transition: number){
this.from = from;
this.to = to;
this.transition = transition;
Expand Down
2 changes: 1 addition & 1 deletion src/validate/validate_projection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Validate projection', () => {
test('Should return errors according to spec violations', () => {
const errors = validateProjection({validateSpec: validate, value: {type: 1 as any}, styleSpec: v8, style: {} as any});
expect(errors).toHaveLength(1);
expect(errors[0].message).toBe('projection: object expected, number found');
expect(errors[0].message).toBe('type: projection expected, invalid type \"number\" found');
});

test('Should return error when value is null', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"propertySpec": {
"type": "string",
"type": "projectionDefinition",
"property-type": "data-constant",
"expression": {
"interpolated": true,
Expand Down Expand Up @@ -31,7 +31,7 @@
"result": "success",
"isFeatureConstant": true,
"isZoomConstant": false,
"type": "string"
"type": "projectionDefinition"
},
"outputs": [
[
Expand Down

0 comments on commit 121d4b7

Please sign in to comment.