|
| 1 | +import 'package:relic/src/util/functional.dart'; |
| 2 | +import 'package:test/test.dart'; |
| 3 | + |
| 4 | +void main() { |
| 5 | + group('Pipe Extension', () { |
| 6 | + test( |
| 7 | + 'Given a value and a function, ' |
| 8 | + 'when the pipe extension is used, ' |
| 9 | + 'then the function should be applied to the value', () { |
| 10 | + // Arrange |
| 11 | + const initialValue = 5; |
| 12 | + int addTwo(final int x) => x + 2; |
| 13 | + |
| 14 | + // Act |
| 15 | + final result = initialValue.pipe(addTwo); |
| 16 | + |
| 17 | + // Assert |
| 18 | + expect(result, equals(7)); |
| 19 | + }); |
| 20 | + |
| 21 | + test( |
| 22 | + 'Given a string and a sequence of string operations, ' |
| 23 | + 'when piped together, ' |
| 24 | + 'then the correct final string is produced', () { |
| 25 | + // Arrange |
| 26 | + const initialValue = 'hello'; |
| 27 | + String toUpper(final String s) => s.toUpperCase(); |
| 28 | + String addWorld(final String s) => '$s WORLD'; |
| 29 | + String exclaim(final String s) => '$s!'; |
| 30 | + |
| 31 | + // Act |
| 32 | + final result = initialValue.pipe(toUpper).pipe(addWorld).pipe(exclaim); |
| 33 | + |
| 34 | + // Assert |
| 35 | + expect(result, equals('HELLO WORLD!')); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + group('Compose Extension', () { |
| 40 | + test( |
| 41 | + 'Given two functions, ' |
| 42 | + 'when they are composed, ' |
| 43 | + 'then the resulting function should be their composition', () { |
| 44 | + // Arrange |
| 45 | + int addTwo(final int x) => x + 2; |
| 46 | + int multiplyByThree(final int x) => x * 3; |
| 47 | + |
| 48 | + // Act |
| 49 | + final composedFunction = |
| 50 | + multiplyByThree.compose(addTwo); // multiplyByThree(addTwo(x)) |
| 51 | + final result = composedFunction( |
| 52 | + 5); // multiplyByThree(addTwo(5)) = multiplyByThree(7) = 21 |
| 53 | + |
| 54 | + // Assert |
| 55 | + expect(result, equals(21)); |
| 56 | + }); |
| 57 | + |
| 58 | + test( |
| 59 | + 'Given three functions for string manipulation, ' |
| 60 | + 'when composed sequentially, ' |
| 61 | + 'then the result is the correct transformation', () { |
| 62 | + // Arrange |
| 63 | + String toUpper(final String s) => s.toUpperCase(); |
| 64 | + String addSuffix(final String s) => '${s}_suffix'; |
| 65 | + String prefixWith(final String prefix, final String s) => '$prefix$s'; |
| 66 | + |
| 67 | + String addHelloPrefix(final String s) => prefixWith('hello_', s); |
| 68 | + |
| 69 | + // Act |
| 70 | + // Desired: hello_INPUT_suffix |
| 71 | + // addSuffix(toUpper(s)) = INPUT_suffix |
| 72 | + // addHelloPrefix(addSuffix(toUpper(s))) = hello_INPUT_suffix |
| 73 | + final composed = addHelloPrefix.compose(addSuffix.compose(toUpper)); |
| 74 | + final result = composed('input'); |
| 75 | + |
| 76 | + // Assert |
| 77 | + expect(result, equals('hello_INPUT_suffix')); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + group('Apply Extensions', () { |
| 82 | + group('Apply1', () { |
| 83 | + test( |
| 84 | + 'Given a function of one argument, ' |
| 85 | + 'when apply is used, ' |
| 86 | + 'then the function is called with the argument', () { |
| 87 | + // Arrange |
| 88 | + int func(final int a) => a * 2; |
| 89 | + const arg = 5; |
| 90 | + |
| 91 | + // Act |
| 92 | + final result = func.apply(arg); |
| 93 | + |
| 94 | + // Assert |
| 95 | + expect(result, equals(10)); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + group('Apply2', () { |
| 100 | + test( |
| 101 | + 'Given a function of two arguments and one argument, ' |
| 102 | + 'when apply is used, ' |
| 103 | + 'then it should return a function that takes the second argument', |
| 104 | + () { |
| 105 | + // Arrange |
| 106 | + int func(final int a, final int b) => a + b; |
| 107 | + const arg1 = 10; |
| 108 | + const arg2 = 5; |
| 109 | + |
| 110 | + // Act |
| 111 | + final partiallyApplied = func.apply(arg1); |
| 112 | + final result = partiallyApplied(arg2); |
| 113 | + |
| 114 | + // Assert |
| 115 | + expect(result, equals(15)); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + group('Apply3', () { |
| 120 | + test( |
| 121 | + 'Given a function of three arguments and one argument, ' |
| 122 | + 'when apply is used, ' |
| 123 | + 'then it should return a function that takes the remaining two arguments', |
| 124 | + () { |
| 125 | + // Arrange |
| 126 | + String func(final String a, final String b, final String c) => |
| 127 | + '$a $b $c'; |
| 128 | + const arg1 = 'Hello'; |
| 129 | + const arg2 = 'World'; |
| 130 | + const arg3 = '!'; |
| 131 | + |
| 132 | + // Act |
| 133 | + final partiallyApplied = func.apply(arg1); |
| 134 | + final result = partiallyApplied(arg2, arg3); |
| 135 | + |
| 136 | + // Assert |
| 137 | + expect(result, equals('Hello World !')); |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + group('Apply4', () { |
| 142 | + test( |
| 143 | + 'Given a function of four arguments and one argument, ' |
| 144 | + 'when apply is used, ' |
| 145 | + 'then it should return a function that takes the remaining three arguments', |
| 146 | + () { |
| 147 | + // Arrange |
| 148 | + int func(final int a, final int b, final int c, final int d) => |
| 149 | + a + b + c + d; |
| 150 | + const arg1 = 1; |
| 151 | + const arg2 = 2; |
| 152 | + const arg3 = 3; |
| 153 | + const arg4 = 4; |
| 154 | + |
| 155 | + // Act |
| 156 | + final partiallyApplied = func.apply(arg1); |
| 157 | + final result = partiallyApplied(arg2, arg3, arg4); |
| 158 | + |
| 159 | + // Assert |
| 160 | + expect(result, equals(10)); |
| 161 | + }); |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + group('Pack Extensions', () { |
| 166 | + group('Pack1', () { |
| 167 | + test( |
| 168 | + 'Given a function of one argument, ' |
| 169 | + 'when pack is used, ' |
| 170 | + 'then it should return a function that takes a 1-tuple', () { |
| 171 | + // Arrange |
| 172 | + int func(final int a) => a * 2; |
| 173 | + const tuple = (5,); |
| 174 | + |
| 175 | + // Act |
| 176 | + final packedFunc = func.pack; |
| 177 | + final result = packedFunc(tuple); |
| 178 | + |
| 179 | + // Assert |
| 180 | + expect(result, equals(10)); |
| 181 | + }); |
| 182 | + }); |
| 183 | + |
| 184 | + group('Pack2', () { |
| 185 | + test( |
| 186 | + 'Given a function of two arguments, ' |
| 187 | + 'when pack is used, ' |
| 188 | + 'then it should return a function that takes a 2-tuple', () { |
| 189 | + // Arrange |
| 190 | + int func(final int a, final int b) => a + b; |
| 191 | + const tuple = (10, 5); |
| 192 | + |
| 193 | + // Act |
| 194 | + final packedFunc = func.pack; |
| 195 | + final result = packedFunc(tuple); |
| 196 | + |
| 197 | + // Assert |
| 198 | + expect(result, equals(15)); |
| 199 | + }); |
| 200 | + }); |
| 201 | + |
| 202 | + group('Pack3', () { |
| 203 | + test( |
| 204 | + 'Given a function of three arguments, ' |
| 205 | + 'when pack is used, ' |
| 206 | + 'then it should return a function that takes a 3-tuple', () { |
| 207 | + // Arrange |
| 208 | + String func(final String a, final String b, final String c) => |
| 209 | + '$a $b $c'; |
| 210 | + const tuple = ('Hello', 'World', '!'); |
| 211 | + |
| 212 | + // Act |
| 213 | + final packedFunc = func.pack; |
| 214 | + final result = packedFunc(tuple); |
| 215 | + |
| 216 | + // Assert |
| 217 | + expect(result, equals('Hello World !')); |
| 218 | + }); |
| 219 | + }); |
| 220 | + |
| 221 | + group('Pack4', () { |
| 222 | + test( |
| 223 | + 'Given a function of four arguments, ' |
| 224 | + 'when pack is used, ' |
| 225 | + 'then it should return a function that takes a 4-tuple', () { |
| 226 | + // Arrange |
| 227 | + int func(final int a, final int b, final int c, final int d) => |
| 228 | + a + b + c + d; |
| 229 | + const tuple = (1, 2, 3, 4); |
| 230 | + |
| 231 | + // Act |
| 232 | + final packedFunc = func.pack; |
| 233 | + final result = packedFunc(tuple); |
| 234 | + |
| 235 | + // Assert |
| 236 | + expect(result, equals(10)); |
| 237 | + }); |
| 238 | + }); |
| 239 | + }); |
| 240 | +} |
0 commit comments