Skip to content

Commit 1987c13

Browse files
committed
Move Lua test helpers out into separate (unpublished) package
1 parent 951d92f commit 1987c13

File tree

5 files changed

+121
-105
lines changed

5 files changed

+121
-105
lines changed

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: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
64+
pub fn new_lua() -> Result<mlua::Lua, mlua::Error> {
65+
let l = mlua::Lua::new();
66+
l.load(TEST_PRELUDE).set_name("test prelude").exec()?;
67+
Ok(l)
68+
}
69+
70+
pub trait CheckLua {
71+
fn check(&self, chunk: &str) -> Result<(), mlua::Error>;
72+
}
73+
74+
impl CheckLua for mlua::Lua {
75+
fn check(&self, chunk: &str) -> Result<(), mlua::Error> {
76+
self.load(chunk).set_name("test chunk").exec()
77+
}
78+
}
79+
80+
#[test]
81+
fn can_deepeq_from_lua() -> Result<(), mlua::Error> {
82+
let l = new_lua()?;
83+
l.check(
84+
r#"
85+
function check_deepeq(lhs, rhs, expected, expected_diffs)
86+
local actual, actual_diffs = deepeq(lhs, rhs)
87+
actual_diffs = table.concat(actual_diffs, ", ")
88+
assert_eq("deepeq", expected, actual)
89+
assert_eq("differences", expected_diffs, actual_diffs)
90+
end
91+
92+
check_deepeq(0, 0, true, "")
93+
check_deepeq(0, 1, false, "different values for lhs (0) and rhs (1)")
94+
95+
check_deepeq({"a", "b", "c"}, {"a", "b", "c"}, true, "")
96+
check_deepeq({"a", "b", "c"}, {"a", "b"}, false, "missing rhs.3")
97+
check_deepeq({"a", "b", "c"}, {"a", "b", "d"}, false, "different values for lhs.3 (c) and rhs.3 (d)")
98+
99+
check_deepeq({a=1, b=2, c=3}, {a=1, b=2, c=3}, true, "")
100+
check_deepeq({a=1, b=2, c=3}, {a=1, b=2}, false, "missing rhs.c")
101+
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)")
102+
check_deepeq({a=1, b=2, c=3}, {a=1, b=2, d=3}, false, "missing lhs.d, missing rhs.c")
103+
"#,
104+
)?;
105+
Ok(())
106+
}

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: 6 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -7,87 +7,16 @@
77

88
use std::collections::HashSet;
99

10+
use lua_helpers::new_lua;
1011
use maplit::hashset;
1112
use stack_graphs::graph::NodeID;
1213
use stack_graphs::graph::StackGraph;
1314

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-
7915
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>;
8316
fn check(&self, graph: &mut StackGraph, chunk: &str) -> Result<(), mlua::Error>;
8417
}
8518

8619
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-
9120
fn check(&self, graph: &mut StackGraph, chunk: &str) -> Result<(), mlua::Error> {
9221
self.scope(|scope| {
9322
let graph = scope.create_userdata_ref_mut(graph);
@@ -96,37 +25,9 @@ impl CheckLua for mlua::Lua {
9625
}
9726
}
9827

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-
12728
#[test]
12829
fn can_create_nodes_from_lua() -> Result<(), anyhow::Error> {
129-
let l = new_lua();
30+
let l = new_lua()?;
13031
let mut graph = StackGraph::new();
13132
l.check(
13233
&mut graph,
@@ -154,7 +55,7 @@ fn can_create_nodes_from_lua() -> Result<(), anyhow::Error> {
15455

15556
#[test]
15657
fn can_set_source_info_from_lua() -> Result<(), anyhow::Error> {
157-
let l = new_lua();
58+
let l = new_lua()?;
15859
let mut graph = StackGraph::new();
15960
l.check(&mut graph,
16061
r#"
@@ -191,7 +92,7 @@ fn can_set_source_info_from_lua() -> Result<(), anyhow::Error> {
19192

19293
#[test]
19394
fn can_set_debug_info_from_lua() -> Result<(), anyhow::Error> {
194-
let l = new_lua();
95+
let l = new_lua()?;
19596
let mut graph = StackGraph::new();
19697
l.check(
19798
&mut graph,
@@ -210,7 +111,7 @@ fn can_set_debug_info_from_lua() -> Result<(), anyhow::Error> {
210111

211112
#[test]
212113
fn can_create_edges_from_lua() -> Result<(), anyhow::Error> {
213-
let l = new_lua();
114+
let l = new_lua()?;
214115
let mut graph = StackGraph::new();
215116
l.check(
216117
&mut graph,
@@ -249,7 +150,7 @@ fn can_create_edges_from_lua() -> Result<(), anyhow::Error> {
249150

250151
#[test]
251152
fn can_create_all_node_types_from_lua() -> Result<(), anyhow::Error> {
252-
let l = new_lua();
153+
let l = new_lua()?;
253154
let mut graph = StackGraph::new();
254155
l.check(
255156
&mut graph,

0 commit comments

Comments
 (0)