Skip to content

Commit

Permalink
test(Pair): add tests for Pair
Browse files Browse the repository at this point in the history
  • Loading branch information
safareli committed Oct 11, 2016
1 parent a451348 commit c776ace
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
15 changes: 12 additions & 3 deletions examples/Pair.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ Pair.prototype.equals = function(b) {

// instance (Semigroup a, Semigroup b) => Semigroup (Pair a b) where
// concat :: Pair a b ~> Pair a b -> Pair a b
Pair.prototype.concat = function(b) {
Pair.prototype.concat = function(x) {
const b = Q.foldIfIsOf(Pair[fl.of], x)
if (Q.isEmpty(this._1) || Q.isEmpty(this._2)) {
return b
} else if (Q.isEmpty(b) || Q.isEmpty(b._1) || Q.isEmpty(b._2)) {
return Pair(this._1[fl.concat](b._1), this._2[fl.concat](b._2))
} else if (Q.isEmpty(b) || (Q.isEmpty(b._1) && Q.isEmpty(b._2))) {
return this
} else if (Q.isEmpty(b._1)) {
return Pair(this._1, this._2[fl.concat](b._2))
} else if (Q.isEmpty(b._2)) {
return Pair(this._1[fl.concat](b._1), this._2)
} else {
return Pair(this._1[fl.concat](b._1), this._2[fl.concat](b._2))
}
Expand Down Expand Up @@ -48,6 +53,10 @@ Pair.prototype.ap = function(f) {
// of :: a -> Pair a
Pair.of = (a) => Pair(Q.empty, a)

Pair.prototype.toString = function() {
return 'Pair(' + this._1 + ',' + this._2 + ')'
}

flPatch([Pair, Pair.prototype])

module.exports = Pair
2 changes: 2 additions & 0 deletions test/List.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ const List = require('../examples/List.js')
test('eq', t => {
const a = 'a'
const as = List.fromArray([a])
const a2s = List.fromArray([a, a])

t.equals('Cons(a,Nil)', as.toString())
t.eqFL(as, as.concat(empty))
t.eqFL(a2s, as.concat(as))
t.notOk(List.fromArray([a, a]).equals(List.fromArray([a])))
t.notOk(List.fromArray([a]).equals(List.fromArray([a, a])))
t.eqFL(of(as), of(a).reduce((acc, a) => List.Cons(a, acc), List.Nil))
Expand Down
32 changes: 32 additions & 0 deletions test/Pair.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { test } = require('./lib')

const { of, empty } = require('../src/quasi.js')

const Pair = require('../examples/Pair.js')
const List = require('../examples/List.js')

test('eq', t => {
const a = 'a'
const as = List.fromArray([a])
const a2s = List.fromArray([a, a])
t.equals('Pair(a,a)', Pair(a, a).toString())
t.eqFL(Pair(as, as), Pair(empty, empty).concat(Pair(as, as)))
t.eqFL(Pair(as, as), Pair(as, as).concat(Pair(empty, empty)))
t.eqFL(Pair(a2s, as), Pair(as, empty).concat(Pair(as, as)))
t.eqFL(Pair(as, a2s), Pair(empty, as).concat(Pair(as, as)))
t.eqFL(Pair(as, as), Pair(as, as).concat(empty))
t.eqFL(Pair(as, as), empty.concat(Pair(as, as)))
t.eqFL(Pair(a2s, as), Pair(as, as).concat(Pair(as, empty)))
t.eqFL(Pair(as, a2s), Pair(as, as).concat(Pair(empty, as)))
t.eqFL(Pair(as, a2s), Pair(as, as).concat(of(as)))
t.eqFL(Pair(as, as), Pair(as, as).concat(Pair(empty, empty)))
t.eqFL(Pair(a2s, a2s), Pair(as, as).concat(Pair(as, as)))
t.eqFL(Pair(a2s, as), Pair(as, as).concat(Pair(as, empty)))
t.eqFL(Pair(as, a2s), Pair(as, as).concat(of(as)))
t.eqFL(Pair(as, a2s), of(as).concat(Pair(as, as)))
t.eqFL(Pair(as, as), Pair(as, as).ap(Pair.of(as => as)))
t.eqFL(Pair(as, as), Pair(as, as).ap(of(as => as)))
t.eqFL(Pair(a2s, as), Pair(as, as).ap(Pair(as, as => as)))
t.eqFL(Pair(as, a2s), Pair(as, as).map((as) => as.concat(as)))
t.end()
})

0 comments on commit c776ace

Please sign in to comment.