-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtests.lua
More file actions
150 lines (115 loc) · 4.49 KB
/
tests.lua
File metadata and controls
150 lines (115 loc) · 4.49 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
-- tnetstring unit testing, using https://github.com/norman/telescope
local tns = require 'tnetstrings'
context('tnetstrings', function()
context('parse', function()
test('general failure', function()
-- no length
assert_nil(tns.parse(':hello,'))
-- no colon
assert_nil(tns.parse('5hello,'))
-- incorrect length
assert_nil(tns.parse('16:hello,'))
-- invalid type code
assert_nil(tns.parse('5:hello?'))
assert_nil(tns.parse('5:hello'))
-- nested errors
assert_nil(tns.parse('16:5:hello!5:hello,}'))
assert_nil(tns.parse('16:5:hello,5:hello!}'))
assert_nil(tns.parse('16:5:hello,5:hello!]'))
end)
test('null', function()
assert_equal(tns.null(), tns.null)
assert_equal(tns.parse('0:~'), tns.null)
-- null must have zero length
assert_nil(tns.parse('5:hello~'))
end)
test('blob', function()
assert_equal(tns.parse('5:Hello,'), 'Hello')
end)
test('number', function()
assert_equal(tns.parse('5:12345#'), 12345)
local res = tns.parse('5:hello#')
assert_nil(res)
end)
test('boolean', function()
assert_equal(tns.parse('4:true!'), true)
assert_equal(tns.parse('5:false!'), false)
assert_nil(tns.parse('5:hello!'))
end)
test('list', function()
local result = tns.parse('16:5:hello,5:world,]')
assert_equal(result[1], 'hello')
assert_equal(result[2], 'world')
-- Deep nesting
result = tns.parse('56:5:hello,5:world,36:5:hello,5:world,16:5:hello,5:world,]]]')
assert_equal(result[3][3][2], 'world')
assert_empty(tns.parse('0:]'))
end)
test('dict', function()
local result = tns.parse('16:5:hello,5:world,}')
assert_equal(result['hello'], 'world')
-- Deep nesting
result = tns.parse('40:5:hello,28:5:hello,16:5:hello,5:world,}}}')
assert_equal(result.hello.hello.hello, 'world')
assert_empty(tns.parse('0:}'))
assert_nil(tns.parse('8:5:hello,}'))
assert_nil(tns.parse('8:5:12345#}'))
end)
end)
context('dump', function()
test('general failure', function()
assert_error(function() tns.dump(function() end) end)
assert_error(function() tns.dump(nil) end)
-- A handy userdata is the file object :D
assert_error(function() tns.dump(io.stdout) end)
-- Must use string keys
assert_error(function() tns.dump({[function()end] = 'hello'}) end)
end)
test('null', function()
assert_equal(tns.dump(tns.null), '0:~')
end)
test('blob', function()
assert_equal(tns.dump('hello'), '5:hello,')
end)
test('number', function()
assert_equal(tns.dump(9000), '4:9000#')
end)
test('boolean', function()
assert_equal(tns.dump(true), '4:true!')
assert_equal(tns.dump(false), '5:false!')
end)
test('list', function()
assert_equal(tns.dump({'hello', 'world'}), '16:5:hello,5:world,]')
end)
test('dict', function()
assert_equal(tns.dump({hello = 'world'}), '16:5:hello,5:world,}')
end)
end)
context('sanity', function()
test('null', function()
assert_equal(tns.parse(tns.dump(tns.null)), tns.null)
end)
test('blob', function()
assert_equal(tns.parse(tns.dump('hello')), 'hello')
end)
test('number', function()
assert_equal(tns.parse(tns.dump(9000)), 9000)
end)
test('boolean', function()
assert_equal(tns.parse(tns.dump(true)), true)
assert_equal(tns.parse(tns.dump(false)), false)
end)
test('list', function()
local res = tns.parse(tns.dump({'hello', 'world'}))
assert_equal(res[1], 'hello')
assert_equal(res[2], 'world')
end)
test('dict', function()
local res = tns.parse(tns.dump({hello = 'world'}))
assert_equal(res.hello, 'world')
-- Deep nesting
res = tns.parse(tns.dump({hello = {hello = {hello = 'world'}}}))
assert_equal(res.hello.hello.hello, 'world')
end)
end)
end)