View Javadoc

1   /*
2      Copyright 2011 Atlassian
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15   */
16  package io.atlassian.fugue;
17  
18  import org.junit.Test;
19  
20  import java.util.Arrays;
21  import java.util.concurrent.atomic.AtomicBoolean;
22  import java.util.function.Predicate;
23  import java.util.HashMap;
24  
25  import static io.atlassian.fugue.Functions.apply;
26  import static io.atlassian.fugue.Functions.constant;
27  import static io.atlassian.fugue.Functions.countingPredicate;
28  import static io.atlassian.fugue.Functions.curried;
29  import static io.atlassian.fugue.Functions.flip;
30  import static io.atlassian.fugue.Functions.forMap;
31  import static io.atlassian.fugue.Functions.forMapWithDefault;
32  import static io.atlassian.fugue.Functions.matches;
33  import static io.atlassian.fugue.Functions.partial;
34  import static io.atlassian.fugue.Functions.toBiFunction;
35  import static io.atlassian.fugue.Iterables.map;
36  import static io.atlassian.fugue.Option.none;
37  import static io.atlassian.fugue.Option.some;
38  import static io.atlassian.fugue.UtilityFunctions.dividableBy;
39  import static io.atlassian.fugue.UtilityFunctions.hasMinLength;
40  import static io.atlassian.fugue.UtilityFunctions.isEven;
41  import static io.atlassian.fugue.UtilityFunctions.leftOfString;
42  import static io.atlassian.fugue.UtilityFunctions.square;
43  import static io.atlassian.fugue.UtilityFunctions.subtract;
44  import static org.hamcrest.Matchers.contains;
45  import static org.hamcrest.Matchers.is;
46  import static org.junit.Assert.assertThat;
47  
48  public class FunctionsTest {
49  
50    @Test public void functionApply() {
51      assertThat(Functions.<Integer, Integer> apply(8).apply(square), is(64));
52    }
53  
54    @Test public void functionLazyApply() {
55      assertThat(Functions.<Integer, Integer> apply(Suppliers.ofInstance(8)).apply(square), is(64));
56    }
57  
58    @Test public void functionLazyApplyIsLazy() {
59      // NoSuchElementException should not be thrown
60      apply(Suppliers.fromOption(none()));
61    }
62  
63    @Test public void partialNone() {
64      assertThat(partial(isEven, square).apply(1), is(Option.<Integer> none()));
65    }
66  
67    @Test public void partialSome() {
68      assertThat(partial(isEven, square).apply(4), is(some(16)));
69    }
70  
71    @Test public void matches2Some() {
72      assertThat(matches(partial(dividableBy(3), square), partial(dividableBy(2), square)).apply(2), is(some(4)));
73    }
74  
75    @Test public void matches2None() {
76      assertThat(matches(partial(dividableBy(3), square), partial(dividableBy(2), square)).apply(1), is(Option.<Integer> none()));
77    }
78  
79    @Test public void matches3Some() {
80      assertThat(matches(partial(dividableBy(4), square), partial(dividableBy(3), square), partial(dividableBy(2), square)).apply(2), is(some(4)));
81    }
82  
83    @Test public void matches3None() {
84      assertThat(matches(partial(dividableBy(4), square), partial(dividableBy(3), square), partial(dividableBy(2), square)).apply(1),
85        is(Option.<Integer> none()));
86    }
87  
88    @Test public void matches4Some() {
89      assertThat(
90        matches(partial(dividableBy(5), square), partial(dividableBy(4), square), partial(dividableBy(3), square), partial(dividableBy(2), square))
91          .apply(2), is(Option.some(4)));
92    }
93  
94    @Test public void matches4None() {
95      assertThat(
96        matches(partial(dividableBy(5), square), partial(dividableBy(4), square), partial(dividableBy(3), square), partial(dividableBy(2), square))
97          .apply(1), is(Option.<Integer> none()));
98    }
99  
100   @Test public void matches5Some() {
101     assertThat(
102       matches(partial(dividableBy(6), square), partial(dividableBy(5), square), partial(dividableBy(4), square), partial(dividableBy(3), square),
103         partial(dividableBy(2), square)).apply(2), is(Option.some(4)));
104   }
105 
106   @Test public void matches5None() {
107     assertThat(
108       matches(partial(dividableBy(6), square), partial(dividableBy(5), square), partial(dividableBy(4), square), partial(dividableBy(3), square),
109         partial(dividableBy(2), square)).apply(1), is(Option.<Integer> none()));
110   }
111 
112   @Test public void functionsFromConsumer() {
113     AtomicBoolean called = new AtomicBoolean(false);
114     Functions.fromConsumer(o -> called.set(true)).apply(new Object());
115     assertThat(called.get(), is(true));
116   }
117 
118   @Test public void functionsToBiFunction() {
119     assertThat(toBiFunction(leftOfString).apply("abcde", 3), is(some("abc")));
120   }
121 
122   @Test public void functionsCurried() {
123     assertThat(curried(subtract).apply(6).apply(2), is(4));
124   }
125 
126   @Test public void flipped() {
127     assertThat(flip(hasMinLength).apply(2).apply("abcde"), is(true));
128   }
129 
130   @Test public void functionsConstant() {
131     assertThat(map(Arrays.asList(1, 2, 3), constant(1)), contains(1, 1, 1));
132   }
133 
134   @Test public void countingPredicateWithOne() {
135     final Predicate<Integer> p = countingPredicate(1);
136     assertThat(p.test(1), is(true));
137     assertThat(p.test(1), is(false));
138   }
139 
140   @Test public void countingPredicateWithZero() {
141     final Predicate<Integer> p = countingPredicate(0);
142     assertThat(p.test(1), is(false));
143   }
144 
145   @Test(expected = IllegalArgumentException.class) public void countingPredicateWithNegative() {
146     countingPredicate(-1);
147   }
148 
149   @Test public void forMapWithDefaultWithValue() {
150     assertThat(forMapWithDefault(new HashMap<Integer, Integer>() {
151       {
152         put(1, 1);
153       }
154     }, 2).apply(1), is(1));
155   }
156 
157   @Test public void forMapWithDefaultWithNullValue() {
158     assertThat(forMapWithDefault(new HashMap<Integer, Integer>() {
159       {
160         put(1, 1);
161       }
162     }, 2).apply(null), is(2));
163   }
164 
165   @Test public void forMapWithDefaultWithNoValue() {
166     assertThat(forMapWithDefault(new HashMap<Integer, Integer>() {
167       {
168         put(1, 1);
169       }
170     }, 2).apply(55), is(2));
171   }
172 
173   @Test public void forMapWithValue() {
174     assertThat(forMap(new HashMap<Integer, Integer>() {
175       {
176         put(1, 1);
177       }
178     }).apply(1), is(some(1)));
179   }
180 
181   @Test public void forMapWithNullValue() {
182     assertThat(forMap(new HashMap<Integer, Integer>() {
183       {
184         put(1, 1);
185       }
186     }).apply(null), is(none()));
187   }
188 
189   @Test public void forMapWithNoValue() {
190     assertThat(forMap(new HashMap<Integer, Integer>() {
191       {
192         put(1, 1);
193       }
194     }).apply(55), is(none()));
195   }
196 }