mirror of
https://github.com/Strum355/mcshader-lsp.git
synced 2025-08-31 05:47:22 +00:00
move tests to their respective files
This commit is contained in:
parent
3957eaed17
commit
9a499d581b
6 changed files with 691 additions and 658 deletions
|
@ -153,3 +153,184 @@ pub mod error {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod dfs_test {
|
||||
use std::path::PathBuf;
|
||||
|
||||
use hamcrest2::prelude::*;
|
||||
use hamcrest2::{assert_that, ok};
|
||||
use petgraph::{algo::is_cyclic_directed, graph::NodeIndex};
|
||||
|
||||
use crate::graph::CachedStableGraph;
|
||||
use crate::{dfs, IncludePosition};
|
||||
|
||||
#[test]
|
||||
fn test_graph_dfs() {
|
||||
{
|
||||
let mut graph = CachedStableGraph::new();
|
||||
|
||||
let idx0 = graph.add_node(&PathBuf::from("0"));
|
||||
let idx1 = graph.add_node(&PathBuf::from("1"));
|
||||
let idx2 = graph.add_node(&PathBuf::from("2"));
|
||||
let idx3 = graph.add_node(&PathBuf::from("3"));
|
||||
|
||||
graph.add_edge(idx0, idx1, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
graph.add_edge(idx0, idx2, IncludePosition { line: 3, start: 0, end: 0 });
|
||||
graph.add_edge(idx1, idx3, IncludePosition { line: 5, start: 0, end: 0 });
|
||||
|
||||
let dfs = dfs::Dfs::new(&graph, idx0);
|
||||
|
||||
let mut collection = Vec::new();
|
||||
|
||||
for i in dfs {
|
||||
assert_that!(&i, ok());
|
||||
collection.push(i.unwrap());
|
||||
}
|
||||
|
||||
let nodes: Vec<NodeIndex> = collection.iter().map(|n| n.0).collect();
|
||||
let parents: Vec<Option<NodeIndex>> = collection.iter().map(|n| n.1).collect();
|
||||
// 0
|
||||
// / \
|
||||
// 1 2
|
||||
// /
|
||||
// 3
|
||||
let expected_nodes = vec![idx0, idx1, idx3, idx2];
|
||||
|
||||
assert_eq!(expected_nodes, nodes);
|
||||
|
||||
let expected_parents = vec![None, Some(idx0), Some(idx1), Some(idx0)];
|
||||
|
||||
assert_eq!(expected_parents, parents);
|
||||
|
||||
assert!(!is_cyclic_directed(&graph.graph));
|
||||
}
|
||||
{
|
||||
let mut graph = CachedStableGraph::new();
|
||||
|
||||
let idx0 = graph.add_node(&PathBuf::from("0"));
|
||||
let idx1 = graph.add_node(&PathBuf::from("1"));
|
||||
let idx2 = graph.add_node(&PathBuf::from("2"));
|
||||
let idx3 = graph.add_node(&PathBuf::from("3"));
|
||||
let idx4 = graph.add_node(&PathBuf::from("4"));
|
||||
let idx5 = graph.add_node(&PathBuf::from("5"));
|
||||
let idx6 = graph.add_node(&PathBuf::from("6"));
|
||||
let idx7 = graph.add_node(&PathBuf::from("7"));
|
||||
|
||||
graph.add_edge(idx0, idx1, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
graph.add_edge(idx0, idx2, IncludePosition { line: 3, start: 0, end: 0 });
|
||||
graph.add_edge(idx1, idx3, IncludePosition { line: 5, start: 0, end: 0 });
|
||||
graph.add_edge(idx1, idx4, IncludePosition { line: 6, start: 0, end: 0 });
|
||||
graph.add_edge(idx2, idx4, IncludePosition { line: 5, start: 0, end: 0 });
|
||||
graph.add_edge(idx2, idx5, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
graph.add_edge(idx3, idx6, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
graph.add_edge(idx4, idx6, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
graph.add_edge(idx6, idx7, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
|
||||
let dfs = dfs::Dfs::new(&graph, idx0);
|
||||
|
||||
let mut collection = Vec::new();
|
||||
|
||||
for i in dfs {
|
||||
assert_that!(&i, ok());
|
||||
collection.push(i.unwrap());
|
||||
}
|
||||
|
||||
let nodes: Vec<NodeIndex> = collection.iter().map(|n| n.0).collect();
|
||||
let parents: Vec<Option<NodeIndex>> = collection.iter().map(|n| n.1).collect();
|
||||
// 0
|
||||
// / \
|
||||
// 1 2
|
||||
// / \ / \
|
||||
// 3 4 5
|
||||
// \ /
|
||||
// 6 - 7
|
||||
let expected_nodes = vec![idx0, idx1, idx3, idx6, idx7, idx4, idx6, idx7, idx2, idx5, idx4, idx6, idx7];
|
||||
|
||||
assert_eq!(expected_nodes, nodes);
|
||||
|
||||
let expected_parents = vec![
|
||||
None,
|
||||
Some(idx0),
|
||||
Some(idx1),
|
||||
Some(idx3),
|
||||
Some(idx6),
|
||||
Some(idx1),
|
||||
Some(idx4),
|
||||
Some(idx6),
|
||||
Some(idx0),
|
||||
Some(idx2),
|
||||
Some(idx2),
|
||||
Some(idx4),
|
||||
Some(idx6),
|
||||
];
|
||||
|
||||
assert_eq!(expected_parents, parents);
|
||||
|
||||
assert!(!is_cyclic_directed(&graph.graph));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_graph_dfs_cycle() {
|
||||
{
|
||||
let mut graph = CachedStableGraph::new();
|
||||
|
||||
let idx0 = graph.add_node(&PathBuf::from("0"));
|
||||
let idx1 = graph.add_node(&PathBuf::from("1"));
|
||||
let idx2 = graph.add_node(&PathBuf::from("2"));
|
||||
let idx3 = graph.add_node(&PathBuf::from("3"));
|
||||
let idx4 = graph.add_node(&PathBuf::from("4"));
|
||||
let idx5 = graph.add_node(&PathBuf::from("5"));
|
||||
let idx6 = graph.add_node(&PathBuf::from("6"));
|
||||
let idx7 = graph.add_node(&PathBuf::from("7"));
|
||||
|
||||
graph.add_edge(idx0, idx1, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
graph.add_edge(idx0, idx2, IncludePosition { line: 3, start: 0, end: 0 });
|
||||
graph.add_edge(idx1, idx3, IncludePosition { line: 5, start: 0, end: 0 });
|
||||
graph.add_edge(idx1, idx4, IncludePosition { line: 6, start: 0, end: 0 });
|
||||
graph.add_edge(idx2, idx4, IncludePosition { line: 5, start: 0, end: 0 });
|
||||
graph.add_edge(idx2, idx5, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
graph.add_edge(idx3, idx6, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
graph.add_edge(idx4, idx6, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
graph.add_edge(idx6, idx7, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
graph.add_edge(idx7, idx4, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
|
||||
let mut dfs = dfs::Dfs::new(&graph, idx0);
|
||||
|
||||
for _ in 0..5 {
|
||||
if let Some(i) = dfs.next() {
|
||||
assert_that!(&i, ok());
|
||||
}
|
||||
}
|
||||
|
||||
// 0
|
||||
// / \
|
||||
// 1 2
|
||||
// / \ / \
|
||||
// 3 4 5
|
||||
// \ / \
|
||||
// 6 - 7
|
||||
|
||||
assert!(is_cyclic_directed(&graph.graph));
|
||||
|
||||
let next = dfs.next().unwrap();
|
||||
assert_that!(next, err());
|
||||
}
|
||||
{
|
||||
let mut graph = CachedStableGraph::new();
|
||||
|
||||
let idx0 = graph.add_node(&PathBuf::from("0"));
|
||||
let idx1 = graph.add_node(&PathBuf::from("1"));
|
||||
|
||||
graph.add_edge(idx0, idx1, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
graph.add_edge(idx1, idx0, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
|
||||
let mut dfs = dfs::Dfs::new(&graph, idx1);
|
||||
|
||||
println!("{:?}", dfs.next());
|
||||
println!("{:?}", dfs.next());
|
||||
println!("{:?}", dfs.next());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -108,3 +108,55 @@ where
|
|||
}
|
||||
diagnostics
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod diagnostics_test {
|
||||
use std::{path::PathBuf, str::FromStr};
|
||||
|
||||
use crate::{diagnostics_parser::parse_diagnostics_output, opengl::MockShaderValidator, test::new_temp_server};
|
||||
|
||||
#[test]
|
||||
fn test_nvidia_diagnostics() {
|
||||
let mut mockgl = MockShaderValidator::new();
|
||||
mockgl.expect_vendor().returning(|| "NVIDIA Corporation".into());
|
||||
let server = new_temp_server(Some(Box::new(mockgl)));
|
||||
|
||||
let output = "/home/noah/.minecraft/shaderpacks/test/shaders/final.fsh(9) : error C0000: syntax error, unexpected '}', expecting ',' or ';' at token \"}\"";
|
||||
|
||||
let results = parse_diagnostics_output(
|
||||
output.to_string(),
|
||||
&PathBuf::from_str("/home/noah/.minecraft/shaderpacks/test").unwrap(),
|
||||
server.opengl_context.as_ref(),
|
||||
);
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
let first = results.into_iter().next().unwrap();
|
||||
assert_eq!(
|
||||
first.0,
|
||||
url::Url::from_file_path("/home/noah/.minecraft/shaderpacks/test/shaders/final.fsh").unwrap()
|
||||
);
|
||||
server.endpoint.request_shutdown();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_amd_diagnostics() {
|
||||
let mut mockgl = MockShaderValidator::new();
|
||||
mockgl.expect_vendor().returning(|| "ATI Technologies".into());
|
||||
let server = new_temp_server(Some(Box::new(mockgl)));
|
||||
|
||||
let output = "ERROR: 0:1: '' : syntax error: #line
|
||||
ERROR: 0:10: '' : syntax error: #line
|
||||
ERROR: 0:15: 'varying' : syntax error: syntax error
|
||||
";
|
||||
|
||||
let results = parse_diagnostics_output(
|
||||
output.to_string(),
|
||||
&PathBuf::from_str("/home/test").unwrap(),
|
||||
server.opengl_context.as_ref(),
|
||||
);
|
||||
assert_eq!(results.len(), 1);
|
||||
let first = results.into_iter().next().unwrap();
|
||||
assert_eq!(first.1.len(), 3);
|
||||
server.endpoint.request_shutdown();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -162,3 +162,177 @@ impl CachedStableGraph {
|
|||
collection
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod graph_test {
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::{graph::CachedStableGraph, IncludePosition};
|
||||
|
||||
#[test]
|
||||
fn test_graph_two_connected_nodes() {
|
||||
let mut graph = CachedStableGraph::new();
|
||||
|
||||
let idx1 = graph.add_node(&PathBuf::from("sample"));
|
||||
let idx2 = graph.add_node(&PathBuf::from("banana"));
|
||||
graph.add_edge(idx1, idx2, IncludePosition { line: 3, start: 0, end: 0 });
|
||||
|
||||
let children = graph.child_node_names(idx1);
|
||||
assert_eq!(children.len(), 1);
|
||||
assert_eq!(children[0], Into::<PathBuf>::into("banana".to_string()));
|
||||
|
||||
let children = graph.child_node_indexes(idx1);
|
||||
assert_eq!(children.len(), 1);
|
||||
assert_eq!(children[0], idx2);
|
||||
|
||||
let parents = graph.parent_node_names(idx1);
|
||||
assert_eq!(parents.len(), 0);
|
||||
|
||||
let parents = graph.parent_node_names(idx2);
|
||||
assert_eq!(parents.len(), 1);
|
||||
assert_eq!(parents[0], Into::<PathBuf>::into("sample".to_string()));
|
||||
|
||||
let parents = graph.parent_node_indexes(idx2);
|
||||
assert_eq!(parents.len(), 1);
|
||||
assert_eq!(parents[0], idx1);
|
||||
|
||||
let ancestors = graph.collect_root_ancestors(idx2);
|
||||
assert_eq!(ancestors.len(), 1);
|
||||
assert_eq!(ancestors[0], idx1);
|
||||
|
||||
let ancestors = graph.collect_root_ancestors(idx1);
|
||||
assert_eq!(ancestors.len(), 0);
|
||||
|
||||
graph.remove_node(&PathBuf::from("sample"));
|
||||
assert_eq!(graph.graph.node_count(), 1);
|
||||
assert!(graph.find_node(&PathBuf::from("sample")).is_none());
|
||||
let neighbors = graph.child_node_names(idx2);
|
||||
assert_eq!(neighbors.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_collect_root_ancestors() {
|
||||
{
|
||||
let mut graph = CachedStableGraph::new();
|
||||
|
||||
let idx0 = graph.add_node(&PathBuf::from("0"));
|
||||
let idx1 = graph.add_node(&PathBuf::from("1"));
|
||||
let idx2 = graph.add_node(&PathBuf::from("2"));
|
||||
let idx3 = graph.add_node(&PathBuf::from("3"));
|
||||
|
||||
graph.add_edge(idx0, idx1, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
graph.add_edge(idx1, idx2, IncludePosition { line: 3, start: 0, end: 0 });
|
||||
graph.add_edge(idx3, idx1, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
|
||||
// 0 3
|
||||
// |/
|
||||
// 1
|
||||
// |
|
||||
// 2
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx2);
|
||||
assert_eq!(roots, vec![idx3, idx0]);
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx1);
|
||||
assert_eq!(roots, vec![idx3, idx0]);
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx0);
|
||||
assert_eq!(roots, vec![]);
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx3);
|
||||
assert_eq!(roots, vec![]);
|
||||
}
|
||||
{
|
||||
let mut graph = CachedStableGraph::new();
|
||||
|
||||
let idx0 = graph.add_node(&PathBuf::from("0"));
|
||||
let idx1 = graph.add_node(&PathBuf::from("1"));
|
||||
let idx2 = graph.add_node(&PathBuf::from("2"));
|
||||
let idx3 = graph.add_node(&PathBuf::from("3"));
|
||||
|
||||
graph.add_edge(idx0, idx1, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
graph.add_edge(idx0, idx2, IncludePosition { line: 3, start: 0, end: 0 });
|
||||
graph.add_edge(idx1, idx3, IncludePosition { line: 5, start: 0, end: 0 });
|
||||
|
||||
// 0
|
||||
// / \
|
||||
// 1 2
|
||||
// /
|
||||
// 3
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx3);
|
||||
assert_eq!(roots, vec![idx0]);
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx2);
|
||||
assert_eq!(roots, vec![idx0]);
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx1);
|
||||
assert_eq!(roots, vec![idx0]);
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx0);
|
||||
assert_eq!(roots, vec![]);
|
||||
}
|
||||
{
|
||||
let mut graph = CachedStableGraph::new();
|
||||
|
||||
let idx0 = graph.add_node(&PathBuf::from("0"));
|
||||
let idx1 = graph.add_node(&PathBuf::from("1"));
|
||||
let idx2 = graph.add_node(&PathBuf::from("2"));
|
||||
let idx3 = graph.add_node(&PathBuf::from("3"));
|
||||
|
||||
graph.add_edge(idx0, idx1, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
graph.add_edge(idx2, idx3, IncludePosition { line: 3, start: 0, end: 0 });
|
||||
graph.add_edge(idx1, idx3, IncludePosition { line: 5, start: 0, end: 0 });
|
||||
|
||||
// 0
|
||||
// |
|
||||
// 1
|
||||
// \
|
||||
// 2 \
|
||||
// \ /
|
||||
// 3
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx3);
|
||||
assert_eq!(roots, vec![idx0, idx2]);
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx2);
|
||||
assert_eq!(roots, vec![]);
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx1);
|
||||
assert_eq!(roots, vec![idx0]);
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx0);
|
||||
assert_eq!(roots, vec![]);
|
||||
}
|
||||
{
|
||||
let mut graph = CachedStableGraph::new();
|
||||
|
||||
let idx0 = graph.add_node(&PathBuf::from("0"));
|
||||
let idx1 = graph.add_node(&PathBuf::from("1"));
|
||||
let idx2 = graph.add_node(&PathBuf::from("2"));
|
||||
let idx3 = graph.add_node(&PathBuf::from("3"));
|
||||
|
||||
graph.add_edge(idx0, idx1, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
graph.add_edge(idx1, idx2, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
graph.add_edge(idx1, idx3, IncludePosition { line: 6, start: 0, end: 0 });
|
||||
|
||||
// 0
|
||||
// |
|
||||
// 1
|
||||
// / \
|
||||
// 2 3
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx3);
|
||||
assert_eq!(roots, vec![idx0]);
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx2);
|
||||
assert_eq!(roots, vec![idx0]);
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx1);
|
||||
assert_eq!(roots, vec![idx0]);
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx0);
|
||||
assert_eq!(roots, vec![]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -104,7 +104,7 @@ fn main() {
|
|||
LSPEndpoint::run_server_from_input(&mut stdin().lock(), endpoint_output, langserver);
|
||||
}
|
||||
|
||||
struct MinecraftShaderLanguageServer {
|
||||
pub struct MinecraftShaderLanguageServer {
|
||||
endpoint: Endpoint,
|
||||
graph: Rc<RefCell<graph::CachedStableGraph>>,
|
||||
root: PathBuf,
|
||||
|
|
|
@ -213,3 +213,284 @@ fn unsafe_get_and_insert(merge_list: &mut LinkedList<&str>, line_directives: &[S
|
|||
merge_list.push_back(&vec_ptr_offset.as_ref().unwrap()[..]);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod merge_view_test {
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::merge_views::generate_merge_list;
|
||||
use crate::test::{copy_to_and_set_root, new_temp_server};
|
||||
use crate::IncludePosition;
|
||||
|
||||
#[test]
|
||||
fn test_generate_merge_list_01() {
|
||||
let mut server = new_temp_server(None);
|
||||
|
||||
let (_tmp_dir, tmp_path) = copy_to_and_set_root("./testdata/01", &mut server);
|
||||
server.endpoint.request_shutdown();
|
||||
|
||||
let final_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{:?}/shaders/final.fsh", tmp_path).try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("final.fsh"));
|
||||
let common_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{:?}/shaders/common.glsl", tmp_path).try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("common.glsl"));
|
||||
|
||||
server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
.add_edge(final_idx, common_idx, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
|
||||
let nodes = server.get_dfs_for_node(final_idx).unwrap();
|
||||
let sources = server.load_sources(&nodes).unwrap();
|
||||
|
||||
let graph_borrow = server.graph.borrow();
|
||||
let result = generate_merge_list(&nodes, &sources, &graph_borrow);
|
||||
|
||||
let merge_file = tmp_path.join("shaders").join("final.fsh.merge");
|
||||
|
||||
let mut truth = fs::read_to_string(merge_file).unwrap();
|
||||
truth = truth.replacen(
|
||||
"!!",
|
||||
&tmp_path.join("shaders").join("common.glsl").to_str().unwrap().replace('\\', "\\\\"),
|
||||
1,
|
||||
);
|
||||
truth = truth.replace(
|
||||
"!!",
|
||||
&tmp_path.join("shaders").join("final.fsh").to_str().unwrap().replace('\\', "\\\\"),
|
||||
);
|
||||
|
||||
assert_eq!(result, truth);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generate_merge_list_02() {
|
||||
let mut server = new_temp_server(None);
|
||||
|
||||
let (_tmp_dir, tmp_path) = copy_to_and_set_root("./testdata/02", &mut server);
|
||||
server.endpoint.request_shutdown();
|
||||
|
||||
let final_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/{}", tmp_path, "final.fsh").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("final.fsh"));
|
||||
let test_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/utils/{}", tmp_path, "test.glsl").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("utils").join("test.glsl"));
|
||||
let burger_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/utils/{}", tmp_path, "burger.glsl").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("utils").join("burger.glsl"));
|
||||
let sample_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/utils/{}", tmp_path, "sample.glsl").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("utils").join("sample.glsl"));
|
||||
|
||||
server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
.add_edge(final_idx, sample_idx, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
.add_edge(sample_idx, burger_idx, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
.add_edge(sample_idx, test_idx, IncludePosition { line: 6, start: 0, end: 0 });
|
||||
|
||||
let nodes = server.get_dfs_for_node(final_idx).unwrap();
|
||||
let sources = server.load_sources(&nodes).unwrap();
|
||||
|
||||
let graph_borrow = server.graph.borrow();
|
||||
let result = generate_merge_list(&nodes, &sources, &graph_borrow);
|
||||
|
||||
let merge_file = tmp_path.join("shaders").join("final.fsh.merge");
|
||||
|
||||
let mut truth = fs::read_to_string(merge_file).unwrap();
|
||||
|
||||
for file in &["sample.glsl", "burger.glsl", "sample.glsl", "test.glsl", "sample.glsl"] {
|
||||
let path = tmp_path.clone();
|
||||
truth = truth.replacen(
|
||||
"!!",
|
||||
&path
|
||||
.join("shaders")
|
||||
.join("utils")
|
||||
.join(file)
|
||||
.to_str()
|
||||
.unwrap()
|
||||
.replace('\\', "\\\\"),
|
||||
1,
|
||||
);
|
||||
}
|
||||
truth = truth.replacen(
|
||||
"!!",
|
||||
&tmp_path.join("shaders").join("final.fsh").to_str().unwrap().replace('\\', "\\\\"),
|
||||
1,
|
||||
);
|
||||
|
||||
assert_eq!(result, truth);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generate_merge_list_03() {
|
||||
let mut server = new_temp_server(None);
|
||||
|
||||
let (_tmp_dir, tmp_path) = copy_to_and_set_root("./testdata/03", &mut server);
|
||||
server.endpoint.request_shutdown();
|
||||
|
||||
let final_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/{}", tmp_path, "final.fsh").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("final.fsh"));
|
||||
let test_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/utils/{}", tmp_path, "test.glsl").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("utils").join("test.glsl"));
|
||||
let burger_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/utils/{}", tmp_path, "burger.glsl").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("utils").join("burger.glsl"));
|
||||
let sample_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/utils/{}", tmp_path, "sample.glsl").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("utils").join("sample.glsl"));
|
||||
|
||||
server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
.add_edge(final_idx, sample_idx, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
.add_edge(sample_idx, burger_idx, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
.add_edge(sample_idx, test_idx, IncludePosition { line: 6, start: 0, end: 0 });
|
||||
|
||||
let nodes = server.get_dfs_for_node(final_idx).unwrap();
|
||||
let sources = server.load_sources(&nodes).unwrap();
|
||||
|
||||
let graph_borrow = server.graph.borrow();
|
||||
let result = generate_merge_list(&nodes, &sources, &graph_borrow);
|
||||
|
||||
let merge_file = tmp_path.join("shaders").join("final.fsh.merge");
|
||||
|
||||
let mut truth = fs::read_to_string(merge_file).unwrap();
|
||||
|
||||
for file in &["sample.glsl", "burger.glsl", "sample.glsl", "test.glsl", "sample.glsl"] {
|
||||
let path = tmp_path.clone();
|
||||
truth = truth.replacen(
|
||||
"!!",
|
||||
&path
|
||||
.join("shaders")
|
||||
.join("utils")
|
||||
.join(file)
|
||||
.to_str()
|
||||
.unwrap()
|
||||
.replace('\\', "\\\\"),
|
||||
1,
|
||||
);
|
||||
}
|
||||
truth = truth.replacen(
|
||||
"!!",
|
||||
&tmp_path.join("shaders").join("final.fsh").to_str().unwrap().replace('\\', "\\\\"),
|
||||
1,
|
||||
);
|
||||
|
||||
assert_eq!(result, truth);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generate_merge_list_04() {
|
||||
let mut server = new_temp_server(None);
|
||||
|
||||
let (_tmp_dir, tmp_path) = copy_to_and_set_root("./testdata/04", &mut server);
|
||||
server.endpoint.request_shutdown();
|
||||
|
||||
let final_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/{}", tmp_path, "final.fsh").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("final.fsh"));
|
||||
let utilities_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/utils/{}", tmp_path, "utilities.glsl").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("utils").join("utilities.glsl"));
|
||||
let stuff1_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/utils/{}", tmp_path, "stuff1.glsl").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("utils").join("stuff1.glsl"));
|
||||
let stuff2_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/utils/{}", tmp_path, "stuff2.glsl").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("utils").join("stuff2.glsl"));
|
||||
let matrices_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/lib/{}", tmp_path, "matrices.glsl").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("lib").join("matrices.glsl"));
|
||||
|
||||
server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
.add_edge(final_idx, utilities_idx, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
.add_edge(utilities_idx, stuff1_idx, IncludePosition { line: 0, start: 0, end: 0 });
|
||||
server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
.add_edge(utilities_idx, stuff2_idx, IncludePosition { line: 1, start: 0, end: 0 });
|
||||
server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
.add_edge(final_idx, matrices_idx, IncludePosition { line: 3, start: 0, end: 0 });
|
||||
|
||||
let nodes = server.get_dfs_for_node(final_idx).unwrap();
|
||||
let sources = server.load_sources(&nodes).unwrap();
|
||||
|
||||
let graph_borrow = server.graph.borrow();
|
||||
let result = generate_merge_list(&nodes, &sources, &graph_borrow);
|
||||
|
||||
let merge_file = tmp_path.join("shaders").join("final.fsh.merge");
|
||||
|
||||
let mut truth = fs::read_to_string(merge_file).unwrap();
|
||||
|
||||
for file in &[
|
||||
PathBuf::new().join("utils").join("utilities.glsl").to_str().unwrap(),
|
||||
PathBuf::new().join("utils").join("stuff1.glsl").to_str().unwrap(),
|
||||
PathBuf::new().join("utils").join("utilities.glsl").to_str().unwrap(),
|
||||
PathBuf::new().join("utils").join("stuff2.glsl").to_str().unwrap(),
|
||||
PathBuf::new().join("utils").join("utilities.glsl").to_str().unwrap(),
|
||||
PathBuf::new().join("final.fsh").to_str().unwrap(),
|
||||
PathBuf::new().join("lib").join("matrices.glsl").to_str().unwrap(),
|
||||
PathBuf::new().join("final.fsh").to_str().unwrap(),
|
||||
] {
|
||||
let path = tmp_path.clone();
|
||||
//path.f
|
||||
truth = truth.replacen("!!", &path.join("shaders").join(file).to_str().unwrap().replace('\\', "\\\\"), 1);
|
||||
}
|
||||
|
||||
assert_eq!(result, truth);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,15 +3,12 @@ use std::fs;
|
|||
use std::io;
|
||||
use std::io::Result;
|
||||
|
||||
use hamcrest2::prelude::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
use slog::o;
|
||||
use slog::Logger;
|
||||
use tempdir::TempDir;
|
||||
|
||||
use petgraph::algo::is_cyclic_directed;
|
||||
|
||||
use fs_extra::{copy_items, dir};
|
||||
|
||||
use jsonrpc_common::*;
|
||||
|
@ -36,7 +33,7 @@ impl io::Write for StdoutNewline {
|
|||
}
|
||||
}
|
||||
|
||||
fn new_temp_server(opengl_context: Option<Box<dyn opengl::ShaderValidator>>) -> MinecraftShaderLanguageServer {
|
||||
pub fn new_temp_server(opengl_context: Option<Box<dyn opengl::ShaderValidator>>) -> MinecraftShaderLanguageServer {
|
||||
let endpoint = LSPEndpoint::create_lsp_output_with_output_stream(|| StdoutNewline { s: Box::new(io::sink()) });
|
||||
|
||||
let context = opengl_context.unwrap_or_else(|| Box::new(opengl::MockShaderValidator::new()));
|
||||
|
@ -63,7 +60,7 @@ fn copy_files(files: &str, dest: &TempDir) {
|
|||
copy_items(&files, dest.path().join("shaders"), opts).unwrap();
|
||||
}
|
||||
|
||||
fn copy_to_and_set_root(test_path: &str, server: &mut MinecraftShaderLanguageServer) -> (Rc<TempDir>, PathBuf) {
|
||||
pub fn copy_to_and_set_root(test_path: &str, server: &mut MinecraftShaderLanguageServer) -> (Rc<TempDir>, PathBuf) {
|
||||
let (_tmp_dir, tmp_path) = copy_to_tmp_dir(test_path);
|
||||
|
||||
server.root = tmp_path.clone(); //format!("{}{}", "file://", tmp_path);
|
||||
|
@ -282,655 +279,3 @@ fn test_05_initialize() {
|
|||
assert!(contains, "doesn't contain ({:?}, {:?})", first, second);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_graph_two_connected_nodes() {
|
||||
let mut graph = graph::CachedStableGraph::new();
|
||||
|
||||
let idx1 = graph.add_node(&PathBuf::from("sample"));
|
||||
let idx2 = graph.add_node(&PathBuf::from("banana"));
|
||||
graph.add_edge(idx1, idx2, IncludePosition { line: 3, start: 0, end: 0 });
|
||||
|
||||
let children = graph.child_node_names(idx1);
|
||||
assert_eq!(children.len(), 1);
|
||||
assert_eq!(children[0], Into::<PathBuf>::into("banana".to_string()));
|
||||
|
||||
let children = graph.child_node_indexes(idx1);
|
||||
assert_eq!(children.len(), 1);
|
||||
assert_eq!(children[0], idx2);
|
||||
|
||||
let parents = graph.parent_node_names(idx1);
|
||||
assert_eq!(parents.len(), 0);
|
||||
|
||||
let parents = graph.parent_node_names(idx2);
|
||||
assert_eq!(parents.len(), 1);
|
||||
assert_eq!(parents[0], Into::<PathBuf>::into("sample".to_string()));
|
||||
|
||||
let parents = graph.parent_node_indexes(idx2);
|
||||
assert_eq!(parents.len(), 1);
|
||||
assert_eq!(parents[0], idx1);
|
||||
|
||||
let ancestors = graph.collect_root_ancestors(idx2);
|
||||
assert_eq!(ancestors.len(), 1);
|
||||
assert_eq!(ancestors[0], idx1);
|
||||
|
||||
let ancestors = graph.collect_root_ancestors(idx1);
|
||||
assert_eq!(ancestors.len(), 0);
|
||||
|
||||
graph.remove_node(&PathBuf::from("sample"));
|
||||
assert_eq!(graph.graph.node_count(), 1);
|
||||
assert!(graph.find_node(&PathBuf::from("sample")).is_none());
|
||||
let neighbors = graph.child_node_names(idx2);
|
||||
assert_eq!(neighbors.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_collect_root_ancestors() {
|
||||
{
|
||||
let mut graph = graph::CachedStableGraph::new();
|
||||
|
||||
let idx0 = graph.add_node(&PathBuf::from("0"));
|
||||
let idx1 = graph.add_node(&PathBuf::from("1"));
|
||||
let idx2 = graph.add_node(&PathBuf::from("2"));
|
||||
let idx3 = graph.add_node(&PathBuf::from("3"));
|
||||
|
||||
graph.add_edge(idx0, idx1, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
graph.add_edge(idx1, idx2, IncludePosition { line: 3, start: 0, end: 0 });
|
||||
graph.add_edge(idx3, idx1, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
|
||||
// 0 3
|
||||
// |/
|
||||
// 1
|
||||
// |
|
||||
// 2
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx2);
|
||||
assert_eq!(roots, vec![idx3, idx0]);
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx1);
|
||||
assert_eq!(roots, vec![idx3, idx0]);
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx0);
|
||||
assert_eq!(roots, vec![]);
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx3);
|
||||
assert_eq!(roots, vec![]);
|
||||
}
|
||||
{
|
||||
let mut graph = graph::CachedStableGraph::new();
|
||||
|
||||
let idx0 = graph.add_node(&PathBuf::from("0"));
|
||||
let idx1 = graph.add_node(&PathBuf::from("1"));
|
||||
let idx2 = graph.add_node(&PathBuf::from("2"));
|
||||
let idx3 = graph.add_node(&PathBuf::from("3"));
|
||||
|
||||
graph.add_edge(idx0, idx1, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
graph.add_edge(idx0, idx2, IncludePosition { line: 3, start: 0, end: 0 });
|
||||
graph.add_edge(idx1, idx3, IncludePosition { line: 5, start: 0, end: 0 });
|
||||
|
||||
// 0
|
||||
// / \
|
||||
// 1 2
|
||||
// /
|
||||
// 3
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx3);
|
||||
assert_eq!(roots, vec![idx0]);
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx2);
|
||||
assert_eq!(roots, vec![idx0]);
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx1);
|
||||
assert_eq!(roots, vec![idx0]);
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx0);
|
||||
assert_eq!(roots, vec![]);
|
||||
}
|
||||
{
|
||||
let mut graph = graph::CachedStableGraph::new();
|
||||
|
||||
let idx0 = graph.add_node(&PathBuf::from("0"));
|
||||
let idx1 = graph.add_node(&PathBuf::from("1"));
|
||||
let idx2 = graph.add_node(&PathBuf::from("2"));
|
||||
let idx3 = graph.add_node(&PathBuf::from("3"));
|
||||
|
||||
graph.add_edge(idx0, idx1, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
graph.add_edge(idx2, idx3, IncludePosition { line: 3, start: 0, end: 0 });
|
||||
graph.add_edge(idx1, idx3, IncludePosition { line: 5, start: 0, end: 0 });
|
||||
|
||||
// 0
|
||||
// |
|
||||
// 1
|
||||
// \
|
||||
// 2 \
|
||||
// \ /
|
||||
// 3
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx3);
|
||||
assert_eq!(roots, vec![idx0, idx2]);
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx2);
|
||||
assert_eq!(roots, vec![]);
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx1);
|
||||
assert_eq!(roots, vec![idx0]);
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx0);
|
||||
assert_eq!(roots, vec![]);
|
||||
}
|
||||
{
|
||||
let mut graph = graph::CachedStableGraph::new();
|
||||
|
||||
let idx0 = graph.add_node(&PathBuf::from("0"));
|
||||
let idx1 = graph.add_node(&PathBuf::from("1"));
|
||||
let idx2 = graph.add_node(&PathBuf::from("2"));
|
||||
let idx3 = graph.add_node(&PathBuf::from("3"));
|
||||
|
||||
graph.add_edge(idx0, idx1, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
graph.add_edge(idx1, idx2, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
graph.add_edge(idx1, idx3, IncludePosition { line: 6, start: 0, end: 0 });
|
||||
|
||||
// 0
|
||||
// |
|
||||
// 1
|
||||
// / \
|
||||
// 2 3
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx3);
|
||||
assert_eq!(roots, vec![idx0]);
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx2);
|
||||
assert_eq!(roots, vec![idx0]);
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx1);
|
||||
assert_eq!(roots, vec![idx0]);
|
||||
|
||||
let roots = graph.collect_root_ancestors(idx0);
|
||||
assert_eq!(roots, vec![]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_graph_dfs() {
|
||||
{
|
||||
let mut graph = graph::CachedStableGraph::new();
|
||||
|
||||
let idx0 = graph.add_node(&PathBuf::from("0"));
|
||||
let idx1 = graph.add_node(&PathBuf::from("1"));
|
||||
let idx2 = graph.add_node(&PathBuf::from("2"));
|
||||
let idx3 = graph.add_node(&PathBuf::from("3"));
|
||||
|
||||
graph.add_edge(idx0, idx1, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
graph.add_edge(idx0, idx2, IncludePosition { line: 3, start: 0, end: 0 });
|
||||
graph.add_edge(idx1, idx3, IncludePosition { line: 5, start: 0, end: 0 });
|
||||
|
||||
let dfs = dfs::Dfs::new(&graph, idx0);
|
||||
|
||||
let mut collection = Vec::new();
|
||||
|
||||
for i in dfs {
|
||||
assert_that!(&i, ok());
|
||||
collection.push(i.unwrap());
|
||||
}
|
||||
|
||||
let nodes: Vec<NodeIndex> = collection.iter().map(|n| n.0).collect();
|
||||
let parents: Vec<Option<NodeIndex>> = collection.iter().map(|n| n.1).collect();
|
||||
// 0
|
||||
// / \
|
||||
// 1 2
|
||||
// /
|
||||
// 3
|
||||
let expected_nodes = vec![idx0, idx1, idx3, idx2];
|
||||
|
||||
assert_eq!(expected_nodes, nodes);
|
||||
|
||||
let expected_parents = vec![None, Some(idx0), Some(idx1), Some(idx0)];
|
||||
|
||||
assert_eq!(expected_parents, parents);
|
||||
|
||||
assert!(!is_cyclic_directed(&graph.graph));
|
||||
}
|
||||
{
|
||||
let mut graph = graph::CachedStableGraph::new();
|
||||
|
||||
let idx0 = graph.add_node(&PathBuf::from("0"));
|
||||
let idx1 = graph.add_node(&PathBuf::from("1"));
|
||||
let idx2 = graph.add_node(&PathBuf::from("2"));
|
||||
let idx3 = graph.add_node(&PathBuf::from("3"));
|
||||
let idx4 = graph.add_node(&PathBuf::from("4"));
|
||||
let idx5 = graph.add_node(&PathBuf::from("5"));
|
||||
let idx6 = graph.add_node(&PathBuf::from("6"));
|
||||
let idx7 = graph.add_node(&PathBuf::from("7"));
|
||||
|
||||
graph.add_edge(idx0, idx1, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
graph.add_edge(idx0, idx2, IncludePosition { line: 3, start: 0, end: 0 });
|
||||
graph.add_edge(idx1, idx3, IncludePosition { line: 5, start: 0, end: 0 });
|
||||
graph.add_edge(idx1, idx4, IncludePosition { line: 6, start: 0, end: 0 });
|
||||
graph.add_edge(idx2, idx4, IncludePosition { line: 5, start: 0, end: 0 });
|
||||
graph.add_edge(idx2, idx5, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
graph.add_edge(idx3, idx6, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
graph.add_edge(idx4, idx6, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
graph.add_edge(idx6, idx7, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
|
||||
let dfs = dfs::Dfs::new(&graph, idx0);
|
||||
|
||||
let mut collection = Vec::new();
|
||||
|
||||
for i in dfs {
|
||||
assert_that!(&i, ok());
|
||||
collection.push(i.unwrap());
|
||||
}
|
||||
|
||||
let nodes: Vec<NodeIndex> = collection.iter().map(|n| n.0).collect();
|
||||
let parents: Vec<Option<NodeIndex>> = collection.iter().map(|n| n.1).collect();
|
||||
// 0
|
||||
// / \
|
||||
// 1 2
|
||||
// / \ / \
|
||||
// 3 4 5
|
||||
// \ /
|
||||
// 6 - 7
|
||||
let expected_nodes = vec![idx0, idx1, idx3, idx6, idx7, idx4, idx6, idx7, idx2, idx5, idx4, idx6, idx7];
|
||||
|
||||
assert_eq!(expected_nodes, nodes);
|
||||
|
||||
let expected_parents = vec![
|
||||
None,
|
||||
Some(idx0),
|
||||
Some(idx1),
|
||||
Some(idx3),
|
||||
Some(idx6),
|
||||
Some(idx1),
|
||||
Some(idx4),
|
||||
Some(idx6),
|
||||
Some(idx0),
|
||||
Some(idx2),
|
||||
Some(idx2),
|
||||
Some(idx4),
|
||||
Some(idx6),
|
||||
];
|
||||
|
||||
assert_eq!(expected_parents, parents);
|
||||
|
||||
assert!(!is_cyclic_directed(&graph.graph));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_graph_dfs_cycle() {
|
||||
{
|
||||
let mut graph = graph::CachedStableGraph::new();
|
||||
|
||||
let idx0 = graph.add_node(&PathBuf::from("0"));
|
||||
let idx1 = graph.add_node(&PathBuf::from("1"));
|
||||
let idx2 = graph.add_node(&PathBuf::from("2"));
|
||||
let idx3 = graph.add_node(&PathBuf::from("3"));
|
||||
let idx4 = graph.add_node(&PathBuf::from("4"));
|
||||
let idx5 = graph.add_node(&PathBuf::from("5"));
|
||||
let idx6 = graph.add_node(&PathBuf::from("6"));
|
||||
let idx7 = graph.add_node(&PathBuf::from("7"));
|
||||
|
||||
graph.add_edge(idx0, idx1, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
graph.add_edge(idx0, idx2, IncludePosition { line: 3, start: 0, end: 0 });
|
||||
graph.add_edge(idx1, idx3, IncludePosition { line: 5, start: 0, end: 0 });
|
||||
graph.add_edge(idx1, idx4, IncludePosition { line: 6, start: 0, end: 0 });
|
||||
graph.add_edge(idx2, idx4, IncludePosition { line: 5, start: 0, end: 0 });
|
||||
graph.add_edge(idx2, idx5, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
graph.add_edge(idx3, idx6, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
graph.add_edge(idx4, idx6, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
graph.add_edge(idx6, idx7, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
graph.add_edge(idx7, idx4, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
|
||||
let mut dfs = dfs::Dfs::new(&graph, idx0);
|
||||
|
||||
for _ in 0..5 {
|
||||
if let Some(i) = dfs.next() {
|
||||
assert_that!(&i, ok());
|
||||
}
|
||||
}
|
||||
|
||||
// 0
|
||||
// / \
|
||||
// 1 2
|
||||
// / \ / \
|
||||
// 3 4 5
|
||||
// \ / \
|
||||
// 6 - 7
|
||||
|
||||
assert!(is_cyclic_directed(&graph.graph));
|
||||
|
||||
let next = dfs.next().unwrap();
|
||||
assert_that!(next, err());
|
||||
}
|
||||
{
|
||||
let mut graph = graph::CachedStableGraph::new();
|
||||
|
||||
let idx0 = graph.add_node(&PathBuf::from("0"));
|
||||
let idx1 = graph.add_node(&PathBuf::from("1"));
|
||||
|
||||
graph.add_edge(idx0, idx1, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
graph.add_edge(idx1, idx0, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
|
||||
let mut dfs = dfs::Dfs::new(&graph, idx1);
|
||||
|
||||
println!("{:?}", dfs.next());
|
||||
println!("{:?}", dfs.next());
|
||||
println!("{:?}", dfs.next());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generate_merge_list_01() {
|
||||
let mut server = new_temp_server(None);
|
||||
|
||||
let (_tmp_dir, tmp_path) = copy_to_and_set_root("./testdata/01", &mut server);
|
||||
server.endpoint.request_shutdown();
|
||||
|
||||
let final_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{:?}/shaders/final.fsh", tmp_path).try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("final.fsh"));
|
||||
let common_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{:?}/shaders/common.glsl", tmp_path).try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("common.glsl"));
|
||||
|
||||
server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
.add_edge(final_idx, common_idx, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
|
||||
let nodes = server.get_dfs_for_node(final_idx).unwrap();
|
||||
let sources = server.load_sources(&nodes).unwrap();
|
||||
|
||||
let graph_borrow = server.graph.borrow();
|
||||
let result = merge_views::generate_merge_list(&nodes, &sources, &graph_borrow);
|
||||
|
||||
let merge_file = tmp_path.join("shaders").join("final.fsh.merge");
|
||||
|
||||
let mut truth = fs::read_to_string(merge_file).unwrap();
|
||||
truth = truth.replacen(
|
||||
"!!",
|
||||
&tmp_path.join("shaders").join("common.glsl").to_str().unwrap().replace('\\', "\\\\"),
|
||||
1,
|
||||
);
|
||||
truth = truth.replace(
|
||||
"!!",
|
||||
&tmp_path.join("shaders").join("final.fsh").to_str().unwrap().replace('\\', "\\\\"),
|
||||
);
|
||||
|
||||
assert_eq!(result, truth);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generate_merge_list_02() {
|
||||
let mut server = new_temp_server(None);
|
||||
|
||||
let (_tmp_dir, tmp_path) = copy_to_and_set_root("./testdata/02", &mut server);
|
||||
server.endpoint.request_shutdown();
|
||||
|
||||
let final_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/{}", tmp_path, "final.fsh").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("final.fsh"));
|
||||
let test_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/utils/{}", tmp_path, "test.glsl").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("utils").join("test.glsl"));
|
||||
let burger_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/utils/{}", tmp_path, "burger.glsl").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("utils").join("burger.glsl"));
|
||||
let sample_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/utils/{}", tmp_path, "sample.glsl").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("utils").join("sample.glsl"));
|
||||
|
||||
server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
.add_edge(final_idx, sample_idx, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
.add_edge(sample_idx, burger_idx, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
.add_edge(sample_idx, test_idx, IncludePosition { line: 6, start: 0, end: 0 });
|
||||
|
||||
let nodes = server.get_dfs_for_node(final_idx).unwrap();
|
||||
let sources = server.load_sources(&nodes).unwrap();
|
||||
|
||||
let graph_borrow = server.graph.borrow();
|
||||
let result = merge_views::generate_merge_list(&nodes, &sources, &graph_borrow);
|
||||
|
||||
let merge_file = tmp_path.join("shaders").join("final.fsh.merge");
|
||||
|
||||
let mut truth = fs::read_to_string(merge_file).unwrap();
|
||||
|
||||
for file in &["sample.glsl", "burger.glsl", "sample.glsl", "test.glsl", "sample.glsl"] {
|
||||
let path = tmp_path.clone();
|
||||
truth = truth.replacen(
|
||||
"!!",
|
||||
&path
|
||||
.join("shaders")
|
||||
.join("utils")
|
||||
.join(file)
|
||||
.to_str()
|
||||
.unwrap()
|
||||
.replace('\\', "\\\\"),
|
||||
1,
|
||||
);
|
||||
}
|
||||
truth = truth.replacen(
|
||||
"!!",
|
||||
&tmp_path.join("shaders").join("final.fsh").to_str().unwrap().replace('\\', "\\\\"),
|
||||
1,
|
||||
);
|
||||
|
||||
assert_eq!(result, truth);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generate_merge_list_03() {
|
||||
let mut server = new_temp_server(None);
|
||||
|
||||
let (_tmp_dir, tmp_path) = copy_to_and_set_root("./testdata/03", &mut server);
|
||||
server.endpoint.request_shutdown();
|
||||
|
||||
let final_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/{}", tmp_path, "final.fsh").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("final.fsh"));
|
||||
let test_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/utils/{}", tmp_path, "test.glsl").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("utils").join("test.glsl"));
|
||||
let burger_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/utils/{}", tmp_path, "burger.glsl").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("utils").join("burger.glsl"));
|
||||
let sample_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/utils/{}", tmp_path, "sample.glsl").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("utils").join("sample.glsl"));
|
||||
|
||||
server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
.add_edge(final_idx, sample_idx, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
.add_edge(sample_idx, burger_idx, IncludePosition { line: 4, start: 0, end: 0 });
|
||||
server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
.add_edge(sample_idx, test_idx, IncludePosition { line: 6, start: 0, end: 0 });
|
||||
|
||||
let nodes = server.get_dfs_for_node(final_idx).unwrap();
|
||||
let sources = server.load_sources(&nodes).unwrap();
|
||||
|
||||
let graph_borrow = server.graph.borrow();
|
||||
let result = merge_views::generate_merge_list(&nodes, &sources, &graph_borrow);
|
||||
|
||||
let merge_file = tmp_path.join("shaders").join("final.fsh.merge");
|
||||
|
||||
let mut truth = fs::read_to_string(merge_file).unwrap();
|
||||
|
||||
for file in &["sample.glsl", "burger.glsl", "sample.glsl", "test.glsl", "sample.glsl"] {
|
||||
let path = tmp_path.clone();
|
||||
truth = truth.replacen(
|
||||
"!!",
|
||||
&path
|
||||
.join("shaders")
|
||||
.join("utils")
|
||||
.join(file)
|
||||
.to_str()
|
||||
.unwrap()
|
||||
.replace('\\', "\\\\"),
|
||||
1,
|
||||
);
|
||||
}
|
||||
truth = truth.replacen(
|
||||
"!!",
|
||||
&tmp_path.join("shaders").join("final.fsh").to_str().unwrap().replace('\\', "\\\\"),
|
||||
1,
|
||||
);
|
||||
|
||||
assert_eq!(result, truth);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generate_merge_list_04() {
|
||||
let mut server = new_temp_server(None);
|
||||
|
||||
let (_tmp_dir, tmp_path) = copy_to_and_set_root("./testdata/04", &mut server);
|
||||
server.endpoint.request_shutdown();
|
||||
|
||||
let final_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/{}", tmp_path, "final.fsh").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("final.fsh"));
|
||||
let utilities_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/utils/{}", tmp_path, "utilities.glsl").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("utils").join("utilities.glsl"));
|
||||
let stuff1_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/utils/{}", tmp_path, "stuff1.glsl").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("utils").join("stuff1.glsl"));
|
||||
let stuff2_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/utils/{}", tmp_path, "stuff2.glsl").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("utils").join("stuff2.glsl"));
|
||||
let matrices_idx = server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
//.add_node(&format!("{}/shaders/lib/{}", tmp_path, "matrices.glsl").try_into().unwrap());
|
||||
.add_node(&tmp_path.join("shaders").join("lib").join("matrices.glsl"));
|
||||
|
||||
server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
.add_edge(final_idx, utilities_idx, IncludePosition { line: 2, start: 0, end: 0 });
|
||||
server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
.add_edge(utilities_idx, stuff1_idx, IncludePosition { line: 0, start: 0, end: 0 });
|
||||
server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
.add_edge(utilities_idx, stuff2_idx, IncludePosition { line: 1, start: 0, end: 0 });
|
||||
server
|
||||
.graph
|
||||
.borrow_mut()
|
||||
.add_edge(final_idx, matrices_idx, IncludePosition { line: 3, start: 0, end: 0 });
|
||||
|
||||
let nodes = server.get_dfs_for_node(final_idx).unwrap();
|
||||
let sources = server.load_sources(&nodes).unwrap();
|
||||
|
||||
let graph_borrow = server.graph.borrow();
|
||||
let result = merge_views::generate_merge_list(&nodes, &sources, &graph_borrow);
|
||||
|
||||
let merge_file = tmp_path.join("shaders").join("final.fsh.merge");
|
||||
|
||||
let mut truth = fs::read_to_string(merge_file).unwrap();
|
||||
|
||||
for file in &[
|
||||
PathBuf::new().join("utils").join("utilities.glsl").to_str().unwrap(),
|
||||
PathBuf::new().join("utils").join("stuff1.glsl").to_str().unwrap(),
|
||||
PathBuf::new().join("utils").join("utilities.glsl").to_str().unwrap(),
|
||||
PathBuf::new().join("utils").join("stuff2.glsl").to_str().unwrap(),
|
||||
PathBuf::new().join("utils").join("utilities.glsl").to_str().unwrap(),
|
||||
PathBuf::new().join("final.fsh").to_str().unwrap(),
|
||||
PathBuf::new().join("lib").join("matrices.glsl").to_str().unwrap(),
|
||||
PathBuf::new().join("final.fsh").to_str().unwrap(),
|
||||
] {
|
||||
let path = tmp_path.clone();
|
||||
//path.f
|
||||
truth = truth.replacen("!!", &path.join("shaders").join(file).to_str().unwrap().replace('\\', "\\\\"), 1);
|
||||
}
|
||||
|
||||
assert_eq!(result, truth);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_nvidia_diagnostics() {
|
||||
let mut mockgl = opengl::MockShaderValidator::new();
|
||||
mockgl.expect_vendor().returning(|| "NVIDIA Corporation".into());
|
||||
let server = new_temp_server(Some(Box::new(mockgl)));
|
||||
|
||||
let output = "/home/noah/.minecraft/shaderpacks/test/shaders/final.fsh(9) : error C0000: syntax error, unexpected '}', expecting ',' or ';' at token \"}\"";
|
||||
|
||||
let results = diagnostics_parser::parse_diagnostics_output(
|
||||
output.to_string(),
|
||||
&PathBuf::from_str("/home/noah/.minecraft/shaderpacks/test").unwrap(),
|
||||
server.opengl_context.as_ref(),
|
||||
);
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
let first = results.into_iter().next().unwrap();
|
||||
assert_eq!(
|
||||
first.0,
|
||||
url::Url::from_file_path("/home/noah/.minecraft/shaderpacks/test/shaders/final.fsh").unwrap()
|
||||
);
|
||||
server.endpoint.request_shutdown();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_amd_diagnostics() {
|
||||
let mut mockgl = opengl::MockShaderValidator::new();
|
||||
mockgl.expect_vendor().returning(|| "ATI Technologies".into());
|
||||
let server = new_temp_server(Some(Box::new(mockgl)));
|
||||
|
||||
let output = "ERROR: 0:1: '' : syntax error: #line
|
||||
ERROR: 0:10: '' : syntax error: #line
|
||||
ERROR: 0:15: 'varying' : syntax error: syntax error
|
||||
";
|
||||
|
||||
let results = diagnostics_parser::parse_diagnostics_output(
|
||||
output.to_string(),
|
||||
&PathBuf::from_str("/home/test").unwrap(),
|
||||
server.opengl_context.as_ref(),
|
||||
);
|
||||
assert_eq!(results.len(), 1);
|
||||
let first = results.into_iter().next().unwrap();
|
||||
assert_eq!(first.1.len(), 3);
|
||||
server.endpoint.request_shutdown();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue