summaryrefslogtreecommitdiff
path: root/platform/darwin/test/MGLFilterTests.mm
diff options
context:
space:
mode:
authorFredrik Karlsson <bjorn.fredrik.karlsson@gmail.com>2016-08-17 13:38:02 +0200
committerFredrik Karlsson <bjorn.fredrik.karlsson@gmail.com>2016-09-02 22:42:05 +0200
commitd77a13eb7320722c48c8a18240adf99615c4b85f (patch)
tree1017781b310e434f943e4ad38b57f319ad7a3a58 /platform/darwin/test/MGLFilterTests.mm
parent6a39cf5aaece81c7a531b12321dd503004cc45b8 (diff)
downloadqtlocation-mapboxgl-d77a13eb7320722c48c8a18240adf99615c4b85f.tar.gz
[ios] Added support for filters (NSPredicate)
[ios, macos] cleaned up filters [ios] added a filter example [ios] utest filters [ios, macos] nested predicates [ios] refactored [ios] filter -> NSPredicate [ios] fixed mbgl::Any/AllFilter -> NSPredicate [ios] translate nested mbgl::NotIn filters [ios] cleanup and added more utests [ios] fixed a bug in the None filter conversion and improved utests [ios, macos] doc [macos] added missing category [ios, macos] additional utests [ios, macos] updated documentation
Diffstat (limited to 'platform/darwin/test/MGLFilterTests.mm')
-rw-r--r--platform/darwin/test/MGLFilterTests.mm137
1 files changed, 137 insertions, 0 deletions
diff --git a/platform/darwin/test/MGLFilterTests.mm b/platform/darwin/test/MGLFilterTests.mm
new file mode 100644
index 0000000000..04cb82fac1
--- /dev/null
+++ b/platform/darwin/test/MGLFilterTests.mm
@@ -0,0 +1,137 @@
+#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] initWithSourceIdentifier:@"test-source" geoJSONData:geoJSONData];
+ [self.mapView.style addSource:source];
+ layer = [[MGLLineStyleLayer alloc] initWithLayerIdentifier:@"test-layer" sourceIdentifier:@"test-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"];
+ return @[equalPredicate,
+ notEqualPredicate,
+ greaterThanPredicate,
+ greaterThanOrEqualToPredicate,
+ lessThanOrEqualToPredicate,
+ lessThanPredicate,
+ inPredicate,
+ notInPredicate,
+ inNotInPredicate,
+ typePredicate,
+ idPredicate,
+ specialCharsPredicate];
+}
+
+- (void)testAllPredicates
+{
+ for (NSPredicate *predicate in self.predicates) {
+ layer.predicate = predicate;
+ XCTAssertEqualObjects(layer.predicate, predicate);
+ }
+ [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