1+ package com .jnape .palatable .lambda .lens .lenses ;
2+
3+ import com .jnape .palatable .lambda .lens .Lens ;
4+ import org .junit .Test ;
5+
6+ import java .util .Optional ;
7+
8+ import static com .jnape .palatable .lambda .functions .builtin .fn2 .Map .map ;
9+ import static com .jnape .palatable .lambda .lens .functions .Over .over ;
10+ import static com .jnape .palatable .lambda .lens .functions .Set .set ;
11+ import static com .jnape .palatable .lambda .lens .functions .View .view ;
12+ import static java .util .Arrays .asList ;
13+ import static java .util .Collections .emptyList ;
14+ import static java .util .Collections .singletonList ;
15+ import static org .junit .Assert .assertEquals ;
16+ import static org .junit .Assert .assertThat ;
17+ import static testsupport .matchers .IterableMatcher .isEmpty ;
18+ import static testsupport .matchers .IterableMatcher .iterates ;
19+
20+ public class IterableLensTest {
21+
22+ @ Test
23+ public void head () {
24+ Lens <Iterable <Integer >, Iterable <Integer >, Optional <Integer >, Integer > head = IterableLens .head ();
25+
26+ assertEquals (Optional .of (1 ), view (head , asList (1 , 2 , 3 )));
27+ assertEquals (Optional .empty (), view (head , emptyList ()));
28+
29+ assertThat (set (head , 1 , emptyList ()), iterates (1 ));
30+ assertThat (set (head , 1 , asList (2 , 2 , 3 )), iterates (1 , 2 , 3 ));
31+
32+ assertThat (over (head , x -> x .orElse (0 ) + 1 , emptyList ()), iterates (1 ));
33+ assertThat (over (head , x -> x .orElse (0 ) + 1 , asList (1 , 2 , 3 )), iterates (2 , 2 , 3 ));
34+ }
35+
36+ @ Test
37+ public void tail () {
38+ Lens .Simple <Iterable <Integer >, Iterable <Integer >> tail = IterableLens .tail ();
39+
40+ assertThat (view (tail , asList (1 , 2 , 3 )), iterates (2 , 3 ));
41+ assertThat (view (tail , emptyList ()), isEmpty ());
42+
43+ assertThat (set (tail , asList (2 , 3 ), singletonList (1 )), iterates (1 , 2 , 3 ));
44+ assertThat (set (tail , emptyList (), asList (1 , 2 , 3 )), iterates (1 ));
45+ assertThat (set (tail , asList (1 , 2 , 3 ), emptyList ()), iterates (1 , 2 , 3 ));
46+ assertThat (set (tail , emptyList (), emptyList ()), isEmpty ());
47+
48+ assertThat (over (tail , map (x -> x + 1 ), emptyList ()), isEmpty ());
49+ assertThat (over (tail , map (x -> x + 1 ), asList (1 , 2 , 3 )), iterates (1 , 3 , 4 ));
50+ }
51+ }
0 commit comments