Skip to content

Commit

Permalink
add/modify tests which can act as a transducer (ramda#3269)
Browse files Browse the repository at this point in the history
  • Loading branch information
zydmayday authored Apr 21, 2022
1 parent d009984 commit a5aea90
Show file tree
Hide file tree
Showing 24 changed files with 123 additions and 78 deletions.
7 changes: 7 additions & 0 deletions test/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,11 @@ describe('all', function() {
});
});

it('can act as a transducer', function() {
var input = [2, 4, 6, 8, 9, 10];
var expected = [false];
eq(R.into([], R.all(even), input), expected);
eq(R.transduce(R.all(even), R.flip(R.append), [], input), expected);
});

});
7 changes: 7 additions & 0 deletions test/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,11 @@ describe('any', function() {
});
});

it('can act as a transducer', function() {
var input = [2, 4, 6, 8, 10];
var expected = [false];
eq(R.into([], R.any(odd), input), expected);
eq(R.transduce(R.any(odd), R.flip(R.append), [], input), expected);
});

});
4 changes: 3 additions & 1 deletion test/aperture.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ describe('aperture', function() {
});

it('can act as a transducer', function() {
eq(R.into([], R.aperture(2), sevenLs), [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7]]);
var expected = [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7]];
eq(R.into([], R.aperture(2), sevenLs), expected);
eq(R.transduce(R.aperture(2), R.flip(R.append), [], sevenLs), expected);
});

});
6 changes: 4 additions & 2 deletions test/countBy.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ describe('countBy', function() {
var transducer = R.compose(
R.countBy(R.prop('genre')),
R.map(R.adjust(1, R.toString)));
eq(R.into({}, transducer, albums), {
var expected = {
Baroque: '2', Rock: '2', Jazz: '2', Romantic: '1', Metal: '1', Modern: '1', Broadway: '1', Folk: '1', Classical: '1'
});
};
eq(R.into({}, transducer, albums), expected);
eq(R.transduce(transducer, (result, input) => {result[input[0]] = input[1]; return result;}, {}, albums), expected);
});

});
8 changes: 4 additions & 4 deletions test/dropLast.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ describe('dropLast', function() {

it('can act as a transducer', function() {
var dropLast2 = R.dropLast(2);
eq(R.into([], dropLast2, [1, 3, 5, 7, 9, 1, 2]), [1, 3, 5, 7, 9]);
eq(R.into([], dropLast2, [1]), []);
eq(R.into([], R.dropLast(0), [1, 3, 5]), [1, 3, 5]);
eq(R.into([], R.dropLast(-1), [1, 3, 5]), [1, 3, 5]);
var input = [1, 3, 5, 7, 9, 1, 2];
var expected = [1, 3, 5, 7, 9];
eq(R.into([], dropLast2, input), expected);
eq(R.transduce(dropLast2, R.flip(R.append), [], input), expected);
});

});
6 changes: 4 additions & 2 deletions test/dropLastWhile.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ describe('dropLastWhile', function() {

it('can act as a transducer', function() {
var dropLt7 = R.dropLastWhile(function(x) {return x < 7;});
eq(R.into([], dropLt7, [1, 3, 5, 7, 9, 1, 2]), [1, 3, 5, 7, 9]);
eq(R.into([], dropLt7, [1, 3, 5]), []);
var input = [1, 3, 5, 7, 9, 1, 2];
var expected = [1, 3, 5, 7, 9];
eq(R.into([], dropLt7, input), expected);
eq(R.transduce(dropLt7, R.flip(R.append), [], input), expected);
});

});
1 change: 1 addition & 0 deletions test/dropRepeats.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('dropRepeats', function() {

it('can act as a transducer', function() {
eq(R.into([], R.dropRepeats, objs2), objs);
eq(R.transduce(R.dropRepeats, R.flip(R.append), [], objs2), objs);
});

it('has R.equals semantics', function() {
Expand Down
1 change: 1 addition & 0 deletions test/dropRepeatsWith.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('dropRepeatsWith', function() {

it('can act as a transducer', function() {
eq(R.into([], R.dropRepeatsWith(eqI), objs2), objs);
eq(R.transduce(R.dropRepeatsWith(eqI), R.flip(R.append), [], objs2), objs);
});

});
8 changes: 8 additions & 0 deletions test/dropWhile.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ describe('dropWhile', function() {
eq(R.dropWhile(function(x) { return x !== 'd'; }, 'Ramda'), 'da');
});

it('can act as a transducer', function() {
var lteTwo = x => x <= 2;
var input = [1, 2, 3, 4, 3, 2, 1];
var expected = [3, 4, 3, 2, 1];
eq(R.into([], R.dropWhile(lteTwo), input), expected);
eq(R.transduce(R.dropWhile(lteTwo), R.flip(R.append), [], input), expected);
});

});
7 changes: 7 additions & 0 deletions test/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,11 @@ describe('filter', function() {
eq(m2.isNothing, true);
});

it('can act as a transducer', function() {
var input = [1, 2, 3, 4];
var expected = [2, 4];
eq(R.into([], R.filter(even), input), expected);
eq(R.transduce(R.filter(even), R.flip(R.append), [], input), expected);
});

});
22 changes: 6 additions & 16 deletions test/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ describe('find', function() {
var gt100 = function(x) { return x > 100; };
var isStr = function(x) { return typeof x === 'string'; };
var xGt100 = function(o) { return o && o.x > 100; };
var intoArray = R.into([]);

it('returns the first element that satisfies the predicate', function() {
eq(R.find(even, a), 10);
Expand All @@ -20,34 +19,25 @@ describe('find', function() {
eq(R.find(xGt100, a), obj2);
});

it('transduces the first element that satisfies the predicate into an array', function() {
eq(intoArray(R.find(even), a), [10]);
eq(intoArray(R.find(gt100), a), [200]);
eq(intoArray(R.find(isStr), a), ['cow']);
eq(intoArray(R.find(xGt100), a), [obj2]);
});

it('returns `undefined` when no element satisfies the predicate', function() {
eq(R.find(even, ['zing']), undefined);
});

it('returns `undefined` in array when no element satisfies the predicate into an array', function() {
eq(intoArray(R.find(even), ['zing']), [undefined]);
});

it('returns `undefined` when given an empty list', function() {
eq(R.find(even, []), undefined);
});

it('returns `undefined` into an array when given an empty list', function() {
eq(intoArray(R.find(even), []), [undefined]);
});

it('dispatches to transformer objects', function() {
eq(R.find(R.identity, listXf), {
f: R.identity,
found: false,
xf: listXf
});
});

it('can act as a transducer', function() {
eq(R.into([], R.find(even), a), [10]);
eq(R.transduce(R.find(even), R.flip(R.append), [], a), [10]);
});

});
18 changes: 6 additions & 12 deletions test/findIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ describe('findIndex', function() {
var gt100 = function(x) { return x > 100; };
var isStr = function(x) { return typeof x === 'string'; };
var xGt100 = function(o) { return o && o.x > 100; };
var intoArray = R.into([]);

it('returns the index of the first element that satisfies the predicate', function() {
eq(R.findIndex(even, a), 1);
Expand All @@ -20,22 +19,11 @@ describe('findIndex', function() {
eq(R.findIndex(xGt100, a), 10);
});

it('returns the index of the first element that satisfies the predicate into an array', function() {
eq(intoArray(R.findIndex(even), a), [1]);
eq(intoArray(R.findIndex(gt100), a), [8]);
eq(intoArray(R.findIndex(isStr), a), [3]);
eq(intoArray(R.findIndex(xGt100), a), [10]);
});

it('returns -1 when no element satisfies the predicate', function() {
eq(R.findIndex(even, ['zing']), -1);
eq(R.findIndex(even, []), -1);
});

it('returns -1 in array when no element satisfies the predicate into an array', function() {
eq(intoArray(R.findIndex(even), ['zing']), [-1]);
});

it('dispatches to transformer objects', function() {
eq(R.findIndex(R.identity, listXf), {
f: R.identity,
Expand All @@ -44,4 +32,10 @@ describe('findIndex', function() {
xf: listXf
});
});

it('can act as a transducer', function() {
eq(R.into([], R.findIndex(even), a), [1]);
eq(R.transduce(R.findIndex(even), R.flip(R.append), [], a), [1]);
});

});
18 changes: 6 additions & 12 deletions test/findLast.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ describe('findLast', function() {
var gt100 = function(x) { return x > 100; };
var isStr = function(x) { return typeof x === 'string'; };
var xGt100 = function(o) { return o && o.x > 100; };
var intoArray = R.into([]);

it('returns the index of the last element that satisfies the predicate', function() {
eq(R.findLast(even, a), 0);
Expand All @@ -20,21 +19,10 @@ describe('findLast', function() {
eq(R.findLast(xGt100, a), obj2);
});

it('returns the index of the last element that satisfies the predicate into an array', function() {
eq(intoArray(R.findLast(even), a), [0]);
eq(intoArray(R.findLast(gt100), a), [300]);
eq(intoArray(R.findLast(isStr), a), ['cow']);
eq(intoArray(R.findLast(xGt100), a), [obj2]);
});

it('returns `undefined` when no element satisfies the predicate', function() {
eq(R.findLast(even, ['zing']), undefined);
});

it('returns `undefined` into an array when no element satisfies the predicate', function() {
eq(intoArray(R.findLast(even), ['zing']), [undefined]);
});

it('works when the first element matches', function() {
eq(R.findLast(even, [2, 3, 5]), 2);
});
Expand All @@ -49,4 +37,10 @@ describe('findLast', function() {
xf: listXf
});
});

it('can act as a transducer', function() {
eq(R.into([], R.findLast(even), a), [0]);
eq(R.transduce(R.findLast(even), R.flip(R.append), [], a), [0]);
});

});
18 changes: 6 additions & 12 deletions test/findLastIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ describe('findLastIndex', function() {
var gt100 = function(x) { return x > 100; };
var isStr = function(x) { return typeof x === 'string'; };
var xGt100 = function(o) { return o && o.x > 100; };
var intoArray = R.into([]);

it('returns the index of the last element that satisfies the predicate', function() {
eq(R.findLastIndex(even, a), 15);
Expand All @@ -24,17 +23,6 @@ describe('findLastIndex', function() {
eq(R.findLastIndex(even, ['zing']), -1);
});

it('returns the index of the last element into an array that satisfies the predicate', function() {
eq(intoArray(R.findLastIndex(even), a), [15]);
eq(intoArray(R.findLastIndex(gt100), a), [9]);
eq(intoArray(R.findLastIndex(isStr), a), [3]);
eq(intoArray(R.findLastIndex(xGt100), a), [10]);
});

it('returns -1 into an array when no element satisfies the predicate', function() {
eq(intoArray(R.findLastIndex(even), ['zing']), [-1]);
});

it('works when the first element matches', function() {
eq(R.findLastIndex(even, [2, 3, 5]), 0);
});
Expand All @@ -51,4 +39,10 @@ describe('findLastIndex', function() {
xf: listXf
});
});

it('can act as a transducer', function() {
eq(R.into([], R.findLastIndex(even), a), [15]);
eq(R.transduce(R.findLastIndex(even), R.flip(R.append), [], a), [15]);
});

});
7 changes: 4 additions & 3 deletions test/groupBy.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ describe('groupBy', function() {
eq(_isTransformer(R.groupBy(byType, xf)), true);
});

it('works with R.into', function() {
it('can act as a transducer', function() {
var evenOdd = x => x % 2 === 0 ? 'even' : 'odd';
var result = R.into({}, R.groupBy(evenOdd), R.range(1, 10));
eq(result, {even: [2, 4, 6, 8], odd: [1, 3, 5, 7, 9]});
var expected = {even: [2, 4, 6, 8], odd: [1, 3, 5, 7, 9]};
eq(R.into({}, R.groupBy(evenOdd), R.range(1, 10)), expected);
eq(R.transduce(R.groupBy(evenOdd), (result, input) => {result[input[0]] = input[1]; return result;}, {}, R.range(1, 10)), expected);
});

});
5 changes: 3 additions & 2 deletions test/indexBy.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ describe('indexBy', function() {
R.adjust(0, R.toUpper),
R.adjust(1, R.omit(['id']))
)));
var result = R.into({}, transducer, list);
eq(result, {ABC: {title: 'B'}, XYZ: {title: 'A'}});
var expected = {ABC: {title: 'B'}, XYZ: {title: 'A'}};
eq(R.into({}, transducer, list), expected);
eq(R.transduce(transducer, (result, input) => {result[input[0]] = input[1]; return result;}, {}, list), expected);
});

});
10 changes: 5 additions & 5 deletions test/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,11 @@ describe('map', function() {
var times2 = function(x) {return x * 2;};
var add1 = function(x) {return x + 1;};
var dec = function(x) { return x - 1; };
var intoArray = R.into([]);

it('maps simple functions over arrays', function() {
eq(R.map(times2, [1, 2, 3, 4]), [2, 4, 6, 8]);
});

it('maps simple functions into arrays', function() {
eq(intoArray(R.map(times2), [1, 2, 3, 4]), [2, 4, 6, 8]);
});

it('maps over objects', function() {
eq(R.map(dec, {}), {});
eq(R.map(dec, {x: 4, y: 5, z: 6}), {x: 3, y: 4, z: 5});
Expand Down Expand Up @@ -70,4 +65,9 @@ describe('map', function() {
eq(m1.value + 1, m2.value);
});

it('can act as a transducer', function() {
eq(R.into([], R.map(times2), [1, 2, 3, 4]), [2, 4, 6, 8]);
eq(R.transduce(R.map(times2), R.flip(R.append), [], [1, 2, 3, 4]), [2, 4, 6, 8]);
});

});
12 changes: 5 additions & 7 deletions test/none.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var eq = require('./shared/eq.js');
describe('none', function() {
var even = function(n) {return n % 2 === 0;};
var T = function() {return true;};
var intoArray = R.into([]);

it('returns true if no elements satisfy the predicate', function() {
eq(R.none(even, [1, 3, 5, 7, 9, 11]), true);
Expand All @@ -19,12 +18,6 @@ describe('none', function() {
eq(R.none(T, []), true);
});

it('returns into array', function() {
eq(intoArray(R.none(even), [1, 3, 5, 7, 9, 11]), [true]);
eq(intoArray(R.none(even), [1, 3, 5, 7, 8, 11]), [false]);
eq(intoArray(R.none(T), []), [true]);
});

it('works with more complex objects', function() {
var xs = [{x: 'abcd'}, {x: 'adef'}, {x: 'fghiajk'}];
function len3(o) { return o.x.length === 3; }
Expand All @@ -33,4 +26,9 @@ describe('none', function() {
eq(R.none(hasA, xs), false);
});

it('can act as a transducer', function() {
eq(R.into([], R.none(even), [1, 3, 5, 7, 9, 11]), [true]);
eq(R.transduce(R.none(even), R.flip(R.append), [], [1, 3, 5, 7, 9, 11]), [true]);
});

});
6 changes: 6 additions & 0 deletions test/reduceBy.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ describe('reduceBy', function() {
R.compose(sumByType, R.map(R.adjust(1, R.multiply(10)))),
sumInput
), {A: 800, B: 800, C: 500});
eq(R.transduce(
R.compose(sumByType, R.map(R.adjust(1, R.multiply(10)))),
(result, input) => {result[input[0]] = result[input[0]] ? result[input[0]] : 0 + input[1]; return result;},
{},
sumInput
), {A: 800, B: 800, C: 500});
});

it('short circuits with reduced', function() {
Expand Down
7 changes: 7 additions & 0 deletions test/reject.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@ describe('reject', function() {
eq(R.reject(R.F, m), m);
});

it('can act as a transducer', function() {
var input = [1, 2, 3, 4];
var expected = [1, 3];
eq(R.into([], R.reject(even), input), expected);
eq(R.transduce(R.reject(even), R.flip(R.append), [], input), expected);
});

});
Loading

0 comments on commit a5aea90

Please sign in to comment.