Skip to content

Commit 9a499d5

Browse files
committed
move tests to their respective files
1 parent 3957eae commit 9a499d5

File tree

6 files changed

+691
-658
lines changed

6 files changed

+691
-658
lines changed

server/src/dfs.rs

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,184 @@ pub mod error {
153153
}
154154
}
155155
}
156+
157+
#[cfg(test)]
158+
mod dfs_test {
159+
use std::path::PathBuf;
160+
161+
use hamcrest2::prelude::*;
162+
use hamcrest2::{assert_that, ok};
163+
use petgraph::{algo::is_cyclic_directed, graph::NodeIndex};
164+
165+
use crate::graph::CachedStableGraph;
166+
use crate::{dfs, IncludePosition};
167+
168+
#[test]
169+
fn test_graph_dfs() {
170+
{
171+
let mut graph = CachedStableGraph::new();
172+
173+
let idx0 = graph.add_node(&PathBuf::from("0"));
174+
let idx1 = graph.add_node(&PathBuf::from("1"));
175+
let idx2 = graph.add_node(&PathBuf::from("2"));
176+
let idx3 = graph.add_node(&PathBuf::from("3"));
177+
178+
graph.add_edge(idx0, idx1, IncludePosition { line: 2, start: 0, end: 0 });
179+
graph.add_edge(idx0, idx2, IncludePosition { line: 3, start: 0, end: 0 });
180+
graph.add_edge(idx1, idx3, IncludePosition { line: 5, start: 0, end: 0 });
181+
182+
let dfs = dfs::Dfs::new(&graph, idx0);
183+
184+
let mut collection = Vec::new();
185+
186+
for i in dfs {
187+
assert_that!(&i, ok());
188+
collection.push(i.unwrap());
189+
}
190+
191+
let nodes: Vec<NodeIndex> = collection.iter().map(|n| n.0).collect();
192+
let parents: Vec<Option<NodeIndex>> = collection.iter().map(|n| n.1).collect();
193+
// 0
194+
// / \
195+
// 1 2
196+
// /
197+
// 3
198+
let expected_nodes = vec![idx0, idx1, idx3, idx2];
199+
200+
assert_eq!(expected_nodes, nodes);
201+
202+
let expected_parents = vec![None, Some(idx0), Some(idx1), Some(idx0)];
203+
204+
assert_eq!(expected_parents, parents);
205+
206+
assert!(!is_cyclic_directed(&graph.graph));
207+
}
208+
{
209+
let mut graph = CachedStableGraph::new();
210+
211+
let idx0 = graph.add_node(&PathBuf::from("0"));
212+
let idx1 = graph.add_node(&PathBuf::from("1"));
213+
let idx2 = graph.add_node(&PathBuf::from("2"));
214+
let idx3 = graph.add_node(&PathBuf::from("3"));
215+
let idx4 = graph.add_node(&PathBuf::from("4"));
216+
let idx5 = graph.add_node(&PathBuf::from("5"));
217+
let idx6 = graph.add_node(&PathBuf::from("6"));
218+
let idx7 = graph.add_node(&PathBuf::from("7"));
219+
220+
graph.add_edge(idx0, idx1, IncludePosition { line: 2, start: 0, end: 0 });
221+
graph.add_edge(idx0, idx2, IncludePosition { line: 3, start: 0, end: 0 });
222+
graph.add_edge(idx1, idx3, IncludePosition { line: 5, start: 0, end: 0 });
223+
graph.add_edge(idx1, idx4, IncludePosition { line: 6, start: 0, end: 0 });
224+
graph.add_edge(idx2, idx4, IncludePosition { line: 5, start: 0, end: 0 });
225+
graph.add_edge(idx2, idx5, IncludePosition { line: 4, start: 0, end: 0 });
226+
graph.add_edge(idx3, idx6, IncludePosition { line: 4, start: 0, end: 0 });
227+
graph.add_edge(idx4, idx6, IncludePosition { line: 4, start: 0, end: 0 });
228+
graph.add_edge(idx6, idx7, IncludePosition { line: 4, start: 0, end: 0 });
229+
230+
let dfs = dfs::Dfs::new(&graph, idx0);
231+
232+
let mut collection = Vec::new();
233+
234+
for i in dfs {
235+
assert_that!(&i, ok());
236+
collection.push(i.unwrap());
237+
}
238+
239+
let nodes: Vec<NodeIndex> = collection.iter().map(|n| n.0).collect();
240+
let parents: Vec<Option<NodeIndex>> = collection.iter().map(|n| n.1).collect();
241+
// 0
242+
// / \
243+
// 1 2
244+
// / \ / \
245+
// 3 4 5
246+
// \ /
247+
// 6 - 7
248+
let expected_nodes = vec![idx0, idx1, idx3, idx6, idx7, idx4, idx6, idx7, idx2, idx5, idx4, idx6, idx7];
249+
250+
assert_eq!(expected_nodes, nodes);
251+
252+
let expected_parents = vec![
253+
None,
254+
Some(idx0),
255+
Some(idx1),
256+
Some(idx3),
257+
Some(idx6),
258+
Some(idx1),
259+
Some(idx4),
260+
Some(idx6),
261+
Some(idx0),
262+
Some(idx2),
263+
Some(idx2),
264+
Some(idx4),
265+
Some(idx6),
266+
];
267+
268+
assert_eq!(expected_parents, parents);
269+
270+
assert!(!is_cyclic_directed(&graph.graph));
271+
}
272+
}
273+
274+
#[test]
275+
fn test_graph_dfs_cycle() {
276+
{
277+
let mut graph = CachedStableGraph::new();
278+
279+
let idx0 = graph.add_node(&PathBuf::from("0"));
280+
let idx1 = graph.add_node(&PathBuf::from("1"));
281+
let idx2 = graph.add_node(&PathBuf::from("2"));
282+
let idx3 = graph.add_node(&PathBuf::from("3"));
283+
let idx4 = graph.add_node(&PathBuf::from("4"));
284+
let idx5 = graph.add_node(&PathBuf::from("5"));
285+
let idx6 = graph.add_node(&PathBuf::from("6"));
286+
let idx7 = graph.add_node(&PathBuf::from("7"));
287+
288+
graph.add_edge(idx0, idx1, IncludePosition { line: 2, start: 0, end: 0 });
289+
graph.add_edge(idx0, idx2, IncludePosition { line: 3, start: 0, end: 0 });
290+
graph.add_edge(idx1, idx3, IncludePosition { line: 5, start: 0, end: 0 });
291+
graph.add_edge(idx1, idx4, IncludePosition { line: 6, start: 0, end: 0 });
292+
graph.add_edge(idx2, idx4, IncludePosition { line: 5, start: 0, end: 0 });
293+
graph.add_edge(idx2, idx5, IncludePosition { line: 4, start: 0, end: 0 });
294+
graph.add_edge(idx3, idx6, IncludePosition { line: 4, start: 0, end: 0 });
295+
graph.add_edge(idx4, idx6, IncludePosition { line: 4, start: 0, end: 0 });
296+
graph.add_edge(idx6, idx7, IncludePosition { line: 4, start: 0, end: 0 });
297+
graph.add_edge(idx7, idx4, IncludePosition { line: 4, start: 0, end: 0 });
298+
299+
let mut dfs = dfs::Dfs::new(&graph, idx0);
300+
301+
for _ in 0..5 {
302+
if let Some(i) = dfs.next() {
303+
assert_that!(&i, ok());
304+
}
305+
}
306+
307+
// 0
308+
// / \
309+
// 1 2
310+
// / \ / \
311+
// 3 4 5
312+
// \ / \
313+
// 6 - 7
314+
315+
assert!(is_cyclic_directed(&graph.graph));
316+
317+
let next = dfs.next().unwrap();
318+
assert_that!(next, err());
319+
}
320+
{
321+
let mut graph = CachedStableGraph::new();
322+
323+
let idx0 = graph.add_node(&PathBuf::from("0"));
324+
let idx1 = graph.add_node(&PathBuf::from("1"));
325+
326+
graph.add_edge(idx0, idx1, IncludePosition { line: 2, start: 0, end: 0 });
327+
graph.add_edge(idx1, idx0, IncludePosition { line: 2, start: 0, end: 0 });
328+
329+
let mut dfs = dfs::Dfs::new(&graph, idx1);
330+
331+
println!("{:?}", dfs.next());
332+
println!("{:?}", dfs.next());
333+
println!("{:?}", dfs.next());
334+
}
335+
}
336+
}

server/src/diagnostics_parser.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,55 @@ where
108108
}
109109
diagnostics
110110
}
111+
112+
#[cfg(test)]
113+
mod diagnostics_test {
114+
use std::{path::PathBuf, str::FromStr};
115+
116+
use crate::{diagnostics_parser::parse_diagnostics_output, opengl::MockShaderValidator, test::new_temp_server};
117+
118+
#[test]
119+
fn test_nvidia_diagnostics() {
120+
let mut mockgl = MockShaderValidator::new();
121+
mockgl.expect_vendor().returning(|| "NVIDIA Corporation".into());
122+
let server = new_temp_server(Some(Box::new(mockgl)));
123+
124+
let output = "/home/noah/.minecraft/shaderpacks/test/shaders/final.fsh(9) : error C0000: syntax error, unexpected '}', expecting ',' or ';' at token \"}\"";
125+
126+
let results = parse_diagnostics_output(
127+
output.to_string(),
128+
&PathBuf::from_str("/home/noah/.minecraft/shaderpacks/test").unwrap(),
129+
server.opengl_context.as_ref(),
130+
);
131+
132+
assert_eq!(results.len(), 1);
133+
let first = results.into_iter().next().unwrap();
134+
assert_eq!(
135+
first.0,
136+
url::Url::from_file_path("/home/noah/.minecraft/shaderpacks/test/shaders/final.fsh").unwrap()
137+
);
138+
server.endpoint.request_shutdown();
139+
}
140+
141+
#[test]
142+
fn test_amd_diagnostics() {
143+
let mut mockgl = MockShaderValidator::new();
144+
mockgl.expect_vendor().returning(|| "ATI Technologies".into());
145+
let server = new_temp_server(Some(Box::new(mockgl)));
146+
147+
let output = "ERROR: 0:1: '' : syntax error: #line
148+
ERROR: 0:10: '' : syntax error: #line
149+
ERROR: 0:15: 'varying' : syntax error: syntax error
150+
";
151+
152+
let results = parse_diagnostics_output(
153+
output.to_string(),
154+
&PathBuf::from_str("/home/test").unwrap(),
155+
server.opengl_context.as_ref(),
156+
);
157+
assert_eq!(results.len(), 1);
158+
let first = results.into_iter().next().unwrap();
159+
assert_eq!(first.1.len(), 3);
160+
server.endpoint.request_shutdown();
161+
}
162+
}

0 commit comments

Comments
 (0)