-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.ts
86 lines (63 loc) · 2.31 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import Hasty, { Hastyable } from ".";
let history: string[] = [];
function Test(name: string, test_fn: Function, result: any) {
const update_stdout = () => {
console.clear();
for (let line of history) {
console.log(line);
}
}
history.push(`[🏃] Running test '${name}'`);
update_stdout();
const start_time = process.hrtime()[1];
const fn_result = test_fn();
const end_time = process.hrtime()[1];
history.pop();
if (fn_result !== result) {
history.push(`[🚩] Test '${name}' failed. ${fn_result} !== ${result} [Took ${end_time - start_time}ns]`);
update_stdout();
process.exit(1);
}
history.push(`[🏁] Test '${name}' finished [Took ${end_time - start_time}ns]`)
update_stdout();
}
function RunTests(Database: Hastyable) {
// Clear
Database.clear();
// :: Basic Setting
Test('Setting', () => Database.set('set_me', 10) === 10 &&
Database.get('set_me') === 10,
true);
// :: Key Setting
Test('Setting Keys', () => Database.set('set_keys.a.very.long.list.of.sub.keys', 10) === 10 &&
Database.get('set_keys.a.very.long.list.of.sub.keys') === 10,
true);
// :: Addition
Database.set('add_me', 20);
Test('Addition', () => Database.add('add_me', 10), 30);
// :: Subtraction
Database.set('reduce_me', 20);
Test('Subtraction', () => Database.subtract('reduce_me', 10), 10);
// :: Item Exists
Database.set('i_exist', true);
Test('Item Exists', () => Database.has('i_exist'), true);
// :: Item Doesn't Exist
Database.delete('i_dont');
Test('Item Doesn\'t Exist', () => Database.has('i_dont'), false);
// :: Pushing array
Database.set('array', [1, 2, 3, 4, 5]);
Test('Array Pushing', () => Database.push('array', 6)!.reduce((a, b) => a + b), 21);
// :: Type checking
Database.set('type', true);
Test('Type Checking', () => Database.type('type'), 'boolean');
// :: Fetching all
Test('Fetch All', () => Database.all().length, 7);
// :: Clearing
Test('Clearing', () => Database.clear(), 7);
}
// Run tests with database
history.push(`\n[ ## STARTING DATABASE TESTS (TS) ## ]`)
RunTests(Hasty());
// Run tests with table
history.push(`\n[ ## STARTING TABLE TESTS (TS) ## ]`)
RunTests(Hasty().Table('tests'));