forked from ChartsOrg/Charts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBarChartTests.swift
370 lines (324 loc) · 15.1 KB
/
BarChartTests.swift
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
@testable import DGCharts
import SnapshotTesting
import XCTest
class BarChartTests: XCTestCase {
private lazy var icon = UIImage(named: "icon", in: Bundle(for: classForCoder), compatibleWith: nil)!
override func setUp() {
super.setUp()
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
// MARK: Prepare
func setupCustomValuesDataEntries(values: [Double]) -> [ChartDataEntry] {
var entries: [ChartDataEntry] = Array()
for (i, value) in values.enumerated() {
entries.append(BarChartDataEntry(x: Double(i), y: value, icon: icon))
}
return entries
}
func setupDefaultValuesDataEntries() -> [ChartDataEntry] {
let values: [Double] = [8, 104, -81, 93, 52, -44, 97, 101, -75, 28,
-76, 25, 20, -13, 52, 44, -57, 23, 45, -91,
99, 14, -84, 48, 40, -71, 106, 41, -45, 61]
return setupCustomValuesDataEntries(values: values)
}
func setupPositiveValuesDataEntries() -> [ChartDataEntry] {
let values: [Double] = [8, 104, 81, 93, 52, 44, 97, 101, 75, 28,
76, 25, 20, 13, 52, 44, 57, 23, 45, 91,
99, 14, 84, 48, 40, 71, 106, 41, 45, 61]
return setupCustomValuesDataEntries(values: values)
}
func setupNegativeValuesDataEntries() -> [ChartDataEntry] {
let values: [Double] = [-8, -104, -81, -93, -52, -44, -97, -101, -75, -28,
-76, -25, -20, -13, -52, -44, -57, -23, -45, -91,
-99, -14, -84, -48, -40, -71, -106, -41, -45, -61]
return setupCustomValuesDataEntries(values: values)
}
func setupZeroValuesDataEntries() -> [ChartDataEntry] {
let values = [Double](repeating: 0.0, count: 30)
return setupCustomValuesDataEntries(values: values)
}
func setupStackedValuesDataEntries() -> [ChartDataEntry] {
var entries: [ChartDataEntry] = Array()
entries.append(BarChartDataEntry(x: 0, yValues: [28, 50, 60, 30, 42], icon: icon))
entries.append(BarChartDataEntry(x: 1, yValues: [-20, -36, -52, -40, -15], icon: icon))
entries.append(BarChartDataEntry(x: 2, yValues: [10, 30, 40, 90, 72], icon: icon))
entries.append(BarChartDataEntry(x: 3, yValues: [-40, -50, -30, -60, -20], icon: icon))
entries.append(BarChartDataEntry(x: 4, yValues: [10, 40, 60, 45, 62], icon: icon))
return entries
}
func setupDefaultStackedDataSet(chartDataEntries: [ChartDataEntry]) -> BarChartDataSet {
let dataSet = BarChartDataSet(entries: chartDataEntries, label: "Stacked bar chart unit test data")
dataSet.drawIconsEnabled = false
dataSet.iconsOffset = CGPoint(x: 0, y: -10.0)
dataSet.colors = Array(arrayLiteral: NSUIColor(red: 46 / 255.0, green: 204 / 255.0, blue: 113 / 255.0, alpha: 1.0),
NSUIColor(red: 241 / 255.0, green: 196 / 255.0, blue: 15 / 255.0, alpha: 1.0),
NSUIColor(red: 231 / 255.0, green: 76 / 255.0, blue: 60 / 255.0, alpha: 1.0),
NSUIColor(red: 52 / 255.0, green: 152 / 255.0, blue: 219 / 255.0, alpha: 1.0))
return dataSet
}
func setupDefaultDataSet(chartDataEntries: [ChartDataEntry]) -> BarChartDataSet {
let dataSet = BarChartDataSet(entries: chartDataEntries, label: "Bar chart unit test data")
dataSet.drawIconsEnabled = false
dataSet.iconsOffset = CGPoint(x: 0, y: -10.0)
return dataSet
}
func setupDefaultChart(dataSets: [BarChartDataSet]) -> BarChartView {
let data = BarChartData(dataSets: dataSets)
data.barWidth = 0.85
let chart = BarChartView(frame: CGRect(x: 0, y: 0, width: 480, height: 350))
chart.backgroundColor = NSUIColor.clear
chart.data = data
return chart
}
// MARK: Start Test
func testDefaultValues() {
let dataEntries = setupDefaultValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
assertChartSnapshot(matching: chart)
}
func testDefaultBarDataSetLabels() {
let dataEntries = setupDefaultValuesDataEntries()
let dataSet = BarChartDataSet(entries: dataEntries)
dataSet.drawIconsEnabled = false
let chart = setupDefaultChart(dataSets: [dataSet])
assertChartSnapshot(matching: chart)
}
func testZeroValues() {
let dataEntries = setupZeroValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
assertChartSnapshot(matching: chart)
}
func testPositiveValues() {
let dataEntries = setupPositiveValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
assertChartSnapshot(matching: chart)
}
func testPositiveValuesWithCustomAxisMaximum() {
let dataEntries = setupPositiveValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
chart.leftAxis.axisMaximum = 50
chart.clipValuesToContentEnabled = true
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testPositiveValuesWithCustomAxisMaximum2() {
let dataEntries = setupPositiveValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
chart.leftAxis.axisMaximum = -10
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testPositiveValuesWithCustomAxisMinimum() {
let dataEntries = setupPositiveValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
chart.leftAxis.axisMinimum = 50
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testPositiveValuesWithCustomAxisMinimum2() {
let dataEntries = setupPositiveValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
chart.leftAxis.axisMinimum = 110
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testPositiveValuesWithCustomAxisMaximumAndCustomAxisMaximum() {
let dataEntries = setupPositiveValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
// If min is greater than max, then min and max will be exchanged.
chart.leftAxis.axisMaximum = 200
chart.leftAxis.axisMinimum = -10
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testNegativeValues() {
let dataEntries = setupNegativeValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
assertChartSnapshot(matching: chart)
}
func testNegativeValuesWithCustomAxisMaximum() {
let dataEntries = setupNegativeValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
chart.leftAxis.axisMaximum = 10
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testNegativeValuesWithCustomAxisMaximum2() {
let dataEntries = setupNegativeValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
chart.leftAxis.axisMaximum = -150
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testNegativeValuesWithCustomAxisMinimum() {
let dataEntries = setupNegativeValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
chart.leftAxis.axisMinimum = -200
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testNegativeValuesWithCustomAxisMinimum2() {
let dataEntries = setupNegativeValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
chart.leftAxis.axisMinimum = 10
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testNegativeValuesWithCustomAxisMaximumAndCustomAxisMaximum() {
let dataEntries = setupNegativeValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
// If min is greater than max, then min and max will be exchanged.
chart.leftAxis.axisMaximum = 10
chart.leftAxis.axisMinimum = -200
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testHidesValues() {
let dataEntries = setupDefaultValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
dataSet.drawValuesEnabled = false
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testNotDrawValueAboveBars() {
let dataEntries = setupDefaultValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
chart.drawValueAboveBarEnabled = false
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testStackedDrawValues() {
let dataEntries = setupStackedValuesDataEntries()
let dataSet = setupDefaultStackedDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testStackedNotDrawValues() {
let dataEntries = setupStackedValuesDataEntries()
let dataSet = setupDefaultStackedDataSet(chartDataEntries: dataEntries)
dataSet.drawValuesEnabled = false
let chart = setupDefaultChart(dataSets: [dataSet])
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testStackedNotDrawValuesAboveBars() {
let dataEntries = setupStackedValuesDataEntries()
let dataSet = setupDefaultStackedDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
chart.drawValueAboveBarEnabled = false
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testHideLeftAxis() {
let dataEntries = setupDefaultValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
chart.leftAxis.enabled = false
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testHideRightAxis() {
let dataEntries = setupDefaultValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
chart.rightAxis.enabled = false
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testInvertedLeftAxis() {
let dataEntries = setupDefaultValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
chart.leftAxis.inverted = true
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testInvertedLeftAxisWithNegativeValues() {
let dataEntries = setupNegativeValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
chart.leftAxis.inverted = true
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testInvertedLeftAxisWithPositiveValues() {
let dataEntries = setupPositiveValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
chart.leftAxis.inverted = true
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testInvertedRightAxis() {
let dataEntries = setupDefaultValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
dataSet.axisDependency = .right
let chart = setupDefaultChart(dataSets: [dataSet])
chart.rightAxis.inverted = true
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testInvertedRightAxisWithNegativeValues() {
let dataEntries = setupNegativeValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
dataSet.axisDependency = .right
let chart = setupDefaultChart(dataSets: [dataSet])
chart.rightAxis.inverted = true
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testInvertedRightAxisWithPositiveValues() {
let dataEntries = setupPositiveValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
dataSet.axisDependency = .right
let chart = setupDefaultChart(dataSets: [dataSet])
chart.rightAxis.inverted = true
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testHideHorizontalGridlines() {
let dataEntries = setupDefaultValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
chart.leftAxis.drawGridLinesEnabled = false
chart.rightAxis.drawGridLinesEnabled = false
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testHideVerticalGridlines() {
let dataEntries = setupDefaultValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
chart.xAxis.drawGridLinesEnabled = false
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
func testDrawIcons() {
let dataEntries = setupDefaultValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
dataSet.drawIconsEnabled = true
chart.notifyDataSetChanged()
assertChartSnapshot(matching: chart)
}
}