11package fj .data ;
22
3+ import fj .P2 ;
34import org .junit .Test ;
45
6+ import static fj .Semigroup .firstSemigroup ;
57import static fj .data .Validation .*;
68import static org .hamcrest .core .Is .is ;
79import static org .junit .Assert .assertThat ;
8- import fj .*;
910
1011public class ValidationTest {
1112 @ Test
@@ -232,6 +233,47 @@ public void testAccumulate8Fail() {
232233 assertThat (v .fail ().length (), is (2 ));
233234 }
234235
236+ @ Test
237+ public void testAccumulate8s () {
238+ final Validation <NumberFormatException , Integer > v1 = parseInt ("1" );
239+ final Validation <NumberFormatException , Integer > v2 = parseInt ("2" );
240+ final Validation <NumberFormatException , Integer > v3 = parseInt ("3" );
241+ final Validation <NumberFormatException , Integer > v4 = parseInt ("4" );
242+ final Validation <NumberFormatException , Integer > v5 = parseInt ("5" );
243+ final Validation <NumberFormatException , Integer > v6 = parseInt ("6" );
244+ final Validation <NumberFormatException , Integer > v7 = parseInt ("7" );
245+ final Validation <NumberFormatException , Integer > v8 = parseInt ("8" );
246+ final Option <NumberFormatException > on2 = v1 .accumulate (firstSemigroup (), v2 );
247+ assertThat (on2 , is (Option .none ()));
248+ final Option <NumberFormatException > on3 = v1 .accumulate (firstSemigroup (), v2 , v3 );
249+ assertThat (on3 , is (Option .none ()));
250+ final Option <NumberFormatException > on4 = v1 .accumulate (firstSemigroup (), v2 , v3 , v4 );
251+ assertThat (on4 , is (Option .none ()));
252+ final Option <NumberFormatException > on5 = v1 .accumulate (firstSemigroup (), v2 , v3 , v4 , v5 );
253+ assertThat (on5 , is (Option .none ()));
254+ final Option <NumberFormatException > on6 = v1 .accumulate (firstSemigroup (), v2 , v3 , v4 , v5 , v6 );
255+ assertThat (on6 , is (Option .none ()));
256+ final Option <NumberFormatException > on7 = v1 .accumulate (firstSemigroup (), v2 , v3 , v4 , v5 , v6 , v7 );
257+ assertThat (on7 , is (Option .none ()));
258+ final Option <NumberFormatException > on8 = v1 .accumulate (firstSemigroup (), v2 , v3 , v4 , v5 , v6 , v7 , v8 );
259+ assertThat (on8 , is (Option .none ()));
260+ }
261+
262+ @ Test
263+ public void testAccumulate8sFail () {
264+ final Option <NumberFormatException > on =
265+ parseInt ("x" ).accumulate (
266+ firstSemigroup (),
267+ parseInt ("2" ),
268+ parseInt ("3" ),
269+ parseInt ("4" ),
270+ parseInt ("5" ),
271+ parseInt ("6" ),
272+ parseInt ("7" ),
273+ parseInt ("y" ));
274+ assertThat (on .some ().getMessage (), is ("For input string: \" x\" " ));
275+ }
276+
235277 @ Test (expected = Error .class )
236278 public void testSuccess () {
237279 parseShort ("x" ).success ();
@@ -241,4 +283,36 @@ public void testSuccess() {
241283 public void testFail () {
242284 parseShort ("12" ).fail ();
243285 }
286+
287+ @ Test
288+ public void testCondition () {
289+ final Validation <String , String > one = condition (true , "not 1" , "one" );
290+ assertThat (one .success (), is ("one" ));
291+ final Validation <String , String > fail = condition (false , "not 1" , "one" );
292+ assertThat (fail .fail (), is ("not 1" ));
293+ }
294+
295+ @ Test
296+ public void testNel () {
297+ assertThat (Validation .success ("success" ).nel ().success (), is ("success" ));
298+ assertThat (Validation .fail ("fail" ).nel ().fail ().head (), is ("fail" ));
299+ }
300+
301+ @ Test
302+ public void testFailNEL () {
303+ Validation <NonEmptyList <Exception >, Integer > v = failNEL (new Exception ("failed" ));
304+ assertThat (v .isFail (), is (true ));
305+ }
306+
307+ @ Test
308+ public void testEither () {
309+ assertThat (either ().f (Validation .success ("success" )).right ().value (), is ("success" ));
310+ assertThat (either ().f (Validation .fail ("fail" )).left ().value (), is ("fail" ));
311+ }
312+
313+ @ Test
314+ public void testValidation () {
315+ assertThat (validation ().f (Either .right ("success" )).success (), is ("success" ));
316+ assertThat (validation ().f (Either .left ("fail" )).fail (), is ("fail" ));
317+ }
244318}
0 commit comments