-
-
Notifications
You must be signed in to change notification settings - Fork 875
/
Copy pathPolygonUnitTests.m
122 lines (94 loc) · 4.16 KB
/
PolygonUnitTests.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* Copyright (c) 2015-present, Parse, LLC.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
@import CoreLocation.CLLocation;
#import "PFPolygon.h"
#import "PFEncoder.h"
#import "PFGeoPoint.h"
#import "PFPolygonPrivate.h"
#import "PFTestCase.h"
#import "PFObject.h"
@interface PolygonUnitTests : PFTestCase {
NSArray *_testPoints;
}
@end
@implementation PolygonUnitTests
- (void)setUp {
[super setUp];
_testPoints = @[@[@0,@0],@[@0,@1],@[@1,@1],@[@1,@0]];
}
- (void)testPolygonFromCoordinates {
PFPolygon *polygon = [PFPolygon polygonWithCoordinates:_testPoints];
XCTAssertEqualObjects(polygon.coordinates, _testPoints);
}
- (void)testPolygonSaveToObject {
PFPolygon *polygon = [PFPolygon polygonWithCoordinates:_testPoints];
PFObject *object = [PFObject objectWithClassName:@"A"];
object[@"bounds"] = polygon;
XCTAssertEqualObjects(object[@"bounds"], polygon);
}
- (void)testPolygonDictionaryEncoding {
PFPolygon *polygon = [PFPolygon polygonWithCoordinates:_testPoints];
NSDictionary *dictionary = [polygon encodeIntoDictionary:nil];
XCTAssertNotNil(dictionary);
PFPolygon *polygonFromDictionary = [PFPolygon polygonWithDictionary:dictionary];
XCTAssertEqualObjects(polygonFromDictionary, polygon);
XCTAssertEqual(polygon.coordinates, polygonFromDictionary.coordinates);
}
- (void)testPolygonPFEncoding {
PFPolygon *polygon = [PFPolygon polygonWithCoordinates:_testPoints];
PFEncoder *encoder = [[PFEncoder alloc] init];
NSDictionary *dictionary = [encoder encodeObject:polygon error:nil];
XCTAssertNotNil(dictionary);
PFPolygon *polygonFromDictionary = [PFPolygon polygonWithDictionary:dictionary];
XCTAssertEqualObjects(polygonFromDictionary, polygon);
XCTAssertEqual(polygon.coordinates, polygonFromDictionary.coordinates);
}
- (void)testPolygonExceptions {
PFPolygon *polygon = [PFPolygon polygonWithCoordinates:_testPoints];
NSArray *lessThan3 = @[@0, @0];
NSArray *nonNumbers = @[@"str1", @"str2", @"str3"];
PFAssertThrowsInvalidArgumentException([polygon setCoordinates:lessThan3]);
PFAssertThrowsInvalidArgumentException([polygon setCoordinates:nonNumbers]);
}
- (void)testPolygonContainsPoint {
PFPolygon *polygon = [PFPolygon polygonWithCoordinates:_testPoints];
PFGeoPoint *inside = [PFGeoPoint geoPointWithLatitude:0.5 longitude:0.5];
PFGeoPoint *outside = [PFGeoPoint geoPointWithLatitude:10 longitude:10];
XCTAssertTrue([polygon containsPoint:inside]);
XCTAssertFalse([polygon containsPoint:outside]);
}
- (void)testPolygonEquality {
PFPolygon *polygonA = [PFPolygon polygonWithCoordinates:_testPoints];
PFPolygon *polygonB = [PFPolygon polygonWithCoordinates:_testPoints];
XCTAssertTrue([polygonA isEqual:polygonB]);
XCTAssertTrue([polygonB isEqual:polygonA]);
XCTAssertFalse([polygonA isEqual:@YES]);
XCTAssertTrue([polygonA isEqual:polygonA]);
}
- (void)testNSCopying {
PFPolygon *polygon = [PFPolygon polygonWithCoordinates:_testPoints];
PFPolygon *polygonCopy = [polygon copy];
XCTAssertEqualObjects(polygonCopy.coordinates, polygon.coordinates, @"Coordinates should be the same.");
}
- (void)testNSCoding {
PFPolygon *polygon = [PFPolygon polygonWithCoordinates:_testPoints];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:polygon];
XCTAssertTrue([data length] > 0, @"Encoded data should not be empty");
PFPolygon *decodedPolygon = [NSKeyedUnarchiver unarchiveObjectWithData:data];
XCTAssertEqualObjects(decodedPolygon.coordinates, polygon.coordinates, @"Coordinates should be the same.");
}
- (void)testPolygonDescription {
PFPolygon *polygon = [PFPolygon polygonWithCoordinates:_testPoints];
NSString *description = [polygon description];
XCTAssertNotNil(description);
polygon.coordinates = @[@[@10,@10], @[@10,@15], @[@15,@15], @[@15,@10], @[@10,@10]];
XCTAssertNotNil([polygon description]);
XCTAssertNotEqualObjects(description, [polygon description]);
}
@end