Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Futureproof inline examples with test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ericrwolfe committed Dec 13, 2016
1 parent 9734002 commit 6f5a8bc
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 28 deletions.
4 changes: 2 additions & 2 deletions platform/darwin/src/MGLCircleStyleLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ typedef NS_ENUM(NSUInteger, MGLCircleTranslateAnchor) {
22: MGLStyleValue(rawValue: 180)
])
layer.circleOpacity = MGLStyleValue(rawValue: 0.7)
layer.predicate = NSPredicate(format: "%K == %@", "ethnicity", "ewok")
mapView.style().add(layer)
layer.predicate = NSPredicate(format: "%K == %@", "marital-status", "married")
mapView.style.addLayer(layer)
```
*/
@interface MGLCircleStyleLayer : MGLVectorStyleLayer
Expand Down
2 changes: 1 addition & 1 deletion platform/darwin/src/MGLFillStyleLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ typedef NS_ENUM(NSUInteger, MGLFillTranslateAnchor) {
layer.sourceLayerIdentifier = "parks"
layer.fillColor = MGLStyleValue(rawValue: UIColor.green)
layer.predicate = NSPredicate(format: "type == %@", "national-park")
mapView.style().add(layer)
mapView.style.addLayer(layer)
```
*/
@interface MGLFillStyleLayer : MGLVectorStyleLayer
Expand Down
8 changes: 4 additions & 4 deletions platform/darwin/src/MGLLineStyleLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ typedef NS_ENUM(NSUInteger, MGLLineTranslateAnchor) {
let layer = MGLLineStyleLayer(identifier: "trails-path", source: trails)
layer.sourceLayerIdentifier = "trails"
layer.lineWidth = MGLStyleValue(base: 1.5, stops: [
14: MGLStyleValue(rawValue: 2),
18: MGLStyleValue(rawValue: 20),
14: MGLStyleValue(rawValue: 2),
18: MGLStyleValue(rawValue: 20),
])
layer.lineColor = MGLStyleValue(rawValue: UIColor.brown)
layer.lineCap = MGLStyleValue(rawValue: NSValue(mglLineCap: .round))
layer.predicate = NSPredicate(format: "%K == %@", "trail-type" "mountain-biking")
mapView.style().add(layer)
layer.predicate = NSPredicate(format: "%K == %@", "trail-type", "mountain-biking")
mapView.style.addLayer(layer)
```
*/
@interface MGLLineStyleLayer : MGLVectorStyleLayer
Expand Down
2 changes: 1 addition & 1 deletion platform/darwin/src/MGLRasterSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ NS_ASSUME_NONNULL_BEGIN
tileset.attribution = "© Mapbox"
// Add source to map
let source = MGLRasterSource(identifier: "clouds", tileSet: tileset, tileSize: 512)
mapView.style().add(source)
mapView.style.addSource(source)
```
@see <a href="https://www.mapbox.com/mapbox-gl-style-spec/#sources-raster">The
Expand Down
10 changes: 7 additions & 3 deletions platform/darwin/src/MGLShapeSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,14 @@ extern const MGLShapeSourceOption MGLShapeSourceOptionSimplificationTolerance;
### Example ###
```swift
var coordinates: [CLLocationCoordinate2D] = []
var coordinates: [CLLocationCoordinate2D] = [
CLLocationCoordinate2D(latitude: 37.77, longitude: -122.42),
CLLocationCoordinate2D(latitude: 38.91, longitude: -77.04),
]
let polyline = MGLPolylineFeature(coordinates: &coordinates, count: UInt(coordinates.count))
let source = MGLGeoJSONSource(identifier: "lines", features: [polyline], options: nil)
self.mapView.style().add(source)
let shape = MGLShapeCollectionFeature(shapes: [polyline])
let source = MGLShapeSource(identifier: "lines", shape: shape, options: nil)
mapView.style.addSource(source)
```
@see <a href="https://www.mapbox.com/mapbox-gl-style-spec/#sources-geojson">The
Expand Down
8 changes: 4 additions & 4 deletions platform/darwin/src/MGLSymbolStyleLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,14 @@ typedef NS_ENUM(NSUInteger, MGLTextTranslateAnchor) {
```swift
let layer = MGLSymbolStyleLayer(identifier: "coffeeshops", source: pois)
layer.sourceLayerIdentifier = "pois"
layer.iconImage = MGLStyleValue(rawValue: "coffee")
layer.iconSize = MGLStyleValue(rawValue: 0.5)
layer.iconImageName = MGLStyleValue(rawValue: "coffee")
layer.iconScale = MGLStyleValue(rawValue: 0.5)
layer.textField = MGLStyleValue(rawValue: "{name}")
layer.textTranslate = MGLStyleValue(rawValue: NSValue(cgVector: CGVector(dx: 10, dy: 0)))
layer.textJustify = MGLStyleValue(rawValue: NSValue(mglTextJustify: .left))
layer.textAnchor = MGLStyleValue(rawValue: NSValue(mglTextAnchor: .left))
layer.predicate = NSPredicate(format: "%K == %@", "venue-type" "coffee")
mapView.style().add(layer)
layer.predicate = NSPredicate(format: "%K == %@", "venue-type", "coffee")
mapView.style.addLayer(layer)
```
*/
@interface MGLSymbolStyleLayer : MGLVectorStyleLayer
Expand Down
2 changes: 1 addition & 1 deletion platform/darwin/src/MGLVectorSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ NS_ASSUME_NONNULL_BEGIN
tileset.attribution = "© Mapbox"
// Add source to map
let source = MGLVectorSource(identifier: "pois", tileSet: tileset)
mapView.style().add(source)
mapView.style.addSource(source)
```
@see <a href="https://www.mapbox.com/mapbox-gl-style-spec/#sources-vector">The
Expand Down
13 changes: 4 additions & 9 deletions platform/darwin/src/MGLVectorStyleLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,10 @@ NS_ASSUME_NONNULL_BEGIN
you could create a predicate using `NSCompoundPredicate` along these lines:
```swift
let meters = 1500.0
let layer = MGLLineStyleLayer(identifier: "contour", source: contours)
layer.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [
NSCompoundPredicate(orPredicateWithSubpredicates: [
NSPredicate(format: "%K == 10", "contour-index"),
NSPredicate(format: "%K == 5", "contour-index"),
]),
NSPredicate(format: "ele >= \(meters)"),
])
let layer = MGLLineStyleLayer(identifier: "contour", source: terrain)
layer.sourceLayerIdentifier = "contours"
layer.predicate = NSPredicate(format: "(index == 10 || index == 5) && ele >= 1500.0")
mapView.style.addLayer(layer)
```
*/
@property (nonatomic, nullable) NSPredicate *predicate;
Expand Down
10 changes: 7 additions & 3 deletions platform/ios/ios.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
40FDA76B1CCAAA6800442548 /* MBXAnnotationView.m in Sources */ = {isa = PBXBuildFile; fileRef = 40FDA76A1CCAAA6800442548 /* MBXAnnotationView.m */; };
554180421D2E97DE00012372 /* OpenGLES.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 554180411D2E97DE00012372 /* OpenGLES.framework */; };
55D8C9961D0F18CE00F42F10 /* libsqlite3.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 55D8C9951D0F18CE00F42F10 /* libsqlite3.tbd */; };
6407D6701E0085FD00F6A9C3 /* MBLDocumentationExampleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6407D66F1E0085FD00F6A9C3 /* MBLDocumentationExampleTests.swift */; };
7E016D7E1D9E86BE00A29A21 /* MGLPolyline+MGLAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 7E016D7C1D9E86BE00A29A21 /* MGLPolyline+MGLAdditions.h */; };
7E016D7F1D9E86BE00A29A21 /* MGLPolyline+MGLAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 7E016D7C1D9E86BE00A29A21 /* MGLPolyline+MGLAdditions.h */; };
7E016D801D9E86BE00A29A21 /* MGLPolyline+MGLAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 7E016D7D1D9E86BE00A29A21 /* MGLPolyline+MGLAdditions.m */; };
Expand Down Expand Up @@ -603,6 +604,7 @@
554180411D2E97DE00012372 /* OpenGLES.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenGLES.framework; path = System/Library/Frameworks/OpenGLES.framework; sourceTree = SDKROOT; };
55D8C9941D0F133500F42F10 /* config.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = config.xcconfig; path = ../../build/ios/config.xcconfig; sourceTree = "<group>"; };
55D8C9951D0F18CE00F42F10 /* libsqlite3.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libsqlite3.tbd; path = usr/lib/libsqlite3.tbd; sourceTree = SDKROOT; };
6407D66F1E0085FD00F6A9C3 /* MBLDocumentationExampleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MBLDocumentationExampleTests.swift; sourceTree = "<group>"; };
7E016D7C1D9E86BE00A29A21 /* MGLPolyline+MGLAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "MGLPolyline+MGLAdditions.h"; sourceTree = "<group>"; };
7E016D7D1D9E86BE00A29A21 /* MGLPolyline+MGLAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "MGLPolyline+MGLAdditions.m"; sourceTree = "<group>"; };
7E016D821D9E890300A29A21 /* MGLPolygon+MGLAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "MGLPolygon+MGLAdditions.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1074,6 +1076,7 @@
DA35A2C31CCA9F8300E826B2 /* MGLClockDirectionFormatterTests.m */,
DA35A2C41CCA9F8300E826B2 /* MGLCompassDirectionFormatterTests.m */,
DA35A2A91CCA058D00E826B2 /* MGLCoordinateFormatterTests.m */,
6407D66F1E0085FD00F6A9C3 /* MBLDocumentationExampleTests.swift */,
DA0CD58F1CF56F6A00A5F5A5 /* MGLFeatureTests.mm */,
DA2E885C1CC0382C00F24E7B /* MGLGeometryTests.mm */,
DA2DBBCC1D51E80400D38FF9 /* MGLStyleLayerTests.h */,
Expand Down Expand Up @@ -1757,7 +1760,7 @@
};
DA2E88501CC036F400F24E7B = {
CreatedOnToolsVersion = 7.3;
LastSwiftMigration = 0800;
LastSwiftMigration = 0810;
};
DA8847D11CBAF91600AB86E3 = {
CreatedOnToolsVersion = 7.3;
Expand Down Expand Up @@ -1916,6 +1919,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
6407D6701E0085FD00F6A9C3 /* MBLDocumentationExampleTests.swift in Sources */,
DA2E88631CC0382C00F24E7B /* MGLOfflineRegionTests.m in Sources */,
3599A3E61DF708BC00E77FB2 /* MGLStyleValueTests.m in Sources */,
DA2E88651CC0382C00F24E7B /* MGLStyleTests.mm in Sources */,
Expand Down Expand Up @@ -2352,7 +2356,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "../darwin/test/test-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 3.0.1;
};
name = Debug;
};
Expand All @@ -2374,7 +2378,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.mapbox.test;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "../darwin/test/test-Bridging-Header.h";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 3.0.1;
};
name = Release;
};
Expand Down
147 changes: 147 additions & 0 deletions platform/ios/test/MBLDocumentationExampleTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import XCTest
import Mapbox

/**
Test cases that ensure the inline examples in the project documentation
compile. If any of these need to be changed, update the corresponding header
documentation accordingly.
*/
class MBLDocumentationExampleTests: XCTestCase {
var mapView: MGLMapView!

override func setUp() {
super.setUp()
self.mapView = MGLMapView(frame: CGRect(x: 0, y: 0, width: 256, height: 256))
}

override func tearDown() {
self.mapView = nil
super.tearDown()
}

// Example code in MGLShapeSource.h
func testMGLShapeSourceExample() {
var coordinates: [CLLocationCoordinate2D] = [
CLLocationCoordinate2D(latitude: 37.77, longitude: -122.42),
CLLocationCoordinate2D(latitude: 38.91, longitude: -77.04),
]
let polyline = MGLPolylineFeature(coordinates: &coordinates, count: UInt(coordinates.count))
let shape = MGLShapeCollectionFeature(shapes: [polyline])
let source = MGLShapeSource(identifier: "lines", shape: shape, options: nil)
mapView.style.addSource(source)

XCTAssertNotNil(mapView.style.source(withIdentifier: "lines"))
}

// Example code in MGLRasterSource.h
func testMGLRasterSourceExample() {
// Define tileset
let tileset = MGLTileSet(tileURLTemplates: ["https://example.com/raster-tiles/{z}/{x}/{y}.png"])
tileset.minimumZoomLevel = 9
tileset.maximumZoomLevel = 16
tileset.attribution = "© Mapbox"
// Add source to map
let source = MGLRasterSource(identifier: "clouds", tileSet: tileset, tileSize: 512)
mapView.style.addSource(source)

XCTAssertNotNil(mapView.style.source(withIdentifier: "clouds"))
}

// Example code in MGLVectorSource.h
func testMGLVectorSource() {
// Define tileset
let tileset = MGLTileSet(tileURLTemplates: ["https://example.com/vector-tiles/{z}/{x}/{y}.mvt"])
tileset.minimumZoomLevel = 9
tileset.maximumZoomLevel = 16
tileset.attribution = "© Mapbox"
// Add source to map
let source = MGLVectorSource(identifier: "pois", tileSet: tileset)
mapView.style.addSource(source)

XCTAssertNotNil(mapView.style.source(withIdentifier: "pois"))
}

// Example code in MGLCircleStyleLayer.h
func testMGLCircleStyleLayerExample() {
let population = MGLVectorSource(identifier: "population", url: URL(string: "https://example.com")!)
mapView.style.addSource(population)

let layer = MGLCircleStyleLayer(identifier: "circles", source: population)
layer.sourceLayerIdentifier = "population"
layer.circleColor = MGLStyleValue(rawValue: UIColor.green)
layer.circleRadius = MGLStyleValue(base: 1.75, stops: [
12: MGLStyleValue(rawValue: 2),
22: MGLStyleValue(rawValue: 180)
])
layer.circleOpacity = MGLStyleValue(rawValue: 0.7)
layer.predicate = NSPredicate(format: "%K == %@", "marital-status", "married")
mapView.style.addLayer(layer)

XCTAssertNotNil(mapView.style.layer(withIdentifier: "circles"))
}

// Example code in MGLLineStyleLayer.h
func testMGLLineStyleLayerExample() {
let trails = MGLVectorSource(identifier: "trails", url: URL(string: "https://example.com")!)
mapView.style.addSource(trails)

let layer = MGLLineStyleLayer(identifier: "trails-path", source: trails)
layer.sourceLayerIdentifier = "trails"
layer.lineWidth = MGLStyleValue(base: 1.5, stops: [
14: MGLStyleValue(rawValue: 2),
18: MGLStyleValue(rawValue: 20),
])
layer.lineColor = MGLStyleValue(rawValue: UIColor.brown)
layer.lineCap = MGLStyleValue(rawValue: NSValue(mglLineCap: .round))
layer.predicate = NSPredicate(format: "%K == %@", "trail-type", "mountain-biking")
mapView.style.addLayer(layer)

XCTAssertNotNil(mapView.style.layer(withIdentifier: "trails-path"))
}

// Example code in MGLFillStyleLayer.h
func testMGLFillStyleLayerExample() {
let parks = MGLVectorSource(identifier: "parks", url: URL(string: "https://example.com")!)
mapView.style.addSource(parks)

let layer = MGLFillStyleLayer(identifier: "parks", source: parks)
layer.sourceLayerIdentifier = "parks"
layer.fillColor = MGLStyleValue(rawValue: UIColor.green)
layer.predicate = NSPredicate(format: "type == %@", "national-park")
mapView.style.addLayer(layer)

XCTAssertNotNil(mapView.style.layer(withIdentifier: "parks"))
}

// Example code in MGLSymbolStyleLayer.h
func testMGLSymbolStyleLayerExample() {
let pois = MGLVectorSource(identifier: "pois", url: URL(string: "https://example.com")!)
mapView.style.addSource(pois)

let layer = MGLSymbolStyleLayer(identifier: "coffeeshops", source: pois)
layer.sourceLayerIdentifier = "pois"
layer.iconImageName = MGLStyleValue(rawValue: "coffee")
layer.iconScale = MGLStyleValue(rawValue: 0.5)
layer.textField = MGLStyleValue(rawValue: "{name}")
layer.textTranslate = MGLStyleValue(rawValue: NSValue(cgVector: CGVector(dx: 10, dy: 0)))
layer.textJustify = MGLStyleValue(rawValue: NSValue(mglTextJustify: .left))
layer.textAnchor = MGLStyleValue(rawValue: NSValue(mglTextAnchor: .left))
layer.predicate = NSPredicate(format: "%K == %@", "venue-type", "coffee")
mapView.style.addLayer(layer)

XCTAssertNotNil(mapView.style.layer(withIdentifier: "coffeeshops"))
}

// Example code in MGLVectorStyleLayer.h
func testMGLVectorStyleLayer() {
let terrain = MGLVectorSource(identifier: "terrain", url: URL(string: "https://example.com")!)
mapView.style.addSource(terrain)

let layer = MGLLineStyleLayer(identifier: "contour", source: terrain)
layer.sourceLayerIdentifier = "contours"
layer.predicate = NSPredicate(format: "(index == 10 || index == 5) && ele >= 1500.0")
mapView.style.addLayer(layer)

XCTAssertNotNil(mapView.style.layer(withIdentifier: "contour"))
}
}

0 comments on commit 6f5a8bc

Please sign in to comment.