1
+ package com .baeldung .scala .forcomprehension
2
+
3
+ import org .junit .Assert .assertEquals
4
+ import org .junit .{Before , Test }
5
+
6
+ class ForLoopImplementation extends ForLoop {
7
+ var sideEffectResult : Seq [Any ] = Nil
8
+ override def sideEffectFunction (arg : Any ): Unit = {
9
+ sideEffectResult = sideEffectResult :+ arg
10
+ }
11
+
12
+ def clean (): Unit = {
13
+ sideEffectResult = Nil ;
14
+ }
15
+ }
16
+
17
+ class ForLoopUnitTest {
18
+ val forLoopTest = new ForLoopImplementation
19
+ val rangeTo : Range = 1 to 3
20
+ val rangeUntil : Range = 1 until 3
21
+ val colors = Seq (" R" , " G" , " B" )
22
+ val map = Map (" R" -> " Red" , " G" -> " Green" , " B" -> " Blue" )
23
+ val deck = Map (" ♣" -> List (" A" , " K" , " Q" ),
24
+ " ♦" -> List (" J" , " 10" ),
25
+ " ♥" -> List (" 9" , " 8" , " 7" ),
26
+ " ♠" -> List (" A" , " K" , " J" , " 6" ))
27
+ val numbers = List (1 , 2 , 3 )
28
+ val someIntValue : Option [Int ] = Some (10 )
29
+ val someStringValue : Option [String ] = Some (" Ten" )
30
+
31
+ @ Before
32
+ def init (): Unit = {
33
+ forLoopTest.clean()
34
+ }
35
+
36
+ // givenXXX_whenYYY_thenZZZ or whenXXX_thenYYY
37
+
38
+ @ Test
39
+ def givenInclusiveRange_whenForLoop_thenIterateOverEachElement (): Unit = {
40
+ forLoopTest.iterateRangeTo(rangeTo)
41
+ val expected = Seq (1 , 2 , 3 )
42
+ assertEquals(expected, forLoopTest.sideEffectResult)
43
+ }
44
+
45
+ @ Test
46
+ def givenExclusiveRange_whenForLoop_thenIterateOverEachElement (): Unit = {
47
+ forLoopTest.iterateRangeUntil(rangeUntil)
48
+ val expected = Seq (1 , 2 )
49
+ assertEquals(expected, forLoopTest.sideEffectResult)
50
+ }
51
+
52
+ @ Test
53
+ def givenExclusiveAndInclusiveRange_whenForLoopWithMltipleGenerators_thenCartesianProduct (): Unit = {
54
+ forLoopTest.multipleGenerators(rangeTo, rangeUntil)
55
+ val expected = Seq (" 1, 1" , " 1, 2" , " 2, 1" , " 2, 2" , " 3, 1" , " 3, 2" )
56
+ assertEquals(expected, forLoopTest.sideEffectResult)
57
+ }
58
+
59
+ @ Test
60
+ def givenCollection_whenForLoop_thenIterateOverEachElement (): Unit = {
61
+ forLoopTest.iterateCollection(colors)
62
+ val expected = Seq (" R" , " G" , " B" )
63
+ assertEquals(expected, forLoopTest.sideEffectResult)
64
+ }
65
+
66
+ @ Test
67
+ def givenCollection_whenForLoopWithMltipleGenerators_thenAllPossibleCombinations (): Unit = {
68
+ forLoopTest.iterateCollectionWithMultipleGenerators(colors)
69
+ val expected = List (" RRR " , " RRG " , " RRB " , " RGR " , " RGG " , " RGB " , " RBR " , " RBG " , " RBB " ,
70
+ " GRR " , " GRG " , " GRB " , " GGR " , " GGG " , " GGB " , " GBR " , " GBG " , " GBB " ,
71
+ " BRR " , " BRG " , " BRB " , " BGR " , " BGG " , " BGB " , " BBR " , " BBG " , " BBB " )
72
+ assertEquals(expected, forLoopTest.sideEffectResult)
73
+ }
74
+
75
+ @ Test
76
+ def givenCollection_whenForLoopWithMltipleGeneratorsAndGuards_thenUniqueLettersCombinations (): Unit = {
77
+ forLoopTest.iterateCollectionsWithGuards(colors)
78
+ val expected = List (" RGB " , " RBG " , " GRB " , " GBR " , " BRG " , " BGR " )
79
+ assertEquals(expected, forLoopTest.sideEffectResult)
80
+ }
81
+
82
+ @ Test
83
+ def givenMap_whenForLoop_thenCollectionOfStrings (): Unit = {
84
+ forLoopTest.iterateMap(map)
85
+ val expected = List (" R is for Red" , " G is for Green" , " B is for Blue" )
86
+ assertEquals(expected, forLoopTest.sideEffectResult)
87
+ }
88
+
89
+ @ Test
90
+ def givenMap_whenForLoopWithMltipleGenerators_thenAllCombinationsOfKeyAndValueList (): Unit = {
91
+ forLoopTest.iterateMapMultipleGenerators(deck)
92
+ val expected = List (" A of ♣" , " K of ♣" , " Q of ♣" , " J of ♦" , " 10 of ♦" , " 9 of ♥" , " 8 of ♥" , " 7 of ♥" , " A of ♠" , " K of ♠" , " J of ♠" , " 6 of ♠" )
93
+ assertEquals(expected, forLoopTest.sideEffectResult)
94
+ }
95
+
96
+ @ Test
97
+ def givenCollection_whenForComprehension_thenReturnedCollectionOfStrings (): Unit = {
98
+ val result = forLoopTest.pureIteration(numbers)
99
+ val expected = List (" 1 + 1 = 2" , " 2 + 2 = 4" , " 3 + 3 = 6" )
100
+ assertEquals(expected, result)
101
+ }
102
+
103
+ @ Test
104
+ def givenOptionals_whenForComprehensionOrMap_thenReturnedOptional (): Unit = {
105
+
106
+ val resultFor = forLoopTest.forComprehensionWithOptionals(someIntValue, someStringValue)
107
+ val resultMap = forLoopTest.mapOptionals(someIntValue, someStringValue)
108
+ val expected = Some (" 10 is Ten" )
109
+ assertEquals(expected, resultFor)
110
+ assertEquals(expected, resultMap)
111
+ assertEquals(resultFor, resultMap)
112
+ }
113
+ }
0 commit comments