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_format_basic_to_stdout() {
let fixture = fixtures_dir().join("simple.toon");
let mut cmd = toon_lsp();
cmd.arg("format").arg(&fixture);
cmd.assert().success().stdout(predicate::str::contains("name"));
}
#[test]
fn test_format_normalizes_spacing() {
let temp = tempdir().expect("create temp dir");
let toon_path = temp.path().join("unformatted.toon");
fs::write(&toon_path, "key:value\nother: value2\n").expect("write file");
let mut cmd = toon_lsp();
cmd.arg("format").arg(&toon_path);
cmd.assert()
.success()
.stdout(predicate::str::contains("key: value"))
.stdout(predicate::str::contains("other: value2"));
}
#[test]
fn test_format_preserves_valid_structure() {
let fixture = fixtures_dir().join("simple.toon");
let _original = fs::read_to_string(&fixture).expect("read fixture");
let mut cmd = toon_lsp();
let output = cmd.arg("format").arg(&fixture).assert().success().get_output().stdout.clone();
let formatted = String::from_utf8_lossy(&output);
assert!(formatted.contains("name"));
assert!(formatted.contains("test"));
}
#[test]
fn test_format_check_formatted_file_succeeds() {
let temp = tempdir().expect("create temp dir");
let toon_path = temp.path().join("formatted.toon");
fs::write(&toon_path, "name: test\ncount: 42\n").expect("write file");
let mut cmd = toon_lsp();
cmd.arg("format").arg("--check").arg(&toon_path);
cmd.assert().success();
}
#[test]
fn test_format_check_unformatted_file_fails() {
let temp = tempdir().expect("create temp dir");
let toon_path = temp.path().join("unformatted.toon");
fs::write(&toon_path, "name:value\n").expect("write file");
let mut cmd = toon_lsp();
cmd.arg("format").arg("--check").arg(&toon_path);
cmd.assert().code(1);
}
#[test]
fn test_format_check_does_not_modify_file() {
let temp = tempdir().expect("create temp dir");
let toon_path = temp.path().join("unformatted.toon");
let original_content = "name:value\n";
fs::write(&toon_path, original_content).expect("write file");
let mut cmd = toon_lsp();
cmd.arg("format").arg("--check").arg(&toon_path);
let _ = cmd.assert();
let after = fs::read_to_string(&toon_path).expect("read file");
assert_eq!(after, original_content);
}
#[test]
fn test_format_with_2_space_indent() {
let temp = tempdir().expect("create temp dir");
let toon_path = temp.path().join("nested.toon");
fs::write(&toon_path, "server:\n host: localhost\n").expect("write file");
let mut cmd = toon_lsp();
cmd.arg("format").arg("--indent").arg("2").arg(&toon_path);
cmd.assert()
.success()
.stdout(predicate::str::contains("server:"))
.stdout(predicate::str::contains(" host:")); }
#[test]
fn test_format_with_4_space_indent() {
let temp = tempdir().expect("create temp dir");
let toon_path = temp.path().join("nested.toon");
fs::write(&toon_path, "server:\n host: localhost\n").expect("write file");
let mut cmd = toon_lsp();
cmd.arg("format").arg("--indent").arg("4").arg(&toon_path);
cmd.assert().success().stdout(predicate::str::contains(" host:")); }
#[test]
fn test_format_always_uses_spaces_not_tabs() {
let temp = tempdir().expect("create temp dir");
let toon_path = temp.path().join("nested.toon");
fs::write(&toon_path, "server:\n host: localhost\n").expect("write file");
let mut cmd = toon_lsp();
cmd.arg("format").arg(&toon_path);
cmd.assert().success().stdout(predicate::str::contains(" host:")); }
#[test]
fn test_format_to_output_file() {
let fixture = fixtures_dir().join("simple.toon");
let temp = tempdir().expect("create temp dir");
let output_path = temp.path().join("formatted.toon");
let mut cmd = toon_lsp();
cmd.arg("format").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"));
}
#[test]
fn test_format_in_place_modifies_file() {
let temp = tempdir().expect("create temp dir");
let toon_path = temp.path().join("unformatted.toon");
fs::write(&toon_path, "key:value\n").expect("write file");
let mut cmd = toon_lsp();
cmd.arg("format").arg(&toon_path).arg("-o").arg(&toon_path);
cmd.assert().success();
let content = fs::read_to_string(&toon_path).expect("read file");
assert!(content.contains("key: value")); }
#[test]
fn test_format_from_stdin() {
let toon_input = "key:value\n";
let mut cmd = toon_lsp();
cmd.arg("format").arg("-").write_stdin(toon_input);
cmd.assert().success().stdout(predicate::str::contains("key: value"));
}
#[test]
fn test_format_from_stdin_nested() {
let toon_input = "server:\n host: localhost\n";
let mut cmd = toon_lsp();
cmd.arg("format").arg("-").arg("--indent").arg("2").write_stdin(toon_input);
cmd.assert().success().stdout(predicate::str::contains(" host:")); }
#[test]
fn test_format_invalid_toon_fails() {
let fixture = fixtures_dir().join("invalid.toon");
let mut cmd = toon_lsp();
cmd.arg("format").arg(&fixture);
cmd.assert().code(2).stderr(predicate::str::is_empty().not());
}
#[test]
fn test_format_invalid_stdin_fails() {
let invalid_toon = "key: [unclosed";
let mut cmd = toon_lsp();
cmd.arg("format").arg("-").write_stdin(invalid_toon);
cmd.assert().code(2);
}
#[test]
fn test_format_nonexistent_file_fails() {
let nonexistent = "/path/to/nonexistent/file.toon";
let mut cmd = toon_lsp();
cmd.arg("format").arg(nonexistent);
cmd.assert().code(1).stderr(predicate::str::is_empty().not());
}
#[test]
fn test_format_empty_file_succeeds() {
let fixture = fixtures_dir().join("empty.toon");
let mut cmd = toon_lsp();
cmd.arg("format").arg(&fixture);
cmd.assert().success();
}
#[test]
fn test_format_preserves_arrays() {
let temp = tempdir().expect("create temp dir");
let toon_path = temp.path().join("arrays.toon");
fs::write(&toon_path, "items:\n - one\n - two\n").expect("write file");
let mut cmd = toon_lsp();
cmd.arg("format").arg(&toon_path);
cmd.assert()
.success()
.stdout(predicate::str::contains("- one"))
.stdout(predicate::str::contains("- two"));
}
#[test]
fn test_format_preserves_inline_arrays() {
let temp = tempdir().expect("create temp dir");
let toon_path = temp.path().join("inline.toon");
fs::write(&toon_path, "numbers[3]: 1,2,3\n").expect("write file");
let mut cmd = toon_lsp();
cmd.arg("format").arg(&toon_path);
cmd.assert()
.success()
.stdout(predicate::str::contains("numbers:"))
.stdout(predicate::str::contains("1"))
.stdout(predicate::str::contains("2"))
.stdout(predicate::str::contains("3"));
}
#[test]
fn test_format_default_indent_is_2() {
let temp = tempdir().expect("create temp dir");
let toon_path = temp.path().join("nested.toon");
fs::write(&toon_path, "server:\n host: localhost\n").expect("write file");
let mut cmd = toon_lsp();
cmd.arg("format").arg(&toon_path);
cmd.assert().success().stdout(predicate::str::contains(" host:")); }