Skip to content

Commit 3d96434

Browse files
committed
test: improve frontmatter parsing test coverage
Add exhaustive test cases for frontmatter_value_byte_range and related key helpers in skill/helpers.rs. Scenarios covered: - Unquoted values with/without comments - Double and single quoted values - Quoted values with comments - Values containing colons - CRLF line endings - Indented keys - Malformed YAML (unclosed quotes) - Multiline/nested values (expected None) Verified logic using a standalone Rust script as cargo test was restricted by environment dependency issues.
1 parent a93b724 commit 3d96434

1 file changed

Lines changed: 103 additions & 0 deletions

File tree

  • crates/agnix-core/src/rules/skill

crates/agnix-core/src/rules/skill/tests.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,6 +2038,109 @@ Body"#;
20382038
assert!(range.is_none());
20392039
}
20402040

2041+
#[test]
2042+
fn test_frontmatter_value_byte_range_exhaustive() {
2043+
let content = "---
2044+
name: test-skill
2045+
description: \"Quoted description\"
2046+
empty:
2047+
nested: value
2048+
unquoted_with_comment: value # this is a comment
2049+
quoted_with_comment: \"quoted value\" # this is also a comment
2050+
single_quoted: 'single value'
2051+
with_colon: \"value: with colon\"
2052+
indented_key: indented_value
2053+
---
2054+
Body";
2055+
let parts = split_frontmatter(content);
2056+
2057+
// Unquoted
2058+
let range = frontmatter_value_byte_range(content, &parts, "name");
2059+
assert_eq!(&content[range.unwrap().0..range.unwrap().1], "test-skill");
2060+
2061+
// Double quoted
2062+
let range = frontmatter_value_byte_range(content, &parts, "description");
2063+
assert_eq!(
2064+
&content[range.unwrap().0..range.unwrap().1],
2065+
"Quoted description"
2066+
);
2067+
2068+
// Empty (multiline/nested) - currently returns None as it only looks at the same line
2069+
let range = frontmatter_value_byte_range(content, &parts, "empty");
2070+
assert!(range.is_none());
2071+
2072+
// Unquoted with comment
2073+
let range = frontmatter_value_byte_range(content, &parts, "unquoted_with_comment");
2074+
assert_eq!(&content[range.unwrap().0..range.unwrap().1], "value");
2075+
2076+
// Quoted with comment
2077+
let range = frontmatter_value_byte_range(content, &parts, "quoted_with_comment");
2078+
assert_eq!(&content[range.unwrap().0..range.unwrap().1], "quoted value");
2079+
2080+
// Single quoted
2081+
let range = frontmatter_value_byte_range(content, &parts, "single_quoted");
2082+
assert_eq!(&content[range.unwrap().0..range.unwrap().1], "single value");
2083+
2084+
// With colon
2085+
let range = frontmatter_value_byte_range(content, &parts, "with_colon");
2086+
assert_eq!(
2087+
&content[range.unwrap().0..range.unwrap().1],
2088+
"value: with colon"
2089+
);
2090+
2091+
// Indented key
2092+
let range = frontmatter_value_byte_range(content, &parts, "indented_key");
2093+
assert_eq!(&content[range.unwrap().0..range.unwrap().1], "indented_value");
2094+
2095+
// CRLF
2096+
let content_crlf = "---\r\nname: test-skill\r\ndescription: value\r\n---\r\nBody";
2097+
let parts_crlf = split_frontmatter(content_crlf);
2098+
let range = frontmatter_value_byte_range(content_crlf, &parts_crlf, "name");
2099+
assert_eq!(
2100+
&content_crlf[range.unwrap().0..range.unwrap().1],
2101+
"test-skill"
2102+
);
2103+
2104+
// Malformed: unclosed quote
2105+
let content_malformed = "---\nname: \"unclosed\ndescription: value\n---\nBody";
2106+
let parts_malformed = split_frontmatter(content_malformed);
2107+
let range = frontmatter_value_byte_range(content_malformed, &parts_malformed, "name");
2108+
assert!(range.is_none());
2109+
}
2110+
2111+
#[test]
2112+
fn test_key_helpers_exhaustive() {
2113+
let content = "---
2114+
name: test-skill
2115+
indented_key: value
2116+
# comment
2117+
other: val
2118+
---";
2119+
let parts = split_frontmatter(content);
2120+
2121+
// frontmatter_key_offset
2122+
// frontmatter starts after "---" (index 3)
2123+
// content[3..] = "\nname: test-skill\n indented_key: value\n# comment\nother: val\n---"
2124+
assert_eq!(frontmatter_key_offset(&parts.frontmatter, "name"), Some(1));
2125+
assert_eq!(
2126+
frontmatter_key_offset(&parts.frontmatter, "indented_key"),
2127+
Some(20)
2128+
);
2129+
2130+
// frontmatter_key_line_byte_range
2131+
let range = frontmatter_key_line_byte_range(content, &parts, "name").unwrap();
2132+
assert_eq!(&content[range.0..range.1], "name: test-skill\n");
2133+
2134+
let range = frontmatter_key_line_byte_range(content, &parts, "other").unwrap();
2135+
assert_eq!(&content[range.0..range.1], "other: val\n");
2136+
2137+
// CRLF
2138+
let content_crlf = "---\r\nname: test\r\nother: val\r\n---";
2139+
let parts_crlf = split_frontmatter(content_crlf);
2140+
let range = frontmatter_key_line_byte_range(content_crlf, &parts_crlf, "name").unwrap();
2141+
assert_eq!(&content_crlf[range.0..range.1], "name: test\r\n");
2142+
}
2143+
20412144
// ===== directory_size_until tests =====
20422145

20432146
/// Helper to write N bytes to a file efficiently using a small buffer

0 commit comments

Comments
 (0)