a_mock 1.0.5 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AUTHORS +1 -1
- package/and/newBinaryAnd.js +19 -19
- package/and/newBinaryAndSpec/test.js +67 -67
- package/and/newMonadicAnd.js +19 -19
- package/and/newMonadicAndSpec/test.js +45 -45
- package/and.js +11 -11
- package/andSpec/test.js +35 -35
- package/andSpec/testIntegration.js +24 -24
- package/eventEmitter/add.js +5 -5
- package/eventEmitter/addSpec/test.js +22 -22
- package/eventEmitter/emitterContext.js +7 -7
- package/eventEmitter/emitterContextSpec/test.js +12 -12
- package/eventEmitter/remove.js +11 -11
- package/eventEmitter/removeSpec/test.js +27 -27
- package/eventEmitter/tryAdd.js +7 -7
- package/eventEmitter/tryAddSpec/test.js +43 -43
- package/eventEmitter/tryRemove.js +7 -7
- package/eventEmitter/tryRemoveSpec/test.js +43 -43
- package/eventEmitter.js +42 -42
- package/eventEmitterSpec/test.js +142 -142
- package/expectRequire.js +20 -20
- package/expectRequireSpec/test.js +67 -67
- package/index.d.ts +96 -0
- package/index.js +9 -10
- package/mock/mockContext.js +9 -9
- package/mock/mockContextSpec/test.js +30 -30
- package/mock/mockFuncProperties.js +16 -16
- package/mock/mockFuncPropertiesSpec/test.js +67 -67
- package/mock/objectMock.js +23 -23
- package/mock/objectMockSpec/test.js +123 -123
- package/mock/propertyMock.js +13 -13
- package/mock/propertyMockSpec/test.js +36 -36
- package/mock/throwUnexpectedArguments.js +13 -13
- package/mock/throwUnexpectedArgumentsSpec/test.js +35 -35
- package/mock.js +19 -19
- package/mockSpec/test.js +65 -65
- package/mockSpec/testIntegration.js +102 -102
- package/newMutableAnd.js +18 -18
- package/newMutableAndSpec/test.js +48 -48
- package/newObject.js +4 -4
- package/newThen.js +111 -111
- package/newThenSpec/test.js +11 -11
- package/newThenSpec/testExecute.js +80 -80
- package/newThenSpec/testNoCallback.js +57 -57
- package/newThenSpec/testPreReject.js +51 -51
- package/newThenSpec/testPreRejectAndThrow.js +50 -50
- package/newThenSpec/testPreResolve.js +49 -49
- package/newThenSpec/testPreResolveAndThrow.js +48 -48
- package/newThenSpec/testReject.js +56 -56
- package/newThenSpec/testRejectAndThrow.js +56 -56
- package/newThenSpec/testResolve.js +56 -56
- package/newThenSpec/testResolveAndThrow.js +54 -54
- package/newThenSpec/testResolveWithFailingPromise.js +65 -65
- package/newThenSpec/testResolveWithPromise.js +65 -65
- package/newTrueNTimesThenFalse.js +29 -29
- package/newTrueNTimesThenFalseSpec/test.js +59 -59
- package/newTrueOnceThenFalse.js +21 -21
- package/newTrueOnceThenFalseSpec/test.js +25 -25
- package/package.json +14 -13
- package/partialMock/execute.js +18 -18
- package/partialMock/executeSpec/test.js +84 -84
- package/partialMock/expect.js +13 -13
- package/partialMock/expectAnything.js +51 -51
- package/partialMock/expectAnythingSpec/test.js +167 -167
- package/partialMock/expectArray.js +11 -11
- package/partialMock/expectArraySpec/test.js +36 -36
- package/partialMock/expectCore.js +70 -70
- package/partialMock/expectCoreSpec/test.js +241 -241
- package/partialMock/expectEmpty.js +47 -47
- package/partialMock/expectEmptySpec/test.js +194 -194
- package/partialMock/expectSpec/test.js +45 -45
- package/partialMock/fallbackWrapper/getStackTrace.js +13 -13
- package/partialMock/hasSameArgument/isEqualArg.js +68 -68
- package/partialMock/hasSameArgument/isEqualArgSpec/test.js +283 -283
- package/partialMock/ignore.js +7 -7
- package/partialMock/ignoreSpec/test.js +30 -30
- package/partialMock/mockContext/reset.js +14 -14
- package/partialMock/mockContext/resetSpec/test.js +42 -42
- package/partialMock/negotiateDecrementExpectCount.js +5 -5
- package/partialMock/negotiateDecrementExpectCountSpec/test.js +31 -31
- package/partialMock/negotiateEnd.js +10 -10
- package/partialMock/negotiateEndSpec/test.js +64 -64
- package/partialMock/negotiateIncrementExpectCount.js +5 -5
- package/partialMock/negotiateIncrementExpectCountSpec/test.js +31 -31
- package/partialMock/newExecute.js +21 -21
- package/partialMock/newExecuteSpec/test.js +61 -61
- package/partialMock/newFallbackWrapper.js +30 -30
- package/partialMock/newFallbackWrapperSpec/test.js +70 -70
- package/partialMock/newFallbackWrapperSpec/testWithMockError.js +59 -59
- package/partialMock/newFallbackWrapperSpec/testWithOtherError.js +48 -48
- package/partialMock/newHasArgument.js +9 -9
- package/partialMock/newHasArgumentSpec/test.js +36 -36
- package/partialMock/newHasEqualArgumentArray.js +23 -23
- package/partialMock/newHasEqualArgumentArraySpec/test.js +59 -59
- package/partialMock/newHasNoMoreArguments.js +15 -15
- package/partialMock/newHasNoMoreArgumentsSpec/test.js +64 -64
- package/partialMock/newHasSameArgument.js +11 -11
- package/partialMock/newHasSameArgumentSpec/test.js +80 -80
- package/partialMock/newMockContext.js +12 -12
- package/partialMock/newMockContextSpec/test.js +36 -36
- package/partialMock/newThrow.js +6 -6
- package/partialMock/newThrowSpec/test.js +20 -20
- package/partialMock/return.js +24 -24
- package/partialMock/returnSpec/test.js +106 -106
- package/partialMock/setExecute.js +15 -15
- package/partialMock/setExecuteSpec/test.js +87 -87
- package/partialMock/simple/expectRequire.js +12 -12
- package/partialMock/simple/expectRequireSpec/test.js +41 -41
- package/partialMock/simple/newMock/throwUnexpectedArguments.js +9 -9
- package/partialMock/simple/newMock/throwUnexpectedArgumentsSpec/test.js +23 -23
- package/partialMock/simple/newMock.js +10 -10
- package/partialMock/simple/newMockSpec/test.js +28 -28
- package/partialMock/simple/newPartialMock.js +124 -124
- package/partialMock/simple/newPartialMockSpec/test.js +141 -141
- package/partialMock/simple/newPartialMockSpec/testRepeat.js +63 -63
- package/partialMock/simple/newPartialMockSpec/testWithMultiArgs.js +54 -54
- package/partialMock/simple/newPartialMockSpec/testWithSingleArg.js +116 -116
- package/partialMock/simple/newRequireMock.js +12 -12
- package/partialMock/simple/newRequireMockSpec/test.js +39 -39
- package/partialMock/verify.js +7 -7
- package/partialMock/verifySpec/test.js +37 -37
- package/partialMock.js +70 -70
- package/partialMockSpec/test.js +235 -235
- package/partialMockSpec/testIntegration.js +568 -568
- package/promise.js +10 -10
- package/requireMock.js +14 -14
- package/requireMockSpec/test.js +45 -45
- package/strictMock.js +10 -10
- package/strictMockSpec/test.js +28 -28
- package/test.js +16 -16
- package/util.js +7 -1
- package/.npmignore +0 -1
|
@@ -1,102 +1,102 @@
|
|
|
1
|
-
var assert = require('assert');
|
|
2
|
-
var test = require('../test');
|
|
3
|
-
var mock = require('../mock');
|
|
4
|
-
|
|
5
|
-
(function clearCache() {
|
|
6
|
-
Object.keys(require.cache).forEach(function(key) { delete require.cache[key]; });
|
|
7
|
-
})();
|
|
8
|
-
|
|
9
|
-
console.log('mock');
|
|
10
|
-
|
|
11
|
-
(function(){
|
|
12
|
-
console.log('an object');
|
|
13
|
-
var realName = 'Alfonzo';
|
|
14
|
-
function newCustomer() {
|
|
15
|
-
|
|
16
|
-
var c = {};
|
|
17
|
-
var _name = realName;
|
|
18
|
-
|
|
19
|
-
c.getName = function ()
|
|
20
|
-
{
|
|
21
|
-
return _name;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
c.realName = 'propValue';
|
|
25
|
-
return c;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
var customer = newCustomer(realName);
|
|
30
|
-
var customerMock = mock(customer);
|
|
31
|
-
var expected = {};
|
|
32
|
-
customerMock.getName.expect().return(expected);
|
|
33
|
-
|
|
34
|
-
var returned = customer.getName();
|
|
35
|
-
var returned2 = customer.getName();
|
|
36
|
-
|
|
37
|
-
test('it should first return from mock',function() {
|
|
38
|
-
assert.equal(returned,expected);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test('it should secondly return from real object',function() {
|
|
42
|
-
assert.equal(returned2,realName);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
test('it verify should return true',function() {
|
|
46
|
-
assert.ok(customerMock.verify());
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
})();
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
(function foo(){
|
|
53
|
-
console.log('violating function throws with minium stack trace');
|
|
54
|
-
|
|
55
|
-
var sut = mock();
|
|
56
|
-
var lines;
|
|
57
|
-
|
|
58
|
-
try {
|
|
59
|
-
sut();
|
|
60
|
-
}
|
|
61
|
-
catch(e) {
|
|
62
|
-
lines = e.stack.split('\n');
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
test('reports current function as first source',function() {
|
|
67
|
-
assert.deepEqual(lines[1].indexOf('at foo') > 0,true);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
test('reports current file as first source',function() {
|
|
71
|
-
assert.deepEqual(lines[1].indexOf('testIntegration') > 0,true);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
})();
|
|
76
|
-
|
|
77
|
-
(function bar(){
|
|
78
|
-
console.log('violating function throws with minium stack trace given two expectations');
|
|
79
|
-
|
|
80
|
-
var sut = mock();
|
|
81
|
-
var lines;
|
|
82
|
-
|
|
83
|
-
try {
|
|
84
|
-
sut.expect(1);
|
|
85
|
-
sut.expect(2);
|
|
86
|
-
sut();
|
|
87
|
-
}
|
|
88
|
-
catch(e) {
|
|
89
|
-
lines = e.stack.split('\n');
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
test('reports current function as first source',function() {
|
|
94
|
-
assert.deepEqual(lines[1].indexOf('at bar') > 0,true);
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
test('reports current file as first source',function() {
|
|
98
|
-
assert.deepEqual(lines[1].indexOf('testIntegration') > 0,true);
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
})();
|
|
1
|
+
var assert = require('assert');
|
|
2
|
+
var test = require('../test');
|
|
3
|
+
var mock = require('../mock');
|
|
4
|
+
|
|
5
|
+
(function clearCache() {
|
|
6
|
+
Object.keys(require.cache).forEach(function(key) { delete require.cache[key]; });
|
|
7
|
+
})();
|
|
8
|
+
|
|
9
|
+
console.log('mock');
|
|
10
|
+
|
|
11
|
+
(function(){
|
|
12
|
+
console.log('an object');
|
|
13
|
+
var realName = 'Alfonzo';
|
|
14
|
+
function newCustomer() {
|
|
15
|
+
|
|
16
|
+
var c = {};
|
|
17
|
+
var _name = realName;
|
|
18
|
+
|
|
19
|
+
c.getName = function ()
|
|
20
|
+
{
|
|
21
|
+
return _name;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
c.realName = 'propValue';
|
|
25
|
+
return c;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
var customer = newCustomer(realName);
|
|
30
|
+
var customerMock = mock(customer);
|
|
31
|
+
var expected = {};
|
|
32
|
+
customerMock.getName.expect().return(expected);
|
|
33
|
+
|
|
34
|
+
var returned = customer.getName();
|
|
35
|
+
var returned2 = customer.getName();
|
|
36
|
+
|
|
37
|
+
test('it should first return from mock',function() {
|
|
38
|
+
assert.equal(returned,expected);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('it should secondly return from real object',function() {
|
|
42
|
+
assert.equal(returned2,realName);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test('it verify should return true',function() {
|
|
46
|
+
assert.ok(customerMock.verify());
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
})();
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
(function foo(){
|
|
53
|
+
console.log('violating function throws with minium stack trace');
|
|
54
|
+
|
|
55
|
+
var sut = mock();
|
|
56
|
+
var lines;
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
sut();
|
|
60
|
+
}
|
|
61
|
+
catch(e) {
|
|
62
|
+
lines = e.stack.split('\n');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
test('reports current function as first source',function() {
|
|
67
|
+
assert.deepEqual(lines[1].indexOf('at foo') > 0,true);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('reports current file as first source',function() {
|
|
71
|
+
assert.deepEqual(lines[1].indexOf('testIntegration') > 0,true);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
})();
|
|
76
|
+
|
|
77
|
+
(function bar(){
|
|
78
|
+
console.log('violating function throws with minium stack trace given two expectations');
|
|
79
|
+
|
|
80
|
+
var sut = mock();
|
|
81
|
+
var lines;
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
sut.expect(1);
|
|
85
|
+
sut.expect(2);
|
|
86
|
+
sut();
|
|
87
|
+
}
|
|
88
|
+
catch(e) {
|
|
89
|
+
lines = e.stack.split('\n');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
test('reports current function as first source',function() {
|
|
94
|
+
assert.deepEqual(lines[1].indexOf('at bar') > 0,true);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test('reports current file as first source',function() {
|
|
98
|
+
assert.deepEqual(lines[1].indexOf('testIntegration') > 0,true);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
})();
|
package/newMutableAnd.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
function mutableAnd() {
|
|
2
|
-
var and = require('./and');
|
|
3
|
-
|
|
4
|
-
function execute() {
|
|
5
|
-
return and.apply(null,arguments);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
execute.add = function() {
|
|
9
|
-
and = and.add.apply(null,arguments);
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
return execute;
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
module.exports = mutableAnd;
|
|
17
|
-
|
|
18
|
-
|
|
1
|
+
function mutableAnd() {
|
|
2
|
+
var and = require('./and');
|
|
3
|
+
|
|
4
|
+
function execute() {
|
|
5
|
+
return and.apply(null,arguments);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
execute.add = function() {
|
|
9
|
+
and = and.add.apply(null,arguments);
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
return execute;
|
|
13
|
+
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
module.exports = mutableAnd;
|
|
17
|
+
|
|
18
|
+
|
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
var assert = require('assert');
|
|
2
|
-
var test = require('../test');
|
|
3
|
-
var newRequireMock = require('../partialMock/simple/newRequireMock');
|
|
4
|
-
var newMock = require('../partialMock/simple/newMock');
|
|
5
|
-
|
|
6
|
-
var and = newRequireMock('./and');
|
|
7
|
-
var newSut = require('../newMutableAnd');
|
|
8
|
-
|
|
9
|
-
(function(){
|
|
10
|
-
console.log('mutableAnd');
|
|
11
|
-
var sut = newSut();
|
|
12
|
-
|
|
13
|
-
(function() {
|
|
14
|
-
console.log('execute');
|
|
15
|
-
var arg = {};
|
|
16
|
-
var expected = {};
|
|
17
|
-
and.expect(arg).return(expected);
|
|
18
|
-
var returned = sut(arg);
|
|
19
|
-
|
|
20
|
-
test('it returns expected',function() {
|
|
21
|
-
assert.equal(returned,expected);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
})();
|
|
25
|
-
|
|
26
|
-
(function() {
|
|
27
|
-
console.log('add');
|
|
28
|
-
var predicate = {};
|
|
29
|
-
var add = newMock();
|
|
30
|
-
var and2 = newMock();
|
|
31
|
-
and.add = add;
|
|
32
|
-
add.expect(predicate).return(and2);
|
|
33
|
-
sut.add(predicate);
|
|
34
|
-
|
|
35
|
-
(function() {
|
|
36
|
-
console.log('execute');
|
|
37
|
-
var arg = {};
|
|
38
|
-
var expected = {};
|
|
39
|
-
and2.expect(arg).return(expected);
|
|
40
|
-
var returned = sut(arg);
|
|
41
|
-
|
|
42
|
-
test('it returns expected',function() {
|
|
43
|
-
assert.equal(returned,expected);
|
|
44
|
-
});
|
|
45
|
-
})();
|
|
46
|
-
})();
|
|
47
|
-
})();
|
|
48
|
-
|
|
1
|
+
var assert = require('assert');
|
|
2
|
+
var test = require('../test');
|
|
3
|
+
var newRequireMock = require('../partialMock/simple/newRequireMock');
|
|
4
|
+
var newMock = require('../partialMock/simple/newMock');
|
|
5
|
+
|
|
6
|
+
var and = newRequireMock('./and');
|
|
7
|
+
var newSut = require('../newMutableAnd');
|
|
8
|
+
|
|
9
|
+
(function(){
|
|
10
|
+
console.log('mutableAnd');
|
|
11
|
+
var sut = newSut();
|
|
12
|
+
|
|
13
|
+
(function() {
|
|
14
|
+
console.log('execute');
|
|
15
|
+
var arg = {};
|
|
16
|
+
var expected = {};
|
|
17
|
+
and.expect(arg).return(expected);
|
|
18
|
+
var returned = sut(arg);
|
|
19
|
+
|
|
20
|
+
test('it returns expected',function() {
|
|
21
|
+
assert.equal(returned,expected);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
})();
|
|
25
|
+
|
|
26
|
+
(function() {
|
|
27
|
+
console.log('add');
|
|
28
|
+
var predicate = {};
|
|
29
|
+
var add = newMock();
|
|
30
|
+
var and2 = newMock();
|
|
31
|
+
and.add = add;
|
|
32
|
+
add.expect(predicate).return(and2);
|
|
33
|
+
sut.add(predicate);
|
|
34
|
+
|
|
35
|
+
(function() {
|
|
36
|
+
console.log('execute');
|
|
37
|
+
var arg = {};
|
|
38
|
+
var expected = {};
|
|
39
|
+
and2.expect(arg).return(expected);
|
|
40
|
+
var returned = sut(arg);
|
|
41
|
+
|
|
42
|
+
test('it returns expected',function() {
|
|
43
|
+
assert.equal(returned,expected);
|
|
44
|
+
});
|
|
45
|
+
})();
|
|
46
|
+
})();
|
|
47
|
+
})();
|
|
48
|
+
|
package/newObject.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
function _new() {
|
|
2
|
-
return {};
|
|
3
|
-
};
|
|
4
|
-
|
|
1
|
+
function _new() {
|
|
2
|
+
return {};
|
|
3
|
+
};
|
|
4
|
+
|
|
5
5
|
module.exports = _new;
|
package/newThen.js
CHANGED
|
@@ -1,111 +1,111 @@
|
|
|
1
|
-
function newThen() {
|
|
2
|
-
var isError;
|
|
3
|
-
var settledValue;
|
|
4
|
-
var isSettled;
|
|
5
|
-
var chains = [];
|
|
6
|
-
|
|
7
|
-
var c = function(valueToResolveWith, errorToFailWith) {
|
|
8
|
-
if (arguments.length == 0)
|
|
9
|
-
return c.resolve();
|
|
10
|
-
if (valueToResolveWith)
|
|
11
|
-
return c.resolve(valueToResolveWith);
|
|
12
|
-
return c.reject(errorToFailWith);
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
c.then = function(success, fail) {
|
|
16
|
-
var chain = {};
|
|
17
|
-
chain.success = extractCallback(success);
|
|
18
|
-
chain.fail = extractCallback(fail);
|
|
19
|
-
var p = newThen();
|
|
20
|
-
chain.promise = p;
|
|
21
|
-
chains.push(chain);
|
|
22
|
-
negotiatePromises();
|
|
23
|
-
return p;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
c.resolve = function(value) {
|
|
27
|
-
isSettled = true;
|
|
28
|
-
settledValue = value;
|
|
29
|
-
negotiatePromises();
|
|
30
|
-
return c;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
c.reject = function(e) {
|
|
34
|
-
isSettled = true;
|
|
35
|
-
isError = true;
|
|
36
|
-
settledValue = e;
|
|
37
|
-
negotiatePromises();
|
|
38
|
-
return c;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
function negotiatePromises() {
|
|
42
|
-
if (!isSettled)
|
|
43
|
-
return;
|
|
44
|
-
for (var i = 0; i < chains.length; i++) {
|
|
45
|
-
var chain = chains[i];
|
|
46
|
-
var cb;
|
|
47
|
-
if (isError)
|
|
48
|
-
cb = chain.fail;
|
|
49
|
-
else
|
|
50
|
-
cb = chain.success;
|
|
51
|
-
|
|
52
|
-
var wrapped = wrap(cb.bind(null, settledValue));
|
|
53
|
-
wrapped.then(function(nextValue) {
|
|
54
|
-
chain.promise.resolve(nextValue);
|
|
55
|
-
}, function(e) {
|
|
56
|
-
chain.promise.reject(e);
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
chains = [];
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
function extractCallback(cb) {
|
|
64
|
-
if (typeof cb === 'function')
|
|
65
|
-
return cb;
|
|
66
|
-
return passThrough;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function passThrough(value) {
|
|
70
|
-
if (isError)
|
|
71
|
-
return newThen().reject(value);
|
|
72
|
-
return newThen().resolve(value);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return c;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
function wrap(cb) {
|
|
80
|
-
try {
|
|
81
|
-
var result = cb();
|
|
82
|
-
if (result && typeof result.then === 'function') {
|
|
83
|
-
return result;
|
|
84
|
-
}
|
|
85
|
-
return {
|
|
86
|
-
then: function(success) {
|
|
87
|
-
success(result);
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
} catch (e) {
|
|
91
|
-
return {
|
|
92
|
-
then: function(success, fail) {
|
|
93
|
-
fail(e);
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
newThen.resolve = function(settledValue) {
|
|
100
|
-
var p = newThen();
|
|
101
|
-
p.resolve(settledValue);
|
|
102
|
-
return p;
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
newThen.reject = function(e) {
|
|
106
|
-
var p = newThen();
|
|
107
|
-
p.reject(e);
|
|
108
|
-
return p;
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
module.exports = newThen;
|
|
1
|
+
function newThen() {
|
|
2
|
+
var isError;
|
|
3
|
+
var settledValue;
|
|
4
|
+
var isSettled;
|
|
5
|
+
var chains = [];
|
|
6
|
+
|
|
7
|
+
var c = function(valueToResolveWith, errorToFailWith) {
|
|
8
|
+
if (arguments.length == 0)
|
|
9
|
+
return c.resolve();
|
|
10
|
+
if (valueToResolveWith)
|
|
11
|
+
return c.resolve(valueToResolveWith);
|
|
12
|
+
return c.reject(errorToFailWith);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
c.then = function(success, fail) {
|
|
16
|
+
var chain = {};
|
|
17
|
+
chain.success = extractCallback(success);
|
|
18
|
+
chain.fail = extractCallback(fail);
|
|
19
|
+
var p = newThen();
|
|
20
|
+
chain.promise = p;
|
|
21
|
+
chains.push(chain);
|
|
22
|
+
negotiatePromises();
|
|
23
|
+
return p;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
c.resolve = function(value) {
|
|
27
|
+
isSettled = true;
|
|
28
|
+
settledValue = value;
|
|
29
|
+
negotiatePromises();
|
|
30
|
+
return c;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
c.reject = function(e) {
|
|
34
|
+
isSettled = true;
|
|
35
|
+
isError = true;
|
|
36
|
+
settledValue = e;
|
|
37
|
+
negotiatePromises();
|
|
38
|
+
return c;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
function negotiatePromises() {
|
|
42
|
+
if (!isSettled)
|
|
43
|
+
return;
|
|
44
|
+
for (var i = 0; i < chains.length; i++) {
|
|
45
|
+
var chain = chains[i];
|
|
46
|
+
var cb;
|
|
47
|
+
if (isError)
|
|
48
|
+
cb = chain.fail;
|
|
49
|
+
else
|
|
50
|
+
cb = chain.success;
|
|
51
|
+
|
|
52
|
+
var wrapped = wrap(cb.bind(null, settledValue));
|
|
53
|
+
wrapped.then(function(nextValue) {
|
|
54
|
+
chain.promise.resolve(nextValue);
|
|
55
|
+
}, function(e) {
|
|
56
|
+
chain.promise.reject(e);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
chains = [];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
function extractCallback(cb) {
|
|
64
|
+
if (typeof cb === 'function')
|
|
65
|
+
return cb;
|
|
66
|
+
return passThrough;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function passThrough(value) {
|
|
70
|
+
if (isError)
|
|
71
|
+
return newThen().reject(value);
|
|
72
|
+
return newThen().resolve(value);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return c;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
function wrap(cb) {
|
|
80
|
+
try {
|
|
81
|
+
var result = cb();
|
|
82
|
+
if (result && typeof result.then === 'function') {
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
then: function(success) {
|
|
87
|
+
success(result);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
} catch (e) {
|
|
91
|
+
return {
|
|
92
|
+
then: function(success, fail) {
|
|
93
|
+
fail(e);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
newThen.resolve = function(settledValue) {
|
|
100
|
+
var p = newThen();
|
|
101
|
+
p.resolve(settledValue);
|
|
102
|
+
return p;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
newThen.reject = function(e) {
|
|
106
|
+
var p = newThen();
|
|
107
|
+
p.reject(e);
|
|
108
|
+
return p;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
module.exports = newThen;
|
package/newThenSpec/test.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
require('./testPreReject');
|
|
2
|
-
require('./testPreResolve');
|
|
3
|
-
require('./testResolve');
|
|
4
|
-
require('./testResolveAndThrow');
|
|
5
|
-
require('./testRejectAndThrow');
|
|
6
|
-
require('./testResolveWithPromise');
|
|
7
|
-
require('./testResolveWithFailingPromise');
|
|
8
|
-
require('./testPreRejectAndThrow');
|
|
9
|
-
require('./testPreResolveAndThrow');
|
|
10
|
-
require('./testNoCallback');
|
|
11
|
-
require('./testExecute');
|
|
1
|
+
require('./testPreReject');
|
|
2
|
+
require('./testPreResolve');
|
|
3
|
+
require('./testResolve');
|
|
4
|
+
require('./testResolveAndThrow');
|
|
5
|
+
require('./testRejectAndThrow');
|
|
6
|
+
require('./testResolveWithPromise');
|
|
7
|
+
require('./testResolveWithFailingPromise');
|
|
8
|
+
require('./testPreRejectAndThrow');
|
|
9
|
+
require('./testPreResolveAndThrow');
|
|
10
|
+
require('./testNoCallback');
|
|
11
|
+
require('./testExecute');
|