mdformat 0.1.5

A formatter for markdown source code.
use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use tempfile::NamedTempFile;

#[test]
fn test_file_input_output() -> Result<(), Box<dyn std::error::Error>> {
    let mut cmd = Command::cargo_bin("mdformat")?;
    let input_file = NamedTempFile::new()?;
    fs::write(input_file.path(), "## title\n\ntext")?;

    let output_file = NamedTempFile::new()?;

    cmd.arg(input_file.path())
        .arg("-o")
        .arg(output_file.path());

    cmd.assert().success();

    let output = fs::read_to_string(output_file.path())?;
    assert_eq!(output, "## title\n\ntext\n");

    Ok(())
}

#[test]
fn test_stdin_stdout() -> Result<(), Box<dyn std::error::Error>> {
    let mut cmd = Command::cargo_bin("mdformat")?;
    cmd.write_stdin("## title\n\ntext");
    cmd.assert()
        .success()
        .stdout("## title\n\ntext\n");
    Ok(())
}

#[test]
fn test_non_existent_input_file() -> Result<(), Box<dyn std::error::Error>> {
    let mut cmd = Command::cargo_bin("mdformat")?;
    cmd.arg("non_existent_file.md");
    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("No such file or directory"));
    Ok(())
}

#[test]
fn test_empty_input() -> Result<(), Box<dyn std::error::Error>> {
    let mut cmd = Command::cargo_bin("mdformat")?;
    cmd.write_stdin("");
    cmd.assert().success().stdout("\n");
    Ok(())
}

#[test]
fn test_indent_argument() -> Result<(), Box<dyn std::error::Error>> {
    let _cmd = Command::cargo_bin("mdformat")?;
    let _input = "1. level 1\n  2. level 2";
    let _expected = "1. level 1\n    1. level 2\n"; // Default indent is 4, but the logic seems to be 2 * (level - 1)
    
    // The current implementation seems to have hardcoded indent logic (2 spaces per level).
    // Let's first test the existing behavior.
    let mut cmd_default = Command::cargo_bin("mdformat")?;
    cmd_default.write_stdin("1. level 1\n  2. level 2");
    cmd_default.assert().success().stdout("1. level 1\n  1. level 2\n");


    // If the indent argument were implemented, the test would look like this:
    /*
    cmd.write_stdin(input).arg("-i").arg("4");
    cmd.assert().success().stdout(expected);
    */

    Ok(())
}

#[test]
fn test_mixed_elements() -> Result<(), Box<dyn std::error::Error>> {
    let mut cmd = Command::cargo_bin("mdformat")?;
    let input = "# Title\n\nSome text.\n\n- list1\n- list2\n\n```rust\nlet a = 1;\n```\n\n| a | b |\n|---|---|\n| 1 | 2 |\n\nAnother line.";
    let expected = "# Title\n\nSome text.\n\n- list1\n- list2\n\n```rust\nlet a = 1;\n```\n\n| a   | b   |\n| --- | --- |\n| 1   | 2   |\n\nAnother line.\n";
    cmd.write_stdin(input);
    cmd.assert().success().stdout(predicate::str::diff(expected));
    Ok(())
}