-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevices.test.lua
More file actions
218 lines (183 loc) · 6.07 KB
/
Copy pathdevices.test.lua
File metadata and controls
218 lines (183 loc) · 6.07 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
local evdev = require "evdev"
local UInput = evdev.uinput.create
local function find_by_id(devs)
for _, dev in ipairs(devs) do
if dev.id_aliases and next(dev.id_aliases) then
return dev
end
end
end
local function find_by_path(devs)
for _, dev in ipairs(devs) do
if dev.path_aliases and next(dev.path_aliases) then
return dev
end
end
end
describe("evdev.devices", function()
local devs
local kb1, kb2, mouse
setup(function()
devs = assert(evdev.devices.list_devices())
kb1 = assert(UInput({ name = "evdev test keyboard" }))
kb2 = assert(UInput({ name = "evdev test keyboard" }))
mouse = assert(UInput({ name = "evdev test mouse" }))
end)
-- stylua: ignore
teardown(function()
if kb1 then kb1:close() end
if kb2 then kb2:close() end
if mouse then mouse:close() end
end)
describe("list_devices() ", function()
local devs, err = evdev.devices.list_devices()
assert.Table(devs)
assert.Nil(err)
---@cast devs -?
it("returns device info tables", function()
for _, info in ipairs(devs) do
assert.Table(info)
assert.String(info.path)
end
end)
it("sorts devices by path", function()
for i, info in ipairs(devs) do
if i > 1 then
assert.True(devs[i - 1].path <= info.path)
end
end
end)
it("includes created virtual devices", function()
local seen = {}
for _, info in ipairs(devs) do
seen[info.path] = true
end
assert.True(seen[kb1.path])
assert.True(seen[kb2.path])
assert.True(seen[mouse.path])
end)
it("attaches alias lists when present", function()
for _, info in ipairs(devs) do
if info.id_aliases then
assert.Table(info.id_aliases)
for _, alias in ipairs(info.id_aliases) do
assert.String(alias)
end
end
if info.path_aliases then
assert.Table(info.path_aliases)
for _, alias in ipairs(info.path_aliases) do
assert.String(alias)
end
end
end
end)
end)
describe("find() ", function()
local find = evdev.devices.find
it("returns nil for a missing path", function()
local dev = find("/dev/input/evdev-lua-test-missing")
assert.Nil(dev)
end)
it("returns nil for a missing name", function()
local dev = find("evdev-lua-test-missing")
assert.Nil(dev)
end)
it("finds a device by path", function()
local dev = find(kb1.path)
assert.Table(dev) ---@cast dev -?
assert.Equal(kb1.path, dev.path)
end)
it("finds a device by name", function()
local dev = find("evdev test keyboard")
assert.Table(dev) ---@cast dev -?
assert.Equal("evdev test keyboard", dev.name)
end)
it("finds a device by by-id alias when present", function()
local dev_with_alias = find_by_id(devs)
if not dev_with_alias then
---@diagnostic disable-next-line: missing-parameter
pending("no discovered device with a by-id alias")
end
local dev = find(dev_with_alias.id_aliases[1])
assert.Table(dev) ---@cast dev -?
assert.Equal(dev_with_alias.path, dev.path)
end)
it("finds a device by by-path alias when present", function()
local dev_with_alias = find_by_path(devs)
if not dev_with_alias then
---@diagnostic disable-next-line: missing-parameter
pending("no discovered device with a by-path alias")
end
local dev = find(dev_with_alias.path_aliases[1])
assert.Table(dev) ---@cast dev -?
assert.Equal(dev_with_alias.path, dev.path)
end)
end)
describe("find_all()", function()
local find_all = evdev.devices.find_all
it("returns an empty list for a missing path", function()
local devs = find_all("/dev/input/evdev-lua-test-missing")
assert.Table(devs)
assert.Equal(0, #devs)
end)
it("returns an empty list for a missing name", function()
local devs = find_all("evdev-lua-test-missing")
assert.Table(devs)
assert.Equal(0, #devs)
end)
it("finds a device by path", function()
local dev = find_all(kb1.path)
assert.Table(dev) ---@cast dev -?
assert.Equal(1, #dev)
assert.Equal(kb1.path, dev[1].path)
end)
it("finds all devices by shared name", function()
local dev = find_all("evdev test keyboard")
assert.Table(dev) ---@cast dev -?
assert.Equal(2, #dev)
assert.Equal("evdev test keyboard", dev[1].name)
assert.Equal("evdev test keyboard", dev[2].name)
end)
it("finds a device by by-id alias when present", function()
local dev_with_alias = find_by_id(devs)
if not dev_with_alias then
---@diagnostic disable-next-line: missing-parameter
pending("no discovered device with a by-id alias")
end
local dev = find_all(dev_with_alias.id_aliases[1])
assert.Table(dev) ---@cast dev -?
assert.Equal(1, #dev)
assert.Equal(dev_with_alias.path, dev[1].path)
end)
it("finds a device by by-path alias when present", function()
local dev_with_alias = find_by_path(devs)
if not dev_with_alias then
---@diagnostic disable-next-line: missing-parameter
pending("no discovered device with a by-path alias")
end
local dev = find_all(dev_with_alias.path_aliases[1])
assert.Table(dev) ---@cast dev -?
assert.Equal(1, #dev)
assert.Equal(dev_with_alias.path, dev[1].path)
end)
end)
describe("device_info", function()
local device_info = evdev.devices.device_info
it("returns device metadata", function()
local info = assert(device_info(mouse.path))
assert.Equal(mouse.path, info.path)
assert.Equal("evdev test mouse", info.name)
end)
it("returns nil for a missing path", function()
local dev, err = device_info("/dev/input/evdev-lua-test-missing")
assert.Nil(dev)
assert.String(err)
end)
it("errors on a non-string path", function()
assert.Error(function()
device_info(false) ---@diagnostic disable-line
end)
end)
end)
end)