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

Commit b8d894b

Browse files
author
Hendrik van Antwerpen
committed
Cleanup tests with extra helpers
1 parent a22b75a commit b8d894b

File tree

2 files changed

+106
-121
lines changed

2 files changed

+106
-121
lines changed

stack-graphs/tests/it/cycles.rs

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

8-
use stack_graphs::graph::NodeID;
98
use stack_graphs::graph::StackGraph;
109
use stack_graphs::partial::PartialPaths;
1110

@@ -16,158 +15,111 @@ use crate::util::*;
1615

1716
#[test]
1817
fn renaming_path_is_productive() {
19-
let mut g = StackGraph::new();
18+
let mut graph = StackGraph::new();
19+
let file = graph.add_file("test").unwrap();
20+
let s = create_scope_node(&mut graph, file, false);
21+
let foo_def = create_pop_symbol_node(&mut graph, file, "foo", true);
22+
let bar_ref = create_push_symbol_node(&mut graph, file, "bar", true);
2023

21-
let f = g.add_file("test").unwrap();
22-
23-
let s = g.add_scope_node(NodeID::new_in_file(f, 0), false).unwrap();
24-
25-
let foo = g.add_symbol("foo");
26-
let bar = g.add_symbol("bar");
27-
let foo_def = g
28-
.add_pop_symbol_node(NodeID::new_in_file(f, 1), foo, true)
29-
.unwrap();
30-
let bar_ref = g
31-
.add_push_symbol_node(NodeID::new_in_file(f, 2), bar, true)
24+
let mut partials = PartialPaths::new();
25+
let p = create_partial_path_and_edges(&mut graph, &mut partials, &[s, foo_def, bar_ref, s])
3226
.unwrap();
3327

34-
let mut ps = PartialPaths::new();
35-
let p = create_partial_path_and_edges(&mut g, &mut ps, &[s, foo_def, bar_ref, s]).unwrap();
36-
37-
assert!(p.is_productive(&g, &mut ps));
28+
assert!(p.is_productive(&graph, &mut partials));
3829
}
3930

4031
#[test]
4132
fn renaming_root_path_is_productive() {
42-
let mut g = StackGraph::new();
43-
44-
let f = g.add_file("test").unwrap();
45-
33+
let mut graph = StackGraph::new();
34+
let file = graph.add_file("test").unwrap();
4635
let s = StackGraph::root_node();
36+
let foo_def = create_pop_symbol_node(&mut graph, file, "foo", true);
37+
let bar_ref = create_push_symbol_node(&mut graph, file, "bar", true);
4738

48-
let foo = g.add_symbol("foo");
49-
let bar = g.add_symbol("bar");
50-
let foo_def = g
51-
.add_pop_symbol_node(NodeID::new_in_file(f, 1), foo, true)
52-
.unwrap();
53-
let bar_ref = g
54-
.add_push_symbol_node(NodeID::new_in_file(f, 2), bar, true)
39+
let mut partials = PartialPaths::new();
40+
let p = create_partial_path_and_edges(&mut graph, &mut partials, &[s, foo_def, bar_ref, s])
5541
.unwrap();
5642

57-
let mut ps = PartialPaths::new();
58-
let p = create_partial_path_and_edges(&mut g, &mut ps, &[s, foo_def, bar_ref, s]).unwrap();
59-
60-
assert!(p.is_productive(&g, &mut ps));
43+
assert!(p.is_productive(&graph, &mut partials));
6144
}
6245

6346
#[test]
6447
fn introducing_path_is_unproductive() {
65-
let mut g = StackGraph::new();
48+
let mut graph = StackGraph::new();
49+
let file = graph.add_file("test").unwrap();
50+
let s = create_scope_node(&mut graph, file, false);
51+
let bar_ref = create_push_symbol_node(&mut graph, file, "bar", true);
6652

67-
let f = g.add_file("test").unwrap();
53+
let mut partials = PartialPaths::new();
54+
let p = create_partial_path_and_edges(&mut graph, &mut partials, &[s, bar_ref, s]).unwrap();
6855

69-
let s = g.add_scope_node(NodeID::new_in_file(f, 0), false).unwrap();
70-
71-
let bar = g.add_symbol("bar");
72-
let bar_ref = g
73-
.add_push_symbol_node(NodeID::new_in_file(f, 1), bar, true)
74-
.unwrap();
75-
76-
let mut ps = PartialPaths::new();
77-
let p = create_partial_path_and_edges(&mut g, &mut ps, &[s, bar_ref, s]).unwrap();
78-
79-
assert!(!p.is_productive(&g, &mut ps));
56+
assert!(!p.is_productive(&graph, &mut partials));
8057
}
8158

8259
#[test]
8360
fn eliminating_path_is_productive() {
84-
let mut g = StackGraph::new();
85-
86-
let f = g.add_file("test").unwrap();
61+
let mut graph = StackGraph::new();
62+
let file = graph.add_file("test").unwrap();
63+
let s = create_scope_node(&mut graph, file, false);
64+
let foo_def = create_pop_symbol_node(&mut graph, file, "foo", true);
8765

88-
let s = g.add_scope_node(NodeID::new_in_file(f, 0), false).unwrap();
89-
90-
let foo = g.add_symbol("foo");
91-
let foo_def = g
92-
.add_pop_symbol_node(NodeID::new_in_file(f, 1), foo, true)
93-
.unwrap();
66+
let mut partials = PartialPaths::new();
67+
let p = create_partial_path_and_edges(&mut graph, &mut partials, &[s, foo_def, s]).unwrap();
9468

95-
let mut ps = PartialPaths::new();
96-
let p = create_partial_path_and_edges(&mut g, &mut ps, &[s, foo_def, s]).unwrap();
97-
98-
assert!(p.is_productive(&g, &mut ps));
69+
assert!(p.is_productive(&graph, &mut partials));
9970
}
10071

10172
#[test]
10273
fn identity_path_is_unproductive() {
103-
let mut g = StackGraph::new();
104-
105-
let f = g.add_file("test").unwrap();
74+
let mut graph = StackGraph::new();
75+
let file = graph.add_file("test").unwrap();
76+
let s = create_scope_node(&mut graph, file, false);
77+
let bar_def = create_pop_symbol_node(&mut graph, file, "bar", true);
78+
let bar_ref = create_push_symbol_node(&mut graph, file, "bar", true);
10679

107-
let s = g.add_scope_node(NodeID::new_in_file(f, 0), false).unwrap();
108-
109-
let bar = g.add_symbol("bar");
110-
let bar_def = g
111-
.add_pop_symbol_node(NodeID::new_in_file(f, 1), bar, true)
112-
.unwrap();
113-
let bar_ref = g
114-
.add_push_symbol_node(NodeID::new_in_file(f, 2), bar, true)
80+
let mut partials = PartialPaths::new();
81+
let p = create_partial_path_and_edges(&mut graph, &mut partials, &[s, bar_def, bar_ref, s])
11582
.unwrap();
11683

117-
let mut ps = PartialPaths::new();
118-
let p = create_partial_path_and_edges(&mut g, &mut ps, &[s, bar_def, bar_ref, s]).unwrap();
119-
120-
assert!(!p.is_productive(&g, &mut ps));
84+
assert!(!p.is_productive(&graph, &mut partials));
12185
}
12286

12387
#[test]
12488
fn one_step_forward_two_steps_back_path_is_unproductive() {
125-
let mut g = StackGraph::new();
126-
127-
let f = g.add_file("test").unwrap();
128-
129-
let s = g.add_scope_node(NodeID::new_in_file(f, 0), false).unwrap();
130-
131-
let foo = g.add_symbol("foo");
132-
let foo_def = g
133-
.add_pop_symbol_node(NodeID::new_in_file(f, 1), foo, true)
134-
.unwrap();
135-
let foo_ref1 = g
136-
.add_push_symbol_node(NodeID::new_in_file(f, 2), foo, true)
137-
.unwrap();
138-
let foo_ref2 = g
139-
.add_push_symbol_node(NodeID::new_in_file(f, 3), foo, true)
140-
.unwrap();
141-
142-
let mut ps = PartialPaths::new();
143-
let p = create_partial_path_and_edges(&mut g, &mut ps, &[s, foo_def, foo_ref1, foo_ref2, s])
144-
.unwrap();
145-
146-
assert!(!p.is_productive(&g, &mut ps));
89+
let mut graph = StackGraph::new();
90+
let file = graph.add_file("test").unwrap();
91+
let s = create_scope_node(&mut graph, file, false);
92+
let foo_def = create_pop_symbol_node(&mut graph, file, "foo", true);
93+
let foo_ref1 = create_push_symbol_node(&mut graph, file, "foo", true);
94+
let foo_ref2 = create_push_symbol_node(&mut graph, file, "foo", true);
95+
96+
let mut partials = PartialPaths::new();
97+
let p = create_partial_path_and_edges(
98+
&mut graph,
99+
&mut partials,
100+
&[s, foo_def, foo_ref1, foo_ref2, s],
101+
)
102+
.unwrap();
103+
104+
assert!(!p.is_productive(&graph, &mut partials));
147105
}
148106

149107
#[test]
150108
fn two_steps_forward_one_step_back_path_is_productive() {
151-
let mut g = StackGraph::new();
152-
153-
let f = g.add_file("test").unwrap();
154-
155-
let s = g.add_scope_node(NodeID::new_in_file(f, 0), false).unwrap();
156-
157-
let foo = g.add_symbol("foo");
158-
let foo_def1 = g
159-
.add_pop_symbol_node(NodeID::new_in_file(f, 1), foo, true)
160-
.unwrap();
161-
let foo_def2 = g
162-
.add_pop_symbol_node(NodeID::new_in_file(f, 2), foo, true)
163-
.unwrap();
164-
let foo_ref = g
165-
.add_push_symbol_node(NodeID::new_in_file(f, 3), foo, true)
166-
.unwrap();
167-
168-
let mut ps = PartialPaths::new();
169-
let p = create_partial_path_and_edges(&mut g, &mut ps, &[s, foo_def1, foo_def2, foo_ref, s])
170-
.unwrap();
171-
172-
assert!(p.is_productive(&g, &mut ps));
109+
let mut graph = StackGraph::new();
110+
let file = graph.add_file("test").unwrap();
111+
let s = create_scope_node(&mut graph, file, false);
112+
let foo_def1 = create_pop_symbol_node(&mut graph, file, "foo", true);
113+
let foo_def2 = create_pop_symbol_node(&mut graph, file, "foo", true);
114+
let foo_ref = create_push_symbol_node(&mut graph, file, "foo", true);
115+
116+
let mut partials = PartialPaths::new();
117+
let p = create_partial_path_and_edges(
118+
&mut graph,
119+
&mut partials,
120+
&[s, foo_def1, foo_def2, foo_ref, s],
121+
)
122+
.unwrap();
123+
124+
assert!(p.is_productive(&graph, &mut partials));
173125
}

stack-graphs/tests/it/util.rs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use controlled_option::ControlledOption;
99
use stack_graphs::arena::Handle;
1010
use stack_graphs::graph::Edge;
11+
use stack_graphs::graph::File;
1112
use stack_graphs::graph::Node;
1213
use stack_graphs::graph::NodeID;
1314
use stack_graphs::graph::StackGraph;
@@ -25,6 +26,41 @@ pub(crate) type NiceScopedSymbol<'a> = (&'a str, Option<NiceScopeStack<'a>>);
2526
pub(crate) type NiceScopeStack<'a> = (&'a [u32], Option<ScopeStackVariable>);
2627
pub(crate) type NicePartialPath<'a> = &'a [Handle<Node>];
2728

29+
pub(crate) fn create_scope_node(
30+
graph: &mut StackGraph,
31+
file: Handle<File>,
32+
is_exported: bool,
33+
) -> Handle<Node> {
34+
let id = graph.new_node_id(file);
35+
graph.add_scope_node(id, is_exported).unwrap()
36+
}
37+
38+
pub(crate) fn create_push_symbol_node(
39+
graph: &mut StackGraph,
40+
file: Handle<File>,
41+
symbol: &str,
42+
is_reference: bool,
43+
) -> Handle<Node> {
44+
let id = graph.new_node_id(file);
45+
let symbol = graph.add_symbol(symbol);
46+
graph
47+
.add_push_symbol_node(id, symbol, is_reference)
48+
.unwrap()
49+
}
50+
51+
pub(crate) fn create_pop_symbol_node(
52+
graph: &mut StackGraph,
53+
file: Handle<File>,
54+
symbol: &str,
55+
is_definition: bool,
56+
) -> Handle<Node> {
57+
let id = graph.new_node_id(file);
58+
let symbol = graph.add_symbol(symbol);
59+
graph
60+
.add_pop_symbol_node(id, symbol, is_definition)
61+
.unwrap()
62+
}
63+
2864
pub(crate) fn create_symbol_stack(
2965
graph: &mut StackGraph,
3066
partials: &mut PartialPaths,
@@ -76,14 +112,11 @@ pub(crate) fn create_partial_path_and_edges(
76112
partials: &mut PartialPaths,
77113
contents: NicePartialPath,
78114
) -> Result<PartialPath, PathResolutionError> {
79-
let mut g = StackGraph::new();
80-
g.add_from_graph(graph).expect("");
81-
82115
let mut nodes = contents.iter();
83116
let mut prev = nodes.next().unwrap();
84117
let mut path = PartialPath::from_node(graph, partials, *prev);
85118
for next in nodes {
86-
g.add_edge(*prev, *next, 0);
119+
graph.add_edge(*prev, *next, 0);
87120
path.append(
88121
graph,
89122
partials,

0 commit comments

Comments
 (0)