specify-core
Version:
Describe, structure and runs tests for the Specify framework.
595 lines • 27.1 kB
JavaScript
var Future = require('data.future');
var fail = Future.rejected;
var delay = require('control.async')(Future).delay;
var Maybe = require('data.maybe');
var alright = require('alright');
var specify = require('specify-core');
var AssertionError = require('assertion-error');
var _ = alright;
var Nothing = Maybe.Nothing;
var Just = Maybe.Just;
function noop() {
}
var cfg = specify.Config(100, 100, function (x) {
return true;
});
var cfgDisabled = specify.Config(100, 100, function (x) {
return false;
});
function Test(x) {
x = x || {};
return specify.Test.Case.create({
name: x.name || '',
test: x.test || Future.of(),
timeout: x.timeout || Nothing(),
slow: x.slow || Nothing(),
enabled: x.enabled || Nothing()
});
}
function Suite(x) {
x = x || {};
return specify.Test.Suite.create({
name: x.name || '',
tests: x.tests || [],
beforeAll: x.beforeAll || specify.Hook([]),
beforeEach: x.beforeEach || specify.Hook([]),
afterAll: x.afterAll || specify.Hook([]),
afterEach: x.afterEach || specify.Hook([])
});
}
function toError(x) {
return new AssertionError('Expected ' + divergence.toString(), divergence);
}
module.exports = function (specify$2) {
var _scope = {
specify: specify$2,
tests: [],
beforeAll: [],
afterAll: [],
beforeEach: [],
afterEach: []
};
(function () {
var _scope$2 = {
specify: _scope.specify,
tests: [],
beforeAll: [],
afterAll: [],
beforeEach: [],
afterEach: []
};
(function () {
var _scope$3 = {
specify: _scope$2.specify,
tests: [],
beforeAll: [],
afterAll: [],
beforeEach: [],
afterEach: []
};
_scope$3.tests.push(_scope$3.specify.Test.Case.create({
name: 'Should run all attached actions in sequence.',
timeout: new _scope$3.specify._Maybe.Nothing(),
slow: new _scope$3.specify._Maybe.Nothing(),
enabled: new _scope$3.specify._Maybe.Nothing(),
test: function () {
var result = new Future(function (_$2, resolve) {
var ax = [];
var add = function (x) {
return new Future(function (_$3, f) {
return f(x, ax.push(x));
});
};
specify.Hook([
add(1),
add(2),
add(3)
]).run().subscribe(noop, noop, function () {
resolve(ax);
});
});
return function (alright$2) {
return alright$2.verifyFuture(result)(_.equal([
1,
2,
3
]));
}(typeof module !== 'undefined' && typeof require !== 'undefined' ? require('alright') : window.alright);
}()
}));
_scope$3.tests.push(_scope$3.specify.Test.Case.create({
name: 'Should stop at the first error.',
timeout: new _scope$3.specify._Maybe.Nothing(),
slow: new _scope$3.specify._Maybe.Nothing(),
enabled: new _scope$3.specify._Maybe.Nothing(),
test: new _scope$3.specify._Future(function (reject, resolve) {
try {
(function () {
var ax = [];
var add = function (x) {
return x === 1 ? new Future(function (f, _$2) {
return f(x, ax.push(x));
}) : new Future(function (_$2, f) {
return f(x, ax.push(x));
});
};
specify.Hook([
add(0),
add(1),
add(2)
]).run().subscribe(noop, function (e) {
(function (alright$2) {
return alright$2.verify(e)(alright$2.equal(1));
}(typeof module !== 'undefined' && typeof require !== 'undefined' ? require('alright') : window.alright));
});
(function (alright$2) {
return alright$2.verify(ax)(alright$2.equal([
0,
1
]));
}(typeof module !== 'undefined' && typeof require !== 'undefined' ? require('alright') : window.alright));
}());
resolve();
} catch (e) {
reject(e);
}
})
}));
_scope$2.tests.push(_scope$2.specify.Test.Suite.create({
name: 'Hook#run()',
tests: _scope$3.tests,
beforeAll: _scope$2.specify.Hook(_scope$3.beforeAll),
beforeEach: _scope$2.specify.Hook(_scope$3.beforeEach),
afterAll: _scope$2.specify.Hook(_scope$3.afterAll),
afterEach: _scope$2.specify.Hook(_scope$3.afterEach)
}));
}());
(function () {
var _scope$3 = {
specify: _scope$2.specify,
tests: [],
beforeAll: [],
afterAll: [],
beforeEach: [],
afterEach: []
};
_scope$3.tests.push(_scope$3.specify.Test.Case.create({
name: 'If not enabled, should return Observable[Ignored]',
timeout: new _scope$3.specify._Maybe.Nothing(),
slow: new _scope$3.specify._Maybe.Nothing(),
enabled: new _scope$3.specify._Maybe.Nothing(),
test: new _scope$3.specify._Future(function (reject, resolve) {
try {
(function () {
var t = Test();
t.run([], cfgDisabled).subscribe(function (x) {
return function (alright$2) {
return alright$2.verify(x.value.isIgnored)(alright$2.equal(true));
}(typeof module !== 'undefined' && typeof require !== 'undefined' ? require('alright') : window.alright);
});
}());
resolve();
} catch (e) {
reject(e);
}
})
}));
_scope$3.tests.push(_scope$3.specify.Test.Case.create({
name: 'If explicitly diabled, should return Observable[Ignored]',
timeout: new _scope$3.specify._Maybe.Nothing(),
slow: new _scope$3.specify._Maybe.Nothing(),
enabled: new _scope$3.specify._Maybe.Nothing(),
test: new _scope$3.specify._Future(function (reject, resolve) {
try {
(function () {
var t = Test({
enabled: Just(function () {
return false;
})
});
t.run([], cfg).subscribe(function (x) {
return function (alright$2) {
return alright$2.verify(x.value.isIgnored)(alright$2.equal(true));
}(typeof module !== 'undefined' && typeof require !== 'undefined' ? require('alright') : window.alright);
});
}());
resolve();
} catch (e) {
reject(e);
}
})
}));
_scope$3.tests.push(_scope$3.specify.Test.Case.create({
name: 'If the future resolves, should pass',
timeout: new _scope$3.specify._Maybe.Nothing(),
slow: new _scope$3.specify._Maybe.Nothing(),
enabled: new _scope$3.specify._Maybe.Nothing(),
test: new _scope$3.specify._Future(function (reject, resolve) {
try {
(function () {
var t = Test();
t.run([], cfg).subscribe(function (x) {
return function (alright$2) {
return alright$2.verify(x.value.isSuccess)(alright$2.equal(true));
}(typeof module !== 'undefined' && typeof require !== 'undefined' ? require('alright') : window.alright);
});
}());
resolve();
} catch (e) {
reject(e);
}
})
}));
_scope$3.tests.push(_scope$3.specify.Test.Case.create({
name: 'If the future fails, should fail',
timeout: new _scope$3.specify._Maybe.Nothing(),
slow: new _scope$3.specify._Maybe.Nothing(),
enabled: new _scope$3.specify._Maybe.Nothing(),
test: new _scope$3.specify._Future(function (reject, resolve) {
try {
(function () {
var t = Test({ test: fail() });
t.run([], cfg).subscribe(function (x) {
return function (alright$2) {
return alright$2.verify(x.value.isFailure)(alright$2.equal(true));
}(typeof module !== 'undefined' && typeof require !== 'undefined' ? require('alright') : window.alright);
});
}());
resolve();
} catch (e) {
reject(e);
}
})
}));
_scope$3.tests.push(_scope$3.specify.Test.Case.create({
name: 'Should consider the slow threshold in the test',
timeout: new _scope$3.specify._Maybe.Nothing(),
slow: new _scope$3.specify._Maybe.Nothing(),
enabled: new _scope$3.specify._Maybe.Nothing(),
test: new _scope$3.specify._Future(function (reject, resolve) {
try {
(function () {
var t = Test({ slow: Just(1) });
t.run([], cfg).subscribe(function (x) {
return function (alright$2) {
return alright$2.verify(x.value.duration.slowThreshold)(alright$2.equal(1));
}(typeof module !== 'undefined' && typeof require !== 'undefined' ? require('alright') : window.alright);
});
}());
resolve();
} catch (e) {
reject(e);
}
})
}));
_scope$3.tests.push(_scope$3.specify.Test.Case.create({
name: 'Should consider the slow threshold of the configuration if not explicit',
timeout: new _scope$3.specify._Maybe.Nothing(),
slow: new _scope$3.specify._Maybe.Nothing(),
enabled: new _scope$3.specify._Maybe.Nothing(),
test: new _scope$3.specify._Future(function (reject, resolve) {
try {
(function () {
var t = Test();
t.run([], cfg).subscribe(function (x) {
return function (alright$2) {
return alright$2.verify(x.value.duration.slowThreshold)(alright$2.equal(100));
}(typeof module !== 'undefined' && typeof require !== 'undefined' ? require('alright') : window.alright);
});
}());
resolve();
} catch (e) {
reject(e);
}
})
}));
_scope$3.tests.push(_scope$3.specify.Test.Case.create({
name: 'Should fail if the test takes too long.',
timeout: new _scope$3.specify._Maybe.Nothing(),
slow: new _scope$3.specify._Maybe.Nothing(),
enabled: new _scope$3.specify._Maybe.Nothing(),
test: function () {
return new Future(function (f, g) {
var t = Test({ test: delay(200) });
t.run([], cfg).subscribe(function (x) {
return _.ok(x.value.isFailure).fold(function (a) {
return f(toError(a));
}, g);
});
});
}()
}));
_scope$3.tests.push(_scope$3.specify.Test.Case.create({
name: 'Should fail if the test takes too long.',
timeout: new _scope$3.specify._Maybe.Nothing(),
slow: new _scope$3.specify._Maybe.Nothing(),
enabled: new _scope$3.specify._Maybe.Nothing(),
test: function () {
return new Future(function (f, g) {
var t = Test({
test: delay(50),
timeout: Just(10)
});
t.run([], cfg).subscribe(function (x) {
return _.ok(x.value.isFailure).fold(function (a) {
return f(toError(a));
}, g);
});
});
}()
}));
_scope$3.tests.push(_scope$3.specify.Test.Case.create({
name: 'Should capture all things logged to the console.',
timeout: new _scope$3.specify._Maybe.Nothing(),
slow: new _scope$3.specify._Maybe.Nothing(),
enabled: new _scope$3.specify._Maybe.Nothing(),
test: new _scope$3.specify._Future(function (reject, resolve) {
try {
(function () {
var t = Test({
test: new Future(function (_$2, f) {
console.log(1, 'a');
console.log(2);
})
});
t.run([], cfg).subscribe(function (x) {
var l = x.value.log;
(function (alright$2) {
return alright$2.verify(l.length)(alright$2.equal(2));
}(typeof module !== 'undefined' && typeof require !== 'undefined' ? require('alright') : window.alright));
(function (alright$2) {
return alright$2.verify(l[0].log)(alright$2.equal([
1,
'a'
]));
}(typeof module !== 'undefined' && typeof require !== 'undefined' ? require('alright') : window.alright));
(function (alright$2) {
return alright$2.verify(l[1].log)(alright$2.equal([2]));
}(typeof module !== 'undefined' && typeof require !== 'undefined' ? require('alright') : window.alright));
});
}());
resolve();
} catch (e) {
reject(e);
}
})
}));
_scope$2.tests.push(_scope$2.specify.Test.Suite.create({
name: 'Case#run()',
tests: _scope$3.tests,
beforeAll: _scope$2.specify.Hook(_scope$3.beforeAll),
beforeEach: _scope$2.specify.Hook(_scope$3.beforeEach),
afterAll: _scope$2.specify.Hook(_scope$3.afterAll),
afterEach: _scope$2.specify.Hook(_scope$3.afterEach)
}));
}());
(function () {
var _scope$3 = {
specify: _scope$2.specify,
tests: [],
beforeAll: [],
afterAll: [],
beforeEach: [],
afterEach: []
};
_scope$3.tests.push(_scope$3.specify.Test.Case.create({
name: 'Should run all things in sequence.',
timeout: new _scope$3.specify._Maybe.Nothing(),
slow: new _scope$3.specify._Maybe.Nothing(),
enabled: new _scope$3.specify._Maybe.Nothing(),
test: function () {
return new Future(function (reject, resolve) {
var ax = [];
var t1 = Test({
name: 'a',
test: new Future(function (_$2, f) {
return f(1, ax.push(1));
})
});
var t2 = Test({
name: 'b',
test: new Future(function (f, _$2) {
return f(2, ax.push(2));
})
});
var t3 = Test({
name: 'c',
test: delay(50)
});
var t4 = Test({
name: 'd',
test: delay(2000),
enabled: Just(function () {
return false;
})
});
var h1 = delay(10).chain(function (x) {
return Future.of(ax.push('h1'));
});
var h2 = delay(30).chain(function (x) {
return Future.of(ax.push('h2'));
});
var s1 = Suite({
name: 'A',
tests: [
t1,
t4,
t3
],
beforeEach: specify.Hook([h2]),
afterEach: specify.Hook([h1])
});
var s2 = Suite({
name: 'B',
tests: [
t1,
s1,
t2
],
beforeAll: specify.Hook([
h1,
h2
]),
afterAll: specify.Hook([
h1,
h1
])
});
s2.run([], cfg).reduce(function (acc, x) {
return x.cata({
Started: function (_$2) {
return acc.concat([[
's',
x.fullTitle()
]]);
},
Finished: function (_$2) {
return acc.concat([[
'f',
x.fullTitle()
]]);
},
TestResult: function (x$2) {
return x$2.cata({
Success: function (_$2) {
return acc.concat([[
'rs',
x$2.fullTitle()
]]);
},
Failure: function (_$2) {
return acc.concat([[
'rf',
x$2.fullTitle()
]]);
},
Ignored: function (_$2) {
return acc.concat([[
'ri',
x$2.fullTitle()
]]);
}
});
}
});
}, []).subscribe(function (v) {
_.equal(v, [
[
's',
'B'
],
[
's',
'B a'
],
[
'rs',
'B a'
],
[
'f',
'B a'
],
[
's',
'B A'
],
[
's',
'B A a'
],
[
'rs',
'B A a'
],
[
'f',
'B A a'
],
[
's',
'B A d'
],
[
'ri',
'B A d'
],
[
'f',
'B A d'
],
[
's',
'B A c'
],
[
'rs',
'B A c'
],
[
'f',
'B A c'
],
[
'f',
'B A'
],
[
's',
'B b'
],
[
'rf',
'B b'
],
[
'f',
'B b'
],
[
'f',
'B'
]
]).fold(reject, function () {
_.equal(ax, [
'h1',
'h2',
1,
'h2',
1,
'h1',
'h2',
'h1',
'h2',
'h1',
2,
'h1',
'h1'
]).fold(reject, resolve);
});
}, function (e) {
reject(e);
});
});
}()
}));
_scope$2.tests.push(_scope$2.specify.Test.Suite.create({
name: 'Suite#run()',
tests: _scope$3.tests,
beforeAll: _scope$2.specify.Hook(_scope$3.beforeAll),
beforeEach: _scope$2.specify.Hook(_scope$3.beforeEach),
afterAll: _scope$2.specify.Hook(_scope$3.afterAll),
afterEach: _scope$2.specify.Hook(_scope$3.afterEach)
}));
}());
_scope.tests.push(_scope.specify.Test.Suite.create({
name: 'Core',
tests: _scope$2.tests,
beforeAll: _scope.specify.Hook(_scope$2.beforeAll),
beforeEach: _scope.specify.Hook(_scope$2.beforeEach),
afterAll: _scope.specify.Hook(_scope$2.afterAll),
afterEach: _scope.specify.Hook(_scope$2.afterEach)
}));
}());
return _scope.tests[0];
}(typeof module !== 'undefined' && typeof require !== 'undefined' ? require('specify-core') : window.Specify.core);