Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to sanctuary-show for string representations #250

Merged
merged 2 commits into from
Jun 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
"dependencies": {
"concurrify": "^1.0.0",
"denque": "^1.1.1",
"inspect-f": "^1.2.0",
"sanctuary-type-classes": "^8.0.0",
"sanctuary-show": "^1.0.0",
"sanctuary-type-classes": "^9.0.0",
"sanctuary-type-identifiers": "^2.0.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var pkg = require('./package.json');
var dependencies = {
'concurrify': 'concurrify',
'denque': 'Denque',
'inspect-f': 'inspectf',
'sanctuary-show': 'sanctuaryShow',
'sanctuary-type-classes': 'sanctuaryTypeClasses',
'sanctuary-type-identifiers': 'sanctuaryTypeIdentifiers'
};
Expand Down
4 changes: 4 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export function isFuture(x){

Future['@@type'] = $$type;

Future.prototype['@@show'] = function Future$show(){
return this.toString();
};

Future.prototype[FL.ap] = function Future$FL$ap(other){
return other._ap(this);
};
Expand Down
7 changes: 3 additions & 4 deletions src/internal/fn.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import Z from 'sanctuary-type-classes';
import inspectf from 'inspect-f';
import show from 'sanctuary-show';

export {show};
export function noop(){}
export function moop(){ return this }
export var show = Z.toString;
export function padf(sf, s){ return s.replace(/^/gm, sf).replace(sf, '') }
export function showf(f){ return padf(' ', inspectf(2, f)) }
export function showf(f){ return padf(' ', show(f)) }

export function partial1(f, a){
return function bound1(b, c, d){
Expand Down
4 changes: 2 additions & 2 deletions test/0.error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('error', function (){
describe('invalidFuture', function (){

var mockType = function (identifier){
return {constructor: {'@@type': identifier}, toString: function (){
return {'constructor': {'@@type': identifier}, '@@show': function (){
return 'mockType("' + identifier + '")';
}};
};
Expand All @@ -67,7 +67,7 @@ describe('error', function (){
));
});

it('Warns us when nothing seems wrong', function (){
it('warns us when nothing seems wrong', function (){
var actual = invalidFuture('Foo', 0, mockType(namespace + '/' + name + '@' + version));
eq(actual, new TypeError(
'Foo expects its first argument to be a valid Future.\n' +
Expand Down
8 changes: 4 additions & 4 deletions test/5.both.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var testInstance = function (both){
return U.assertCrashed(both(F.crashedSlow, F.resolved), new Error(
'Error came up while interpreting a Future:\n' +
' Intentional error for unit testing\n\n' +
' In: Future.after(20, null).and(Future(function(){ throw new Error("Intentional error for unit testing") })).both(Future.of("resolved"))\n'
' In: Future.after(20, null).and(Future(function(){ throw new Error ("Intentional error for unit testing") })).both(Future.of("resolved"))\n'
));
});

Expand All @@ -51,7 +51,7 @@ var testInstance = function (both){
'Error came up while interpreting a Future:\n' +
' Intentional error for unit testing\n\n' +
' In: Future.after(20, null)' +
'.and(Future(function(){ throw new Error("Intentional error for unit testing") }))' +
'.and(Future(function(){ throw new Error ("Intentional error for unit testing") }))' +
'.map(function Resolved$both$mapper(right){\n return [left, right];\n })\n'
));
});
Expand All @@ -61,7 +61,7 @@ var testInstance = function (both){
'Error came up while interpreting a Future:\n' +
' Intentional error for unit testing\n\n' +
' In: Future.after(20, "resolvedSlow")' +
'.both(Future(function(){ throw new Error("Intentional error for unit testing") }))\n'
'.both(Future(function(){ throw new Error ("Intentional error for unit testing") }))\n'
));
});

Expand All @@ -78,7 +78,7 @@ var testInstance = function (both){
'Error came up while interpreting a Future:\n' +
' Intentional error for unit testing\n\n' +
' In: Future.rejectAfter(20, "rejectedSlow")' +
'.both(Future(function(){ throw new Error("Intentional error for unit testing") }))\n'
'.both(Future(function(){ throw new Error ("Intentional error for unit testing") }))\n'
));
});

Expand Down
8 changes: 4 additions & 4 deletions test/5.cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('cache()', function (){
return U.assertCrashed(cache(F.crashed), new Error(
'Error came up while Future.cache was running the cached Future:\n' +
' Intentional error for unit testing\n\n' +
' In: Future(function(){ throw new Error("Intentional error for unit testing") })\n'
' In: Future(function(){ throw new Error ("Intentional error for unit testing") })\n'
));
});

Expand All @@ -49,9 +49,9 @@ describe('cache()', function (){
' Intentional error for unit testing\n' +
' \n' +
' In: Future.after(20, null)' +
'.and(Future(function(){ throw new Error("Intentional error for unit testing") }))\n\n' +
'.and(Future(function(){ throw new Error ("Intentional error for unit testing") }))\n\n' +
' In: Future.after(20, null)' +
'.and(Future(function(){ throw new Error("Intentional error for unit testing") }))\n'
'.and(Future(function(){ throw new Error ("Intentional error for unit testing") }))\n'
);
var a = U.assertCrashed(m, e);
var b = U.assertCrashed(m, e);
Expand Down Expand Up @@ -81,7 +81,7 @@ describe('cache()', function (){
return U.assertCrashed(m, new Error(
'Error came up while Future.cache was running the cached Future:\n' +
' Intentional error for unit testing\n\n' +
' In: Future(function(){ throw new Error("Intentional error for unit testing") })\n'
' In: Future(function(){ throw new Error ("Intentional error for unit testing") })\n'
));
});

Expand Down
8 changes: 4 additions & 4 deletions test/5.parallel-ap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var testInstance = function (pap){
' Intentional error for unit testing\n\n' +
' In: Future.after(20, "resolvedSlow")' +
'.map(function (x){ return function (y){ return [x, y] } })' +
'._parallelAp(Future(function(){ throw new Error("Intentional error for unit testing") }))\n'
'._parallelAp(Future(function(){ throw new Error ("Intentional error for unit testing") }))\n'
));
});

Expand All @@ -32,7 +32,7 @@ var testInstance = function (pap){
' Intentional error for unit testing\n\n' +
' In: Future.of("resolved")' +
'.map(function (x){ return function (y){ return [x, y] } })' +
'._parallelAp(Future.after(20, null).and(Future(function(){ throw new Error("Intentional error for unit testing") })))\n'
'._parallelAp(Future.after(20, null).and(Future(function(){ throw new Error ("Intentional error for unit testing") })))\n'
));
});

Expand All @@ -46,7 +46,7 @@ var testInstance = function (pap){
' Intentional error for unit testing\n\n' +
' In: Future.rejectAfter(20, "rejectedSlow")' +
'.map(function (x){ return function (y){ return [x, y] } })' +
'._parallelAp(Future(function(){ throw new Error("Intentional error for unit testing") }))\n'
'._parallelAp(Future(function(){ throw new Error ("Intentional error for unit testing") }))\n'
));
});

Expand All @@ -63,7 +63,7 @@ var testInstance = function (pap){
'Error came up while interpreting a Future:\n' +
' Intentional error for unit testing\n\n' +
' In: Future.after(20, null)' +
'.and(Future(function(){ throw new Error("Intentional error for unit testing") }))' +
'.and(Future(function(){ throw new Error ("Intentional error for unit testing") }))' +
'.map(function (x){ return function (y){ return [x, y] } })' +
'._parallelAp(Future.of("resolved"))\n'
));
Expand Down
4 changes: 2 additions & 2 deletions test/5.parallel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ describe('Parallel', function (){
return U.assertCrashed(parallel(2, [F.resolved, F.crashed]), new Error(
'Error came up while Future.parallel was running the second future:\n' +
' Intentional error for unit testing\n\n' +
' In: Future(function(){ throw new Error("Intentional error for unit testing") })\n'
' In: Future(function(){ throw new Error ("Intentional error for unit testing") })\n'
));
});

it('crashes when one of the Futures crash', function (){
return U.assertCrashed(parallel(2, [F.resolved, F.resolved, F.resolved, F.resolved, F.resolved, F.crashed]), new Error(
'Error came up while Future.parallel was running future 6:\n' +
' Intentional error for unit testing\n\n' +
' In: Future(function(){ throw new Error("Intentional error for unit testing") })\n'
' In: Future(function(){ throw new Error ("Intentional error for unit testing") })\n'
));
});

Expand Down
4 changes: 2 additions & 2 deletions test/5.race.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var testInstance = function (race){
'Error came up while interpreting a Future:\n' +
' Intentional error for unit testing\n\n' +
' In: Future.after(20, "resolvedSlow")' +
'.race(Future(function(){ throw new Error("Intentional error for unit testing") }))\n'
'.race(Future(function(){ throw new Error ("Intentional error for unit testing") }))\n'
));
});

Expand All @@ -70,7 +70,7 @@ var testInstance = function (race){
'Error came up while interpreting a Future:\n' +
' Intentional error for unit testing\n\n' +
' In: Future.rejectAfter(20, "rejectedSlow")' +
'.race(Future(function(){ throw new Error("Intentional error for unit testing") }))\n'
'.race(Future(function(){ throw new Error ("Intentional error for unit testing") }))\n'
));
});

Expand Down
2 changes: 1 addition & 1 deletion test/6.go.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('Go', function (){
return U.assertCrashed(m, new Error(
'Error came up while Future.do was consuming a generated Future:\n' +
' Intentional error for unit testing\n\n' +
' In: Future(function(){ throw new Error("Intentional error for unit testing") })\n'
' In: Future(function(){ throw new Error ("Intentional error for unit testing") })\n'
));
});

Expand Down
45 changes: 23 additions & 22 deletions test/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {expect} from 'chai';
import {Future, isFuture} from '../index.mjs.js';
import show from 'sanctuary-show';
import Z from 'sanctuary-type-classes';
import {AssertionError, strictEqual} from 'assert';

Expand All @@ -15,7 +16,7 @@ export var throwit = function (it){ throw it };

export var eq = function eq (actual, expected){
strictEqual(arguments.length, eq.length);
strictEqual(Z.toString(actual), Z.toString(expected));
strictEqual(show(actual), show(expected));
strictEqual(Z.equals(actual, expected), true);
};

Expand Down Expand Up @@ -48,30 +49,30 @@ export var assertEqual = function (a, b){
if(astate === bstate && Z.equals(aval, bval)){ return true }
throw new Error(
'\n ' + (a.toString()) +
' :: Future({ <' + states[astate] + '> ' + Z.toString(aval) + ' })' +
' :: Future({ <' + states[astate] + '> ' + show(aval) + ' })' +
'\n does not equal:\n ' + b.toString() +
' :: Future({ <' + states[bstate] + '> ' + Z.toString(bval) + ' })\n '
' :: Future({ <' + states[bstate] + '> ' + show(bval) + ' })\n '
);
};

export var interpertAndGuard = function (m, rec, rej, res){
var rejected = false, resolved = false, crashed = false;
m._interpret(function (e){
if(crashed){ throw new Error(m.toString() + ' crashed twice with: ' + Z.toString(e)) }
if(rejected){ throw new Error(m.toString() + ' crashed after rejecting: ' + Z.toString(e)) }
if(resolved){ throw new Error(m.toString() + ' crashed after resolving: ' + Z.toString(e)) }
if(crashed){ throw new Error(m.toString() + ' crashed twice with: ' + show(e)) }
if(rejected){ throw new Error(m.toString() + ' crashed after rejecting: ' + show(e)) }
if(resolved){ throw new Error(m.toString() + ' crashed after resolving: ' + show(e)) }
crashed = true;
rec(e);
}, function (e){
if(crashed){ throw new Error(m.toString() + ' rejected after crashing: ' + Z.toString(e)) }
if(rejected){ throw new Error(m.toString() + ' rejected twice with: ' + Z.toString(e)) }
if(resolved){ throw new Error(m.toString() + ' rejected after resolving: ' + Z.toString(e)) }
if(crashed){ throw new Error(m.toString() + ' rejected after crashing: ' + show(e)) }
if(rejected){ throw new Error(m.toString() + ' rejected twice with: ' + show(e)) }
if(resolved){ throw new Error(m.toString() + ' rejected after resolving: ' + show(e)) }
rejected = true;
rej(e);
}, function (x){
if(crashed){ throw new Error(m.toString() + ' resolved after crashing: ' + Z.toString(x)) }
if(rejected){ throw new Error(m.toString() + ' resolved twice with: ' + Z.toString(x)) }
if(resolved){ throw new Error(m.toString() + ' resolved after rejecting: ' + Z.toString(x)) }
if(crashed){ throw new Error(m.toString() + ' resolved after crashing: ' + show(x)) }
if(rejected){ throw new Error(m.toString() + ' resolved twice with: ' + show(x)) }
if(resolved){ throw new Error(m.toString() + ' resolved after rejecting: ' + show(x)) }
resolved = true;
res(x);
});
Expand All @@ -85,12 +86,12 @@ export var assertCrashed = function (m, x){
Z.equals(x, e) ? res() : rej(new AssertionError({
expected: x,
actual: e,
message: 'Expected the Future to crash with ' + Z.toString(x) + '; got: ' + Z.toString(e)
message: 'Expected the Future to crash with ' + show(x) + '; got: ' + show(e)
}));
}, function (e){
rej(new Error('Expected the Future to crash. Instead rejected with: ' + Z.toString(e)));
rej(new Error('Expected the Future to crash. Instead rejected with: ' + show(e)));
}, function (x){
rej(new Error('Expected the Future to crash. Instead resolved with: ' + Z.toString(x)));
rej(new Error('Expected the Future to crash. Instead resolved with: ' + show(x)));
});
});
};
Expand All @@ -100,15 +101,15 @@ export var assertRejected = function (m, x){
assertIsFuture(m);
interpertAndGuard(
m, function (e){
rej(new Error('Expected the Future to reject. Instead crashed with: ' + Z.toString(e)));
rej(new Error('Expected the Future to reject. Instead crashed with: ' + show(e)));
}, function (e){
Z.equals(x, e) ? res() : rej(new AssertionError({
expected: x,
actual: e,
message: 'Expected the Future to reject with ' + Z.toString(x) + '; got: ' + Z.toString(e)
message: 'Expected the Future to reject with ' + show(x) + '; got: ' + show(e)
}));
}, function (x){
rej(new Error('Expected the Future to reject. Instead resolved with: ' + Z.toString(x)));
rej(new Error('Expected the Future to reject. Instead resolved with: ' + show(x)));
});
});
};
Expand All @@ -118,14 +119,14 @@ export var assertResolved = function (m, x){
assertIsFuture(m);
interpertAndGuard(
m, function (e){
rej(new Error('Expected the Future to resolve. Instead crashed with: ' + Z.toString(e)));
rej(new Error('Expected the Future to resolve. Instead crashed with: ' + show(e)));
}, function (e){
rej(new Error('Expected the Future to resolve. Instead rejected with: ' + Z.toString(e)));
rej(new Error('Expected the Future to resolve. Instead rejected with: ' + show(e)));
}, function (y){
Z.equals(x, y) ? res() : rej(new AssertionError({
expected: x,
actual: y,
message: 'Expected the Future to resolve with ' + Z.toString(x) + '; got: ' + Z.toString(y)
message: 'Expected the Future to resolve with ' + show(x) + '; got: ' + show(y)
}));
});
});
Expand All @@ -134,7 +135,7 @@ export var assertResolved = function (m, x){
export var onceOrError = function (f){
var called = false;
return function (){
if(called){ throw new Error('Function ' + Z.toString(f) + ' was called twice') }
if(called){ throw new Error('Function ' + show(f) + ' was called twice') }
called = true;
f.apply(null, arguments);
};
Expand Down