Skip to main content

codama_nodes/
instruction_account_node.rs

1use crate::{CamelCaseString, Docs, InstructionAccountNode, IsSigner};
2
3impl InstructionAccountNode {
4    pub fn new<T, U>(name: T, is_writable: bool, is_signer: U) -> Self
5    where
6        T: Into<CamelCaseString>,
7        U: Into<IsSigner>,
8    {
9        Self {
10            name: name.into(),
11            is_writable,
12            is_signer: is_signer.into(),
13            is_optional: None,
14            docs: Docs::default(),
15            default_value: Box::new(None),
16        }
17    }
18}
19
20#[cfg(test)]
21mod tests {
22    use super::*;
23    use crate::{AccountValueNode, InstructionInputValueNode};
24
25    #[test]
26    fn new() {
27        let node = InstructionAccountNode::new("my_account", false, true);
28        assert_eq!(node.name, CamelCaseString::new("myAccount"));
29        assert!(!node.is_writable);
30        assert_eq!(node.is_signer, IsSigner::True);
31        assert_eq!(node.is_optional, None);
32        assert_eq!(node.docs, Docs::default());
33        assert_eq!(*node.default_value, None);
34    }
35
36    #[test]
37    fn direct_instantiation() {
38        let node = InstructionAccountNode {
39            name: "myAccount".into(),
40            is_writable: false,
41            is_signer: IsSigner::Either,
42            is_optional: Some(true),
43            docs: vec!["Hello".to_string()].into(),
44            default_value: Box::new(Some(AccountValueNode::new("myOtherAccount").into())),
45        };
46        assert_eq!(node.name, CamelCaseString::new("myAccount"));
47        assert!(!node.is_writable);
48        assert_eq!(node.is_signer, IsSigner::Either);
49        assert_eq!(node.is_optional, Some(true));
50        assert_eq!(*node.docs, vec!["Hello".to_string()]);
51        assert_eq!(
52            *node.default_value,
53            Some(InstructionInputValueNode::AccountValue(
54                AccountValueNode::new("myOtherAccount")
55            ))
56        );
57    }
58
59    #[test]
60    fn to_json() {
61        let node = InstructionAccountNode::new("myAccount", false, true);
62        let json = serde_json::to_string(&node).unwrap();
63        assert_eq!(
64            json,
65            r#"{"kind":"instructionAccountNode","name":"myAccount","isWritable":false,"isSigner":true}"#
66        );
67    }
68
69    #[test]
70    fn from_json() {
71        let json = r#"{"kind":"instructionAccountNode","name":"myAccount","isWritable":false,"isSigner":true}"#;
72        let node: InstructionAccountNode = serde_json::from_str(json).unwrap();
73        assert_eq!(node, InstructionAccountNode::new("myAccount", false, true));
74    }
75}