Skip to main content

codama_nodes/
instruction_byte_delta_node.rs

1use crate::{InstructionByteDeltaNode, InstructionByteDeltaValue};
2
3impl InstructionByteDeltaNode {
4    pub fn new<T>(value: T, with_header: bool) -> Self
5    where
6        T: Into<InstructionByteDeltaValue>,
7    {
8        Self {
9            value: Box::new(value.into()),
10            with_header,
11            subtract: None,
12        }
13    }
14
15    pub fn minus<T>(value: T, with_header: bool) -> Self
16    where
17        T: Into<InstructionByteDeltaValue>,
18    {
19        Self {
20            value: Box::new(value.into()),
21            with_header,
22            subtract: Some(true),
23        }
24    }
25}
26
27#[cfg(test)]
28mod tests {
29    use crate::{
30        ArgumentValueNode, InstructionByteDeltaNode, InstructionByteDeltaValue, NumberValueNode,
31    };
32
33    #[test]
34    fn new() {
35        let node = InstructionByteDeltaNode::new(ArgumentValueNode::new("myArgument"), true);
36        assert_eq!(
37            *node.value,
38            InstructionByteDeltaValue::ArgumentValue(ArgumentValueNode::new("myArgument"))
39        );
40        assert!(node.with_header);
41        assert_eq!(node.subtract, None);
42    }
43
44    #[test]
45    fn minus() {
46        let node = InstructionByteDeltaNode::minus(NumberValueNode::new(42), true);
47        assert_eq!(
48            *node.value,
49            InstructionByteDeltaValue::NumberValue(NumberValueNode::new(42))
50        );
51        assert!(node.with_header);
52        assert_eq!(node.subtract, Some(true));
53    }
54
55    #[test]
56    fn to_json() {
57        let node = InstructionByteDeltaNode::new(ArgumentValueNode::new("myArgument"), true);
58        let json = serde_json::to_string(&node).unwrap();
59        assert_eq!(
60            json,
61            r#"{"kind":"instructionByteDeltaNode","withHeader":true,"value":{"kind":"argumentValueNode","name":"myArgument"}}"#
62        );
63    }
64
65    #[test]
66    fn from_json() {
67        let json = r#"{"kind":"instructionByteDeltaNode","withHeader":true,"value":{"kind":"argumentValueNode","name":"myArgument"}}"#;
68        let node: InstructionByteDeltaNode = serde_json::from_str(json).unwrap();
69        assert_eq!(
70            node,
71            InstructionByteDeltaNode::new(ArgumentValueNode::new("myArgument"), true)
72        );
73    }
74
75    #[test]
76    fn to_json_minus() {
77        let node = InstructionByteDeltaNode::minus(ArgumentValueNode::new("myArgument"), true);
78        let json = serde_json::to_string(&node).unwrap();
79        assert_eq!(
80            json,
81            r#"{"kind":"instructionByteDeltaNode","withHeader":true,"subtract":true,"value":{"kind":"argumentValueNode","name":"myArgument"}}"#
82        );
83    }
84
85    #[test]
86    fn from_json_minus() {
87        let json = r#"{"kind":"instructionByteDeltaNode","withHeader":true,"subtract":true,"value":{"kind":"argumentValueNode","name":"myArgument"}}"#;
88        let node: InstructionByteDeltaNode = serde_json::from_str(json).unwrap();
89        assert_eq!(
90            node,
91            InstructionByteDeltaNode::minus(ArgumentValueNode::new("myArgument"), true)
92        );
93    }
94}