Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit ea2e203

Browse files
committed
Move Lua test helpers out into separate (unpublished) package
1 parent 9a30497 commit ea2e203

5 files changed

Lines changed: 135 additions & 119 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ resolver = "1"
33
members = [
44
# library projects
55
"lsp-positions",
6+
"lua-helpers",
67
"stack-graphs",
78
"tree-sitter-stack-graphs",
89
"languages/*",

lua-helpers/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "lua-helpers"
3+
version = "0.0.0"
4+
description = "Unpublished helper methods for our Lua test cases"
5+
6+
[dependencies]
7+
mlua = { version = "0.9" }

lua-helpers/src/lib.rs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// -*- coding: utf-8 -*-
2+
// ------------------------------------------------------------------------------------------------
3+
// Copyright © 2023, stack-graphs authors.
4+
// Licensed under either of Apache License, Version 2.0, or MIT license, at your option.
5+
// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
6+
// ------------------------------------------------------------------------------------------------
7+
8+
const TEST_PRELUDE: &str = r#"
9+
function assert_eq(thing, expected, actual)
10+
if expected ~= actual then
11+
error("Expected "..thing.." "..expected..", got "..actual)
12+
end
13+
end
14+
15+
function deepeq(t1, t2, prefix)
16+
prefix = prefix or ""
17+
local ty1 = type(t1)
18+
local ty2 = type(t2)
19+
if ty1 ~= ty2 then
20+
local msg = "different types for lhs"..prefix.." ("..ty1..") and rhs"..prefix.." ("..ty2..")"
21+
return false, {msg}
22+
end
23+
24+
-- non-table types can be directly compared
25+
if ty1 ~= 'table' and ty2 ~= 'table' then
26+
if t1 ~= t2 then
27+
local msg = "different values for lhs"..prefix.." ("..t1..") and rhs"..prefix.." ("..t2..")"
28+
return false, {msg}
29+
end
30+
return true, {}
31+
end
32+
33+
local equal = true
34+
local diffs = {}
35+
for k2, v2 in pairs(t2) do
36+
local v1 = t1[k2]
37+
if v1 == nil then
38+
equal = false
39+
diffs[#diffs+1] = "missing lhs"..prefix.."."..k2
40+
else
41+
local e, d = deepeq(v1, v2, prefix.."."..k2)
42+
equal = equal and e
43+
table.move(d, 1, #d, #diffs+1, diffs)
44+
end
45+
end
46+
for k1, v1 in pairs(t1) do
47+
local v2 = t2[k1]
48+
if v2 == nil then
49+
equal = false
50+
diffs[#diffs+1] = "missing rhs"..prefix.."."..k1
51+
end
52+
end
53+
return equal, diffs
54+
end
55+
56+
function assert_deepeq(thing, expected, actual)
57+
local eq, diffs = deepeq(expected, actual)
58+
if not eq then
59+
error("Unexpected "..thing..": "..table.concat(diffs, ", "))
60+
end
61+
end
62+
63+
function values(t)
64+
local i = 0
65+
return function() i = i + 1; return t[i] end
66+
end
67+
68+
function iter_tostring(...)
69+
local result = {}
70+
for element in ... do
71+
table.insert(result, tostring(element))
72+
end
73+
return result
74+
end
75+
"#;
76+
77+
pub fn new_lua() -> Result<mlua::Lua, mlua::Error> {
78+
let l = mlua::Lua::new();
79+
l.load(TEST_PRELUDE).set_name("test prelude").exec()?;
80+
Ok(l)
81+
}
82+
83+
pub trait CheckLua {
84+
fn check(&self, chunk: &str) -> Result<(), mlua::Error>;
85+
}
86+
87+
impl CheckLua for mlua::Lua {
88+
fn check(&self, chunk: &str) -> Result<(), mlua::Error> {
89+
self.load(chunk).set_name("test chunk").exec()
90+
}
91+
}
92+
93+
#[test]
94+
fn can_deepeq_from_lua() -> Result<(), mlua::Error> {
95+
let l = new_lua()?;
96+
l.check(
97+
r#"
98+
function check_deepeq(lhs, rhs, expected, expected_diffs)
99+
local actual, actual_diffs = deepeq(lhs, rhs)
100+
actual_diffs = table.concat(actual_diffs, ", ")
101+
assert_eq("deepeq", expected, actual)
102+
assert_eq("differences", expected_diffs, actual_diffs)
103+
end
104+
105+
check_deepeq(0, 0, true, "")
106+
check_deepeq(0, 1, false, "different values for lhs (0) and rhs (1)")
107+
108+
check_deepeq({"a", "b", "c"}, {"a", "b", "c"}, true, "")
109+
check_deepeq({"a", "b", "c"}, {"a", "b"}, false, "missing rhs.3")
110+
check_deepeq({"a", "b", "c"}, {"a", "b", "d"}, false, "different values for lhs.3 (c) and rhs.3 (d)")
111+
112+
check_deepeq({a=1, b=2, c=3}, {a=1, b=2, c=3}, true, "")
113+
check_deepeq({a=1, b=2, c=3}, {a=1, b=2}, false, "missing rhs.c")
114+
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)")
115+
check_deepeq({a=1, b=2, c=3}, {a=1, b=2, d=3}, false, "missing lhs.d, missing rhs.c")
116+
"#,
117+
)?;
118+
Ok(())
119+
}

stack-graphs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ thiserror = { version = "1.0" }
4646
anyhow = "1.0"
4747
assert-json-diff = "2"
4848
itertools = "0.10"
49+
lua-helpers = { path = "../lua-helpers" }
4950
maplit = "1.0"
5051
pretty_assertions = "0.7"
5152
serde_json = { version = "1.0" }

stack-graphs/tests/it/lua.rs

Lines changed: 7 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -5,99 +5,15 @@
55
// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
66
// ------------------------------------------------------------------------------------------------
77

8+
use lua_helpers::new_lua;
89
use stack_graphs::graph::NodeID;
910
use 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-
8912
trait 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

9616
impl 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]
13826
fn 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]
16654
fn 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]
20492
fn 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]
223111
fn 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]
271159
fn 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]
316204
fn 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

Comments
 (0)