-
Notifications
You must be signed in to change notification settings - Fork 47
/
SingleLayerAssert.kt
128 lines (108 loc) · 4.27 KB
/
SingleLayerAssert.kt
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
/*
* Copyright (c) 2019. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
package jetbrains.datalore.plot.server.config
import jetbrains.datalore.plot.base.Aes
import jetbrains.datalore.plot.base.data.DataFrameUtil
import jetbrains.datalore.plot.builder.tooltip.MappedAes
import jetbrains.datalore.plot.builder.tooltip.VariableValue
import jetbrains.datalore.plot.config.LayerConfig
import jetbrains.datalore.plot.config.Option.Geom.Choropleth.GEO_POSITIONS
import org.assertj.core.api.AbstractAssert
import org.assertj.core.api.Assertions
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import kotlin.test.fail
class SingleLayerAssert private constructor(layers: List<LayerConfig>) :
AbstractAssert<SingleLayerAssert, List<LayerConfig>>(layers, SingleLayerAssert::class.java) {
private val myLayer: LayerConfig
init {
assertEquals(1, layers.size)
myLayer = layers[0]
}
fun haveBinding(key: Aes<*>, value: String): SingleLayerAssert {
haveBindings(mapOf(key to value))
return this
}
fun haveBindings(expectedBindings: Map<Aes<*>, String>): SingleLayerAssert {
for (aes in expectedBindings.keys) {
assertBinding(aes, expectedBindings[aes]!!)
}
return this
}
fun haveDataVector(key: String, value: List<*>): SingleLayerAssert {
haveDataVectors(mapOf(key to value))
return this
}
fun haveDataVectors(expectedDataVectors: Map<String, List<*>>): SingleLayerAssert {
val df = myLayer.combinedData
val layerData = DataFrameUtil.toMap(df)
for (`var` in expectedDataVectors.keys) {
assertTrue(layerData.containsKey(`var`), "No data key '$`var`' found")
val vector = layerData[`var`]
val expectedVector = expectedDataVectors[`var`]
assertEquals(expectedVector, vector)
}
return this
}
fun haveTooltipList(expectedNames : List<String>?): SingleLayerAssert {
if (expectedNames != null) {
assertTooltipListCount(expectedNames.size)
for (aes in expectedNames)
assertExpectedTooltip(aes)
}
else {
val tooltipNames = getUserTooltipNames()
assertTrue(tooltipNames.isNullOrEmpty())
}
return this
}
internal fun haveMapVectors(expectedMapVectors: Map<String, List<*>>): SingleLayerAssert {
Assertions.assertThat(expectedMapVectors).isEqualTo(myLayer[GEO_POSITIONS])
return this
}
private fun haveMapValues(key: String, expectedMapValues: List<*>): SingleLayerAssert {
val geoPositions = myLayer[GEO_POSITIONS] as Map<*, *>?
assertTrue(geoPositions!!.containsKey(key))
Assertions.assertThat(expectedMapValues).isEqualTo(geoPositions[key])
return this
}
private fun assertBinding(aes: Aes<*>, varName: String) {
val varBindings = myLayer.varBindings
for (varBinding in varBindings) {
if (varBinding.aes == aes) {
assertEquals(varName, varBinding.variable.name)
return
}
}
fail("No binding $aes -> $varName")
}
private fun getUserAesTooltipNames(): List<String> {
return myLayer.tooltips
?.flatMap { it.data }
?.filterIsInstance<MappedAes>()
?.map(MappedAes::getAesName)
?: emptyList()
}
private fun getUserVariableNames(): List<String> {
return myLayer.tooltips
?.flatMap { it.data }
?.filterIsInstance<VariableValue>()?.map(VariableValue::getVariableName)
?: emptyList()
}
private fun getUserTooltipNames(): List<String> {
return getUserAesTooltipNames() + getUserVariableNames()
}
private fun assertExpectedTooltip(name: String) {
assertTrue(getUserTooltipNames().contains(name), "No tooltip for var with name: '$name'")
}
private fun assertTooltipListCount(expectedCount: Int) {
assertEquals(expectedCount, getUserTooltipNames().size, "Wrong size of tooltip list")
}
companion object {
fun assertThat(layers: List<LayerConfig>): SingleLayerAssert {
return SingleLayerAssert(layers)
}
}
}