add parser tests

This commit is contained in:
Simon 2023-02-08 01:43:55 +01:00
parent b4722b5016
commit 1dd170c696

View File

@ -367,6 +367,18 @@ fn test_array1() {
#[test] #[test]
fn test_array2() { fn test_array2() {
let parsed = Parser::new(vec![
Token::LBracket("[".to_string()),
Token::Int("123".to_string()),
Token::Comma(",".to_string()),
Token::RBracket("]".to_string()),
])
.parse_top_level();
assert_eq!(parsed, Ok(Node::Array(vec![Node::Int(123)])))
}
#[test]
fn test_array3() {
let parsed = Parser::new(vec![ let parsed = Parser::new(vec![
Token::LBracket("[".to_string()), Token::LBracket("[".to_string()),
Token::Int("123".to_string()), Token::Int("123".to_string()),
@ -384,6 +396,26 @@ fn test_array2() {
) )
} }
#[test]
fn test_array4() {
let parsed = Parser::new(vec![
Token::LBracket("[".to_string()),
Token::Int("123".to_string()),
Token::Comma(",".to_string()),
Token::String("\"hello\n\"".to_string()),
Token::Comma(",".to_string()),
Token::RBracket("]".to_string()),
])
.parse_top_level();
assert_eq!(
parsed,
Ok(Node::Array(vec![
Node::Int(123),
Node::String("hello\n".to_string()),
]))
)
}
#[test] #[test]
fn test_object0() { fn test_object0() {
let parsed = Parser::new(vec![ let parsed = Parser::new(vec![
@ -398,13 +430,77 @@ fn test_object0() {
fn test_object1() { fn test_object1() {
let parsed = Parser::new(vec![ let parsed = Parser::new(vec![
Token::LBrace("{".to_string()), Token::LBrace("{".to_string()),
Token::Name("abc".to_string()),
Token::Colon(":".to_string()),
Token::String("\"hello\"".to_string()),
Token::RBrace("}".to_string()), Token::RBrace("}".to_string()),
]) ])
.parse_top_level(); .parse_top_level();
assert_eq!( assert_eq!(
parsed, parsed,
Ok(Node::Object({ Ok(Node::Object({
let fields = HashMap::new(); let mut fields = HashMap::new();
fields.insert(
"abc".to_string(),
Box::new(Node::String("hello".to_string())),
);
fields
}))
)
}
#[test]
fn test_object2() {
let parsed = Parser::new(vec![
Token::LBrace("{".to_string()),
Token::Name("abc".to_string()),
Token::Colon(":".to_string()),
Token::String("\"hello\"".to_string()),
Token::Comma(",".to_string()),
Token::String("\"foo\"".to_string()),
Token::Colon(":".to_string()),
Token::Int("123".to_string()),
Token::RBrace("}".to_string()),
])
.parse_top_level();
assert_eq!(
parsed,
Ok(Node::Object({
let mut fields = HashMap::new();
fields.insert(
"abc".to_string(),
Box::new(Node::String("hello".to_string())),
);
fields.insert("foo".to_string(), Box::new(Node::Int(123)));
fields
}))
)
}
#[test]
fn test_object3() {
let parsed = Parser::new(vec![
Token::LBrace("{".to_string()),
Token::Name("abc".to_string()),
Token::Colon(":".to_string()),
Token::String("\"hello\"".to_string()),
Token::Comma(",".to_string()),
Token::String("\"foo\"".to_string()),
Token::Colon(":".to_string()),
Token::Int("123".to_string()),
Token::Comma(",".to_string()),
Token::RBrace("}".to_string()),
])
.parse_top_level();
assert_eq!(
parsed,
Ok(Node::Object({
let mut fields = HashMap::new();
fields.insert(
"abc".to_string(),
Box::new(Node::String("hello".to_string())),
);
fields.insert("foo".to_string(), Box::new(Node::Int(123)));
fields fields
})) }))
) )