Skip to content

Commit cc4fb1e

Browse files
authored
Update utils.test.js
1 parent e8b1f84 commit cc4fb1e

File tree

1 file changed

+65
-38
lines changed

1 file changed

+65
-38
lines changed

test/utils.test.js

Lines changed: 65 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import { BuildingShapeUtils } from '../src/extras/BuildingShapeUtils.js';
1414
// import { JSDOM } from 'jsdom';
1515

1616

17-
// Test createShape
17+
/** Test createShape */
18+
19+
/** Test isClosed */
1820

1921
test('Test Closed Way', () => {
2022
var way = '<way id="1"><nd ref="2"/><nd ref="3"/><nd ref="4"/><nd ref="5"/><nd ref="2"/></way>';
@@ -23,22 +25,30 @@ test('Test Closed Way', () => {
2325
expect(BuildingShapeUtils.isClosed(xmlData)).toBe(true);
2426
});
2527

26-
test('Reverse way', () => {
27-
var way1 = '<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/></way>';
28-
var way2 = '<way id="1"><nd ref="3"/><nd ref="2"/><nd ref="1"/></way>';
29-
let parser = new window.DOMParser();
30-
let xml1 = parser.parseFromString(way1, 'text/xml').getElementsByTagName('way')[0];
31-
let result = BuildingShapeUtils.reverseWay(xml1);
32-
expect(result.outerHTML).toBe(way2);
33-
});
34-
3528
test('Test Open Way', () => {
3629
var way = '<way id="1"><nd ref="2"/><nd ref="3"/><nd ref="4"/><nd ref="5"/><nd ref="6"/></way>';
3730
let parser = new window.DOMParser();
3831
let xmlData = parser.parseFromString(way, 'text/xml');
3932
expect(BuildingShapeUtils.isClosed(xmlData)).toBe(false);
4033
});
4134

35+
/** Test isSelfIntersecting */
36+
37+
describe.each([
38+
['<way id="1"><nd ref="1"/><nd ref="2"/></way>', false, 'open non-intersecting'],
39+
['<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/><nd ref="1"/></way>', false, 'closed non-intersecting'],
40+
['<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/><nd ref="2"/></way>', true, 'open intersecting'],
41+
['<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/><nd ref="4"/><nd ref="3"/><nd ref="1"/></way>', true, 'closed intersecting'],
42+
])('isSelfIntersecting', (way, expected, description) => {
43+
test(`${description}`, () => {
44+
let parser = new window.DOMParser();
45+
let xml = parser.parseFromString(way, 'text/xml').getElementsByTagName('way')[0];
46+
expect(BuildingShapeUtils.isSelfIntersecting(xml)).toBe(expected);
47+
});
48+
});
49+
50+
/** Test joinWays */
51+
4252
test('Test joining 2 ways', () => {
4353
var way1 = '<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/></way>';
4454
var way2 = '<way id="2"><nd ref="3"/><nd ref="4"/><nd ref="1"/></way>';
@@ -50,6 +60,35 @@ test('Test joining 2 ways', () => {
5060
expect(result.outerHTML).toBe(way3);
5161
});
5262

63+
/** Test joinAllWays */
64+
65+
/** Test reverseWay */
66+
67+
test('Reverse way', () => {
68+
var way1 = '<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/></way>';
69+
var way2 = '<way id="1"><nd ref="3"/><nd ref="2"/><nd ref="1"/></way>';
70+
let parser = new window.DOMParser();
71+
let xml1 = parser.parseFromString(way1, 'text/xml').getElementsByTagName('way')[0];
72+
let result = BuildingShapeUtils.reverseWay(xml1);
73+
expect(result.outerHTML).toBe(way2);
74+
});
75+
76+
/** Test center */
77+
78+
test('Center', () => {
79+
expect(BuildingShapeUtils.center(rightTriangle)).toStrictEqual([0, 0]);
80+
});
81+
82+
/** Test getWidth */
83+
84+
test('Get Width', () => {
85+
expect(BuildingShapeUtils.getWidth(rightTriangle)).toBe(2);
86+
});
87+
88+
/** Test combineCoordinates */
89+
90+
/** Test extents */
91+
5392
const rightTriangle = new Shape();
5493
rightTriangle.moveTo(1, 1);
5594
rightTriangle.lineTo(1, -1);
@@ -70,49 +109,37 @@ test('Extents Rotation', () => {
70109
expect(BuildingShapeUtils.extents(rightTriangle, angle)).toBeDeepCloseTo([-sqrt2, 0, sqrt2, sqrt2], 10);
71110
});
72111

112+
/** Test primaryDirection */
113+
114+
/** Test edgeLength */
73115
test('Edge Lengths', () => {
74116
expect(BuildingShapeUtils.edgeLength(rightTriangle)).toBeDeepCloseTo([2, Math.sqrt(2) * 2, 2]);
75117
});
76118

77-
test('Edge Direction', () => {
78-
expect(BuildingShapeUtils.edgeDirection(rightTriangle)).toBeDeepCloseTo([-Math.PI / 2, 3 * Math.PI / 4, 0]);
79-
});
119+
/** Test vertexAngle */
80120

81-
test('Edge Direction2', () => {
82-
expect(BuildingShapeUtils.edgeDirection(rightTriangle2)).toBeDeepCloseTo([-Math.PI, -Math.PI / 4, Math.PI / 2]);
121+
test('Vertex Angles', () => {
122+
expect(BuildingShapeUtils.vertexAngle(rightTriangle)).toStrictEqual([Math.PI / 2, Math.PI / 4, Math.PI / 4]);
83123
});
84124

85-
test('Longest side angle', () => {
86-
expect(BuildingShapeUtils.longestSideAngle(rightTriangle)).toBe(3 * Math.PI / 4);
125+
test('Vertex Angles counterclockwise', () => {
126+
expect(BuildingShapeUtils.vertexAngle(rightTriangle2)).toStrictEqual([-Math.PI / 2, -Math.PI / 4, -Math.PI / 4]);
87127
});
88128

89-
describe.each([
90-
['<way id="1"><nd ref="1"/><nd ref="2"/></way>', false, 'open non-intersecting'],
91-
['<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/><nd ref="1"/></way>', false, 'closed non-intersecting'],
92-
['<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/><nd ref="2"/></way>', true, 'open intersecting'],
93-
['<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/><nd ref="4"/><nd ref="3"/><nd ref="1"/></way>', true, 'closed intersecting'],
94-
])('isSelfIntersecting', (way, expected, description) => {
95-
test(`${description}`, () => {
96-
let parser = new window.DOMParser();
97-
let xml = parser.parseFromString(way, 'text/xml').getElementsByTagName('way')[0];
98-
expect(BuildingShapeUtils.isSelfIntersecting(xml)).toBe(expected);
99-
});
100-
});
129+
/** Test edgeDirection */
101130

102-
test('Center', () => {
103-
expect(BuildingShapeUtils.center(rightTriangle)).toStrictEqual([0, 0]);
131+
test('Edge Direction', () => {
132+
expect(BuildingShapeUtils.edgeDirection(rightTriangle)).toBeDeepCloseTo([-Math.PI / 2, 3 * Math.PI / 4, 0]);
104133
});
105134

106-
test('Get Width', () => {
107-
expect(BuildingShapeUtils.getWidth(rightTriangle)).toBe(2);
135+
test('Edge Direction2', () => {
136+
expect(BuildingShapeUtils.edgeDirection(rightTriangle2)).toBeDeepCloseTo([-Math.PI, -Math.PI / 4, Math.PI / 2]);
108137
});
109138

110-
test('Vertex Angles', () => {
111-
expect(BuildingShapeUtils.vertexAngle(rightTriangle)).toStrictEqual([Math.PI / 2, Math.PI / 4, Math.PI / 4]);
112-
});
139+
/** Test surrounds */
113140

114-
test('Vertex Angles counterclockwise', () => {
115-
expect(BuildingShapeUtils.vertexAngle(rightTriangle2)).toStrictEqual([-Math.PI / 2, -Math.PI / 4, -Math.PI / 4]);
141+
test('Longest side angle', () => {
142+
expect(BuildingShapeUtils.longestSideAngle(rightTriangle)).toBe(3 * Math.PI / 4);
116143
});
117144

118145
describe.each([

0 commit comments

Comments
 (0)