Skip to content

Commit 816fd5d

Browse files
committed
Fiddle
1 parent 7b0eb42 commit 816fd5d

File tree

2 files changed

+139
-102
lines changed

2 files changed

+139
-102
lines changed

lib/addAdditionalPromiseMethods.js

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
const Context = require('./Context');
22

3-
function addAdditionalPromiseMethods(promise, expect, subject, args) {
3+
function addAdditionalPromiseMethods(
4+
promise,
5+
expect,
6+
subject,
7+
args,
8+
wrappedExpect
9+
) {
410
promise.and = function (...args) {
511
function executeAnd() {
612
if (expect.findTypeOf(args[0]).is('expect.it')) {
@@ -32,12 +38,25 @@ function addAdditionalPromiseMethods(promise, expect, subject, args) {
3238
subject
3339
);
3440
} else {
35-
return expect.execute(
36-
shiftedSubject,
37-
methodName.replace(/[A-Z]/g, ($0) => ` ${$0.toLowerCase()}`),
38-
...args
39-
);
40-
return camelCaseMethods[methodName](shiftedSubject)(...args);
41+
if (wrappedExpect) {
42+
return wrappedExpect.shifty(
43+
shiftedSubject,
44+
wrappedExpect.args || [],
45+
[
46+
methodName.replace(/[A-Z]/g, ($0) => ` ${$0.toLowerCase()}`),
47+
...args,
48+
]
49+
);
50+
} else {
51+
// find ud af at fange konteksten
52+
return expect._executeExpect(
53+
new Context(),
54+
shiftedSubject,
55+
methodName.replace(/[A-Z]/g, ($0) => ` ${$0.toLowerCase()}`),
56+
args,
57+
{} // forwardedFlags
58+
);
59+
}
4160
}
4261
}
4362

@@ -48,23 +67,30 @@ function addAdditionalPromiseMethods(promise, expect, subject, args) {
4867
}
4968
};
5069

51-
promise[
52-
`and${methodName.replace(/^[a-z]/, ($0) => $0.toUpperCase())}`
53-
] = function (...args) {
54-
function execute() {
55-
if (expect.findTypeOf(args[0]).is('expect.it')) {
56-
return addAdditionalPromiseMethods(args[0](subject), expect, subject);
57-
} else {
58-
return camelCaseMethods[methodName](subject)(...args);
70+
promise[`and${methodName.replace(/^[a-z]/, ($0) => $0.toUpperCase())}`] =
71+
function (...args) {
72+
function execute() {
73+
if (expect.findTypeOf(args[0]).is('expect.it')) {
74+
return addAdditionalPromiseMethods(
75+
args[0](subject),
76+
expect,
77+
subject
78+
);
79+
} else {
80+
return camelCaseMethods[methodName](subject)(...args);
81+
}
5982
}
60-
}
6183

62-
if (this.isFulfilled()) {
63-
return execute(this.value());
64-
} else {
65-
return addAdditionalPromiseMethods(this.then(execute), expect, subject);
66-
}
67-
};
84+
if (this.isFulfilled()) {
85+
return execute(this.value());
86+
} else {
87+
return addAdditionalPromiseMethods(
88+
this.then(execute),
89+
expect,
90+
subject
91+
);
92+
}
93+
};
6894
});
6995

7096
return promise;

lib/createTopLevelExpect.js

Lines changed: 91 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -247,27 +247,25 @@ function createExpectIt(expect, expectations, forwardedFlags) {
247247

248248
const camelCaseMethods = expect._camelCaser(expect.context || new Context());
249249
Object.keys(camelCaseMethods).forEach((methodName) => {
250-
expectIt[
251-
`and${methodName.replace(/^[a-z]/, ($0) => $0.toUpperCase())}`
252-
] = function (...args) {
253-
const copiedExpectations = expectations.slice();
254-
copiedExpectations.push([
255-
methodName.replace(/[A-Z]/g, ($0) => ` ${$0.toLowerCase()}`), // Eeeeek, fix this
256-
...args,
257-
]);
258-
return createExpectIt(expect, copiedExpectations, forwardedFlags);
259-
};
250+
expectIt[`and${methodName.replace(/^[a-z]/, ($0) => $0.toUpperCase())}`] =
251+
function (...args) {
252+
const copiedExpectations = expectations.slice();
253+
copiedExpectations.push([
254+
methodName.replace(/[A-Z]/g, ($0) => ` ${$0.toLowerCase()}`), // Eeeeek, fix this
255+
...args,
256+
]);
257+
return createExpectIt(expect, copiedExpectations, forwardedFlags);
258+
};
260259

261-
expectIt[
262-
`or${methodName.replace(/^[a-z]/, ($0) => $0.toUpperCase())}`
263-
] = function (...args) {
264-
const copiedExpectations = expectations.slice();
265-
copiedExpectations.push(OR, [
266-
methodName.replace(/[A-Z]/g, ($0) => ` ${$0.toLowerCase()}`), // Eeeeek, fix this
267-
...args,
268-
]);
269-
return createExpectIt(expect, copiedExpectations, forwardedFlags);
270-
};
260+
expectIt[`or${methodName.replace(/^[a-z]/, ($0) => $0.toUpperCase())}`] =
261+
function (...args) {
262+
const copiedExpectations = expectations.slice();
263+
copiedExpectations.push(OR, [
264+
methodName.replace(/[A-Z]/g, ($0) => ` ${$0.toLowerCase()}`), // Eeeeek, fix this
265+
...args,
266+
]);
267+
return createExpectIt(expect, copiedExpectations, forwardedFlags);
268+
};
271269
});
272270

273271
return expectIt;
@@ -1358,14 +1356,15 @@ expectPrototype._createWrappedExpect = function (
13581356
for (let i = 2; i < arguments.length; i += 1) {
13591357
args[i - 2] = arguments[i];
13601358
}
1361-
return wrappedExpect._callInNestedContext(() =>
1362-
parentExpect._executeExpect(
1363-
context.child(),
1364-
subject,
1365-
testDescriptionString,
1366-
args,
1367-
wrappedExpect.flags
1368-
)
1359+
return wrappedExpect._callInNestedContext(
1360+
() =>
1361+
parentExpect._executeExpect(
1362+
context.child(),
1363+
subject,
1364+
testDescriptionString,
1365+
args,
1366+
wrappedExpect.flags
1367+
).result
13691368
);
13701369
}
13711370

@@ -1476,7 +1475,10 @@ expectPrototype._executeExpect = function (
14761475
forwardedFlags
14771476
);
14781477

1479-
return oathbreaker(assertionRule.handler(wrappedExpect, subject, ...args));
1478+
return {
1479+
result: oathbreaker(assertionRule.handler(wrappedExpect, subject, ...args)),
1480+
wrappedExpect,
1481+
};
14801482
};
14811483

14821484
expectPrototype._camelCaser = function (context, subjectType, subject) {
@@ -1490,13 +1492,15 @@ expectPrototype._camelCaser = function (context, subjectType, subject) {
14901492
subjectType.is(assertion.subject.type)
14911493
)
14921494
) {
1493-
let method = (subject) => (...args) => {
1494-
return this._expect(context, [
1495-
subject,
1496-
testDescriptionString,
1497-
...args,
1498-
]);
1499-
};
1495+
let method =
1496+
(subject) =>
1497+
(...args) => {
1498+
return this._expect(context, [
1499+
subject,
1500+
testDescriptionString,
1501+
...args,
1502+
]);
1503+
};
15001504

15011505
if (subjectType) {
15021506
method = method(subject);
@@ -1532,7 +1536,7 @@ expectPrototype._expect = function (context, args, forwardedFlags) {
15321536
}
15331537

15341538
try {
1535-
let result = this._executeExpect(
1539+
let { result, wrappedExpect } = this._executeExpect(
15361540
context,
15371541
subject,
15381542
testDescriptionString,
@@ -1557,7 +1561,8 @@ expectPrototype._expect = function (context, args, forwardedFlags) {
15571561
result,
15581562
this,
15591563
subject,
1560-
Array.prototype.slice.call(args, 0, 2)
1564+
Array.prototype.slice.call(args, 0, 2),
1565+
wrappedExpect
15611566
);
15621567
} catch (e) {
15631568
if (e && e._isUnexpected) {
@@ -1837,6 +1842,53 @@ expectPrototype._callInNestedContext = function (callback) {
18371842
}
18381843
};
18391844

1845+
expectPrototype.shifty = function (subject, args, rest) {
1846+
this._assertWrappedExpect();
1847+
1848+
const nextArgumentType = this.findTypeOf(rest[0]);
1849+
if (arguments.length > 1) {
1850+
// Legacy
1851+
this.argsOutput = (output) => {
1852+
args.forEach((arg, index) => {
1853+
if (index > 0) {
1854+
output.text(', ');
1855+
}
1856+
output.appendInspected(arg);
1857+
});
1858+
1859+
if (args.length > 0) {
1860+
output.sp();
1861+
}
1862+
if (nextArgumentType.is('string')) {
1863+
output.error(rest[0]);
1864+
} else if (rest.length > 0) {
1865+
output.appendInspected(rest[0]);
1866+
}
1867+
if (rest.length > 1) {
1868+
output.sp();
1869+
}
1870+
rest.slice(1).forEach((arg, index) => {
1871+
if (index > 0) {
1872+
output.text(', ');
1873+
}
1874+
output.appendInspected(arg);
1875+
});
1876+
};
1877+
}
1878+
if (nextArgumentType.is('expect.it')) {
1879+
return this.withError(
1880+
() => rest[0](subject),
1881+
(err) => {
1882+
this.fail(err);
1883+
}
1884+
);
1885+
} else if (nextArgumentType.is('string')) {
1886+
return this.execute(subject, ...rest);
1887+
} else {
1888+
return subject;
1889+
}
1890+
};
1891+
18401892
expectPrototype.shift = function (subject, assertionIndex) {
18411893
this._assertWrappedExpect();
18421894
if (arguments.length <= 1) {
@@ -1860,48 +1912,7 @@ expectPrototype.shift = function (subject, assertionIndex) {
18601912
if (assertionIndex !== -1) {
18611913
const args = this.args.slice(0, assertionIndex);
18621914
const rest = this.args.slice(assertionIndex);
1863-
const nextArgumentType = this.findTypeOf(rest[0]);
1864-
if (arguments.length > 1) {
1865-
// Legacy
1866-
this.argsOutput = (output) => {
1867-
args.forEach((arg, index) => {
1868-
if (index > 0) {
1869-
output.text(', ');
1870-
}
1871-
output.appendInspected(arg);
1872-
});
1873-
1874-
if (args.length > 0) {
1875-
output.sp();
1876-
}
1877-
if (nextArgumentType.is('string')) {
1878-
output.error(rest[0]);
1879-
} else if (rest.length > 0) {
1880-
output.appendInspected(rest[0]);
1881-
}
1882-
if (rest.length > 1) {
1883-
output.sp();
1884-
}
1885-
rest.slice(1).forEach((arg, index) => {
1886-
if (index > 0) {
1887-
output.text(', ');
1888-
}
1889-
output.appendInspected(arg);
1890-
});
1891-
};
1892-
}
1893-
if (nextArgumentType.is('expect.it')) {
1894-
return this.withError(
1895-
() => rest[0](subject),
1896-
(err) => {
1897-
this.fail(err);
1898-
}
1899-
);
1900-
} else if (nextArgumentType.is('string')) {
1901-
return this.execute(subject, ...rest);
1902-
} else {
1903-
return subject;
1904-
}
1915+
return this.shifty(subject, args, rest);
19051916
} else {
19061917
// No assertion to delegate to. Provide the new subject as the fulfillment value:
19071918
return subject;

0 commit comments

Comments
 (0)