summaryrefslogtreecommitdiff
path: root/test/files/jvm/try-type-tests.scala
blob: eecbb0ae57ec006c33641bc000f45f7744fa8768 (plain) (blame)
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
import scala.util.{Try, Success, Failure}

// tests the basic combinators on Try
trait TryStandard {

	def testForeachSuccess(): Unit = {
		val t = Success(1)
		var res = 0
		t.foreach(x => res = x * 10)
		assert(res == 10)
	}

	def testForeachFailure(): Unit = {
		val t = Failure(new Exception("foo"))
		t.foreach(x => assert(false))
	}

	def testFlatMapSuccess(): Unit = {
		val t = Success(1)
		val n = t.flatMap(x => Try(x * 10))
		assert(n.get == 10)
	}

	def testFlatMapFailure(): Unit = {
		val t = Failure(new Exception("foo"))
		val n = t.flatMap{ x => assert(false); Try() }
	}

	def testMapSuccess(): Unit = {
		val t = Success(1)
		val n = t.map(x => x * 10)
		assert(n.get == 10)
	}

	def testMapFailure(): Unit = {
		val t = Failure(new Exception("foo"))
		val n = t.map(x => assert(false))
	}

	def testFilterSuccessTrue(): Unit = {
		val t = Success(1)
		val n = t.filter(x => x > 0)
		assert(n.get == 1)
	}

	def testFilterSuccessFalse(): Unit = {
		val t = Success(1)
		val n = t.filter(x => x < 0)
		n match {
			case Success(v) => assert(false)
			case Failure(e: NoSuchElementException) => assert(true)
		}
	}

	def testFilterFailure(): Unit = {
		val t = Failure(new Exception("foo"))
		val n = t.filter{ x => assert(false); true }
	}

	def testRescueSuccess(): Unit = {
		val t = Success(1)
		t.rescue{ case x => assert(false); Try() }
	}

	def testRescueFailure(): Unit = {
		val t = Failure(new Exception("foo"))
		val n = t.rescue{ case x => Try(1) }
		assert(n.get == 1)
	}

	def testRecoverSuccess(): Unit = {
		val t = Success(1)
		t.recover{ case x => assert(false); 99 }
	}

	def testRecoverFailure(): Unit = {
		val t = Failure(new Exception("foo"))
		val n = t.recover{ case x => 1 }
		assert(n.get == 1)
	}

	def testFlattenSuccess(): Unit = {
		val f = Failure(new Exception("foo"))
		val t = Success(f)
		assert(t.flatten == f)
	}

	def testFailedSuccess(): Unit = {
		val t = Success(1)
		val n = t.failed
		n match {
			case Failure(e: UnsupportedOperationException) => assert(true)
			case _ => assert(false)
		}
	}

	def testFailedFailure(): Unit = {
		val t = Failure(new Exception("foo"))
		val n = t.failed
		n match {
			case Success(e: Exception) => assert(true)
			case _ => assert(false)
		}
	}

	testForeachSuccess()
	testForeachFailure()
	testFlatMapSuccess()
	testFlatMapFailure()
	testMapSuccess()
	testMapFailure()
	testFilterSuccessTrue()
	testFilterSuccessFalse()
	testFilterFailure()
	testRescueSuccess()
	testRescueFailure()
	testRecoverSuccess()
	testRecoverFailure()
	testFlattenSuccess()
	testFailedSuccess()
	testFailedFailure()
}

// tests that implicit conversions from Try to Either behave as expected
trait TryImplicitConversionTry2Either {

	def testTry2RightMap(): Unit = {
		val t = Success(1)
		val n = t.right.map(x => x * 100)
		assert(n == Right(100))
	}

	def testTry2LeftMap(): Unit = {
		val e = new Exception("foo")
		val t = Failure(e)
		val n = t.left.map(x => x)
		assert(n == Left(e))
	}

	def testTry2FoldSuccess(): Unit = {
		val t = Success(1)
		val n = t.fold(x => assert(false), y => y * 200)
		assert(n == 200)
	}

	def testTry2FoldFailure(): Unit = {
		val e = new Exception("foo")
		val t = Failure(e)
		val n = t.fold(x => x, y => assert(false))
		assert(n == e)
	}

	def testTry2SwapSuccess(): Unit = {
		val t = Success(1)
		val n = t.swap
		assert(n == Left(1))
	}

	def testTry2SwapFailure(): Unit = {
		val e = new Exception("foo")
		val t = Failure(e)
		val n = t.swap
		assert(n == Right(e))
	}

	// def testTry2MergeSucccess(): Unit = {
	// 	val t: Try[Int] = Success(1)
	// 	val n = (t: Either[Any, Any]).t.merge // connecting two implicit conversions
	// 	assert(n == 1)
	// }

	// def testTry2MergeFailure(): Unit = {
	// 	val e = new Exception("foo")
	// 	val t = Failure(e)
	// 	val n = (t: Either[Any, Any]).merge // connecting two implicit conversions
	// 	assert(n == e)
	// }

	testTry2RightMap()
	testTry2LeftMap()
	testTry2FoldSuccess()
	testTry2FoldFailure()
	testTry2SwapSuccess()
	testTry2SwapFailure()
	// testTry2MergeSucccess()
	// testTry2MergeFailure()
}

// tests that implicit conversions from Either to Try behave as expected
trait TryImplicitConversionEither2Try {

	def testRight2FilterSuccessTrue(): Unit = {
	  def expectsTry[U <% Try[Int]](rght: U): Try[Int] = {
			val n = rght.filter(x => x > 0) // this should be converted to a Try
			n
	  }
		val r = Right(1)
    val n = expectsTry(r)
		assert(n == Success(1))
	}

	def testRight2FilterSuccessFalse(): Unit = {
	  def expectsTry[U <% Try[Int]](rght: U): Try[Int] = {
			val n = rght.filter(x => x < 0) // this should be converted to a Try
			n
	  }
		val r = Right(1)
    val n = expectsTry(r)
    n match {
	case Failure(e: NoSuchElementException) => assert(true)
	case _ => assert(false)
    }
	}

	def testLeft2FilterFailure(): Unit = {
	  def expectsTry[U <% Try[Int]](rght: U): Try[Int] = {
			val n = rght.filter(x => x > 0) // this should be converted to a Try
			n
	  }
		val r = Left(new Exception("foo"))
    val n = expectsTry(r)
    n match {
	case Failure(e: Exception) => assert(true)
	case _ => assert(false)
    }
	}

	def testRight2GetSuccess(): Unit = {
	  def expectsTry[U <% Try[Int]](rght: U): Int = {
			val n = rght.get // this should be converted to a Try
			n
	  }
		val r = Right(1)
    val n = expectsTry(r)
    assert(n == 1)
	}

	testRight2FilterSuccessTrue()
	testRight2FilterSuccessFalse()
	testLeft2FilterFailure()
	testRight2GetSuccess()
}

object Test
extends App
with TryStandard
with TryImplicitConversionTry2Either
with TryImplicitConversionEither2Try {
  System.exit(0)
}