|
| 1 | +package fj.data; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import static fj.data.Validation.*; |
| 6 | +import static org.hamcrest.core.Is.is; |
| 7 | +import static org.junit.Assert.assertThat; |
| 8 | +import fj.*; |
| 9 | + |
| 10 | +public class ValidationTest { |
| 11 | + @Test |
| 12 | + public void testParseShort() { |
| 13 | + final List<Validation<NumberFormatException, Short>> l = |
| 14 | + List.list(parseShort("10"), parseShort("x"), parseShort("20")); |
| 15 | + assertThat(successes(l).foldLeft1((s, a) -> (short)(s + a)), is((short)30)); |
| 16 | + } |
| 17 | + |
| 18 | + @Test |
| 19 | + public void testParseLong() { |
| 20 | + final List<Validation<NumberFormatException, Long>> l = |
| 21 | + List.list(parseLong("10"), parseLong("x"), parseLong("20")); |
| 22 | + P2<List<NumberFormatException>, List<Long>> p2 = partition(l); |
| 23 | + assertThat(p2._1().length(), is(1)); |
| 24 | + assertThat(p2._2().length(), is(2)); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void testParseInt() { |
| 29 | + final List<Validation<NumberFormatException, Integer>> l = |
| 30 | + List.list(parseInt("10"), parseInt("x"), parseInt("20")); |
| 31 | + assertThat(l.map(v -> v.validation(e -> 0, i -> 1)).foldLeft1((s, a) -> s + a), |
| 32 | + is(2)); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testParseFloat() { |
| 37 | + final List<Validation<NumberFormatException, Float>> l = |
| 38 | + List.list(parseFloat("2.0"), parseFloat("x"), parseFloat("3.0")); |
| 39 | + assertThat(l.map(v -> v.validation(e -> 0, i -> 1)).foldLeft1((s, a) -> s + a), |
| 40 | + is(2)); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testParseByte() { |
| 45 | + final List<Validation<NumberFormatException, Byte>> l = |
| 46 | + List.list(parseByte("10"), parseByte("x"), parseByte("-10")); |
| 47 | + assertThat(l.map(v -> v.validation(e -> 0, i -> 1)).foldLeft1((s, a) -> s + a), |
| 48 | + is(2)); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testAccumulate1() { |
| 53 | + final Validation<List<NumberFormatException>, Double> v = |
| 54 | + parseDouble("10.0").accumulate( |
| 55 | + f1 -> f1); |
| 56 | + assertThat(v.success(), is(10.0)); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testAccumulate1Fail() { |
| 61 | + final Validation<List<NumberFormatException>, Double> v = |
| 62 | + parseDouble("x").accumulate( |
| 63 | + f1 -> f1); |
| 64 | + assertThat(v.fail().length(), is(1)); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testAccumulate2() { |
| 69 | + final Validation<List<NumberFormatException>, Double> v = |
| 70 | + parseDouble("1.0").accumulate( |
| 71 | + parseDouble("2.0"), |
| 72 | + (f1, f2) -> f1 + f2); |
| 73 | + assertThat(v.success(), is(3.0)); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testAccumulate2Fail() { |
| 78 | + final Validation<List<NumberFormatException>, Double> v = |
| 79 | + parseDouble("x").accumulate( |
| 80 | + parseDouble("y"), |
| 81 | + (f1, f2) -> f1 + f2); |
| 82 | + assertThat(v.fail().length(), is(2)); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testAccumulate3() { |
| 87 | + final Validation<List<NumberFormatException>, Double> v = |
| 88 | + parseDouble("1.0").accumulate( |
| 89 | + parseDouble("2.0"), |
| 90 | + parseDouble("3.0"), |
| 91 | + (f1, f2, f3) -> f1 + f2 + f3); |
| 92 | + assertThat(v.success(), is(6.0)); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testAccumulate3Fail() { |
| 97 | + final Validation<List<NumberFormatException>, Double> v = |
| 98 | + parseDouble("x").accumulate( |
| 99 | + parseDouble("2.0"), |
| 100 | + parseDouble("y"), |
| 101 | + (f1, f2, f3) -> f1 + f2 + f3); |
| 102 | + assertThat(v.fail().length(), is(2)); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testAccumulate4() { |
| 107 | + final Validation<List<NumberFormatException>, Double> v = |
| 108 | + parseDouble("1.0").accumulate( |
| 109 | + parseDouble("2.0"), |
| 110 | + parseDouble("3.0"), |
| 111 | + parseDouble("4.0"), |
| 112 | + (f1, f2, f3, f4) -> f1 + f2 + f3 + f4); |
| 113 | + assertThat(v.success(), is(10.0)); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testAccumulate4Fail() { |
| 118 | + final Validation<List<NumberFormatException>, Double> v = |
| 119 | + parseDouble("x").accumulate( |
| 120 | + parseDouble("2.0"), |
| 121 | + parseDouble("3.0"), |
| 122 | + parseDouble("y"), |
| 123 | + (f1, f2, f3, f4) -> f1 + f2 + f3 + f4); |
| 124 | + assertThat(v.fail().length(), is(2)); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void testAccumulate5() { |
| 129 | + final Validation<List<NumberFormatException>, Double> v = |
| 130 | + parseDouble("1.0").accumulate( |
| 131 | + parseDouble("2.0"), |
| 132 | + parseDouble("3.0"), |
| 133 | + parseDouble("4.0"), |
| 134 | + parseDouble("5.0"), |
| 135 | + (f1, f2, f3, f4, f5) -> f1 + f2 + f3 + f4 + f5); |
| 136 | + assertThat(v.success(), is(15.0)); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void testAccumulate5Fail() { |
| 141 | + final Validation<List<NumberFormatException>, Double> v = |
| 142 | + parseDouble("x").accumulate( |
| 143 | + parseDouble("2.0"), |
| 144 | + parseDouble("3.0"), |
| 145 | + parseDouble("4.0"), |
| 146 | + parseDouble("y"), |
| 147 | + (f1, f2, f3, f4, f5) -> f1 + f2 + f3 + f4 + f5); |
| 148 | + assertThat(v.fail().length(), is(2)); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testAccumulate6() { |
| 153 | + final Validation<List<NumberFormatException>, Double> v = |
| 154 | + parseDouble("1.0").accumulate( |
| 155 | + parseDouble("2.0"), |
| 156 | + parseDouble("3.0"), |
| 157 | + parseDouble("4.0"), |
| 158 | + parseDouble("5.0"), |
| 159 | + parseDouble("6.0"), |
| 160 | + (f1, f2, f3, f4, f5, f6) -> f1 + f2 + f3 + f4 + f5 + f6); |
| 161 | + assertThat(v.success(), is(21.0)); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void testAccumulate6Fail() { |
| 166 | + final Validation<List<NumberFormatException>, Double> v = |
| 167 | + parseDouble("x").accumulate( |
| 168 | + parseDouble("2.0"), |
| 169 | + parseDouble("3.0"), |
| 170 | + parseDouble("4.0"), |
| 171 | + parseDouble("5.0"), |
| 172 | + parseDouble("y"), |
| 173 | + (f1, f2, f3, f4, f5, f6) -> f1 + f2 + f3 + f4 + f5 + f6); |
| 174 | + assertThat(v.fail().length(), is(2)); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void testAccumulate7() { |
| 179 | + final Validation<List<NumberFormatException>, Double> v = |
| 180 | + parseDouble("1.0").accumulate( |
| 181 | + parseDouble("2.0"), |
| 182 | + parseDouble("3.0"), |
| 183 | + parseDouble("4.0"), |
| 184 | + parseDouble("5.0"), |
| 185 | + parseDouble("6.0"), |
| 186 | + parseDouble("7.0"), |
| 187 | + (f1, f2, f3, f4, f5, f6, f7) -> f1 + f2 + f3 + f4 + f5 + f6 + f7); |
| 188 | + assertThat(v.success(), is(28.0)); |
| 189 | + } |
| 190 | + |
| 191 | + @Test |
| 192 | + public void testAccumulate7Fail() { |
| 193 | + final Validation<List<NumberFormatException>, Double> v = |
| 194 | + parseDouble("x").accumulate( |
| 195 | + parseDouble("2.0"), |
| 196 | + parseDouble("3.0"), |
| 197 | + parseDouble("4.0"), |
| 198 | + parseDouble("5.0"), |
| 199 | + parseDouble("6.0"), |
| 200 | + parseDouble("y"), |
| 201 | + (f1, f2, f3, f4, f5, f6, f7) -> f1 + f2 + f3 + f4 + f5 + f6 + f7); |
| 202 | + assertThat(v.fail().length(), is(2)); |
| 203 | + } |
| 204 | + |
| 205 | + @Test |
| 206 | + public void testAccumulate8() { |
| 207 | + final Validation<List<NumberFormatException>, Double> v = |
| 208 | + parseDouble("1.0").accumulate( |
| 209 | + parseDouble("2.0"), |
| 210 | + parseDouble("3.0"), |
| 211 | + parseDouble("4.0"), |
| 212 | + parseDouble("5.0"), |
| 213 | + parseDouble("6.0"), |
| 214 | + parseDouble("7.0"), |
| 215 | + parseDouble("8.0"), |
| 216 | + (f1, f2, f3, f4, f5, f6, f7, f8) -> f1 + f2 + f3 + f4 + f5 + f6 + f7 + f8); |
| 217 | + assertThat(v.success(), is(36.0)); |
| 218 | + } |
| 219 | + |
| 220 | + @Test |
| 221 | + public void testAccumulate8Fail() { |
| 222 | + final Validation<List<NumberFormatException>, Double> v = |
| 223 | + parseDouble("x").accumulate( |
| 224 | + parseDouble("2.0"), |
| 225 | + parseDouble("3.0"), |
| 226 | + parseDouble("4.0"), |
| 227 | + parseDouble("5.0"), |
| 228 | + parseDouble("6.0"), |
| 229 | + parseDouble("7.0"), |
| 230 | + parseDouble("y"), |
| 231 | + (f1, f2, f3, f4, f5, f6, f7, f8) -> f1 + f2 + f3 + f4 + f5 + f6 + f7 + f8); |
| 232 | + assertThat(v.fail().length(), is(2)); |
| 233 | + } |
| 234 | + |
| 235 | + @Test(expected = Error.class) |
| 236 | + public void testSuccess() { |
| 237 | + parseShort("x").success(); |
| 238 | + } |
| 239 | + |
| 240 | + @Test(expected = Error.class) |
| 241 | + public void testFail() { |
| 242 | + parseShort("12").fail(); |
| 243 | + } |
| 244 | +} |
0 commit comments