Skip to content

Commit

Permalink
Update TypeClass.test to respect constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
Avaq authored and davidchambers committed Apr 21, 2021
1 parent 52c06e6 commit b5b7fe5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
38 changes: 29 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
var Maybe = __doctest.require ('sanctuary-maybe');
var Pair = __doctest.require ('sanctuary-pair');
var Sum = __doctest.require ('./test/Sum');
var Useless = __doctest.require ('sanctuary-useless');

var Nil = List.Nil, Cons = List.Cons;
var Nothing = Maybe.Nothing, Just = Maybe.Just;
Expand Down Expand Up @@ -294,7 +295,7 @@
(function() {
var $seen = [];
return function(x) {
// if ($seen.includes (x)) return true;
if ($seen.includes (x)) return true;

$seen.push (x);
try {
Expand Down Expand Up @@ -334,6 +335,15 @@
//. ```javascript
//. > Setoid.test (null)
//. true
//.
//. > Setoid.test (Useless)
//. false
//.
//. > Setoid.test ([1, 2, 3])
//. true
//.
//. > Setoid.test ([Useless])
//. false
//. ```
var Setoid = $ ('Setoid', [], {equals: Value});

Expand All @@ -347,6 +357,12 @@
//.
//. > Ord.test (Math.sqrt)
//. false
//.
//. > Ord.test ([1, 2, 3])
//. true
//.
//. > Ord.test ([Math.sqrt])
//. false
//. ```
var Ord = $ ('Ord', [Setoid], {lte: Value});

Expand Down Expand Up @@ -768,7 +784,7 @@
return [];
}

// Array$prototype$equals :: Array a ~> Array a -> Boolean
// Array$prototype$equals :: Setoid a => Array a ~> Array a -> Boolean
function Array$prototype$equals(other) {
if (other.length !== this.length) return false;
for (var idx = 0; idx < this.length; idx += 1) {
Expand All @@ -777,7 +793,7 @@
return true;
}

// Array$prototype$lte :: Array a ~> Array a -> Boolean
// Array$prototype$lte :: Ord a => Array a ~> Array a -> Boolean
function Array$prototype$lte(other) {
for (var idx = 0; true; idx += 1) {
if (idx === this.length) return true;
Expand Down Expand Up @@ -883,15 +899,15 @@
return {};
}

// Object$prototype$equals :: StrMap a ~> StrMap a -> Boolean
// Object$prototype$equals :: Setoid a => StrMap a ~> StrMap a -> Boolean
function Object$prototype$equals(other) {
var self = this;
var keys = sortedKeys (this);
return equals (keys, sortedKeys (other)) &&
keys.every (function(k) { return equals (self[k], other[k]); });
}

// Object$prototype$lte :: StrMap a ~> StrMap a -> Boolean
// Object$prototype$lte :: Ord a => StrMap a ~> StrMap a -> Boolean
function Object$prototype$lte(other) {
var theseKeys = sortedKeys (this);
var otherKeys = sortedKeys (other);
Expand Down Expand Up @@ -1092,9 +1108,9 @@
case 'String#fantasy-land/concat':
return String$prototype$concat;
case 'Array#fantasy-land/equals':
return Array$prototype$equals;
return value.every (Setoid.test) ? Array$prototype$equals : null;
case 'Array#fantasy-land/lte':
return Array$prototype$lte;
return value.every (Ord.test) ? Array$prototype$lte : null;
case 'Array#fantasy-land/concat':
return Array$prototype$concat;
case 'Array#fantasy-land/filter':
Expand All @@ -1120,9 +1136,13 @@
case 'Error#fantasy-land/equals':
return Error$prototype$equals;
case 'Object#fantasy-land/equals':
return Object$prototype$equals;
return (Object.values (value)).every (Setoid.test) ?
Object$prototype$equals :
null;
case 'Object#fantasy-land/lte':
return Object$prototype$lte;
return (Object.values (value)).every (Ord.test) ?
Object$prototype$lte :
null;
case 'Object#fantasy-land/concat':
return Object$prototype$concat;
case 'Object#fantasy-land/filter':
Expand Down
4 changes: 4 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ test ('Setoid', function() {
eq (Z.Setoid.test ([]), true);
eq (Z.Setoid.test ({}), true);
eq (Z.Setoid.test (Useless), false);
eq (Z.Setoid.test ([Useless]), false);
eq (Z.Setoid.test ({foo: Useless}), false);
});

test ('Ord', function() {
Expand All @@ -334,6 +336,8 @@ test ('Ord', function() {
eq (Z.Ord.test ([]), true);
eq (Z.Ord.test ({}), true);
eq (Z.Ord.test (Math.abs), false);
eq (Z.Ord.test ([Math.abs]), false);
eq (Z.Ord.test ({foo: Math.abs}), false);
});

test ('Semigroupoid', function() {
Expand Down

0 comments on commit b5b7fe5

Please sign in to comment.