Skip to content

Commit

Permalink
Fix tests and last comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
HarelM committed Nov 16, 2024
1 parent 0adabc3 commit e56dada
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/expression/definitions/interpolate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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 type {ProjectionDefinitionTypeT, Type} from '../types';

export type InterpolationType = {
name: 'linear';
Expand All @@ -23,7 +23,7 @@ export type InterpolationType = {
name: 'cubic-bezier';
controlPoints: [number, number, number, number];
};
type InterpolatedValueType = NumberTypeT | ColorTypeT | StringTypeT | ProjectionDefinitionTypeT | PaddingTypeT | VariableAnchorOffsetCollectionTypeT | ArrayType<NumberTypeT>;
type InterpolatedValueType = NumberTypeT | ColorTypeT | ProjectionDefinitionTypeT | PaddingTypeT | VariableAnchorOffsetCollectionTypeT | ArrayType<NumberTypeT>;
export class Interpolate implements Expression {
type: InterpolatedValueType;

Expand Down
36 changes: 33 additions & 3 deletions src/expression/types/projection_definition.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,39 @@
import {ProjectionDefinition} from './projection_definition';

describe('Projection class', () => {
test('should parse projection with multiple inputs', () => {
const projection = ProjectionDefinition.parse(['mercator', 'vertical-perspective', 0.5]);
expect(projection.from).toBe('mercator');
expect(projection.to).toBe('vertical-perspective');
expect(projection.transition).toBe(0.5);
});

test('should parse projection with single input', () => {
const projection = ProjectionDefinition.parse('mercator');
expect(projection.from).toBe('mercator');
expect(projection.to).toBe('mercator');
expect(projection.transition).toBe(1);
});

test('should return undefined when input is not an array or string', () => {
const projection = ProjectionDefinition.parse({} as any);
expect(projection).toBeUndefined();
});

test('should return undefined when input is an array with length not equal to 3', () => {
const projection = ProjectionDefinition.parse(['mercator', 'vertical-perspective'] as any);
expect(projection).toBeUndefined();
});

test('should return undefined when input is an array with non-string and non-number elements', () => {
const projection = ProjectionDefinition.parse([1, 2, 3] as any);
expect(projection).toBeUndefined();
});

test('should serialize projection, with [from, to, transition]', () => {
expect(`${new ProjectionDefinition('mercator', 'vertical-perspective', 1)}`).toBe('["mercator", "vertical-perspective", 1]');
expect(`${new ProjectionDefinition('vertical-perspective', 'mercator', 0.3)}`).toBe('["vertical-perspective", "mercator", 0.3]');
test('should interpolate projections', () => {
const projection = ProjectionDefinition.interpolate('mercator', 'vertical-perspective', 0.5);
expect(projection.from).toBe('mercator');
expect(projection.to).toBe('vertical-perspective');
expect(projection.transition).toBe(0.5);
});
});
10 changes: 1 addition & 9 deletions src/expression/types/projection_definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ export class ProjectionDefinition {
this.to = to;
this.transition = transition;
}

toString() {
return `["${this.from}", "${this.to}", ${this.transition}]`;
}

toJSON() {
return [this.from, this.to, this.transition];
}

static interpolate(from: string, to: string, t: number) {
return new ProjectionDefinition(from, to, t);
Expand All @@ -25,7 +17,7 @@ export class ProjectionDefinition {
if (input instanceof ProjectionDefinition) {
return input;
}
if (Array.isArray(input) && input.length === 3) {
if (Array.isArray(input) && input.length === 3 && typeof input[0] === 'string' && typeof input[1] === 'string' && typeof input[2] === 'number') {
return new ProjectionDefinition(input[0], input[1], input[2]);
}
if (typeof input === 'string') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
"type": "projectionDefinition"
},
"outputs": [
[
"vertical-perspective",
"mercator",
0.5
]
{
"from": "vertical-perspective",
"to": "mercator",
"transition": 0.5
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
"type": "projectionDefinition"
},
"outputs": [
[
"mercator",
"mercator",
0.5
]
{
"from": "mercator",
"to": "mercator",
"transition": 0.5
}
]
}
}

0 comments on commit e56dada

Please sign in to comment.