Lines
100 %
Functions
Branches
use ron::de::{from_str, Error, Position, Span, SpannedError as RonErr};
#[test]
fn test_simple() {
assert_eq!(
from_str(
"/*
* We got a hexadecimal number here!
*
*/0x507"
),
Ok(0x507)
);
}
fn test_nested() {
/* quite * some * nesting * going * on * /* here /* (yeah, maybe a bit too much) */ */ */
*/
// The actual value comes.. /*
// very soon, these are just checks that */
// multi-line comments don't trigger in line comments /*
\"THE VALUE\" /* This is the value /* :) */ */
"
Ok("THE VALUE".to_owned())
fn test_unclosed() {
from_str::<String>("\"hi\" /*"),
Err(RonErr {
code: Error::UnclosedBlockComment,
span: Span {
start: Position { line: 1, col: 6 },
end: Position { line: 1, col: 8 }
})
from_str::<String>(
/* Unfortunately, this comment won't get closed :(
\"THE VALUE (which is invalid)\"
start: Position { line: 7, col: 3 },
end: Position { line: 9, col: 1 }
fn test_unexpected_byte() {
from_str::<u8>("42 /q"),
code: Error::UnexpectedChar('q'),
start: Position { line: 1, col: 4 },
end: Position { line: 1, col: 6 },
fn test_comment_after_literal_in_newtype_lookahead() {
#[derive(Debug, PartialEq, serde::Deserialize)]
enum Cmd {
Echo(Msg),
#[serde(untagged)]
enum Msg {
One(ron::Value),
Many(Vec<ron::Value>),
enum Untagged {
Cmd(Cmd),
// A comment directly after a literal must not be scanned as code
for (ron, inner) in [
("\"hi\"", ron::Value::String(String::from("hi"))),
("'h'", ron::Value::Char('h')),
("b\"hi\"", ron::Value::Bytes(b"hi".to_vec())),
] {
let expected = Cmd::Echo(Msg::One(inner));
for comment in ["", "/*,x*/", "// note\n"] {
let ron = format!("Echo({}{})", ron, comment);
from_str::<Cmd>(&format!("#![enable(unwrap_variant_newtypes)] {}", ron)).as_ref(),
Ok(&expected),
"{}",
ron,
from_str::<Untagged>(&ron)
.map(|Untagged::Cmd(cmd)| cmd)
.as_ref(),
fn test_whitespace_and_comments_inside_unit() {
struct Unit;
struct Struct {
a: (),
b: Unit,
for unit in ["()", "( )", "(\n)", "(/* empty */)", "( // empty\n)"] {
assert_eq!(from_str::<()>(unit), Ok(()), "{}", unit);
assert_eq!(from_str::<Unit>(unit), Ok(Unit), "{}", unit);
from_str::<Struct>(&format!("(a: {0}, b: {0})", unit)),
Ok(Struct { a: (), b: Unit }),
unit,
from_str::<()>("( 42 )"),
code: Error::ExpectedUnit,
start: Position { line: 1, col: 1 },
end: Position { line: 1, col: 1 },
from_str::<()>("( /* )"),
start: Position { line: 1, col: 5 },
end: Position { line: 1, col: 7 },
fn test_whitespace_and_comments_inside_untyped_unit() {
use ron::value::{Map, Number, Value};
Struct { a: () },
assert_eq!(from_str::<Value>(unit), Ok(Value::Unit), "{}", unit);
from_str::<Untagged>(&format!("(a: {})", unit)),
Ok(Untagged::Struct { a: () }),
from_str::<Value>("( /* 1, */ 2 )"),
Ok(Value::Seq(vec![Value::Number(Number::U8(2))]))
from_str::<Value>("( 1, 2 )"),
Ok(Value::Seq(vec![
Value::Number(Number::U8(1)),
Value::Number(Number::U8(2)),
]))
let mut map = Map::new();
map.insert(
Value::String(String::from("a")),
assert_eq!(from_str::<Value>("( /* a */ a: 1 )"), Ok(Value::Map(map)));
enum NotUnit {
Newtype(i32),
from_str::<NotUnit>("( )"),
code: Error::Message(String::from(
"data did not match any variant of untagged enum NotUnit"
)),
end: Position { line: 1, col: 4 },