use std::path::PathBuf;
use super::*;
use crate::cli::commands::{
ast_command, check_command, dump_command, exec_command, infer_command, trace_command,
};
#[test]
fn dump_accepts_trace_flags() {
let cmd = dump_command();
let result = cmd.try_get_matches_from(["dump", "query.ptk", "--fuel", "500", "-vv"]);
assert!(
result.is_ok(),
"dump should accept trace flags: {:?}",
result.err()
);
let m = result.unwrap();
let params = DumpParams::from_matches(&m);
assert_eq!(params.query_path, Some(PathBuf::from("query.ptk")));
}
#[test]
fn dump_accepts_exec_flags() {
let cmd = dump_command();
let result = cmd.try_get_matches_from([
"dump",
"query.ptk",
"--compact",
"--check",
"--verbose-nodes",
"--entry",
"Foo",
]);
assert!(
result.is_ok(),
"dump should accept exec flags: {:?}",
result.err()
);
let m = result.unwrap();
let params = DumpParams::from_matches(&m);
assert_eq!(params.query_path, Some(PathBuf::from("query.ptk")));
}
#[test]
fn dump_accepts_source_positional() {
let cmd = dump_command();
let result = cmd.try_get_matches_from(["dump", "query.ptk", "app.js"]);
assert!(
result.is_ok(),
"dump should accept source positional: {:?}",
result.err()
);
let m = result.unwrap();
let params = DumpParams::from_matches(&m);
assert_eq!(params.query_path, Some(PathBuf::from("query.ptk")));
}
#[test]
fn dump_accepts_source_flag() {
let cmd = dump_command();
let result = cmd.try_get_matches_from(["dump", "query.ptk", "-s", "let x = 1"]);
assert!(
result.is_ok(),
"dump should accept -s flag: {:?}",
result.err()
);
}
#[test]
fn exec_accepts_trace_flags() {
let cmd = exec_command();
let result = cmd.try_get_matches_from([
"exec",
"query.ptk",
"app.js",
"--fuel",
"500",
"-vv",
"--no-result",
]);
assert!(
result.is_ok(),
"exec should accept trace flags: {:?}",
result.err()
);
let m = result.unwrap();
let params = ExecParams::from_matches(&m);
assert_eq!(params.query_path, Some(PathBuf::from("query.ptk")));
assert_eq!(params.source_path, Some(PathBuf::from("app.js")));
}
#[test]
fn trace_accepts_exec_flags() {
let cmd = trace_command();
let result = cmd.try_get_matches_from([
"trace",
"query.ptk",
"app.js",
"--compact",
"--verbose-nodes",
"--check",
]);
assert!(
result.is_ok(),
"trace should accept exec flags: {:?}",
result.err()
);
let m = result.unwrap();
let params = TraceParams::from_matches(&m);
assert_eq!(params.query_path, Some(PathBuf::from("query.ptk")));
assert_eq!(params.source_path, Some(PathBuf::from("app.js")));
}
#[test]
fn check_accepts_source_args() {
let cmd = check_command();
let result = cmd.try_get_matches_from(["check", "query.ptk", "app.js", "-s", "let x"]);
assert!(
result.is_ok(),
"check should accept source args: {:?}",
result.err()
);
}
#[test]
fn check_accepts_exec_flags() {
let cmd = check_command();
let result = cmd.try_get_matches_from([
"check",
"query.ptk",
"--compact",
"--verbose-nodes",
"--check",
"--entry",
"Foo",
]);
assert!(
result.is_ok(),
"check should accept exec flags: {:?}",
result.err()
);
}
#[test]
fn check_accepts_trace_flags() {
let cmd = check_command();
let result =
cmd.try_get_matches_from(["check", "query.ptk", "--fuel", "500", "-vv", "--no-result"]);
assert!(
result.is_ok(),
"check should accept trace flags: {:?}",
result.err()
);
}
#[test]
fn infer_accepts_source_args() {
let cmd = infer_command();
let result = cmd.try_get_matches_from(["infer", "query.ptk", "app.js", "-s", "let x"]);
assert!(
result.is_ok(),
"infer should accept source args: {:?}",
result.err()
);
}
#[test]
fn infer_accepts_exec_flags() {
let cmd = infer_command();
let result = cmd.try_get_matches_from([
"infer",
"query.ptk",
"--compact",
"--check",
"--entry",
"Foo",
]);
assert!(
result.is_ok(),
"infer should accept exec flags: {:?}",
result.err()
);
}
#[test]
fn infer_accepts_trace_flags() {
let cmd = infer_command();
let result =
cmd.try_get_matches_from(["infer", "query.ptk", "--fuel", "500", "-vv", "--no-result"]);
assert!(
result.is_ok(),
"infer should accept trace flags: {:?}",
result.err()
);
}
#[test]
fn dump_help_hides_trace_flags() {
let mut cmd = dump_command();
let help = cmd.render_help().to_string();
assert!(!help.contains("--fuel"), "dump help should not show --fuel");
assert!(
!help.contains("--no-result"),
"dump help should not show --no-result"
);
assert!(
!help.contains("Verbosity level"),
"dump help should not show -v description"
);
}
#[test]
fn dump_help_hides_exec_flags() {
let mut cmd = dump_command();
let help = cmd.render_help().to_string();
assert!(
!help.contains("--compact"),
"dump help should not show --compact"
);
assert!(
!help.contains("--verbose-nodes"),
"dump help should not show --verbose-nodes"
);
assert!(
!help.contains("--check"),
"dump help should not show --check"
);
assert!(
!help.contains("--entry"),
"dump help should not show --entry"
);
}
#[test]
fn dump_help_hides_source_args() {
let mut cmd = dump_command();
let help = cmd.render_help().to_string();
assert!(
!help.contains("[SOURCE]"),
"dump help should not show SOURCE positional"
);
assert!(
!help.contains("Inline source text"),
"dump help should not show -s description"
);
}
#[test]
fn exec_help_hides_trace_flags() {
let mut cmd = exec_command();
let help = cmd.render_help().to_string();
assert!(!help.contains("--fuel"), "exec help should not show --fuel");
assert!(
!help.contains("--no-result"),
"exec help should not show --no-result"
);
}
#[test]
fn trace_help_hides_exec_output_flags() {
let mut cmd = trace_command();
let help = cmd.render_help().to_string();
assert!(
!help.contains("--compact"),
"trace help should not show --compact"
);
assert!(
!help.contains("--verbose-nodes"),
"trace help should not show --verbose-nodes"
);
assert!(
!help.contains("Validate output"),
"trace help should not show --check"
);
}
#[test]
fn check_help_hides_unified_flags() {
let mut cmd = check_command();
let help = cmd.render_help().to_string();
assert!(
!help.contains("[SOURCE]"),
"check help should not show SOURCE"
);
assert!(
!help.contains("Inline source text"),
"check help should not show -s"
);
assert!(
!help.contains("--compact"),
"check help should not show --compact"
);
assert!(
!help.contains("--verbose-nodes"),
"check help should not show --verbose-nodes"
);
assert!(
!help.contains("--entry"),
"check help should not show --entry"
);
assert!(
!help.contains("--fuel"),
"check help should not show --fuel"
);
assert!(
!help.contains("--no-result"),
"check help should not show --no-result"
);
}
#[test]
fn infer_help_hides_unified_flags() {
let mut cmd = infer_command();
let help = cmd.render_help().to_string();
assert!(
!help.contains("[SOURCE]"),
"infer help should not show SOURCE"
);
assert!(
!help.contains("Inline source text"),
"infer help should not show -s"
);
assert!(
!help.contains("--compact"),
"infer help should not show --compact"
);
assert!(
!help.contains("--entry"),
"infer help should not show --entry"
);
assert!(
!help.contains("--fuel"),
"infer help should not show --fuel"
);
assert!(
!help.contains("--no-result"),
"infer help should not show --no-result"
);
assert!(
help.contains("--verbose-nodes"),
"infer help SHOULD show --verbose-nodes"
);
}
#[test]
fn exec_shifts_positional_with_inline_query() {
let cmd = exec_command();
let result = cmd.try_get_matches_from(["exec", "-q", "(identifier) @id", "app.js"]);
assert!(result.is_ok());
let m = result.unwrap();
let params = ExecParams::from_matches(&m);
assert_eq!(params.query_path, None);
assert_eq!(params.query_text, Some("(identifier) @id".to_string()));
assert_eq!(params.source_path, Some(PathBuf::from("app.js")));
}
#[test]
fn exec_no_shift_with_both_positionals() {
let cmd = exec_command();
let result = cmd.try_get_matches_from(["exec", "query.ptk", "app.js"]);
assert!(result.is_ok());
let m = result.unwrap();
let params = ExecParams::from_matches(&m);
assert_eq!(params.query_path, Some(PathBuf::from("query.ptk")));
assert_eq!(params.source_path, Some(PathBuf::from("app.js")));
}
#[test]
fn trace_shifts_positional_with_inline_query() {
let cmd = trace_command();
let result = cmd.try_get_matches_from(["trace", "-q", "(identifier) @id", "app.js"]);
assert!(result.is_ok());
let m = result.unwrap();
let params = TraceParams::from_matches(&m);
assert_eq!(params.query_path, None);
assert_eq!(params.query_text, Some("(identifier) @id".to_string()));
assert_eq!(params.source_path, Some(PathBuf::from("app.js")));
}
#[test]
fn trace_params_extracts_all_fields() {
let cmd = trace_command();
let result = cmd.try_get_matches_from([
"trace",
"query.ptk",
"app.js",
"-l",
"typescript",
"--entry",
"Main",
"-vv",
"--no-result",
"--fuel",
"500",
"--color",
"always",
]);
assert!(result.is_ok());
let m = result.unwrap();
let params = TraceParams::from_matches(&m);
assert_eq!(params.query_path, Some(PathBuf::from("query.ptk")));
assert_eq!(params.source_path, Some(PathBuf::from("app.js")));
assert_eq!(params.lang, Some("typescript".to_string()));
assert_eq!(params.entry, Some("Main".to_string()));
assert_eq!(params.verbose, 2);
assert!(params.no_result);
assert_eq!(params.fuel, 500);
assert!(matches!(params.color, ColorChoice::Always));
}
#[test]
fn exec_params_extracts_all_fields() {
let cmd = exec_command();
let result = cmd.try_get_matches_from([
"exec",
"query.ptk",
"app.js",
"-l",
"javascript",
"--compact",
"--entry",
"Query",
"--color",
"never",
"--verbose-nodes",
"--check",
]);
assert!(result.is_ok());
let m = result.unwrap();
let params = ExecParams::from_matches(&m);
assert_eq!(params.query_path, Some(PathBuf::from("query.ptk")));
assert_eq!(params.source_path, Some(PathBuf::from("app.js")));
assert_eq!(params.lang, Some("javascript".to_string()));
assert!(params.compact);
assert_eq!(params.entry, Some("Query".to_string()));
assert!(matches!(params.color, ColorChoice::Never));
}
#[test]
fn dump_params_extracts_only_relevant_fields() {
let cmd = dump_command();
let result = cmd.try_get_matches_from([
"dump",
"query.ptk",
"-l",
"rust",
"--color",
"auto",
"app.rs",
"--fuel",
"100",
"--compact",
]);
assert!(result.is_ok());
let m = result.unwrap();
let params = DumpParams::from_matches(&m);
assert_eq!(params.query_path, Some(PathBuf::from("query.ptk")));
assert_eq!(params.lang, Some("rust".to_string()));
assert!(matches!(params.color, ColorChoice::Auto));
}
#[test]
fn ast_accepts_exec_flags() {
let cmd = ast_command();
let result = cmd.try_get_matches_from([
"ast",
"query.ptk",
"app.js",
"--compact",
"--check",
"--verbose-nodes",
"--entry",
"Foo",
]);
assert!(
result.is_ok(),
"ast should accept exec flags: {:?}",
result.err()
);
let m = result.unwrap();
let params = AstParams::from_matches(&m);
assert_eq!(params.query_path, Some(PathBuf::from("query.ptk")));
assert_eq!(params.source_path, Some(PathBuf::from("app.js")));
}
#[test]
fn ast_accepts_trace_flags() {
let cmd = ast_command();
let result = cmd.try_get_matches_from([
"ast",
"query.ptk",
"app.js",
"--fuel",
"500",
"-vv",
"--no-result",
]);
assert!(
result.is_ok(),
"ast should accept trace flags: {:?}",
result.err()
);
let m = result.unwrap();
let params = AstParams::from_matches(&m);
assert_eq!(params.query_path, Some(PathBuf::from("query.ptk")));
assert_eq!(params.source_path, Some(PathBuf::from("app.js")));
}
#[test]
fn ast_shifts_positional_with_inline_query() {
let cmd = ast_command();
let result = cmd.try_get_matches_from(["ast", "-q", "(identifier) @id", "app.js"]);
assert!(result.is_ok());
let m = result.unwrap();
let params = AstParams::from_matches(&m);
assert_eq!(params.query_path, None);
assert_eq!(params.query_text, Some("(identifier) @id".to_string()));
assert_eq!(params.source_path, Some(PathBuf::from("app.js")));
}
#[test]
fn ast_no_shift_with_both_positionals() {
let cmd = ast_command();
let result = cmd.try_get_matches_from(["ast", "query.ptk", "app.js"]);
assert!(result.is_ok());
let m = result.unwrap();
let params = AstParams::from_matches(&m);
assert_eq!(params.query_path, Some(PathBuf::from("query.ptk")));
assert_eq!(params.source_path, Some(PathBuf::from("app.js")));
}
#[test]
fn ast_help_shows_raw_flag() {
let mut cmd = ast_command();
let help = cmd.render_help().to_string();
assert!(help.contains("--raw"), "ast help should show --raw");
}
#[test]
fn ast_help_hides_unified_flags() {
let mut cmd = ast_command();
let help = cmd.render_help().to_string();
assert!(
!help.contains("--compact"),
"ast help should not show --compact"
);
assert!(
!help.contains("--verbose-nodes"),
"ast help should not show --verbose-nodes"
);
assert!(
!help.contains("--check"),
"ast help should not show --check"
);
assert!(
!help.contains("--entry"),
"ast help should not show --entry"
);
assert!(!help.contains("--fuel"), "ast help should not show --fuel");
assert!(
!help.contains("--no-result"),
"ast help should not show --no-result"
);
assert!(
!help.contains("Verbosity level"),
"ast help should not show -v description"
);
}
#[test]
fn ast_params_extracts_all_fields() {
let cmd = ast_command();
let result = cmd.try_get_matches_from([
"ast",
"query.ptk",
"app.js",
"-l",
"typescript",
"--raw",
"--color",
"always",
]);
assert!(result.is_ok());
let m = result.unwrap();
let params = AstParams::from_matches(&m);
assert_eq!(params.query_path, Some(PathBuf::from("query.ptk")));
assert_eq!(params.source_path, Some(PathBuf::from("app.js")));
assert_eq!(params.lang, Some("typescript".to_string()));
assert!(params.raw);
assert!(matches!(params.color, ColorChoice::Always));
}
#[test]
fn dump_accepts_raw_flag() {
let cmd = dump_command();
let result = cmd.try_get_matches_from(["dump", "query.ptk", "--raw"]);
assert!(
result.is_ok(),
"dump should accept --raw flag: {:?}",
result.err()
);
}
#[test]
fn exec_accepts_raw_flag() {
let cmd = exec_command();
let result = cmd.try_get_matches_from(["exec", "query.ptk", "app.js", "--raw"]);
assert!(
result.is_ok(),
"exec should accept --raw flag: {:?}",
result.err()
);
}
#[test]
fn trace_accepts_raw_flag() {
let cmd = trace_command();
let result = cmd.try_get_matches_from(["trace", "query.ptk", "app.js", "--raw"]);
assert!(
result.is_ok(),
"trace should accept --raw flag: {:?}",
result.err()
);
}
#[test]
fn check_accepts_raw_flag() {
let cmd = check_command();
let result = cmd.try_get_matches_from(["check", "query.ptk", "--raw"]);
assert!(
result.is_ok(),
"check should accept --raw flag: {:?}",
result.err()
);
}