-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: add Quickstart example to tests (#36)
* ci: add quickstart tests * chore: remove deps * chore: improve tests
- Loading branch information
1 parent
de35d95
commit dc19f68
Showing
3 changed files
with
142 additions
and
50 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,142 @@ | ||
import { describe, it, assert } from 'poku'; | ||
import { createLRU } from '../src/index.ts'; | ||
|
||
describe('Quickstart example test', () => { | ||
it('Default', () => { | ||
const evicteds: string[] = []; | ||
const max = 2; | ||
const onEviction = (key: string, value: string) => | ||
evicteds.push(`Key "${key}" with value "${value}" has been evicted.`); | ||
|
||
const LRU = createLRU<string, string>({ | ||
max, | ||
onEviction, | ||
}); | ||
|
||
LRU.set('A', 'My Value'); | ||
LRU.set('B', 'Other Value'); | ||
LRU.set('C', 'Another Value'); | ||
|
||
assert.deepStrictEqual(evicteds, [ | ||
'Key "A" with value "My Value" has been evicted.', | ||
]); | ||
|
||
assert.deepStrictEqual( | ||
[...LRU.entries()], | ||
[ | ||
['C', 'Another Value'], | ||
['B', 'Other Value'], | ||
] | ||
); | ||
|
||
assert.strictEqual(LRU.has('B'), true); | ||
assert.strictEqual(LRU.get('B'), 'Other Value'); | ||
assert.strictEqual(LRU.delete('B'), true); | ||
|
||
assert.deepStrictEqual(evicteds, [ | ||
'Key "A" with value "My Value" has been evicted.', | ||
'Key "B" with value "Other Value" has been evicted.', | ||
]); | ||
assert.deepStrictEqual([...LRU.entries()], [['C', 'Another Value']]); | ||
|
||
assert.strictEqual(LRU.peek('C'), 'Another Value'); | ||
|
||
LRU.clear(); | ||
|
||
assert.deepStrictEqual(evicteds, [ | ||
'Key "A" with value "My Value" has been evicted.', | ||
'Key "B" with value "Other Value" has been evicted.', | ||
'Key "C" with value "Another Value" has been evicted.', | ||
]); | ||
assert.deepStrictEqual([...LRU.entries()], []); | ||
|
||
LRU.set('D', "You're amazing 💛"); | ||
|
||
assert.deepStrictEqual([...LRU.entries()], [['D', "You're amazing 💛"]]); | ||
|
||
assert.strictEqual(LRU.size, 1); | ||
assert.strictEqual(LRU.max, 2); | ||
assert.strictEqual(LRU.available, 1); | ||
|
||
LRU.resize(10); | ||
|
||
assert.strictEqual(LRU.size, 1); | ||
assert.strictEqual(LRU.max, 10); | ||
assert.strictEqual(LRU.available, 9); | ||
assert.deepStrictEqual([...LRU.entries()], [['D', "You're amazing 💛"]]); | ||
assert.deepStrictEqual(evicteds, [ | ||
'Key "A" with value "My Value" has been evicted.', | ||
'Key "B" with value "Other Value" has been evicted.', | ||
'Key "C" with value "Another Value" has been evicted.', | ||
]); | ||
}); | ||
|
||
it('Alternative', () => { | ||
const evicteds: string[] = []; | ||
const max = 2; | ||
const onEviction = (key: string, value: string) => | ||
evicteds.push(`Key "${key}" with value "${value}" has been evicted.`); | ||
|
||
const LRU = createLRU<string, string>({ | ||
max, | ||
onEviction, | ||
}); | ||
|
||
LRU.set('A', 'My Value'); | ||
LRU.set('B', 'Other Value'); | ||
LRU.set('C', 'Another Value'); | ||
|
||
assert.deepStrictEqual(evicteds, [ | ||
'Key "A" with value "My Value" has been evicted.', | ||
]); | ||
|
||
assert.deepStrictEqual( | ||
[...LRU.entries()], | ||
[ | ||
['C', 'Another Value'], | ||
['B', 'Other Value'], | ||
] | ||
); | ||
|
||
assert.strictEqual(LRU.has('B'), true); | ||
assert.strictEqual(LRU.get('B'), 'Other Value'); | ||
assert.strictEqual(LRU.delete('B'), true); | ||
|
||
assert.deepStrictEqual(evicteds, [ | ||
'Key "A" with value "My Value" has been evicted.', | ||
'Key "B" with value "Other Value" has been evicted.', | ||
]); | ||
assert.deepStrictEqual([...LRU.entries()], [['C', 'Another Value']]); | ||
|
||
assert.strictEqual(LRU.peek('C'), 'Another Value'); | ||
|
||
LRU.evict(max); | ||
|
||
assert.deepStrictEqual(evicteds, [ | ||
'Key "A" with value "My Value" has been evicted.', | ||
'Key "B" with value "Other Value" has been evicted.', | ||
'Key "C" with value "Another Value" has been evicted.', | ||
]); | ||
assert.deepStrictEqual([...LRU.entries()], []); | ||
|
||
LRU.set('D', "You're amazing 💛"); | ||
|
||
assert.deepStrictEqual([...LRU.entries()], [['D', "You're amazing 💛"]]); | ||
|
||
assert.strictEqual(LRU.size, 1); | ||
assert.strictEqual(LRU.max, 2); | ||
assert.strictEqual(LRU.available, 1); | ||
|
||
LRU.resize(10); | ||
|
||
assert.strictEqual(LRU.size, 1); | ||
assert.strictEqual(LRU.max, 10); | ||
assert.strictEqual(LRU.available, 9); | ||
assert.deepStrictEqual([...LRU.entries()], [['D', "You're amazing 💛"]]); | ||
assert.deepStrictEqual(evicteds, [ | ||
'Key "A" with value "My Value" has been evicted.', | ||
'Key "B" with value "Other Value" has been evicted.', | ||
'Key "C" with value "Another Value" has been evicted.', | ||
]); | ||
}); | ||
}); |