use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use tempfile::tempdir;
fn fixtures_dir() -> std::path::PathBuf {
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests").join("fixtures")
}
#[allow(deprecated)]
fn toon_lsp() -> Command {
Command::cargo_bin("toon-lsp").expect("Failed to find toon-lsp binary")
}
#[test]
fn test_encode_json_file_to_stdout() {
let fixture = fixtures_dir().join("simple.json");
let mut cmd = toon_lsp();
cmd.arg("encode").arg(&fixture);
cmd.assert()
.success()
.stdout(predicate::str::contains("name"))
.stdout(predicate::str::contains("test"))
.stdout(predicate::str::contains("count"));
}
#[test]
fn test_encode_nested_json_file() {
let fixture = fixtures_dir().join("nested.json");
let mut cmd = toon_lsp();
cmd.arg("encode").arg(&fixture);
cmd.assert()
.success()
.stdout(predicate::str::contains("server"))
.stdout(predicate::str::contains("host"))
.stdout(predicate::str::contains("port"));
}
#[test]
fn test_encode_yaml_file_to_stdout() {
let fixture = fixtures_dir().join("simple.yaml");
let mut cmd = toon_lsp();
cmd.arg("encode").arg(&fixture).args(["-f", "yaml"]);
cmd.assert()
.success()
.stdout(predicate::str::contains("name"))
.stdout(predicate::str::contains("value"));
}
#[test]
fn test_encode_roundtrip_preserves_data() {
let fixture = fixtures_dir().join("array.json");
let json_content = fs::read_to_string(&fixture).expect("read fixture");
let mut cmd = toon_lsp();
let encode_output =
cmd.arg("encode").arg(&fixture).assert().success().get_output().stdout.clone();
let toon_content = String::from_utf8_lossy(&encode_output);
assert!(toon_content.contains("Alice"));
assert!(toon_content.contains("Bob"));
let original: serde_json::Value = serde_json::from_str(&json_content).expect("parse original");
assert!(original.get("users").is_some());
}
#[test]
fn test_encode_from_stdin() {
let json_input = r#"{"message":"hello","count":42}"#;
let mut cmd = toon_lsp();
cmd.arg("encode").arg("-").write_stdin(json_input);
cmd.assert()
.success()
.stdout(predicate::str::contains("message"))
.stdout(predicate::str::contains("hello"))
.stdout(predicate::str::contains("count"))
.stdout(predicate::str::contains("42"));
}
#[test]
fn test_encode_from_stdin_with_yaml_format() {
let yaml_input = "message: hello\ncount: 42\n";
let mut cmd = toon_lsp();
cmd.arg("encode").arg("-").args(["-f", "yaml"]).write_stdin(yaml_input);
cmd.assert()
.success()
.stdout(predicate::str::contains("message"))
.stdout(predicate::str::contains("hello"));
}
#[test]
fn test_encode_from_stdin_complex() {
let json_input = r#"{"server":{"host":"localhost","port":8080},"enabled":true}"#;
let mut cmd = toon_lsp();
cmd.arg("encode").arg("-").write_stdin(json_input);
cmd.assert()
.success()
.stdout(predicate::str::contains("server"))
.stdout(predicate::str::contains("host"))
.stdout(predicate::str::contains("localhost"))
.stdout(predicate::str::contains("enabled"));
}
#[test]
fn test_encode_to_output_file() {
let fixture = fixtures_dir().join("simple.json");
let temp = tempdir().expect("create temp dir");
let output_path = temp.path().join("output.toon");
let mut cmd = toon_lsp();
cmd.arg("encode").arg(&fixture).arg("-o").arg(&output_path);
cmd.assert().success();
let output_content = fs::read_to_string(&output_path).expect("read output");
assert!(output_content.contains("name"));
assert!(output_content.contains("test"));
}
#[test]
fn test_encode_to_output_file_long_option() {
let fixture = fixtures_dir().join("simple.json");
let temp = tempdir().expect("create temp dir");
let output_path = temp.path().join("output.toon");
let mut cmd = toon_lsp();
cmd.arg("encode").arg(&fixture).arg("--output").arg(&output_path);
cmd.assert().success();
assert!(output_path.exists());
}
#[test]
fn test_encode_stdin_to_output_file() {
let json_input = r#"{"key":"value"}"#;
let temp = tempdir().expect("create temp dir");
let output_path = temp.path().join("output.toon");
let mut cmd = toon_lsp();
cmd.arg("encode").arg("-").arg("-o").arg(&output_path).write_stdin(json_input);
cmd.assert().success();
let output_content = fs::read_to_string(&output_path).expect("read output");
assert!(output_content.contains("key"));
assert!(output_content.contains("value"));
}
#[test]
fn test_encode_invalid_json_file_fails() {
let temp = tempdir().expect("create temp dir");
let invalid_path = temp.path().join("invalid.json");
fs::write(&invalid_path, "{invalid json}").expect("write invalid file");
let mut cmd = toon_lsp();
cmd.arg("encode").arg(&invalid_path);
cmd.assert()
.code(2)
.stderr(predicate::str::contains("error").or(predicate::str::contains("Error")));
}
#[test]
fn test_encode_invalid_stdin_fails() {
let invalid_json = "{not valid json";
let mut cmd = toon_lsp();
cmd.arg("encode").arg("-").write_stdin(invalid_json);
cmd.assert()
.code(2)
.stderr(predicate::str::contains("error").or(predicate::str::contains("Error")));
}
#[test]
fn test_encode_nonexistent_file_fails() {
let nonexistent = "/path/to/nonexistent/file.json";
let mut cmd = toon_lsp();
cmd.arg("encode").arg(nonexistent);
cmd.assert().code(1).stderr(predicate::str::is_empty().not());
}
#[test]
fn test_encode_invalid_yaml_fails() {
let temp = tempdir().expect("create temp dir");
let invalid_path = temp.path().join("invalid.yaml");
fs::write(&invalid_path, "invalid: yaml: [unclosed").expect("write invalid file");
let mut cmd = toon_lsp();
cmd.arg("encode").arg(&invalid_path).args(["-f", "yaml"]);
cmd.assert().code(2).stderr(predicate::str::is_empty().not());
}
#[test]
fn test_encode_with_indent_option() {
let fixture = fixtures_dir().join("nested.json");
let mut cmd = toon_lsp();
cmd.arg("encode").arg(&fixture).args(["--indent", "4"]);
cmd.assert().success().stdout(predicate::str::contains("server"));
}
#[test]
fn test_encode_empty_json_object() {
let fixture = fixtures_dir().join("empty.json");
let mut cmd = toon_lsp();
cmd.arg("encode").arg(&fixture);
cmd.assert().success();
}
#[test]
fn test_encode_auto_detects_json_format() {
let fixture = fixtures_dir().join("simple.json");
let mut cmd = toon_lsp();
cmd.arg("encode").arg(&fixture);
cmd.assert().success();
}
#[test]
fn test_encode_auto_detects_yaml_format() {
let fixture = fixtures_dir().join("simple.yaml");
let mut cmd = toon_lsp();
cmd.arg("encode").arg(&fixture);
cmd.assert().success();
}