summaryrefslogtreecommitdiff
path: root/platform/darwin/test/MGLDocumentationGuideTests.swift
blob: 4de1d81aa93c6016fe3c86db3cf7a754a9f2f2a5 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import XCTest
import Foundation
import Mapbox
#if os(iOS)
    import UIKit
#else
    import Cocoa
#endif

/**
 Test cases that ensure the inline examples in the jazzy guides compile.

 To add an example:
 1. Add a test case named in the form `testGuideName$ExampleName`.
 2. Wrap the code you’d like to appear in the documentation within the
    following comment blocks:
    ```
    //#-example-code
    ...
    //#-end-example-code
    ```
 3. Insert a call to `guideExample()`  where you’d like the example code to be
    inserted in the guide’s Markdown.
    ```
    <%- guideExample('GuideName', 'ExampleName', 'iOS') %>
    ```
 4. Run `make darwin-style-code` to extract example code from the test method
    below and insert it into the guide.
 */
class MGLDocumentationGuideTests: XCTestCase, MGLMapViewDelegate {
    var mapView: MGLMapView!
    var styleLoadingExpectation: XCTestExpectation!
    
    override func setUp() {
        super.setUp()
        let styleURL = Bundle(for: MGLDocumentationGuideTests.self).url(forResource: "one-liner", withExtension: "json")
        mapView = MGLMapView(frame: CGRect(x: 0, y: 0, width: 256, height: 256), styleURL: styleURL)
        mapView.delegate = self
        styleLoadingExpectation = expectation(description: "Map view should finish loading style")
        waitForExpectations(timeout: 1, handler: nil)
    }
    
    override func tearDown() {
        mapView = nil
        styleLoadingExpectation = nil
        super.tearDown()
    }
    
    func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
        styleLoadingExpectation.fulfill()
    }
    
    func testMigratingToExpressions$Stops() {
        //#-example-code
        #if os(macOS)
            let stops: [NSNumber: NSColor] = [
                0: .yellow,
                2.5: .orange,
                5: .red,
                7.5: .blue,
                10: .white,
            ]
        #else
            let stops: [NSNumber: UIColor] = [
                0: .yellow,
                2.5: .orange,
                5: .red,
                7.5: .blue,
                10: .white,
            ]
        #endif
        //#-end-example-code
        
        let _ = NSExpression(format: "mgl_step:from:stops:(mag, %@, %@)",
                             stops[0]!, stops)
    }
    
    func testMigratingToExpressions$Linear() {
        //#-example-code
        let url = URL(string: "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson")!
        let symbolSource = MGLSource(identifier: "source")
        let symbolLayer = MGLSymbolStyleLayer(identifier: "place-city-sm", source: symbolSource)
        
        let source = MGLShapeSource(identifier: "earthquakes", url: url, options: nil)
        mapView.style?.addSource(source)
        
        #if os(macOS)
            let stops: [NSNumber: NSColor] = [
                0: .yellow,
                2.5: .orange,
                5: .red,
                7.5: .blue,
                10: .white,
            ]
        #else
            let stops: [NSNumber: UIColor] = [
                0: .yellow,
                2.5: .orange,
                5: .red,
                7.5: .blue,
                10: .white,
            ]
        #endif
        
        let layer = MGLCircleStyleLayer(identifier: "circles", source: source)
        #if os(macOS)
            layer.circleColor = NSExpression(format: "mgl_interpolate:withCurveType:parameters:stops:(mag, 'linear', nil, %@)",
                                             stops)
        #else
            layer.circleColor = NSExpression(format: "mgl_interpolate:withCurveType:parameters:stops:(mag, 'linear', nil, %@)",
                                             stops)
        #endif
        layer.circleRadius = NSExpression(forConstantValue: 10)
        mapView.style?.insertLayer(layer, below: symbolLayer)
        //#-end-example-code
    }
    
    func testMigratingToExpressions$LinearConvenience() {
        let source = MGLShapeSource(identifier: "circles", shape: nil, options: nil)
        let layer = MGLCircleStyleLayer(identifier: "circles", source: source)
        
        #if os(macOS)
        let stops: [NSNumber: NSColor] = [
            0: .yellow,
            2.5: .orange,
            5: .red,
            7.5: .blue,
            10: .white,
            ]
        #else
        let stops: [NSNumber: UIColor] = [
            0: .yellow,
            2.5: .orange,
            5: .red,
            7.5: .blue,
            10: .white,
            ]
        #endif
        
        //#-example-code
        layer.circleColor = NSExpression(forMGLInterpolating: NSExpression(forKeyPath: "mag"), curveType: .linear, parameters: nil, stops: NSExpression(forConstantValue: stops))
        //#-end-example-code
        
        layer.circleRadius = NSExpression(forConstantValue: 10)
        mapView.style?.addLayer(layer)
        
    }
    func testMigratingToExpressions$Exponential() {
        let source = MGLShapeSource(identifier: "circles", shape: nil, options: nil)
        let layer = MGLCircleStyleLayer(identifier: "circles", source: source)
        
        //#-example-code
        let stops = [
            12: 0.5,
            14: 2,
            18: 18,
        ]
        
        layer.circleRadius = NSExpression(format: "mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'exponential', 1.5, %@)",
                                          stops)
        //#-end-example-code
    }
    
    func testMigratingToExpressions$ExponentialConvenience() {
        let source = MGLShapeSource(identifier: "circles", shape: nil, options: nil)
        let layer = MGLCircleStyleLayer(identifier: "circles", source: source)
        
        //#-example-code
        let stops = [
            12: 0.5,
            14: 2,
            18: 18,
            ]
        
        layer.circleRadius =  NSExpression(forMGLInterpolating: NSExpression.zoomLevelVariable, curveType: MGLExpressionInterpolationMode.exponential, parameters: NSExpression(forConstantValue: 1.5), stops: NSExpression(forConstantValue: stops))
        //#-end-example-code
    }
    func testMigratingToExpressions$Interval() {
        let source = MGLShapeSource(identifier: "circles", shape: nil, options: nil)
        let layer = MGLCircleStyleLayer(identifier: "circles", source: source)
        
        //#-example-code
        #if os(macOS)
            let stops: [NSNumber: NSColor] = [
                0: .yellow,
                2.5: .orange,
                5: .red,
                7.5: .blue,
                10: .white,
            ]
            
            layer.circleColor = NSExpression(format: "mgl_step:from:stops:(mag, %@, %@)",
                                             NSColor.green, stops)
        #else
            let stops: [NSNumber: UIColor] = [
                0: .yellow,
                2.5: .orange,
                5: .red,
                7.5: .blue,
                10: .white,
            ]
            
            layer.circleColor = NSExpression(format: "mgl_step:from:stops:(mag, %@, %@)",
                                             UIColor.green, stops)
        #endif
        //#-end-example-code
    }
    
    func testMigratingToExpressions$Categorical() {
        let source = MGLShapeSource(identifier: "circles", shape: nil, options: nil)
        let layer = MGLCircleStyleLayer(identifier: "circles", source: source)
        
        //#-example-code
        #if os(macOS)
            let defaultColor = NSColor.blue
            layer.circleColor = NSExpression(
            format: "MGL_MATCH(type, 'earthquake', %@, 'explosion', %@, 'quarry blast', %@, %@)",
                NSColor.orange, NSColor.red, NSColor.yellow, defaultColor)
        #else
            let defaultColor = UIColor.blue
            layer.circleColor = NSExpression(format: "MGL_MATCH(type, 'earthquake', %@, 'explosion', %@, 'quarry blast', %@, %@)",
                UIColor.orange, UIColor.red, UIColor.yellow, defaultColor)
        #endif
        //#-end-example-code
    }
    
    func testMigratingToExpressions$CategoricalValue() {
        let source = MGLShapeSource(identifier: "circles", shape: nil, options: nil)
        let layer = MGLCircleStyleLayer(identifier: "circles", source: source)
        
        //#-example-code
        #if os(macOS)
        let stops : [String : NSColor] = ["earthquake" : NSColor.orange,
                                          "explosion" : NSColor.red,
                                          "quarry blast" : NSColor.yellow]
        layer.circleColor = NSExpression(
            format: "FUNCTION(%@, 'valueForKeyPath:', type)",
            stops)
        #else
        let stops : [String : UIColor] = ["earthquake" : UIColor.orange,
                                          "explosion" : UIColor.red,
                                          "quarry blast" : UIColor.yellow]
        layer.circleColor = NSExpression(
            format: "FUNCTION(%@, 'valueForKeyPath:', type)",
            stops)
        #endif
        //#-end-example-code
    }
    func testMigratingToExpressions$Identity() {
        let source = MGLShapeSource(identifier: "circles", shape: nil, options: nil)
        let layer = MGLCircleStyleLayer(identifier: "circles", source: source)
        
        //#-example-code
        layer.circleRadius = NSExpression(forKeyPath: "mag")
        //#-end-example-code
    }
    
    func testMigratingToExpressions$Multiply() {
        let source = MGLShapeSource(identifier: "circles", shape: nil, options: nil)
        let layer = MGLCircleStyleLayer(identifier: "circles", source: source)
        
        //#-example-code
        layer.circleRadius = NSExpression(forFunction: "multiply:by:", arguments: [NSExpression(forKeyPath: "mag"), 3])
        //#-end-example-code
    }
    
    func testMigratingToExpressions$Cast() {
        let source = MGLShapeSource(identifier: "circles", shape: nil, options: nil)
        
        //#-example-code
        let magnitudeLayer = MGLSymbolStyleLayer(identifier: "mag-layer", source: source)
        magnitudeLayer.text = NSExpression(format: "CAST(mag, 'NSString')")
        mapView.style?.addLayer(magnitudeLayer)
        //#-end-example-code
    }
}