-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrains_stdlib.praia
More file actions
92 lines (81 loc) · 2.67 KB
/
grains_stdlib.praia
File metadata and controls
92 lines (81 loc) · 2.67 KB
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
87
88
89
90
91
92
// ── Test all standard library grains ──
use "collections"
use "testing"
use "csv"
use "uuid"
// ── Collections ──
testing.test("Stack", lam{ in
let s = collections.Stack()
s.push(1)
s.push(2)
s.push(3)
testing.assertEqual(s.peek(), 3, "peek should be 3")
testing.assertEqual(s.pop(), 3, "pop should be 3")
testing.assertEqual(s.size(), 2, "size should be 2")
testing.assert(!s.isEmpty(), "should not be empty")
})
testing.test("Queue", lam{ in
let q = collections.Queue()
q.enqueue("a")
q.enqueue("b")
q.enqueue("c")
testing.assertEqual(q.dequeue(), "a", "dequeue should be a")
testing.assertEqual(q.peek(), "b", "peek should be b")
testing.assertEqual(q.size(), 2, "size should be 2")
})
testing.test("Set", lam{ in
let s = collections.Set()
s.add(1)
s.add(2)
s.add(2)
s.add(3)
testing.assertEqual(s.size(), 3, "set size should be 3 (no dupes)")
testing.assert(s.has(2), "should have 2")
s.remove(2)
testing.assert(!s.has(2), "should not have 2 after remove")
let s2 = collections.Set()
s2.add(2)
s2.add(3)
s2.add(4)
s.add(2)
s.add(3)
let u = s.union(s2)
testing.assertEqual(u.size(), 4, "union should have 4 elements")
})
// ── CSV ──
testing.test("CSV parse", lam{ in
let text = "name,age,active\nAlice,30,true\nBob,17,false"
let data = csv.parse(text)
testing.assertEqual(len(data), 2, "should have 2 rows")
testing.assertEqual(data[0].name, "Alice", "first name")
testing.assertEqual(data[0].age, 30, "first age as number")
testing.assertEqual(data[1].active, false, "second active as bool")
})
testing.test("CSV stringify", lam{ in
let data = [
{name: "Alice", age: 30},
{name: "Bob", age: 17}
]
let text = csv.stringify(data, ["name", "age"])
testing.assert(text.contains("Alice"), "should contain Alice")
testing.assert(text.contains("name,age"), "should have header")
})
testing.test("CSV quoted fields", lam{ in
let text = "name,bio\nAlice,\"likes cats, dogs\"\nBob,\"said \"\"hello\"\"\""
let data = csv.parse(text)
testing.assertEqual(data[0].bio, "likes cats, dogs", "comma in quotes")
})
// ── UUID ──
testing.test("UUID v4", lam{ in
let id1 = uuid.v4()
let id2 = uuid.v4()
testing.assertEqual(len(id1), 36, "UUID should be 36 chars")
testing.assertNotEqual(id1, id2, "UUIDs should be unique")
testing.assertEqual(id1[14], "4", "version should be 4")
print(" sample:", id1)
})
// ── Testing framework itself ──
testing.test("assertThrows", lam{ in
testing.assertThrows(lam{ in throw "boom" }, "should catch throw")
})
testing.summary()