Repository navigation
Expand file tree
/
Copy pathmix_cpp_test.lua
More file actions
105 lines (92 loc) · 3.28 KB
/
Copy pathmix_cpp_test.lua
File metadata and controls
105 lines (92 loc) · 3.28 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
-- Prefer the locally built shared library next to the test binary
package.cpath = "./?.so;./?.dylib;./?.dll;" .. package.cpath
package.path = "./?.lua;" .. package.path
local libdifflua = require("libdifflua")
require("pure_lua_diff")
local regression = require("flua_regression")
local function cpp_diff(src, dst, get_id)
if get_id then
return libdifflua.cal_diff(src, dst, get_id)
end
return libdifflua.cal_diff(src, dst)
end
local function cpp_patch(src, diff, get_id)
if get_id then
return libdifflua.patch_diff(src, diff, get_id)
end
return libdifflua.patch_diff(src, diff)
end
local failed = 0
local total = 0
local f, t = regression.run(cpp_diff, cpp_patch, "libdifflua")
failed = failed + f
total = total + t
f, t = regression.run_cross(lua_diff, lua_patch, cpp_diff, cpp_patch)
failed = failed + f
total = total + t
-- Backward-compatible 4-arg C callback form, injected by flua_test.
if type(lua_get_id) == "function" and type(lua_new_func) == "function" then
local src = {
{ id = 10, a = 1 },
{ id = 20, a = 2 },
}
local dst = {
{ id = 10, a = 100 },
{ id = 20, a = 2 },
}
local ok, diff = pcall(libdifflua.cal_diff, src, dst, lua_get_id, lua_new_func)
if not ok then
failed = failed + 1
print("FAIL legacy_c_callbacks: cal_diff error: " .. tostring(diff))
else
local src_copy = regression.deep_copy(src)
local pok, patched = pcall(libdifflua.patch_diff, src_copy, diff, lua_get_id, lua_new_func)
if not pok then
failed = failed + 1
print("FAIL legacy_c_callbacks: patch_diff error: " .. tostring(patched))
elseif not regression.deep_equal(dst, patched, function(v) return v.id end) then
failed = failed + 1
print("FAIL legacy_c_callbacks: patched result does not match dst")
elseif diff.__diff_lua_array ~= true or type(diff[10]) ~= "table" then
failed = failed + 1
print("FAIL legacy_c_callbacks: expected id-keyed object-array diff")
else
print("OK legacy_c_callbacks")
end
end
total = total + 1
end
local function expect_error(name, fn, needle)
total = total + 1
local ok, err = pcall(fn)
if ok then
failed = failed + 1
print("FAIL " .. name .. ": expected an error")
return
end
err = tostring(err)
if needle and not string.find(err, needle, 1, true) then
failed = failed + 1
print("FAIL " .. name .. ": unexpected error: " .. err)
return
end
print("OK " .. name)
end
expect_error("error_unsupported_type", function()
libdifflua.cal_diff({ f = function() end }, { f = 1 })
end, "unsupported Lua type")
expect_error("error_get_id_throws", function()
libdifflua.cal_diff({ { id = 1 } }, { { id = 2 } }, function()
error("boom")
end)
end, "get_id failed")
expect_error("error_bad_get_id_arg", function()
libdifflua.cal_diff({}, {}, "not a function")
end, "get_id must be a function")
expect_error("error_too_few_args", function()
libdifflua.cal_diff({})
end, "requires at least 2 arguments")
print(string.format("mix_cpp_test: %d/%d passed", total - failed, total))
if failed > 0 then
error("mix_cpp_test failed: " .. failed .. " case(s)")
end