Skip to content

Commit 4ebc1f5

Browse files
补充UT。
1 parent 40612fa commit 4ebc1f5

File tree

15 files changed

+1388
-0
lines changed

15 files changed

+1388
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
require('../../../src/common/commontypes/BaseTypes');
2+
3+
describe('BaseTypes', function () {
4+
it('startsWith, contains', function () {
5+
var str = "BaseTypesTest";
6+
var isStartWith = SuperMap.String.startsWith(str, 'Base');
7+
var isContains = SuperMap.String.contains(str, 'contain');
8+
expect(isStartWith).toBeTruthy();
9+
expect(isContains).toBeFalsy();
10+
});
11+
12+
it('trim', function () {
13+
var str = "this is a BaseTypesTest ";
14+
var newStr = SuperMap.String.trim(str);
15+
expect(newStr).toEqual("this is a BaseTypesTest");
16+
});
17+
18+
it('camelize', function () {
19+
var str = SuperMap.String.camelize('chicken-head');
20+
expect(str).toEqual('chickenHead');
21+
});
22+
23+
it('format_String', function () {
24+
var template1 = "${a,b}";
25+
var result1 = SuperMap.String.format(template1, null, null);
26+
expect(result1).toEqual("${a,b}");
27+
var template = "${a.b}";
28+
var context = {a: {b: "format"}};
29+
var args = null;
30+
var result = SuperMap.String.format(template, context, args);
31+
expect(result).toEqual("format");
32+
});
33+
34+
it('isNumeric, numericIf', function () {
35+
var result1 = SuperMap.String.isNumeric("6.02e23");
36+
var result2 = SuperMap.String.isNumeric(" 4 ");
37+
var result3 = SuperMap.String.numericIf("4");
38+
expect(result1).toBeTruthy();
39+
expect(result2).toBeFalsy();
40+
expect(result3).toEqual(4);
41+
});
42+
43+
//数值操作的一系列常用扩展函数
44+
it('limitSigDigs', function () {
45+
var number = SuperMap.Number.limitSigDigs(123.123456789, 6);
46+
expect(number).toEqual(123.123);
47+
});
48+
49+
//数字格式化输出.
50+
it('format_Number', function () {
51+
var number1 = SuperMap.Number.format(123456789, null, ',', '.');
52+
var number2 = SuperMap.Number.format(123.123456789, 0, ',', '.');
53+
var number3 = SuperMap.Number.format(123.123456789, 5, ',', '.');
54+
expect(number1).toEqual("123,456,789");
55+
expect(number2).toEqual("123");
56+
expect(number3).toEqual("123.12346");
57+
});
58+
59+
it('Number.limitSigDigs', function () {
60+
var number = 123.123456789;
61+
var newNm = number.limitSigDigs(8);
62+
expect(newNm).toEqual(123.12346);
63+
});
64+
65+
//数组操作的一系列常用扩展函数
66+
//过滤数组
67+
it('filter', function () {
68+
var array = [10, 9, 53, 7, 25, 45];
69+
var newArray = SuperMap.Array.filter(array, function (val, i, array) {
70+
if (val > 20) {
71+
return true;
72+
}
73+
});
74+
expect(newArray).not.toBeNull();
75+
expect(newArray[0]).toEqual(53);
76+
expect(newArray[1]).toEqual(25);
77+
expect(newArray[2]).toEqual(45);
78+
});
79+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require('../../../src/common/commontypes/Date');
2+
3+
describe('Date', function () {
4+
it('toISOString', function () {
5+
var dateString = SuperMap.Date.toISOString(new Date());
6+
expect(dateString).not.toBeNaN();
7+
});
8+
9+
it('parse', function () {
10+
var date1 = SuperMap.Date.parse("2010-08-07T11:58:23.123-06");
11+
var date2 = SuperMap.Date.parse("sca123");
12+
var date1String = SuperMap.Date.toISOString(date1);
13+
expect(date1String).toEqual("2010-08-07T17:58:23.123Z");
14+
expect(date2).not.toBeNull();
15+
});
16+
});
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
var LonLat = require('../../../src/common/commontypes/LonLat').LonLat;
2+
3+
describe('LonLat', function () {
4+
it('constructor, toString, toShortString, clone, destroy', function () {
5+
var lonLat1 = new LonLat(30, 45);
6+
var lonLat2 = new LonLat([10, 12], 45);
7+
expect(lonLat1).not.toBeNull();
8+
expect(lonLat1.CLASS_NAME).toEqual("SuperMap.LonLat");
9+
expect(lonLat1.lat).toEqual(45);
10+
expect(lonLat1.lon).toEqual(30);
11+
expect(lonLat2).not.toBeNull();
12+
expect(lonLat2.CLASS_NAME).toEqual("SuperMap.LonLat");
13+
expect(lonLat2.lat).toEqual(12);
14+
expect(lonLat2.lon).toEqual(10);
15+
var str1 = lonLat1.toString();
16+
expect(str1).toEqual("lon=30,lat=45");
17+
var str2 = lonLat1.toShortString();
18+
expect(str2).toEqual("30,45");
19+
var newLonLat = lonLat1.clone();
20+
expect(newLonLat).toEqual(lonLat1);
21+
lonLat1.destroy();
22+
expect(lonLat1.lat).toBeNull();
23+
expect(lonLat1.lon).toBeNull();
24+
lonLat2.destroy();
25+
});
26+
27+
it('add', function () {
28+
var lonLat1 = new LonLat(30, 45);
29+
var lonLat2 = lonLat1.add(10, 10);
30+
expect(lonLat2.CLASS_NAME).toEqual("SuperMap.LonLat");
31+
expect(lonLat2.lat).toEqual(55);
32+
expect(lonLat2.lon).toEqual(40);
33+
lonLat1.destroy();
34+
lonLat2.destroy();
35+
});
36+
37+
it('equals', function () {
38+
var lonLat1 = new LonLat(30, 45);
39+
var lonLat2 = new LonLat(30, 45);
40+
var lonLat3 = new LonLat(31, 45);
41+
var isEquals1 = lonLat2.equals(lonLat1);
42+
var isEquals2 = lonLat3.equals(lonLat1);
43+
expect(isEquals1).toBeTruthy();
44+
expect(isEquals2).toBeFalsy();
45+
lonLat1.destroy();
46+
lonLat2.destroy();
47+
lonLat3.destroy();
48+
});
49+
50+
it('wrapDateLine', function () {
51+
var lonLat1 = new LonLat(420, 50);
52+
var lonLat2 = new LonLat(-190, 50);
53+
var lonLat3 = lonLat1.wrapDateLine(
54+
new SuperMap.Bounds(-180, -90, 180, 90)
55+
);
56+
var lonLat4 = lonLat2.wrapDateLine(
57+
new SuperMap.Bounds(-180, -90, 180, 90)
58+
);
59+
expect(lonLat3).not.toBeNull();
60+
expect(lonLat4).not.toBeNull();
61+
lonLat1.destroy();
62+
lonLat2.destroy();
63+
lonLat3.destroy();
64+
lonLat4.destroy();
65+
});
66+
67+
it('fromString', function () {
68+
var str = "100,50";
69+
var lonLat = new LonLat.fromString(str);
70+
expect(lonLat).not.toBeNull();
71+
expect(lonLat.CLASS_NAME).toEqual("SuperMap.LonLat");
72+
expect(lonLat.lat).toEqual(50);
73+
expect(lonLat.lon).toEqual(100);
74+
lonLat.destroy();
75+
});
76+
77+
it('fromArray', function () {
78+
var arr = [100, 50];
79+
var lonLat = new LonLat.fromArray(arr);
80+
expect(lonLat).not.toBeNull();
81+
expect(lonLat.CLASS_NAME).toEqual("SuperMap.LonLat");
82+
expect(lonLat.lat).toEqual(50);
83+
expect(lonLat.lon).toEqual(100);
84+
lonLat.destroy();
85+
});
86+
});
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
var Vector = require('../../../src/common/commontypes/Vector').Vector;
2+
var Point = require('../../../src/common/commontypes/geometry/Point').Point;
3+
4+
describe('Vector', function () {
5+
it('constructor, destroy', function () {
6+
var point = new Point(-115, 10);
7+
var style = {
8+
strokeColor: "#339933"
9+
};
10+
var pointFeature = new Vector(point, null, style);
11+
expect(pointFeature.CLASS_NAME).toEqual("SuperMap.Feature.Vector");
12+
expect(pointFeature.id).toContain("SuperMap.Feature");
13+
expect(pointFeature.geometry.type).toEqual("Point");
14+
expect(pointFeature.geometry.x).toEqual(-115);
15+
expect(pointFeature.geometry.y).toEqual(10);
16+
expect(pointFeature.style.strokeColor).toEqual("#339933");
17+
pointFeature.destroy();
18+
expect(pointFeature.id).toBeNull();
19+
expect(pointFeature.attributes).toEqual({});
20+
expect(pointFeature.geometry).toBeNull();
21+
});
22+
23+
it('clone',function () {
24+
var point = new Point(-115, 10);
25+
var style = {
26+
strokeColor: "#339933"
27+
};
28+
var vector = new Vector(point, null, style);
29+
var newVector = vector.clone();
30+
expect(newVector).not.toBeNull();
31+
expect(newVector.CLASS_NAME).toEqual("SuperMap.Feature.Vector");
32+
expect(newVector.id).toContain("SuperMap.Feature");
33+
expect(newVector.geometry.type).toEqual("Point");
34+
expect(newVector.geometry.x).toEqual(-115);
35+
expect(newVector.geometry.y).toEqual(10);
36+
expect(newVector.style.strokeColor).toEqual("#339933");
37+
});
38+
39+
it('toState',function () {
40+
var point = new Point(-115, 10);
41+
var style = {
42+
strokeColor: "#339933"
43+
};
44+
var vector = new Vector(point, null, style);
45+
spyOn(vector,'toState').and.callThrough();
46+
vector.state = SuperMap.State.DELETE;
47+
vector.toState(SuperMap.State.UPDATE);
48+
expect( vector.toState).toHaveBeenCalledWith(SuperMap.State.UPDATE);
49+
expect(vector.state).toEqual("Update");
50+
vector.state = SuperMap.State.INSERT;
51+
vector.toState(SuperMap.State.UPDATE);
52+
expect( vector.toState).toHaveBeenCalledWith(SuperMap.State.UPDATE);
53+
expect(vector.state).toEqual("Insert");
54+
vector.state = SuperMap.State.INSERT;
55+
vector.toState(SuperMap.State.INSERT);
56+
expect( vector.toState).toHaveBeenCalledWith(SuperMap.State.INSERT);
57+
expect(vector.state).toEqual("Insert");
58+
vector.state = SuperMap.State.UNKNOWN;
59+
vector.toState(SuperMap.State.INSERT);
60+
expect( vector.toState).toHaveBeenCalledWith(SuperMap.State.INSERT);
61+
expect(vector.state).toEqual("Unknown");
62+
vector.state = SuperMap.State.INSERT;
63+
vector.toState(SuperMap.State.DELETE);
64+
expect( vector.toState).toHaveBeenCalledWith(SuperMap.State.DELETE);
65+
expect(vector.state).toEqual("Insert");
66+
vector.state = SuperMap.State.DELETE;
67+
vector.toState(SuperMap.State.DELETE);
68+
expect( vector.toState).toHaveBeenCalledWith(SuperMap.State.DELETE);
69+
expect(vector.state).toEqual("Delete");
70+
vector.state = SuperMap.State.UNKNOWN;
71+
vector.toState(SuperMap.State.DELETE);
72+
expect( vector.toState).toHaveBeenCalledWith(SuperMap.State.DELETE);
73+
expect(vector.state).toEqual("Delete");
74+
vector.toState(SuperMap.State.UNKNOWN);
75+
expect( vector.toState).toHaveBeenCalledWith(SuperMap.State.UNKNOWN);
76+
expect(vector.state).toEqual("Unknown");
77+
});
78+
});

test/common/format/WKTSpec.js

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
require('../../../src/common/format/WKT');
2+
3+
describe('WKT', function () {
4+
//point的read、write方法
5+
it('read, write_point', function () {
6+
var wkt = new SuperMap.Format.WKT({keepData: true});
7+
var data = "POINT(6 10)";
8+
var feature = wkt.read(data);
9+
expect(feature).not.toBeNull();
10+
expect(feature.CLASS_NAME).toEqual("SuperMap.Feature.Vector");
11+
expect(feature.id).toContain("SuperMap.Feature");
12+
expect(feature.geometry.id).toContain("SuperMap.Geometry");
13+
expect(feature.geometry.type).toEqual("Point");
14+
expect(feature.geometry.x).toEqual(6);
15+
expect(feature.geometry.y).toEqual(10);
16+
var pointWkt = wkt.write(feature);
17+
expect(pointWkt).toEqual("POINT(6 10)");
18+
});
19+
20+
//multipoint的read、write方法
21+
it('read, write_multipoint的read', function () {
22+
var wkt = new SuperMap.Format.WKT({keepData: true});
23+
var data = "MULTIPOINT((3.5 5.6),(4.8 10.5))";
24+
var feature = wkt.read(data);
25+
expect(feature).not.toBeNull();
26+
expect(feature.geometry.componentTypes.length).toEqual(1);
27+
expect(feature.geometry.componentTypes[0]).toEqual("SuperMap.Geometry.Point");
28+
expect(feature.geometry.components.length).toEqual(2);
29+
expect(feature.geometry.components[0].type).toEqual("Point");
30+
expect(feature.geometry.components[0].x).toEqual(3.5);
31+
expect(feature.geometry.components[0].y).toEqual(5.6);
32+
expect(feature.geometry.components[1].type).toEqual("Point");
33+
expect(feature.geometry.components[1].x).toEqual(4.8);
34+
expect(feature.geometry.components[1].y).toEqual(10.5);
35+
var newWkt = wkt.write(feature);
36+
expect(newWkt).toEqual("MULTIPOINT((3.5 5.6),(4.8 10.5))");
37+
});
38+
39+
//multilinestring 的read、write方法
40+
it('read, write_multilinestring', function () {
41+
var wkt = new SuperMap.Format.WKT({keepData: true});
42+
var data = "MULTILINESTRING((3 4,10 50,20 25),(-5 -8,-10 -8,-15 -4))";
43+
var feature = wkt.read(data);
44+
expect(feature).not.toBeNull();
45+
expect(feature.id).not.toBeNull();
46+
expect(feature.geometry.CLASS_NAME).toEqual("SuperMap.Geometry.MultiLineString");
47+
expect(feature.geometry.componentTypes[0]).toEqual("SuperMap.Geometry.LineString");
48+
expect(feature.geometry.components[0].components.length).toEqual(3);
49+
for (var i = 0; i < feature.geometry.components[0].components.length; i++) {
50+
expect(feature.geometry.components[0].components[i].type).toEqual("Point");
51+
}
52+
expect(feature.geometry.components[0].components[0].x).toEqual(3);
53+
expect(feature.geometry.components[0].components[0].y).toEqual(4);
54+
expect(feature.geometry.components[0].components[1].x).toEqual(10);
55+
expect(feature.geometry.components[0].components[1].y).toEqual(50);
56+
expect(feature.geometry.components[0].components[2].x).toEqual(20);
57+
expect(feature.geometry.components[0].components[2].y).toEqual(25);
58+
var newWkt = wkt.write(feature);
59+
expect(newWkt).toEqual("MULTILINESTRING((3 4,10 50,20 25),(-5 -8,-10 -8,-15 -4))");
60+
});
61+
62+
//polygon 的read、write方法
63+
it('read, write_polygon', function () {
64+
var wkt = new SuperMap.Format.WKT({keepData: true});
65+
var data = "POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2))";
66+
var feature = wkt.read(data);
67+
expect(feature).not.toBeNull();
68+
expect(feature.id).not.toBeNull();
69+
expect(feature.geometry.componentTypes[0]).toEqual("SuperMap.Geometry.LinearRing");
70+
expect(feature.geometry.components.length).toEqual(2);
71+
for (var i = 0; i < feature.geometry.components.length; i++) {
72+
expect(feature.geometry.components[i].components.length).toEqual(5);
73+
for (var j = 0; j < 5; j++) {
74+
expect(feature.geometry.components[i].components[j].type).toEqual("Point");
75+
}
76+
}
77+
var newWkt = wkt.write(feature);
78+
expect(newWkt).toEqual("POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2))");
79+
});
80+
81+
//multipolygon 的read、write方法
82+
it('read, write_multipolygon', function () {
83+
var wkt = new SuperMap.Format.WKT({keepData: true});
84+
var data = "MULTIPOLYGON(((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2)),((6 3,9 2,9 4,6 3)))";
85+
var feature = wkt.read(data);
86+
expect(feature).not.toBeNull();
87+
expect(feature.id).not.toBeNull();
88+
expect(feature.geometry.componentTypes[0]).toEqual("SuperMap.Geometry.Polygon");
89+
expect(feature.geometry.components.length).toEqual(2);
90+
expect(feature.geometry.components[0].components.length).toEqual(2);
91+
for (var i = 0; i < 2; i++) {
92+
expect(feature.geometry.components[0].components[i].CLASS_NAME).toEqual("SuperMap.Geometry.LinearRing");
93+
var pointComponents = feature.geometry.components[0].components[i].components;
94+
expect(pointComponents.length).toEqual(5);
95+
for (var j = 0; j < pointComponents.length; j++) {
96+
expect(pointComponents[i].type).toEqual("Point");
97+
}
98+
}
99+
var newWkt = wkt.write(feature);
100+
expect(newWkt).toEqual("MULTIPOLYGON(((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2)),((6 3,9 2,9 4,6 3)))");
101+
});
102+
103+
//geometrycollection的read、write方法
104+
it('read, write_geometrycollection', function () {
105+
var wkt = new SuperMap.Format.WKT({keepData: true});
106+
var data = "GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10))";
107+
var feature = wkt.read(data);
108+
expect(feature).not.toBeNull();
109+
expect(feature.length).toEqual(2);
110+
expect(feature[0].id).toContain("SuperMap.Feature");
111+
expect(feature[0].geometry.id).toContain("SuperMap.Geometry");
112+
expect(feature[0].geometry.type).toEqual("Point");
113+
expect(feature[0].geometry.x).toEqual(4);
114+
expect(feature[0].geometry.y).toEqual(6);
115+
expect(feature[1].id).toContain("SuperMap.Feature");
116+
expect(feature[1].geometry.id).toContain("SuperMap.Geometry");
117+
expect(feature[1].geometry.componentTypes[0]).toEqual("SuperMap.Geometry.Point");
118+
expect(feature[1].geometry.componentTypes[1]).toEqual("SuperMap.PointWithMeasure");
119+
expect(feature[1].geometry.components.length).toEqual(2);
120+
expect(feature[1].geometry.components[0].type).toEqual("Point");
121+
expect(feature[1].geometry.components[0].x).toEqual(4);
122+
expect(feature[1].geometry.components[0].y).toEqual(6);
123+
expect(feature[1].geometry.components[1].type).toEqual("Point");
124+
expect(feature[1].geometry.components[1].x).toEqual(7);
125+
expect(feature[1].geometry.components[1].y).toEqual(10);
126+
var newWkt = wkt.write(feature);
127+
expect(newWkt).toEqual("GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10))");
128+
});
129+
});

0 commit comments

Comments
 (0)