7
7
8
8
use std:: collections:: HashSet ;
9
9
10
+ use lua_helpers:: new_lua;
10
11
use maplit:: hashset;
11
12
use stack_graphs:: graph:: NodeID ;
12
13
use stack_graphs:: graph:: StackGraph ;
13
14
14
- const TEST_PRELUDE : & str = r#"
15
- function assert_eq(thing, expected, actual)
16
- if expected ~= actual then
17
- error("Expected "..thing.." "..expected..", got "..actual)
18
- end
19
- end
20
-
21
- function deepeq(t1, t2, prefix)
22
- prefix = prefix or ""
23
- local ty1 = type(t1)
24
- local ty2 = type(t2)
25
- if ty1 ~= ty2 then
26
- local msg = "different types for lhs"..prefix.." ("..ty1..") and rhs"..prefix.." ("..ty2..")"
27
- return false, {msg}
28
- end
29
-
30
- -- non-table types can be directly compared
31
- if ty1 ~= 'table' and ty2 ~= 'table' then
32
- if t1 ~= t2 then
33
- local msg = "different values for lhs"..prefix.." ("..t1..") and rhs"..prefix.." ("..t2..")"
34
- return false, {msg}
35
- end
36
- return true, {}
37
- end
38
-
39
- equal = true
40
- diffs = {}
41
- for k2, v2 in pairs(t2) do
42
- local v1 = t1[k2]
43
- if v1 == nil then
44
- equal = false
45
- diffs[#diffs+1] = "missing lhs"..prefix.."."..k2
46
- else
47
- local e, d = deepeq(v1, v2, prefix.."."..k2)
48
- equal = equal and e
49
- table.move(d, 1, #d, #diffs+1, diffs)
50
- end
51
- end
52
- for k1, v1 in pairs(t1) do
53
- local v2 = t2[k1]
54
- if v2 == nil then
55
- equal = false
56
- diffs[#diffs+1] = "missing rhs"..prefix.."."..k1
57
- end
58
- end
59
- return equal, diffs
60
- end
61
-
62
- function assert_deepeq(thing, expected, actual)
63
- local eq, diffs = deepeq(expected, actual)
64
- if not eq then
65
- error("Unexpected "..thing..": "..table.concat(diffs, ", "))
66
- end
67
- end
68
- "# ;
69
-
70
- fn new_lua ( ) -> mlua:: Lua {
71
- let l = mlua:: Lua :: new ( ) ;
72
- l. load ( TEST_PRELUDE )
73
- . set_name ( "test prelude" )
74
- . exec ( )
75
- . expect ( "Error loading test prelude" ) ;
76
- l
77
- }
78
-
79
15
trait CheckLua {
80
- /// Executes a chunk of Lua code. If it returns a string, interprets that string as an
81
- /// error message, and translates that into an `anyhow` error.
82
- fn check_without_graph ( & self , chunk : & str ) -> Result < ( ) , mlua:: Error > ;
83
16
fn check ( & self , graph : & mut StackGraph , chunk : & str ) -> Result < ( ) , mlua:: Error > ;
84
17
}
85
18
86
19
impl CheckLua for mlua:: Lua {
87
- fn check_without_graph ( & self , chunk : & str ) -> Result < ( ) , mlua:: Error > {
88
- self . load ( chunk) . set_name ( "test chunk" ) . exec ( )
89
- }
90
-
91
20
fn check ( & self , graph : & mut StackGraph , chunk : & str ) -> Result < ( ) , mlua:: Error > {
92
21
self . scope ( |scope| {
93
22
let graph = scope. create_userdata_ref_mut ( graph) ;
@@ -96,37 +25,9 @@ impl CheckLua for mlua::Lua {
96
25
}
97
26
}
98
27
99
- #[ test]
100
- fn can_deepeq_from_lua ( ) -> Result < ( ) , anyhow:: Error > {
101
- let l = new_lua ( ) ;
102
- l. check_without_graph (
103
- r#"
104
- function check_deepeq(lhs, rhs, expected, expected_diffs)
105
- local actual, actual_diffs = deepeq(lhs, rhs)
106
- actual_diffs = table.concat(actual_diffs, ", ")
107
- assert_eq("deepeq", expected, actual)
108
- assert_eq("differences", expected_diffs, actual_diffs)
109
- end
110
-
111
- check_deepeq(0, 0, true, "")
112
- check_deepeq(0, 1, false, "different values for lhs (0) and rhs (1)")
113
-
114
- check_deepeq({"a", "b", "c"}, {"a", "b", "c"}, true, "")
115
- check_deepeq({"a", "b", "c"}, {"a", "b"}, false, "missing rhs.3")
116
- check_deepeq({"a", "b", "c"}, {"a", "b", "d"}, false, "different values for lhs.3 (c) and rhs.3 (d)")
117
-
118
- check_deepeq({a=1, b=2, c=3}, {a=1, b=2, c=3}, true, "")
119
- check_deepeq({a=1, b=2, c=3}, {a=1, b=2}, false, "missing rhs.c")
120
- 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)")
121
- check_deepeq({a=1, b=2, c=3}, {a=1, b=2, d=3}, false, "missing lhs.d, missing rhs.c")
122
- "# ,
123
- ) ?;
124
- Ok ( ( ) )
125
- }
126
-
127
28
#[ test]
128
29
fn can_create_nodes_from_lua ( ) -> Result < ( ) , anyhow:: Error > {
129
- let l = new_lua ( ) ;
30
+ let l = new_lua ( ) ? ;
130
31
let mut graph = StackGraph :: new ( ) ;
131
32
l. check (
132
33
& mut graph,
@@ -154,7 +55,7 @@ fn can_create_nodes_from_lua() -> Result<(), anyhow::Error> {
154
55
155
56
#[ test]
156
57
fn can_set_source_info_from_lua ( ) -> Result < ( ) , anyhow:: Error > {
157
- let l = new_lua ( ) ;
58
+ let l = new_lua ( ) ? ;
158
59
let mut graph = StackGraph :: new ( ) ;
159
60
l. check ( & mut graph,
160
61
r#"
@@ -191,7 +92,7 @@ fn can_set_source_info_from_lua() -> Result<(), anyhow::Error> {
191
92
192
93
#[ test]
193
94
fn can_set_debug_info_from_lua ( ) -> Result < ( ) , anyhow:: Error > {
194
- let l = new_lua ( ) ;
95
+ let l = new_lua ( ) ? ;
195
96
let mut graph = StackGraph :: new ( ) ;
196
97
l. check (
197
98
& mut graph,
@@ -210,7 +111,7 @@ fn can_set_debug_info_from_lua() -> Result<(), anyhow::Error> {
210
111
211
112
#[ test]
212
113
fn can_create_edges_from_lua ( ) -> Result < ( ) , anyhow:: Error > {
213
- let l = new_lua ( ) ;
114
+ let l = new_lua ( ) ? ;
214
115
let mut graph = StackGraph :: new ( ) ;
215
116
l. check (
216
117
& mut graph,
@@ -249,7 +150,7 @@ fn can_create_edges_from_lua() -> Result<(), anyhow::Error> {
249
150
250
151
#[ test]
251
152
fn can_create_all_node_types_from_lua ( ) -> Result < ( ) , anyhow:: Error > {
252
- let l = new_lua ( ) ;
153
+ let l = new_lua ( ) ? ;
253
154
let mut graph = StackGraph :: new ( ) ;
254
155
l. check (
255
156
& mut graph,
0 commit comments