-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for head, last, and tail, fix string handling for head and …
…last
- Loading branch information
Showing
6 changed files
with
134 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const | ||
{ head } = require('portable-fp'), | ||
{ sparseList } = require('./util'), | ||
{ expect } = require('chai'); | ||
|
||
describe('head :: [a] → a', function() { | ||
|
||
it('returns the first element in a list', function() { | ||
expect(head([])).eql(undefined); | ||
expect(head(['a'])).eql('a'); | ||
expect(head(sparseList)).eql(sparseList[0]); | ||
}); | ||
|
||
it('returns the first character of a string', function() { | ||
expect(head('')).eql(''); | ||
expect(head('a')).eql('a'); | ||
expect(head('ab')).eql('a'); | ||
}); | ||
|
||
it('returns the first value of an arguments object', function() { | ||
function fn() {return arguments}; | ||
expect(head(fn())).eql(undefined); | ||
expect(head(fn(1))).eql(1); | ||
expect(head(fn(1, 2))).eql(1); | ||
}); | ||
|
||
it('throws error when given null or undefined', function() { | ||
const run = arg => () => head(arg); | ||
expect(run(null)).throw(TypeError); | ||
expect(run(undefined)).throw(TypeError); | ||
}); | ||
|
||
it('returns empty array on invalid defined input', function() { | ||
const args = [true, false, {}, head, x => x, /x/, String]; | ||
args.map(arg => expect(head(arg)).eql(undefined)); | ||
}); | ||
|
||
it('has arity of 1', () => expect(head).lengthOf(1)); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const | ||
{ last } = require('portable-fp'), | ||
{ sparseList } = require('./util'), | ||
{ expect } = require('chai'); | ||
|
||
describe('last :: [a] → a', function() { | ||
|
||
it('returns the last element in a list', function() { | ||
expect(last([])).eql(undefined); | ||
expect(last(['a'])).eql('a'); | ||
expect(last(sparseList)).eql(sparseList[sparseList.length-1]); | ||
}); | ||
|
||
it('returns the last character of a string', function() { | ||
expect(last('')).eql(''); | ||
expect(last('a')).eql('a'); | ||
expect(last('ab')).eql('b'); | ||
}); | ||
|
||
it('returns the last value of an arguments object', function() { | ||
function fn() {return arguments}; | ||
expect(last(fn())).eql(undefined); | ||
expect(last(fn(1))).eql(1); | ||
expect(last(fn(1, 2))).eql(2); | ||
expect(last(fn(1, 2, undefined))).eql(undefined); | ||
}); | ||
|
||
it('throws error when given null or undefined', function() { | ||
const run = arg => () => last(arg); | ||
expect(run(null)).throw(TypeError); | ||
expect(run(undefined)).throw(TypeError); | ||
}); | ||
|
||
it('returns empty array on invalid defined input', function() { | ||
const args = [true, false, {}, last, x => x, /x/, String]; | ||
args.map(arg => expect(last(arg)).eql(undefined)); | ||
}); | ||
|
||
it('has arity of 1', () => expect(last).lengthOf(1)); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const | ||
{ tail } = require('portable-fp'), | ||
{ testCurrying } = require('./util'), | ||
{ expect } = require('chai'); | ||
|
||
describe('tail :: [a] → [a]', function() { | ||
|
||
it('returns all but the first element of a list', function() { | ||
const arg = [432, undefined, null, 2.5, 'blah', 0, -5]; | ||
expect(tail([])).eql([]); | ||
expect(tail(['a'])).eql([]); | ||
expect(tail(['a', 'b'])).eql(['b']); | ||
expect(tail(arg)).eql(arg.slice(1)); | ||
}); | ||
|
||
it('returns all but the first character of a string', function() { | ||
const arg = 'blah blah'; | ||
expect(tail('')).eql(''); | ||
expect(tail('a')).eql(''); | ||
expect(tail('ab')).eql('b'); | ||
expect(tail(arg)).eql(arg.slice(1)); | ||
}); | ||
|
||
it('returns all but the first value of an arguments object', function() { | ||
function fn() {return arguments}; | ||
expect(tail(fn())).eql([]); | ||
expect(tail(fn(1))).eql([]); | ||
expect(tail(fn(1, 2))).eql([2]); | ||
expect(tail(fn(1, 2, 3))).eql([2, 3]); | ||
}); | ||
|
||
// Known divergence from Ramda behavior (test would fail): | ||
// - expect(tail(String)).eql(''); // [] | ||
// Instead, portable-fp is consistent with head and last | ||
|
||
it('throws error when given null or undefined', function() { | ||
const run = arg => () => tail(arg); | ||
expect(run(null)).throw(TypeError); | ||
expect(run(undefined)).throw(TypeError); | ||
}); | ||
|
||
it('returns empty array on (some) invalid defined input', function() { | ||
const args = [NaN, true, false, {}, tail, x => x, /x/]; | ||
args.map(arg => expect(tail(arg)).an('array').eql([])); | ||
}); | ||
|
||
it('has arity of 1', () => expect(tail).lengthOf(1)); | ||
|
||
}); |