Skip to content

Commit

Permalink
Updated tests for find, findIndex, flatMap and flatMapWith
Browse files Browse the repository at this point in the history
  • Loading branch information
ascartabelli committed May 27, 2016
1 parent 6a4c953 commit c25994b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 19 deletions.
4 changes: 2 additions & 2 deletions dist/lamb.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @overview lamb - A lightweight, and docile, JavaScript library to help embracing functional programming.
* @author Andrea Scartabelli <andrea.scartabelli@gmail.com>
* @version 0.28.0-alpha.4
* @version 0.28.0-alpha.5
* @module lamb
* @license MIT
* @preserve
Expand All @@ -18,7 +18,7 @@
* @category Core
* @type String
*/
lamb._version = "0.28.0-alpha.4";
lamb._version = "0.28.0-alpha.5";

// alias used as a placeholder argument for partial application
var _ = lamb;
Expand Down
4 changes: 2 additions & 2 deletions dist/lamb.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/lamb.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"coveralls": "gulp coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
},
"tonicExample": "var _ = require('lamb');",
"version": "0.28.0-alpha.4",
"version": "0.28.0-alpha.5",
"devDependencies": {
"coveralls": "^2.11.9",
"gulp": "^3.9.1",
Expand Down
52 changes: 39 additions & 13 deletions test/spec/arraySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,26 +195,50 @@ describe("lamb.array", function () {
return "AEIOUaeiou".indexOf(char) !== -1;
};

var is40YO = lamb.hasKeyValue("age", 40);

describe("find", function () {
it("should find an element in an array-like object by using the given predicate", function () {
expect(lamb.find(persons, lamb.hasKeyValue("age", 40))).toEqual(persons[1]);
expect(lamb.find(persons, is40YO)).toEqual(persons[1]);
expect(lamb.find(testString, isVowel, fakeContext)).toBe("e");
});

it("should return `undefined` if there is no element satisfying the predicate", function () {
expect(lamb.find(persons, lamb.hasKeyValue("age", 41))).toBeUndefined();
});

it("should throw an exception if supplied with `null` or `undefined´ instead of an array-like", function () {
expect(function () { lamb.find(null, is40YO); }).toThrow();
expect(function () { lamb.find(void 0, is40YO); }).toThrow();
});

it("should treat every other value as an empty array and return `undefined`", function () {
[/foo/, 1, function () {}, NaN, true, new Date()].forEach(function (value) {
expect(lamb.find(value, is40YO)).toBeUndefined();
});
});
});

describe("findIndex", function () {
it("should find the index of an element in an array-like object by using the given predicate", function () {
expect(lamb.findIndex(persons, lamb.hasKeyValue("age", 40))).toBe(1);
expect(lamb.findIndex(persons, is40YO)).toBe(1);
expect(lamb.findIndex(testString, isVowel, fakeContext)).toBe(1);
});

it("should return `-1` if there is no element satisfying the predicate", function () {
expect(lamb.findIndex(persons, lamb.hasKeyValue("age", 41))).toBe(-1);
});

it("should throw an exception if supplied with `null` or `undefined´ instead of an array-like", function () {
expect(function () { lamb.findIndex(null, is40YO); }).toThrow();
expect(function () { lamb.findIndex(void 0, is40YO); }).toThrow();
});

it("should treat every other value as an empty array and return `-1`", function () {
[/foo/, 1, function () {}, NaN, true, new Date()].forEach(function (value) {
expect(lamb.findIndex(value, is40YO)).toBe(-1);
});
});
});
});

Expand Down Expand Up @@ -283,21 +307,23 @@ describe("lamb.array", function () {
expect(lamb.flatMapWith(toUpperCase)(testString)).toEqual(result);
});

it("should return an empty array if is supplied with a non-array value", function () {
expect(lamb.flatMap({}, lamb.identity)).toEqual([]);
expect(lamb.flatMapWith(lamb.identity)({})).toEqual([]);
it("should throw an error if not supplied with a mapper function", function () {
expect(function () {lamb.flatMap([1, 2, 3]);}).toThrow();
expect(function () {lamb.flatMapWith()([1, 2, 3]);}).toThrow();
});

it("should throw an error if called with null of undefined in place of the array", function () {
expect(function () {lamb.flatMap(null, lamb.identity);}).toThrow();
expect(lamb.flatMap).toThrow();
expect(function () {lamb.flatMapWith(lamb.identity)(null);}).toThrow();
expect(lamb.flatMapWith(lamb.identity)).toThrow();
it("should throw an exception if supplied with `null` or `undefined´ instead of an array-like", function () {
expect(function () { lamb.flatMap(null, lamb.identity); }).toThrow();
expect(function () { lamb.flatMap(void 0, lamb.identity); }).toThrow();
expect(function () { lamb.flatMapWith(lamb.identity)(null); }).toThrow();
expect(function () { lamb.flatMapWith(lamb.identity)(void 0); }).toThrow();
});

it("should throw an error if not supplied with a mapper function", function () {
expect(function () {lamb.flatMap([1, 2, 3]);}).toThrow();
expect(function () {lamb.flatMapWith()([1, 2, 3]);}).toThrow();
it("should return an empty array for every other value", function () {
[/foo/, 1, function () {}, NaN, true, new Date()].forEach(function (value) {
expect(lamb.flatMap(value, lamb.identity)).toEqual([]);
expect(lamb.flatMapWith(lamb.identity)(value)).toEqual([]);
});
});
});

Expand Down

0 comments on commit c25994b

Please sign in to comment.