-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_edge_cases.lua
More file actions
executable file
·277 lines (235 loc) · 8.55 KB
/
test_edge_cases.lua
File metadata and controls
executable file
·277 lines (235 loc) · 8.55 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env luajit
-- Test script for edge cases and error handling in the Lua bindings
-- Add lua directory to package path
package.path = package.path .. ";./lua/?.lua"
local resvg = require("resvg")
print("Testing Resvg Lua bindings edge cases and error handling")
print("=======================================================")
local function test_case(description, test_func)
io.write(description .. ": ")
local success, result = pcall(test_func)
if success then
print("✓ PASS")
return true
else
print("✗ FAIL - " .. tostring(result))
return false
end
end
local function expect_error_or_nil(description, test_func)
io.write(description .. ": ")
local success, result = pcall(test_func)
if not success then
-- Function threw an error
print("✓ PASS (expected error: " .. tostring(result) .. ")")
return true
else
-- Function completed, check if it returned nil + error
local tree, err = result, nil
if type(test_func) == "function" then
tree, err = test_func()
end
if tree == nil and err then
print("✓ PASS (expected nil+error: " .. tostring(err) .. ")")
return true
else
print("✗ FAIL - Expected error but got success")
return false
end
end
end
local function expect_error(description, test_func)
io.write(description .. ": ")
local success, result = pcall(test_func)
if not success then
print("✓ PASS (expected error: " .. tostring(result) .. ")")
return true
else
print("✗ FAIL - Expected error but got success")
return false
end
end
local passed = 0
local total = 0
-- Test DPI validation
total = total + 1
passed = passed + (expect_error("Invalid DPI (negative)", function()
local opts = resvg.Options.new()
opts:set_dpi(-96)
end) and 1 or 0)
total = total + 1
passed = passed + (expect_error("Invalid DPI (NaN)", function()
local opts = resvg.Options.new()
opts:set_dpi(0/0)
end) and 1 or 0)
total = total + 1
passed = passed + (expect_error("Invalid DPI (string)", function()
local opts = resvg.Options.new()
opts:set_dpi("96")
end) and 1 or 0)
total = total + 1
passed = passed + (test_case("Valid DPI", function()
local opts = resvg.Options.new()
opts:set_dpi(96)
end) and 1 or 0)
-- Test font size validation
total = total + 1
passed = passed + (expect_error("Invalid font size (zero)", function()
local opts = resvg.Options.new()
opts:set_font_size(0)
end) and 1 or 0)
total = total + 1
passed = passed + (expect_error("Invalid font size (negative)", function()
local opts = resvg.Options.new()
opts:set_font_size(-12)
end) and 1 or 0)
-- Test font family validation
total = total + 1
passed = passed + (expect_error("Empty font family", function()
local opts = resvg.Options.new()
opts:set_font_family("")
end) and 1 or 0)
total = total + 1
passed = passed + (expect_error("Non-string font family", function()
local opts = resvg.Options.new()
opts:set_font_family(123)
end) and 1 or 0)
-- Test render dimensions validation
total = total + 1
passed = passed + (expect_error("Invalid render width (negative)", function()
local opts = resvg.Options.new()
local tree = resvg.Tree.from_data('<svg width="100" height="100"><rect width="50" height="50"/></svg>', opts)
tree:render(-100, 100)
end) and 1 or 0)
total = total + 1
passed = passed + (expect_error("Invalid render width (too large)", function()
local opts = resvg.Options.new()
local tree = resvg.Tree.from_data('<svg width="100" height="100"><rect width="50" height="50"/></svg>', opts)
tree:render(100000, 100)
end) and 1 or 0)
total = total + 1
passed = passed + (expect_error("Invalid render height (string)", function()
local opts = resvg.Options.new()
local tree = resvg.Tree.from_data('<svg width="100" height="100"><rect width="50" height="50"/></svg>', opts)
tree:render(100, "100")
end) and 1 or 0)
-- Test file parsing validation
total = total + 1
passed = passed + (test_case("Empty file path returns error", function()
local tree, err = resvg.Tree.from_file("", nil)
return tree == nil and err ~= nil
end) and 1 or 0)
total = total + 1
passed = passed + (test_case("Non-string file path returns error", function()
local tree, err = resvg.Tree.from_file(123, nil)
return tree == nil and err ~= nil
end) and 1 or 0)
-- Test data parsing validation
total = total + 1
passed = passed + (test_case("Empty SVG data returns error", function()
local tree, err = resvg.Tree.from_data("", nil)
return tree == nil and err ~= nil
end) and 1 or 0)
total = total + 1
passed = passed + (test_case("Non-string SVG data returns error", function()
local tree, err = resvg.Tree.from_data(123, nil)
return tree == nil and err ~= nil
end) and 1 or 0)
-- Test node operations validation
local opts = resvg.Options.new()
local tree = resvg.Tree.from_data('<svg width="100" height="100"><rect id="test" width="50" height="50"/></svg>', opts)
total = total + 1
passed = passed + (test_case("Valid node exists", function()
return tree:node_exists("test")
end) and 1 or 0)
total = total + 1
passed = passed + (test_case("Invalid node (empty string)", function()
return not tree:node_exists("")
end) and 1 or 0)
total = total + 1
passed = passed + (test_case("Invalid node (non-string)", function()
return not tree:node_exists(123)
end) and 1 or 0)
-- Test render_node validation
total = total + 1
passed = passed + (test_case("render_node with empty ID returns error", function()
local pixmap, err = tree:render_node("", 100, 100)
return pixmap == nil and err ~= nil
end) and 1 or 0)
total = total + 1
passed = passed + (test_case("render_node with non-string ID returns error", function()
local pixmap, err = tree:render_node(123, 100, 100)
return pixmap == nil and err ~= nil
end) and 1 or 0)
-- Test Transform validation
total = total + 1
passed = passed + (expect_error("Transform.new with NaN", function()
resvg.Transform.new(1, 0/0, 0, 1, 0, 0)
end) and 1 or 0)
total = total + 1
passed = passed + (expect_error("Transform.translate with non-number", function()
resvg.Transform.translate("10", 20)
end) and 1 or 0)
total = total + 1
passed = passed + (expect_error("Transform.scale with NaN", function()
resvg.Transform.scale(0/0)
end) and 1 or 0)
total = total + 1
passed = passed + (expect_error("Transform.rotate with non-number", function()
resvg.Transform.rotate("45")
end) and 1 or 0)
-- Test Pixmap validation
total = total + 1
passed = passed + (expect_error("Pixmap.new with negative width", function()
resvg.Pixmap.new(-100, 100)
end) and 1 or 0)
total = total + 1
passed = passed + (expect_error("Pixmap.new with too large dimensions", function()
resvg.Pixmap.new(100000, 100000)
end) and 1 or 0)
total = total + 1
passed = passed + (expect_error("Pixmap.from_data with size mismatch", function()
resvg.Pixmap.from_data("toolittledata", 100, 100)
end) and 1 or 0)
-- Test RGB conversion efficiency (should be fast)
total = total + 1
passed = passed + (test_case("RGB conversion performance", function()
local pixmap = resvg.Pixmap.new(200, 200)
local start_time = os.clock()
local rgb_data = pixmap:to_rgb()
local end_time = os.clock()
-- Should complete in reasonable time (< 1 second for 200x200)
return (end_time - start_time) < 1.0 and #rgb_data == 200 * 200 * 3
end) and 1 or 0)
-- Test font loading validation
total = total + 1
passed = passed + (test_case("load_font_file with empty path returns error", function()
local opts = resvg.Options.new()
local ok, err = opts:load_font_file("")
return ok == nil and err ~= nil
end) and 1 or 0)
total = total + 1
passed = passed + (test_case("load_font_file with non-string returns error", function()
local opts = resvg.Options.new()
local ok, err = opts:load_font_file(123)
return ok == nil and err ~= nil
end) and 1 or 0)
total = total + 1
passed = passed + (expect_error("load_font_data with empty data", function()
local opts = resvg.Options.new()
opts:load_font_data("")
end) and 1 or 0)
total = total + 1
passed = passed + (expect_error("load_font_data with non-string", function()
local opts = resvg.Options.new()
opts:load_font_data(123)
end) and 1 or 0)
print("\n=======================================================")
print(string.format("Test Results: %d/%d passed (%.1f%%)", passed, total, (passed/total)*100))
if passed == total then
print("✓ All tests passed!")
os.exit(0)
else
print("✗ Some tests failed!")
os.exit(1)
end