Skip to content

Commit

Permalink
fix(ruby_hash parser): drop initial colon in key parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
tessneau committed Sep 24, 2024
1 parent a8aa30e commit 93dd991
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/parsing/ruby_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn parse_colon_key<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
char(':'),
alt((parse_str('"'), parse_str('\''), parse_symbol_key)),
),
|res| (String::from(":") + res).into(),
|res| res.into(),
)(input)
}

Expand Down Expand Up @@ -215,13 +215,24 @@ mod tests {
}

#[test]
fn test_parse_symbol_value() {
let result = parse_ruby_hash(r#"{ "key" => :foo }"#).unwrap();
fn test_parse_symbol_key() {
let result = parse_ruby_hash(r#"{ :key => "foo", :number => 500 }"#).unwrap();
assert!(result.is_object());
let result = result.as_object().unwrap();
let value = result.get("key").unwrap();
assert!(value.is_bytes());
assert_eq!(value.as_bytes().unwrap(), ":foo");
assert_eq!(value.as_bytes().unwrap(), "foo");
assert!(result.get("number").unwrap().is_float());
}

#[test]
fn test_parse_symbol_colon_separator() {
let result = parse_ruby_hash(r#"{ key: "foo" }"#).unwrap();
assert!(result.is_object());
let result = result.as_object().unwrap();
let value = result.get("key").unwrap();
assert!(value.is_bytes());
assert_eq!(value.as_bytes().unwrap(), "foo");
}

#[test]
Expand Down Expand Up @@ -257,17 +268,17 @@ mod tests {
.unwrap();
assert!(result.is_object());
let result = result.as_object().unwrap();
assert!(result.get(":colon").unwrap().is_bytes());
assert!(result.get(":double").unwrap().is_bytes());
assert!(result.get(":simple").unwrap().is_bytes());
assert!(result.get("colon").unwrap().is_bytes());
assert!(result.get("double").unwrap().is_bytes());
assert!(result.get("simple").unwrap().is_bytes());
}

#[test]
fn test_parse_arrow_object_key_underscore() {
let result = parse_ruby_hash(r#"{ :with_underscore => "hello world" }"#).unwrap();
assert!(result.is_object());
let result = result.as_object().unwrap();
assert!(result.get(":with_underscore").unwrap().is_bytes());
assert!(result.get("with_underscore").unwrap().is_bytes());
}

#[test]
Expand Down Expand Up @@ -320,7 +331,7 @@ mod tests {
parse_ruby_hash(r#"{:hello=>"world",'number'=>42,"weird"=>'format\'here'}"#).unwrap();
assert!(result.is_object());
let result = result.as_object().unwrap();
assert!(result.get(":hello").unwrap().is_bytes());
assert!(result.get("hello").unwrap().is_bytes());
assert!(result.get("number").unwrap().is_float());
}

Expand Down

0 comments on commit 93dd991

Please sign in to comment.