Skip to content

Commit 189c710

Browse files
committed
test(compile): add basic taproot/sh field validation tests
- Add basic tests validating that taproot results contain 'r' field while sh results do not - Fix test execution by enabling 'compiler' feature - Add error message validation for invalid cases Future iterations will include more comprehensive test coverage.
1 parent d763986 commit 189c710

File tree

1 file changed

+43
-16
lines changed

1 file changed

+43
-16
lines changed

tests/compile.rs

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,61 @@
1010
//!
1111
//! Tests for compile command and subcommands
1212
13-
use std::process::{Command, Output};
13+
use std::process::Command;
1414

15-
fn run_cmd(cmd: &str) -> Output {
16-
Command::new("cargo")
17-
.args(format!("run -- {}", cmd).split_whitespace())
18-
.output()
19-
.unwrap()
15+
fn run_cmd(cmd: &str) -> Result<String, String> {
16+
let full_cmd = format!("run --features compiler -- {}", cmd);
17+
let args = shlex::split(&full_cmd).unwrap();
18+
19+
let output = Command::new("cargo").args(args).output().unwrap();
20+
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
21+
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
22+
23+
if output.status.success() {
24+
Ok(stdout)
25+
} else {
26+
Err(stderr)
27+
}
28+
}
29+
30+
#[test]
31+
fn test_compile_taproot() {
32+
let stdout = run_cmd(r#"compile "pk(ABC)" -t tr"#).unwrap();
33+
let json: serde_json::Value = serde_json::from_str(&stdout).unwrap();
34+
35+
assert!(json.get("descriptor").is_some());
36+
assert!(json.get("r").is_some());
37+
}
38+
39+
#[test]
40+
fn test_compile_sh() {
41+
let stdout = run_cmd(r#"compile "pk(ABC)" -t sh"#).unwrap();
42+
let json: serde_json::Value = serde_json::from_str(&stdout).unwrap();
43+
44+
assert!(json.get("descriptor").is_some());
45+
assert!(json.get("r").is_none());
2046
}
2147

2248
#[test]
2349
fn test_invalid_cases() {
2450
// Test invalid policy syntax
25-
let output = run_cmd("compile invalid_policy");
26-
assert!(!output.status.success());
51+
let stderr = run_cmd(r#"compile "invalid_policy""#).unwrap_err();
52+
assert!(stderr.contains("Miniscript error"));
2753

2854
// Test invalid script type
29-
let output = run_cmd("compile pk(A) -t invalid_type");
30-
assert!(!output.status.success());
55+
let stderr = run_cmd(r#"compile "pk(A)" -t invalid_type"#).unwrap_err();
56+
assert!(stderr.contains("error: invalid value 'invalid_type' for '--type <SCRIPT_TYPE>'"));
3157

3258
// Test empty policy
33-
let output = run_cmd("compile");
34-
assert!(!output.status.success());
59+
let stderr = run_cmd("compile").unwrap_err();
60+
assert!(stderr.contains("error: the following required arguments were not provided"));
61+
assert!(stderr.contains("<POLICY>"));
3562

3663
// Test malformed policy with unmatched parentheses
37-
let output = run_cmd(r#"compile "pk(A"#);
38-
assert!(!output.status.success());
64+
let stderr = run_cmd(r#"compile "pk(A""#).unwrap_err();
65+
assert!(stderr.contains("Miniscript error: expected )"));
3966

4067
// Test policy with unknown function
41-
let output = run_cmd(r#"compile "unknown_func(A)"#);
42-
assert!(!output.status.success());
68+
let stderr = run_cmd(r#"compile "unknown_func(A)""#).unwrap_err();
69+
assert!(stderr.contains("Miniscript error: unexpected «unknown_func»"));
4370
}

0 commit comments

Comments
 (0)