Skip to content

Commit

Permalink
ci: add Quickstart example to tests (#36)
Browse files Browse the repository at this point in the history
* ci: add quickstart tests

* chore: remove deps

* chore: improve tests
  • Loading branch information
wellwelwel authored Aug 28, 2024
1 parent de35d95 commit dc19f68
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 50 deletions.
49 changes: 0 additions & 49 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"@biomejs/biome": "^1.8.3",
"@types/node": "^22.5.0",
"esbuild": "^0.23.1",
"happy-dom": "^15.0.0",
"monocart-coverage-reports": "^2.10.3",
"packages-update": "^2.0.0",
"poku": "^2.5.0",
Expand Down
142 changes: 142 additions & 0 deletions test/quickstart.test.ts
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.',
]);
});
});

0 comments on commit dc19f68

Please sign in to comment.