summaryrefslogtreecommitdiff
path: root/platform/darwin/test/MGLFilterTests.mm
blob: b18400f83a2f4b42ef8441f967267e195fa9a14a (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#import "MGLStyleLayerTests.h"

#import "NSPredicate+MGLAdditions.h"
#import "MGLValueEvaluator.h"


@interface MGLFilterTests : MGLStyleLayerTests {
    MGLGeoJSONSource *source;
    MGLLineStyleLayer *layer;
}
@end

@implementation MGLFilterTests

- (void)setUp
{
    [super setUp];
    NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:@"amsterdam" ofType:@"geojson"];
    NSURL *url = [NSURL fileURLWithPath:filePath];
    NSData *geoJSONData = [NSData dataWithContentsOfURL:url];
    source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"test-source" geoJSONData:geoJSONData options:nil];
    [self.mapView.style addSource:source];
    layer = [[MGLLineStyleLayer alloc] initWithIdentifier:@"test-layer" source:source];
}

- (void)tearDown
{
    [self.mapView.style removeLayer:layer];
    [self.mapView.style removeSource:source];
}

- (NSArray<NSPredicate *> *)predicates
{
    NSPredicate *equalPredicate = [NSPredicate predicateWithFormat:@"type == 'neighbourhood'"];
    NSPredicate *notEqualPredicate = [NSPredicate predicateWithFormat:@"type != 'park'"];
    NSPredicate *greaterThanPredicate = [NSPredicate predicateWithFormat:@"%K > %@", @"stroke-width", @2.1];
    NSPredicate *greaterThanOrEqualToPredicate = [NSPredicate predicateWithFormat:@"%K >= %@", @"stroke-width", @2.1];
    NSPredicate *lessThanOrEqualToPredicate = [NSPredicate predicateWithFormat:@"%K <= %@", @"stroke-width", @2.1];
    NSPredicate *lessThanPredicate = [NSPredicate predicateWithFormat:@"%K < %@", @"stroke-width", @2.1];
    NSPredicate *inPredicate = [NSPredicate predicateWithFormat:@"type IN %@", @[@"park", @"neighbourhood"]];
    NSPredicate *notInPredicate = [NSPredicate predicateWithFormat:@"NOT (type IN %@)", @[@"park", @"neighbourhood"]];
    NSPredicate *inNotInPredicate = [NSPredicate predicateWithFormat:@"type IN %@ AND NOT (type IN %@)", @[@"park"], @[@"neighbourhood", @"test"]];
    NSPredicate *typePredicate = [NSPredicate predicateWithFormat:@"%K == %@", @"$type", @"Feature"];
    NSPredicate *idPredicate = [NSPredicate predicateWithFormat:@"%K == %@", @"$id", @"1234123"];
    NSPredicate *specialCharsPredicate = [NSPredicate predicateWithFormat:@"%K == %@", @"ty-’pè", @"sŒm-ethįng"];
    NSPredicate *booleanPredicate = [NSPredicate predicateWithFormat:@"cluster != YES"];
    NSPredicate *nilEqualsPredicate = [NSPredicate predicateWithFormat:@"type == %@", nil];
    NSPredicate *nilNotEqualsPredicate = [NSPredicate predicateWithFormat:@"type != %@", nil];
    return @[
             equalPredicate,
             notEqualPredicate,
             greaterThanPredicate,
             greaterThanOrEqualToPredicate,
             lessThanOrEqualToPredicate,
             lessThanPredicate,
             inPredicate,
             notInPredicate,
             inNotInPredicate,
             typePredicate,
             idPredicate,
             specialCharsPredicate,
             booleanPredicate,
             nilEqualsPredicate,
             nilNotEqualsPredicate
             ];
}

- (void)testAllPredicates
{
    for (NSPredicate *predicate in self.predicates) {
        layer.predicate = predicate;
        XCTAssertEqualObjects(layer.predicate, predicate);
    }
    [self.mapView.style addLayer:layer];
}

- (void)testContainsPredicate
{
    // core does not have a "contains" filter but we can achieve the equivalent by creating an `mbgl::style::InFilter`
    // and searching the value for the key
    NSPredicate *expectedPredicate = [NSPredicate predicateWithFormat:@"park IN %@", @[@"park", @"neighbourhood"]];
    NSPredicate *containsPredicate = [NSPredicate predicateWithFormat:@"%@ CONTAINS %@", @[@"park", @"neighbourhood"], @"park"];
    
    layer.predicate = containsPredicate;
    XCTAssertEqualObjects(layer.predicate, expectedPredicate);
    [self.mapView.style addLayer:layer];
}

- (void)testBetweenPredicate
{
    // core does not have a "between" filter but we can achieve the equivalent by creating a set of greater than or equal / less than or equal
    // filters for the lower and upper bounds (inclusive)
    NSPredicate *expectedPredicate = [NSCompoundPredicate predicateWithFormat:@"%K >= 2 AND %K <= 3", @"stroke-width", @"stroke-width"];
    NSPredicate *betweenPredicate = [NSPredicate predicateWithFormat:@"%K BETWEEN %@", @"stroke-width", @[@2.0, @3.0]];
    
    layer.predicate = betweenPredicate;
    XCTAssertEqualObjects(layer.predicate, expectedPredicate);
    [self.mapView.style addLayer:layer];
}

- (void)testTruePredicate
{
    // This comes out of the class cluster as an NSTruePredicate and it is equal to `[NSPredicate predicateWithValue:YES]`
    NSPredicate *truePredicate = [NSPredicate predicateWithFormat:@"TRUEPREDICATE"];
    
    layer.predicate = truePredicate;
    XCTAssertEqualObjects(layer.predicate.description, truePredicate.description);
    [self.mapView.style addLayer:layer];
}

- (void)testFalsePredicate
{
    // This comes out of the class cluster as an NSFalsePredicate and it is equal to `[NSPredicate predicateWithValue:NO]`
    NSPredicate *falsePredicate = [NSPredicate predicateWithFormat:@"FALSEPREDICATE"];
    
    layer.predicate = falsePredicate;
    XCTAssertEqualObjects(layer.predicate.description, falsePredicate.description);
    [self.mapView.style addLayer:layer];
}

- (void)testIntermittentEncoding
{
    NSPredicate *specialCharsPredicate = [NSPredicate predicateWithFormat:@"%K == %@", @"ty-’pè", @"sŒm-ethįng"];
    layer.predicate = specialCharsPredicate;
    
    NSComparisonPredicate *getPredicate = (NSComparisonPredicate *)layer.predicate;
    mbgl::style::EqualsFilter filter = layer.predicate.mgl_filter.get<mbgl::style::EqualsFilter>();
    
    id objcKey = getPredicate.leftExpression.keyPath;
    id cppKey = @(filter.key.c_str());
    id objcValue = mbgl::Value::visit(getPredicate.rightExpression.mgl_filterValue, ValueEvaluator());
    id cppValue = mbgl::Value::visit(filter.value, ValueEvaluator());
    
    XCTAssertEqualObjects(objcKey, cppKey);
    XCTAssertEqualObjects(objcValue, cppValue);
    
    [self.mapView.style addLayer:layer];
}

- (void)testNestedFilters
{
    NSPredicate *equalPredicate = [NSPredicate predicateWithFormat:@"type == 'neighbourhood'"];
    NSPredicate *notEqualPredicate = [NSPredicate predicateWithFormat:@"type != 'park'"];
    
    NSPredicate *allPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[equalPredicate, notEqualPredicate]];
    NSPredicate *anyPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[equalPredicate, notEqualPredicate]];
    
    layer.predicate = allPredicate;
    XCTAssertEqualObjects(layer.predicate, allPredicate);
    layer.predicate = anyPredicate;
    XCTAssertEqualObjects(layer.predicate, anyPredicate);
    
    [self.mapView.style addLayer:layer];
}

- (void)testAndPredicates
{
    NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:self.predicates];
    layer.predicate = predicate;
    XCTAssertEqualObjects(predicate, layer.predicate);
    [self.mapView.style addLayer:layer];
}

- (void)testOrPredicates
{
    NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:self.predicates];
    layer.predicate = predicate;
    XCTAssertEqualObjects(predicate, layer.predicate);
    [self.mapView.style addLayer:layer];
}

- (void)testNotAndPredicates
{
    NSPredicate *predicates = [NSCompoundPredicate andPredicateWithSubpredicates:self.predicates];
    NSCompoundPredicate *predicate = [NSCompoundPredicate notPredicateWithSubpredicate:predicates];
    layer.predicate = predicate;
    XCTAssertEqualObjects(predicate, layer.predicate);
    [self.mapView.style addLayer:layer];
}

- (void)testNotOrPredicates
{
    NSPredicate *predicates = [NSCompoundPredicate orPredicateWithSubpredicates:self.predicates];
    NSCompoundPredicate *predicate = [NSCompoundPredicate notPredicateWithSubpredicate:predicates];
    layer.predicate = predicate;
    XCTAssertEqualObjects(predicate, layer.predicate);
    [self.mapView.style addLayer:layer];
}

@end