55import org .junit .Before ;
66import org .junit .Test ;
77
8- import java .util .NoSuchElementException ;
9- import java .util .Optional ;
8+ import java .util .*;
109import java .util .function .Function ;
1110
1211import static org .junit .Assert .*;
1312import static io .atlassian .fugue .Unit .Unit ;
1413
1514public class FugueTest {
1615
17- @ Before
18- public void setup () {
19- }
20-
2116 @ Test
2217 public void whenSome_thenDefined () {
2318 Option <String > some = Option .some ("value" );
@@ -47,7 +42,11 @@ public void whenSomeNull_thenException() {
4742
4843 @ Test
4944 public void whenNullOption_thenSome () {
50- Option <Object > some = Option .some ("value" ).map (x -> null );
45+ Option <String > some = Option .some ("value" ) .map (String ::toUpperCase );
46+
47+ assertEquals ("VALUE" , some .get ());
48+
49+ some = some .map (x -> null );
5150
5251 assertNull (some .get ());
5352 some .forEach (Assert ::assertNull );
@@ -74,6 +73,33 @@ public void whenNone_thenEmptyOptional() {
7473 }
7574
7675 @ Test
76+ public void whenOption_thenIterable () {
77+ Option <String > some = Option .some ("value" );
78+ Iterable <String > strings = Iterables .concat (some , Arrays .asList ("a" , "b" , "c" ));
79+ List <String > stringList = new ArrayList <>();
80+ Iterables .addAll (stringList , strings );
81+
82+ assertEquals (4 , stringList .size ());
83+ }
84+
85+ @ Test
86+ public void whenOption_thenStream () {
87+ assertEquals (0 , Option .none ().toStream ().count ());
88+ assertEquals (1 , Option .some ("value" ).toStream ().count ());
89+ }
90+
91+ @ Test
92+ public void whenLift_thenPartialFunction () {
93+ Function <Integer , Integer > f = (Integer x ) -> x > 0 ? x + 1 : null ;
94+ Function <Option <Integer >, Option <Integer >> lifted = Options .lift (f );
95+
96+ assertEquals (2 , (long ) lifted .apply (Option .some (1 )).get ());
97+ assertTrue (lifted .apply (Option .none ()).isEmpty ());
98+ assertEquals (null , lifted .apply (Option .some (0 )).get ());
99+ }
100+
101+ @ Test
102+
77103 public void whenLeft_thenEither () {
78104 Either <Integer , String > right = Either .right ("value" );
79105 Either <Integer , String > left = Either .left (-1 );
@@ -92,6 +118,8 @@ public void whenLeft_thenEither() {
92118
93119 assertTrue (either .isRight ());
94120 assertEquals ("value" , either .right ().get ());
121+
122+ either .right ().forEach (x -> assertEquals ("value" , x ));
95123 }
96124
97125 private static String decodeSQLErrorCode (Integer x ) {
@@ -193,4 +221,12 @@ Unit doSomething() {
193221 return Unit ();
194222 }
195223
224+ @ Test
225+ public void whenPair_thenLeftAndRight () {
226+ Pair <Integer , String > pair = Pair .pair (1 , "a" );
227+
228+ assertEquals (1 , (int ) pair .left ());
229+ assertEquals ("a" , pair .right ());
230+ }
231+
196232}
0 commit comments