use assert_cmd::cargo_bin;
use assert_cmd::Command;
use std::fs;
use tempfile::NamedTempFile;
#[test]
fn test_with_counts_adds_counts_to_simple_lists() {
let input = r#"%V:2.0
%NULL:~
%QUOTE:"
%S:Team:[id,name]
---
teams:@Team
|t1,Warriors
|t2,Lakers
|t3,Celtics
"#;
let temp_file = NamedTempFile::new().unwrap();
fs::write(temp_file.path(), input).unwrap();
let output = Command::new(cargo_bin!("hedl"))
.args([
"format",
temp_file.path().to_str().unwrap(),
"--with-counts",
])
.output()
.expect("Failed to execute hedl");
let output_str = String::from_utf8_lossy(&output.stdout);
assert!(
output_str.contains("%C:Team.total=3"),
"Expected %C:Team.total=3, got: {output_str}"
);
assert!(output_str.contains("teams:@Team"));
}
#[test]
fn test_with_counts_overwrites_existing_counts() {
let input = r#"%V:2.0
%NULL:~
%QUOTE:"
%S:Team:[id,name]
%C:Team.total=5
---
teams:@Team
|t1,Warriors
|t2,Lakers
"#;
let temp_file = NamedTempFile::new().unwrap();
fs::write(temp_file.path(), input).unwrap();
let output = Command::new(cargo_bin!("hedl"))
.args([
"format",
temp_file.path().to_str().unwrap(),
"--with-counts",
])
.output()
.expect("Failed to execute hedl");
let output_str = String::from_utf8_lossy(&output.stdout);
assert!(
output_str.contains("%C:Team.total=2"),
"Expected %C:Team.total=2, got: {output_str}"
);
assert!(output_str.contains("teams:@Team"));
assert!(!output_str.contains(".total=5"));
}
#[test]
fn test_with_counts_handles_empty_lists() {
let input = r#"%V:2.0
%NULL:~
%QUOTE:"
%S:Team:[id,name]
---
teams:@Team
"#;
let temp_file = NamedTempFile::new().unwrap();
fs::write(temp_file.path(), input).unwrap();
let output = Command::new(cargo_bin!("hedl"))
.args([
"format",
temp_file.path().to_str().unwrap(),
"--with-counts",
])
.output()
.expect("Failed to execute hedl");
let output_str = String::from_utf8_lossy(&output.stdout);
assert!(
output_str.contains("%C:Team.total=0"),
"Expected %C:Team.total=0, got: {output_str}"
);
assert!(output_str.contains("teams:@Team"));
}
#[test]
fn test_with_counts_handles_nested_objects() {
let input = r#"%V:2.0
%NULL:~
%QUOTE:"
%S:Team:[id,name]
---
sports:
basketball:
teams:@Team
|t1,Lakers
|t2,Celtics
football:
teams:@Team
|t3,Chiefs
"#;
let temp_file = NamedTempFile::new().unwrap();
fs::write(temp_file.path(), input).unwrap();
let output = Command::new(cargo_bin!("hedl"))
.args([
"format",
temp_file.path().to_str().unwrap(),
"--with-counts",
])
.output()
.expect("Failed to execute hedl");
let output_str = String::from_utf8_lossy(&output.stdout);
assert!(
output_str.contains("%C:Team.total=3"),
"Expected %C:Team.total=3, got: {output_str}"
);
assert!(output_str.contains("teams:@Team"));
}
#[test]
fn test_without_counts_flag_still_emits_actual_counts() {
let input = r#"%V:2.0
%NULL:~
%QUOTE:"
%S:Team:[id,name]
---
teams:@Team
|t1,Warriors
|t2,Lakers
"#;
let temp_file = NamedTempFile::new().unwrap();
fs::write(temp_file.path(), input).unwrap();
let output = Command::new(cargo_bin!("hedl"))
.args(["format", temp_file.path().to_str().unwrap()])
.output()
.expect("Failed to execute hedl");
let output_str = String::from_utf8_lossy(&output.stdout);
assert!(output_str.contains("teams:@Team"));
assert!(
output_str.contains("%C:Team.total=2"),
"Expected %C:Team.total=2 in canonical output, got: {output_str}"
);
}
#[test]
fn test_with_counts_multiple_lists() {
let input = r#"%V:2.0
%NULL:~
%QUOTE:"
%S:Team:[id,name]
%S:Player:[id,name]
---
teams:@Team
|t1,Warriors
|t2,Lakers
|t3,Celtics
players:@Player
|p1,Curry
|p2,James
"#;
let temp_file = NamedTempFile::new().unwrap();
fs::write(temp_file.path(), input).unwrap();
let output = Command::new(cargo_bin!("hedl"))
.args([
"format",
temp_file.path().to_str().unwrap(),
"--with-counts",
])
.output()
.expect("Failed to execute hedl");
let output_str = String::from_utf8_lossy(&output.stdout);
assert!(
output_str.contains("%C:Player.total=2"),
"Expected %C:Player.total=2, got: {output_str}"
);
assert!(
output_str.contains("%C:Team.total=3"),
"Expected %C:Team.total=3, got: {output_str}"
);
assert!(output_str.contains("teams:@Team"));
assert!(output_str.contains("players:@Player"));
}
#[test]
fn test_with_counts_inline_schema() {
let input = r#"%V:2.0
%NULL:~
%QUOTE:"
---
teams:@Team[id,name]
|t1,Warriors
|t2,Lakers
"#;
let temp_file = NamedTempFile::new().unwrap();
fs::write(temp_file.path(), input).unwrap();
let output = Command::new(cargo_bin!("hedl"))
.args([
"format",
temp_file.path().to_str().unwrap(),
"--with-counts",
])
.output()
.expect("Failed to execute hedl");
let output_str = String::from_utf8_lossy(&output.stdout);
assert!(output_str.contains("teams:@Team"));
assert!(
output_str.contains("%C:Team.total=2"),
"Expected %C:Team.total=2, got: {output_str}"
);
}