-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add exhaustiveWeakMapSearch with tests
- Loading branch information
Showing
4 changed files
with
192 additions
and
5 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,69 @@ | ||
import test from 'ava'; | ||
import LavaTube from '../src/index.js'; | ||
|
||
const generateKey = (key) => key | ||
|
||
test('exhaustiveWeakMapSearch', t => { | ||
const map = new WeakMap(); | ||
const obj = {}; | ||
const secret = {}; | ||
map.set(obj, secret); | ||
const start = { | ||
map, | ||
obj, | ||
}; | ||
const opts = { | ||
generateKey, | ||
exhaustiveWeakMapSearch: true, | ||
} | ||
|
||
const shouldBeMissing = find({ generateKey }, start, secret); | ||
t.deepEqual(shouldBeMissing, undefined); | ||
const shouldBeFound = find(opts, start, secret); | ||
t.deepEqual(shouldBeFound, [ | ||
'map', | ||
'<weakmap key (obj)>', | ||
]); | ||
}) | ||
|
||
test('exhaustiveWeakMapSearch - deep', t => { | ||
const firstWeakMap = new WeakMap() | ||
let lastWeakMap = firstWeakMap | ||
const addWeakMap = () => { | ||
const weakMap = new WeakMap() | ||
lastWeakMap.set(lastWeakMap, weakMap) | ||
lastWeakMap = weakMap | ||
} | ||
addWeakMap() | ||
addWeakMap() | ||
addWeakMap() | ||
|
||
const secret = {}; | ||
lastWeakMap.set(firstWeakMap, secret); | ||
const start = firstWeakMap; | ||
const opts = { | ||
exhaustiveWeakMapSearch: true, | ||
generateKey, | ||
} | ||
|
||
const shouldBeMissing = find({ generateKey }, start, secret); | ||
t.deepEqual(shouldBeMissing, undefined); | ||
const shouldBeFound = find(opts, start, secret); | ||
t.deepEqual(shouldBeFound, [ | ||
'<weakmap key ()>', | ||
'<weakmap key (<weakmap key ()>)>', | ||
'<weakmap key (<weakmap key ()>,<weakmap key (<weakmap key ()>)>)>', | ||
'<weakmap key ()>', | ||
]); | ||
}) | ||
|
||
function find (opts, start, target) { | ||
let result; | ||
new LavaTube(opts).walk(start, (value, path) => { | ||
if (value === target) { | ||
result = path | ||
return true | ||
} | ||
}); | ||
return result; | ||
} |