Add Command unit tests

This commit is contained in:
mars 2022-09-17 05:22:18 -06:00
parent 4f3363fc0f
commit bb72e6d753
1 changed files with 76 additions and 2 deletions

View File

@ -35,6 +35,16 @@ impl Command for CreateNode {
}
}
impl From<Node> for CreateNode {
fn from(node: Node) -> Self {
Self {
kind: node.kind,
pos: node.pos,
inputs: node.inputs,
}
}
}
#[derive(Clone, Debug)]
pub struct DeleteNode {
pub target: usize,
@ -119,10 +129,10 @@ impl Command for SetEdge {
let input = target
.inputs
.get_mut(self.edge.input.slot)
.get_mut(self.edge.output.slot)
.ok_or(GraphError::InvalidReference)?;
*input = Some(self.edge.output);
*input = Some(self.edge.input);
Ok(())
}
@ -169,3 +179,67 @@ impl Command for DeleteEdge {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::node::AtomOp;
#[test]
fn create_node() -> GraphResult<()> {
let mut graph = Graph::new();
let cmd = CreateNode {
kind: NodeKind::Lit(0.0),
pos: glam::Vec2::ZERO,
inputs: vec![],
};
let undo = cmd.undo(&graph)?;
cmd.apply(&mut graph)?;
assert_eq!(graph.nodes.len(), 1);
undo.apply(&mut graph)?;
assert_eq!(graph.nodes.len(), 0);
Ok(())
}
#[test]
fn set_edge() -> GraphResult<()> {
let mut graph = Graph::new();
CreateNode::from(Node::literal(1.0)).apply(&mut graph)?;
CreateNode::from(Node::literal(2.0)).apply(&mut graph)?;
CreateNode::from(Node {
kind: NodeKind::Op(AtomOp::Add),
pos: glam::Vec2::ZERO,
inputs: vec![None; 2],
})
.apply(&mut graph)?;
SetEdge {
edge: Edge {
input: SlotIndex { node: 0, slot: 0 },
output: SlotIndex { node: 2, slot: 0 },
},
}
.apply(&mut graph)?;
SetEdge {
edge: Edge {
input: SlotIndex { node: 1, slot: 0 },
output: SlotIndex { node: 2, slot: 1 },
},
}
.apply(&mut graph)?;
println!("{:#?}", graph);
graph.topological_order()?;
Ok(())
}
}