-
Notifications
You must be signed in to change notification settings - Fork 533
/
zipper.scala
215 lines (165 loc) · 5.93 KB
/
zipper.scala
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
/*
* Copyright (c) 2011-14 Miles Sabin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package shapeless
import org.junit.Test
import org.junit.Assert._
import test._
import syntax.zipper._
class ZipperTests {
@Test
def testTraversal: Unit = {
val l = 1 :: "foo" :: 3.0 :: HNil
val z = l.toZipper
val i = z.get
typed[Int](i)
assertEquals(1, i)
val s = z.right.get
typed[String](s)
assertEquals("foo", s)
val d = z.right.right.get
typed[Double](d)
assertEquals(3.0, d, Double.MinPositiveValue)
val zl = z.last
val d2 = zl.left.get
typed[Double](d2)
assertEquals(3.0, d2, Double.MinPositiveValue)
val zf = zl.first
val i2 = zf.get
typed[Int](i2)
assertEquals(1, i2)
}
@Test
def testUpdate: Unit = {
val l = 1 :: "foo" :: 3.0 :: HNil
val l2 = l.toZipper.right.put("wibble", 45).reify
typed[Int :: (String, Int) :: Double :: HNil](l2)
assertEquals(1 :: ("wibble", 45) :: 3.0 :: HNil, l2)
val l3 = l.toZipper.right.delete.reify
typed[Int :: Double :: HNil](l3)
assertEquals(1 :: 3.0 :: HNil, l3)
val l4 = l.toZipper.insert("bar").reify
typed[String :: Int :: String :: Double :: HNil](l4)
assertEquals("bar" :: 1 :: "foo" :: 3.0 :: HNil, l4)
val l5 = l.toZipper.right.right.right.insert("bar").reify
typed[Int :: String :: Double :: String :: HNil](l5)
assertEquals(1 :: "foo" :: 3.0 :: "bar" :: HNil, l5)
val l6 = l.toZipper.right.modify(_.toUpperCase).reify
typed[Int :: String :: Double :: HNil](l6)
assertEquals(1 :: "FOO" :: 3.0 :: HNil, l6)
}
@Test
def testTypeIndexing: Unit = {
val l = 1 :: "foo" :: 3.0 :: HNil
val l6 = l.toZipper.rightTo[Double]
val d6 = l6.get
typed[Double](d6)
assertEquals(3.0, d6, Double.MinPositiveValue)
val l7 = l.toZipper.last.leftTo[Int]
val i7 = l7.get
typed[Int](i7)
assertEquals(1, i7)
}
@Test
def testNatIndexing: Unit = {
val l = 1 :: "foo" :: 3.0 :: HNil
val l8 = l.toZipper.rightBy(2)
val d8 = l8.get
typed[Double](d8)
assertEquals(3.0, d8, Double.MinPositiveValue)
val l9 = l8.leftBy(1)
val s9 = l9.get
typed[String](s9)
assertEquals("foo", s9)
}
@Test
def testEmpty: Unit = {
val l = HNil
val z = l.toZipper
val l2 = z.insert(23).insert("foo").insert(true).reify
typed[Int :: String :: Boolean :: HNil](l2)
assertEquals(23 :: "foo" :: true :: HNil, l2)
}
case class Address(street : String, city : String, postcode : String)
case class Person(name : String, age : Int, address : Address)
val p1 = Person("Joe Grey", 37, Address("Southover Street", "Brighton", "BN2 9UA"))
case class Dept[E <: HList](manager : Employee, employees : E)
case class Employee(name : String, salary : Int)
type D = Dept[Employee :: Employee :: Employee :: HNil]
val dept =
Dept(
Employee("Agamemnon", 5000),
Employee("Menelaus", 3000) ::
Employee("Achilles", 2000) ::
Employee("Odysseus", 2000) ::
HNil
)
@Test
def testCaseClasses: Unit = {
val z = p1.toZipper
val name = z.get
typed[String](name)
assertEquals("Joe Grey", name)
val age = z.right.get
typed[Int](age)
assertEquals(37, age)
val address = z.right.right.get
typed[Address](address)
assertEquals(Address("Southover Street", "Brighton", "BN2 9UA"), address)
val street = z.right.right.down.get
typed[String](street)
assertEquals("Southover Street", street)
val city = z.right.right.down.right.get
typed[String](city)
assertEquals("Brighton", city)
val postcode = z.right.right.down.right.right.get
typed[String](postcode)
assertEquals("BN2 9UA", postcode)
val updatedName = z.put("Fred Bloggs").reify
typed[Person](updatedName)
assertEquals(Person("Fred Bloggs", 37, Address("Southover Street", "Brighton", "BN2 9UA")), updatedName)
val updatedCity = z.right.right.down.right.put("London")
typed[Address](updatedCity.reify)
assertEquals(Address("Southover Street", "London", "BN2 9UA"), updatedCity.reify)
typed[Person](updatedCity.up.reify)
assertEquals(Person("Joe Grey", 37, Address("Southover Street", "London", "BN2 9UA")), updatedCity.up.reify)
typed[Person](updatedCity.root.reify)
assertEquals(Person("Joe Grey", 37, Address("Southover Street", "London", "BN2 9UA")), updatedCity.up.reify)
val agedPerson = z.right.modify(_ + 1).reify
typed[Person](agedPerson)
assertEquals(Person("Joe Grey", 38, Address("Southover Street", "Brighton", "BN2 9UA")), agedPerson)
val reifiedAddress = z.right.right.down.reify
typed[Address](reifiedAddress)
val reifiedCity = z.right.right.down.right.reify
typed[Address](reifiedCity)
val underAddress = z.right.right.down
val addressAgain = underAddress.up.get
typed[Address](addressAgain)
val root1 = z.root
typed[Person](root1.reify)
val root2 = underAddress.root
typed[Person](root2.reify)
val z2 = dept.toZipper
val z3 = z2.down.put("King Agamemnon").right.put(8000).up.right.down.right.down.right.put(3000).up.right.down.right.modify(_ * 2).root.reify
typed[D](z3)
assertEquals(
Dept(
Employee("King Agamemnon", 8000),
Employee("Menelaus", 3000) ::
Employee("Achilles", 3000) ::
Employee("Odysseus", 4000) ::
HNil), z3)
}
}