// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. //! The VDF tokenizer, parser and writer on hand-written input. use arena_core::ParseError; use arena_core::vdf::{self, Obj, TokenKind, Value}; #[test] fn tokens_and_offsets() { let t = vdf::tokenize(b"\"a\" {\n\t\"b\\\\c\" \"d\\\"e\"\n}").unwrap(); let kinds: Vec<&TokenKind> = t.iter().map(|t| &t.kind).collect(); assert_eq!( kinds, [ &TokenKind::Str("a".into()), &TokenKind::Open, &TokenKind::Str("b\\c".into()), &TokenKind::Str("d\"e".into()), &TokenKind::Close ] ); let at: Vec = t.iter().map(|t| t.at).collect(); assert_eq!(at, [0, 4, 7, 14, 21]); } #[test] fn escapes() { let t = vdf::tokenize(br#""\n\t\r\\\"""#).unwrap(); assert_eq!(t[0].kind, TokenKind::Str("\n\t\r\\\"".into())); for bad in [&br#""\x""#[..], br#""\0""#, br#""\'""#, br#""\""#, b"\"\\"] { assert!(matches!(vdf::tokenize(bad), Err(ParseError::Syntax { .. })), "{:?}", String::from_utf8_lossy(bad)); } } #[test] fn comments_and_whitespace() { let doc = vdf::parse(b"// top\r\n\"a\" // after key\r\n{ \"b\" \"c\" } // end").unwrap(); let Some(Value::Obj(a)) = doc.get("A") else { panic!("{doc:?}") }; assert_eq!(a.get("b"), Some(&Value::Str("c".into()))); } #[test] fn refuses_unquoted_and_directives() { for bad in [ &b"a { }"[..], b"\"a\" b", b"#include \"x.vdf\"", b"#base \"x\"", b"\"a\" \"b\" [$WIN32]", b"/ x", b"\"a\" { \"b\" \"c\" }}", ] { assert!(matches!(vdf::parse(bad), Err(ParseError::Syntax { .. })), "{:?}", String::from_utf8_lossy(bad)); } } #[test] fn structure_errors() { assert!(matches!(vdf::parse(b"\"a\""), Err(ParseError::Syntax { .. }))); assert!(matches!(vdf::parse(b"\"a\" {"), Err(ParseError::Syntax { .. }))); assert!(matches!(vdf::parse(b"{ }"), Err(ParseError::Syntax { at: 0 }))); assert!(matches!(vdf::parse(b"\"a\" { { } }"), Err(ParseError::Syntax { .. }))); assert_eq!(vdf::parse(b"\"\" \"v\""), Err(ParseError::Missing("key"))); assert_eq!(vdf::parse(b"\"a\" \"1\" \"A\" \"2\""), Err(ParseError::Duplicate("key"))); assert!(vdf::parse(b"\"a\" { \"k\" \"1\" } \"b\" { \"k\" \"2\" }").is_ok(), "same key on different objects"); assert_eq!(vdf::parse(b"").unwrap(), Obj::new()); assert_eq!(vdf::parse(b" \n// only a comment").unwrap(), Obj::new()); } #[test] fn syntax_offset_is_where_the_problem_is() { assert_eq!(vdf::parse(b"\"a\" \"b\"\n\"c\" x"), Err(ParseError::Syntax { at: 12 })); assert_eq!(vdf::tokenize(b"\"ab\0\""), Err(ParseError::Syntax { at: 3 })); } #[test] fn bom_only_at_the_start() { assert!(vdf::parse(b"\xEF\xBB\xBF\"a\" \"b\"").is_ok()); assert!(vdf::parse(b"\"a\" \xEF\xBB\xBF\"b\"").is_err()); } #[test] fn obj_insert_rules() { let mut o = Obj::new(); o.insert("Key".into(), Value::Str("v".into())).unwrap(); assert_eq!(o.insert("KEY".into(), Value::Str("w".into())), Err(ParseError::Duplicate("key"))); assert_eq!(o.insert(String::new(), Value::Str("w".into())), Err(ParseError::Missing("key"))); assert_eq!(o.insert("k".repeat(257), Value::Str("w".into())), Err(ParseError::TooLong("key"))); assert_eq!(o.insert("k2".into(), Value::Str("v".repeat(4097))), Err(ParseError::TooLong("value"))); assert_eq!(o.len(), 1); assert_eq!(o.iter().next(), Some(("Key", &Value::Str("v".into())))); } #[test] fn write_then_parse() { let mut inner = Obj::new(); inner.insert("path".into(), Value::Str("C:\\Program Files (x86)\\Steam".into())).unwrap(); inner.insert("quote".into(), Value::Str("say \"hi\"\n\tbye".into())).unwrap(); let mut root = Obj::new(); root.insert("0".into(), Value::Obj(inner)).unwrap(); let mut doc = Obj::new(); doc.insert("libraryfolders".into(), Value::Obj(root)).unwrap(); let text = vdf::write(&doc); assert!(text.contains(r#""path" "C:\\Program Files (x86)\\Steam""#), "{text}"); assert_eq!(vdf::parse(text.as_bytes()).unwrap(), doc); } #[test] fn fixture_round_trips_through_the_writer() { let doc = vdf::parse(include_bytes!("fixtures/steam/libraryfolders.vdf")).unwrap(); assert_eq!(vdf::parse(vdf::write(&doc).as_bytes()).unwrap(), doc); }