use toon_lsp::ast::AstNode;
use toon_lsp::parser::{parse, parse_with_errors};
mod objects {
use super::*;
#[test]
fn test_simple_key_value() {
let ast = parse("name: Alice").expect("should parse");
insta::assert_debug_snapshot!(ast);
}
#[test]
fn test_nested_objects() {
let source = "person:\n name: Alice\n age: 30";
let ast = parse(source).expect("should parse");
insta::assert_debug_snapshot!(ast);
}
#[test]
fn test_multiple_top_level_keys() {
let source = "name: Alice\nage: 30\ncity: NYC";
let ast = parse(source).expect("should parse");
insta::assert_debug_snapshot!(ast);
}
}
mod primitives {
use super::*;
#[test]
fn test_quoted_strings() {
let source = r#"greeting: "Hello, World!""#;
let ast = parse(source).expect("should parse");
insta::assert_debug_snapshot!(ast);
}
#[test]
fn test_number_values() {
let source = "int: 42\nnegative: -17\nfloat: 3.14\nscientific: 1e10";
let ast = parse(source).expect("should parse");
insta::assert_debug_snapshot!(ast);
}
#[test]
fn test_bool_and_null() {
let source = "active: true\ndeleted: false\ndata: null";
let ast = parse(source).expect("should parse");
insta::assert_debug_snapshot!(ast);
}
#[test]
fn test_unquoted_strings() {
let source = "message: Hello world without quotes";
let ast = parse(source).expect("should parse");
insta::assert_debug_snapshot!(ast);
}
}
mod expanded_arrays {
use super::*;
#[test]
fn test_dash_primitive_items() {
let source = "items:\n - apple\n - banana\n - cherry";
let ast = parse(source).expect("should parse");
insta::assert_debug_snapshot!(ast);
}
#[test]
fn test_dash_object_items() {
let source = "users:\n - name: Alice\n age: 30\n - name: Bob\n age: 25";
let ast = parse(source).expect("should parse");
insta::assert_debug_snapshot!(ast);
}
#[test]
fn test_nested_arrays() {
let source = "matrix:\n -\n - 1\n - 2\n -\n - 3\n - 4";
let ast = parse(source).expect("should parse");
insta::assert_debug_snapshot!(ast);
}
}
mod inline_arrays {
use super::*;
#[test]
fn test_inline_comma_array() {
let source = "tags[3]: one,two,three";
let ast = parse(source).expect("should parse");
insta::assert_debug_snapshot!(ast);
}
#[test]
fn test_empty_inline_array() {
let source = "empty[0]:";
let ast = parse(source).expect("should parse");
insta::assert_debug_snapshot!(ast);
}
#[test]
fn test_inline_quoted_with_commas() {
let source = r#"items[2]: "a,b","c,d""#;
let ast = parse(source).expect("should parse");
insta::assert_debug_snapshot!(ast);
}
}
mod error_recovery {
use super::*;
#[test]
fn test_error_recovery_missing_colon() {
let source = "name Alice\nage: 30";
let (ast, errors) = parse_with_errors(source);
assert!(ast.is_some(), "should return partial AST");
assert!(!errors.is_empty(), "should report error");
insta::assert_debug_snapshot!((ast, errors));
}
#[test]
fn test_error_recovery_multiple_errors() {
let source = "name Alice\nage Bob\nvalid: true";
let (ast, errors) = parse_with_errors(source);
assert!(ast.is_some(), "should return partial AST");
assert!(errors.len() >= 2, "should collect multiple errors");
insta::assert_debug_snapshot!((ast, errors));
}
#[test]
fn test_error_recovery_resumes() {
let source = ": invalid\nname: Alice";
let (ast, errors) = parse_with_errors(source);
assert!(ast.is_some(), "should return partial AST");
if let Some(AstNode::Document { children, .. }) = &ast {
assert!(!children.is_empty(), "should have parsed valid content");
}
insta::assert_debug_snapshot!((ast, errors));
}
}
mod tabular_arrays {
use super::*;
#[test]
fn test_tabular_comma() {
let source = "users[2]{id,name}:\n 1,Alice\n 2,Bob";
let ast = parse(source).expect("should parse");
insta::assert_debug_snapshot!(ast);
}
#[test]
fn test_tabular_tab() {
let source = "users[2]{id,name}\t:\n 1\tAlice\n 2\tBob";
let ast = parse(source).expect("should parse");
insta::assert_debug_snapshot!(ast);
}
#[test]
fn test_tabular_pipe() {
let source = "users[2]{id,name}:\n 1,Alice\n 2,Bob";
let ast = parse(source).expect("should parse");
insta::assert_debug_snapshot!(ast);
}
}
mod indentation {
use super::*;
#[test]
fn test_tab_indentation_error() {
let source = "person:\n\tname: Alice"; let (ast, errors) = parse_with_errors(source);
assert!(ast.is_some(), "should produce partial AST");
insta::assert_debug_snapshot!((ast, errors));
}
#[test]
fn test_misaligned_dedent_error() {
let source = "outer:\n inner: value\n misaligned: bad";
let (ast, errors) = parse_with_errors(source);
insta::assert_debug_snapshot!((ast, errors));
}
#[test]
fn test_valid_indentation() {
let source = "outer:\n inner:\n deep: value\n sibling: data";
let (ast, errors) = parse_with_errors(source);
assert!(errors.is_empty(), "valid indentation should have no errors");
assert!(ast.is_some(), "should produce AST");
}
}
mod edge_cases {
use super::*;
#[test]
fn test_empty_input() {
let ast = parse("").expect("empty input should parse");
insta::assert_debug_snapshot!(ast);
}
#[test]
fn test_key_without_value() {
let source = "empty:";
let ast = parse(source).expect("should parse");
insta::assert_debug_snapshot!(ast);
}
#[test]
fn test_deep_nesting() {
let source = "a:\n b:\n c:\n d:\n e: deep";
let ast = parse(source).expect("should parse");
insta::assert_debug_snapshot!(ast);
}
}
#[cfg(test)]
mod property_tests {
use super::*;
use proptest::prelude::*;
proptest! {
#[test]
fn parser_never_panics(input in ".*") {
let _ = parse(&input);
let _ = parse_with_errors(&input);
}
}
proptest! {
#[test]
fn spans_always_valid(input in "[a-z]+: [a-z]+") {
if let Ok(ast) = parse(&input) {
let span = ast.span();
prop_assert!(span.start.offset <= span.end.offset);
}
}
}
}