55// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
66// ------------------------------------------------------------------------------------------------
77
8+ use lua_helpers:: new_lua;
89use stack_graphs:: graph:: NodeID ;
910use stack_graphs:: graph:: StackGraph ;
1011
11- const TEST_PRELUDE : & str = r#"
12- function assert_eq(thing, expected, actual)
13- if expected ~= actual then
14- error("Expected "..thing.." "..expected..", got "..actual)
15- end
16- end
17-
18- function deepeq(t1, t2, prefix)
19- prefix = prefix or ""
20- local ty1 = type(t1)
21- local ty2 = type(t2)
22- if ty1 ~= ty2 then
23- local msg = "different types for lhs"..prefix.." ("..ty1..") and rhs"..prefix.." ("..ty2..")"
24- return false, {msg}
25- end
26-
27- -- non-table types can be directly compared
28- if ty1 ~= 'table' and ty2 ~= 'table' then
29- if t1 ~= t2 then
30- local msg = "different values for lhs"..prefix.." ("..t1..") and rhs"..prefix.." ("..t2..")"
31- return false, {msg}
32- end
33- return true, {}
34- end
35-
36- equal = true
37- diffs = {}
38- for k2, v2 in pairs(t2) do
39- local v1 = t1[k2]
40- if v1 == nil then
41- equal = false
42- diffs[#diffs+1] = "missing lhs"..prefix.."."..k2
43- else
44- local e, d = deepeq(v1, v2, prefix.."."..k2)
45- equal = equal and e
46- table.move(d, 1, #d, #diffs+1, diffs)
47- end
48- end
49- for k1, v1 in pairs(t1) do
50- local v2 = t2[k1]
51- if v2 == nil then
52- equal = false
53- diffs[#diffs+1] = "missing rhs"..prefix.."."..k1
54- end
55- end
56- return equal, diffs
57- end
58-
59- function assert_deepeq(thing, expected, actual)
60- local eq, diffs = deepeq(expected, actual)
61- if not eq then
62- error("Unexpected "..thing..": "..table.concat(diffs, ", "))
63- end
64- end
65-
66- function values(t)
67- local i = 0
68- return function() i = i + 1; return t[i] end
69- end
70-
71- function iter_tostring(...)
72- local result = {}
73- for element in ... do
74- table.insert(result, tostring(element))
75- end
76- return result
77- end
78- "# ;
79-
80- fn new_lua ( ) -> mlua:: Lua {
81- let l = mlua:: Lua :: new ( ) ;
82- l. load ( TEST_PRELUDE )
83- . set_name ( "test prelude" )
84- . exec ( )
85- . expect ( "Error loading test prelude" ) ;
86- l
87- }
88-
8912trait CheckLua {
90- /// Executes a chunk of Lua code. If it returns a string, interprets that string as an
91- /// error message, and translates that into an `anyhow` error.
92- fn check_without_graph ( & self , chunk : & str ) -> Result < ( ) , mlua:: Error > ;
9313 fn check ( & self , graph : & mut StackGraph , chunk : & str ) -> Result < ( ) , mlua:: Error > ;
9414}
9515
9616impl CheckLua for mlua:: Lua {
97- fn check_without_graph ( & self , chunk : & str ) -> Result < ( ) , mlua:: Error > {
98- self . load ( chunk) . set_name ( "test chunk" ) . exec ( )
99- }
100-
10117 fn check ( & self , graph : & mut StackGraph , chunk : & str ) -> Result < ( ) , mlua:: Error > {
10218 self . scope ( |scope| {
10319 let graph = scope. create_userdata_ref_mut ( graph) ;
@@ -106,37 +22,9 @@ impl CheckLua for mlua::Lua {
10622 }
10723}
10824
109- #[ test]
110- fn can_deepeq_from_lua ( ) -> Result < ( ) , anyhow:: Error > {
111- let l = new_lua ( ) ;
112- l. check_without_graph (
113- r#"
114- function check_deepeq(lhs, rhs, expected, expected_diffs)
115- local actual, actual_diffs = deepeq(lhs, rhs)
116- actual_diffs = table.concat(actual_diffs, ", ")
117- assert_eq("deepeq", expected, actual)
118- assert_eq("differences", expected_diffs, actual_diffs)
119- end
120-
121- check_deepeq(0, 0, true, "")
122- check_deepeq(0, 1, false, "different values for lhs (0) and rhs (1)")
123-
124- check_deepeq({"a", "b", "c"}, {"a", "b", "c"}, true, "")
125- check_deepeq({"a", "b", "c"}, {"a", "b"}, false, "missing rhs.3")
126- check_deepeq({"a", "b", "c"}, {"a", "b", "d"}, false, "different values for lhs.3 (c) and rhs.3 (d)")
127-
128- check_deepeq({a=1, b=2, c=3}, {a=1, b=2, c=3}, true, "")
129- check_deepeq({a=1, b=2, c=3}, {a=1, b=2}, false, "missing rhs.c")
130- check_deepeq({a=1, b=2, c=3}, {a=1, b=2, c=4}, false, "different values for lhs.c (3) and rhs.c (4)")
131- check_deepeq({a=1, b=2, c=3}, {a=1, b=2, d=3}, false, "missing lhs.d, missing rhs.c")
132- "# ,
133- ) ?;
134- Ok ( ( ) )
135- }
136-
13725#[ test]
13826fn can_create_nodes_from_lua ( ) -> Result < ( ) , anyhow:: Error > {
139- let l = new_lua ( ) ;
27+ let l = new_lua ( ) ? ;
14028 let mut graph = StackGraph :: new ( ) ;
14129 l. check (
14230 & mut graph,
@@ -164,7 +52,7 @@ fn can_create_nodes_from_lua() -> Result<(), anyhow::Error> {
16452
16553#[ test]
16654fn can_set_source_info_from_lua ( ) -> Result < ( ) , anyhow:: Error > {
167- let l = new_lua ( ) ;
55+ let l = new_lua ( ) ? ;
16856 let mut graph = StackGraph :: new ( ) ;
16957 l. check (
17058 & mut graph,
@@ -202,7 +90,7 @@ fn can_set_source_info_from_lua() -> Result<(), anyhow::Error> {
20290
20391#[ test]
20492fn can_set_debug_info_from_lua ( ) -> Result < ( ) , anyhow:: Error > {
205- let l = new_lua ( ) ;
93+ let l = new_lua ( ) ? ;
20694 let mut graph = StackGraph :: new ( ) ;
20795 l. check (
20896 & mut graph,
@@ -221,7 +109,7 @@ fn can_set_debug_info_from_lua() -> Result<(), anyhow::Error> {
221109
222110#[ test]
223111fn can_create_edges_from_lua ( ) -> Result < ( ) , anyhow:: Error > {
224- let l = new_lua ( ) ;
112+ let l = new_lua ( ) ? ;
225113 let mut graph = StackGraph :: new ( ) ;
226114 l. check (
227115 & mut graph,
@@ -269,7 +157,7 @@ fn can_create_edges_from_lua() -> Result<(), anyhow::Error> {
269157
270158#[ test]
271159fn can_create_all_node_types_from_lua ( ) -> Result < ( ) , anyhow:: Error > {
272- let l = new_lua ( ) ;
160+ let l = new_lua ( ) ? ;
273161 let mut graph = StackGraph :: new ( ) ;
274162 l. check (
275163 & mut graph,
@@ -314,7 +202,7 @@ fn can_create_all_node_types_from_lua() -> Result<(), anyhow::Error> {
314202
315203#[ test]
316204fn can_iterate_nodes_in_file ( ) -> Result < ( ) , anyhow:: Error > {
317- let l = new_lua ( ) ;
205+ let l = new_lua ( ) ? ;
318206 let mut graph = StackGraph :: new ( ) ;
319207 l. check (
320208 & mut graph,
0 commit comments