-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimentally implement
t.like()
assertion
Co-authored-by: Mark Wubben <[email protected]>
- Loading branch information
1 parent
952a017
commit 19c4f35
Showing
27 changed files
with
930 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
function isLikeSelector(selector) { | ||
return selector !== null && | ||
typeof selector === 'object' && | ||
Reflect.getPrototypeOf(selector) === Object.prototype && | ||
Reflect.ownKeys(selector).length > 0; | ||
} | ||
|
||
exports.isLikeSelector = isLikeSelector; | ||
|
||
const CIRCULAR_SELECTOR = new Error('Encountered a circular selector'); | ||
exports.CIRCULAR_SELECTOR = CIRCULAR_SELECTOR; | ||
|
||
function selectComparable(lhs, selector, circular = new Set()) { | ||
if (circular.has(selector)) { | ||
throw CIRCULAR_SELECTOR; | ||
} | ||
|
||
circular.add(selector); | ||
|
||
if (lhs === null || typeof lhs !== 'object') { | ||
return lhs; | ||
} | ||
|
||
const comparable = {}; | ||
for (const [key, rhs] of Object.entries(selector)) { | ||
if (isLikeSelector(rhs)) { | ||
comparable[key] = selectComparable(Reflect.get(lhs, key), rhs, circular); | ||
} else { | ||
comparable[key] = Reflect.get(lhs, key); | ||
} | ||
} | ||
|
||
return comparable; | ||
} | ||
|
||
exports.selectComparable = selectComparable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import test from '..'; | ||
|
||
test('like', t => { | ||
t.like({ | ||
map: new Map([['foo', 'bar']]), | ||
nested: { | ||
baz: 'thud', | ||
qux: 'quux' | ||
} | ||
}, { | ||
map: new Map([['foo', 'bar']]), | ||
nested: { | ||
baz: 'thud' | ||
} | ||
}); | ||
}); | ||
|
Oops, something went wrong.