Skip to content

Commit 8f416d6

Browse files
committed
Add tests for cq
1 parent ae379aa commit 8f416d6

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

tests/test_cp2cp.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,54 @@ mod test {
8080
assert_eq!(exit_code, 1);
8181
Ok(())
8282
}
83+
84+
mod cq {
85+
use super::*;
86+
87+
fn run_cq(data: &RpcValue, filter: &str) -> Result<RpcValue, String> {
88+
let block = data.to_chainpack();
89+
let mut cmd = Command::new(cargo_bin!("cp2cp"));
90+
cmd.stdin(Stdio::piped())
91+
.stdout(Stdio::piped())
92+
.stderr(Stdio::piped())
93+
.arg("--oc")
94+
.arg("--cq").arg(filter);
95+
let mut child = cmd.spawn().map_err(|e| e.to_string())?;
96+
let mut stdin = child.stdin.take().expect("cp2cp should be running");
97+
thread::spawn(move || {
98+
stdin.write_all(&block).expect("Failed to write to stdin");
99+
});
100+
child.wait_with_output().map_err(|err| err.to_string()).and_then(|output| {
101+
RpcValue::from_chainpack(output.stdout).map_err(|err| err.to_string())
102+
})
103+
}
104+
105+
#[test]
106+
fn dot_filter() -> Result<(), String> {
107+
let input = RpcValue::from_cpon(r#"{"foo": true}"#).unwrap();
108+
let output = run_cq(&input, ".")?;
109+
assert_eq!(input, output);
110+
Ok(())
111+
}
112+
113+
#[test]
114+
fn key_lookup() -> Result<(), String> {
115+
for (input, filter, expected_output) in [
116+
(r#"null"#, ".[0]", r#"null"#),
117+
(r#"null"#, ".foo", r#"null"#),
118+
(r#"{"foo": true}"#, ".foo", "true"),
119+
(r#""some_string""#, ".[0]", r#""s""#),
120+
(r#""some_string""#, ".[1]", r#""o""#),
121+
(r#"["first_elem", "second_elem"]"#, ".[0]", r#""first_elem""#),
122+
(r#"["first_elem", "second_elem"]"#, ".[1]", r#""second_elem""#),
123+
// (r#"i{1: "one", 2: "two"}"#, ".asd", "\"two\"")
124+
] {
125+
let input = RpcValue::from_cpon(input).expect("valid cpon expected");
126+
let expected_output = RpcValue::from_cpon(expected_output).expect("valid cpon expected");
127+
let output = run_cq(&input, filter)?;
128+
assert_eq!(output, expected_output);
129+
}
130+
Ok(())
131+
}
132+
}
83133
}

0 commit comments

Comments
 (0)