ac_algorithm/testcase_yaml.rs
1//! cargo-compete-compatible testcase YAML import (issue #26, AC-05).
2//!
3//! ac-test wrote `testcases/<alias>.yml` documents of the form:
4//!
5//! ```yaml
6//! # <problem URL>
7//! ---
8//! type: Batch
9//! timelimit: 2s
10//! match: Lines
11//! cases:
12//! - name: sample1
13//! in: |
14//! ...
15//! out: |
16//! ...
17//! extend: []
18//! ```
19//!
20//! This module parses that shape leniently: only `cases[].name`,
21//! `cases[].in`, and optional `cases[].out` are required; every other
22//! key is ignored. Cases without `out` stay execution-check only —
23//! they are never treated as correctness passes (ADR-0005).
24
25use serde::Deserialize;
26
27/// One `cases:` entry from a cargo-compete testcase YAML document.
28#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
29pub struct YamlCase {
30 /// Case name (`sample1`, `generated-min`, …).
31 pub name: String,
32 /// stdin payload (the `in:` block scalar).
33 #[serde(rename = "in")]
34 pub input: String,
35 /// Expected stdout (the `out:` block scalar). `None` means the case
36 /// carries no expected output and is an execution check only.
37 #[serde(rename = "out", default)]
38 pub output: Option<String>,
39}
40
41/// Top-level document; unknown keys (`type`, `timelimit`, `match`,
42/// `extend`, comments) are ignored for forward compatibility.
43#[derive(Debug, Deserialize)]
44struct Document {
45 /// Case list; absent or empty yields no cases.
46 #[serde(default)]
47 cases: Vec<YamlCase>,
48}
49
50/// Parses a cargo-compete testcase YAML document into its cases.
51///
52/// # Errors
53///
54/// Returns a description when the text is not valid YAML or the
55/// `cases` entries do not match the expected shape.
56pub fn parse_cases(yaml: &str) -> Result<Vec<YamlCase>, String> {
57 let document: Document =
58 serde_yaml::from_str(yaml).map_err(|err| format!("cannot parse testcase YAML: {err}"))?;
59 for case in &document.cases {
60 if case.name.trim().is_empty() {
61 return Err("testcase YAML contains a case with an empty name".to_owned());
62 }
63 }
64 Ok(document.cases)
65}
66
67#[cfg(test)]
68mod tests {
69 use super::*;
70
71 const SAMPLE: &str = r#"# https://atcoder.jp/contests/abc351/tasks/abc351_a
72---
73type: Batch
74timelimit: 2s
75match: Lines
76
77cases:
78 - name: sample1
79 in: |
80 3
81 out: |
82 6
83 - name: generated-min
84 in: |
85 1
86 1 2 3
87 - name: edge
88 in: "5\n"
89 out: ""
90
91extend: []
92"#;
93
94 #[test]
95 fn parses_cargo_compete_yaml() {
96 let cases = parse_cases(SAMPLE).expect("parse");
97 assert_eq!(cases.len(), 3);
98 assert_eq!(cases[0].name, "sample1");
99 assert_eq!(cases[0].input, "3\n");
100 assert_eq!(cases[0].output.as_deref(), Some("6\n"));
101 // Generated cases carry no `out` → execution check only.
102 assert_eq!(cases[1].output, None);
103 // Empty-string `out` is still an expected output.
104 assert_eq!(cases[2].output.as_deref(), Some(""));
105 }
106
107 #[test]
108 fn rejects_malformed_yaml() {
109 assert!(parse_cases("cases: [unclosed").is_err());
110 assert!(parse_cases("cases:\n - in: |\n x\n").is_err());
111 assert!(parse_cases("cases:\n - name: \"\"\n in: x\n").is_err());
112 }
113}