1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
use ron::error::{Error, Position, Span, SpannedError};
use serde::Deserialize;
#[derive(Debug, Deserialize, PartialEq)]
struct MyUnitStruct;
#[derive(Debug, Deserialize, PartialEq)]
struct MyTupleStruct(bool, i32);
#[derive(Debug, Deserialize, PartialEq)]
struct MyNewtypeStruct(MyTupleStruct);
#[derive(Debug, Deserialize, PartialEq)]
struct MyStruct {
a: bool,
b: i32,
}
#[test]
fn test_unit_struct_name_mismatch() {
assert_eq!(ron::from_str::<MyUnitStruct>("()"), Ok(MyUnitStruct),);
assert_eq!(
ron::from_str::<MyUnitStruct>("MyUnitStruct"),
Ok(MyUnitStruct),
);
assert_eq!(
ron::from_str::<MyUnitStruct>("MyUnit Struct"),
Err(SpannedError {
code: Error::ExpectedDifferentStructName {
expected: "MyUnitStruct",
found: String::from("MyUnit")
},
span: Span {
start: ron::error::Position { line: 1, col: 1 },
end: Position { line: 1, col: 7 }
}
}),
);
assert_eq!(
ron::from_str::<MyUnitStruct>("42"),
Err(SpannedError {
code: Error::ExpectedNamedStructLike("MyUnitStruct"),
span: Span {
start: ron::error::Position { line: 1, col: 1 },
end: Position { line: 1, col: 1 }
}
}),
);
}
#[test]
fn test_tuple_struct_name_mismatch() {
assert_eq!(
ron::from_str::<MyTupleStruct>("(true, 42)"),
Ok(MyTupleStruct(true, 42)),
);
assert_eq!(
ron::from_str::<MyTupleStruct>("MyTupleStruct(true, 42)"),
Ok(MyTupleStruct(true, 42)),
);
assert_eq!(
ron::from_str::<MyTupleStruct>("MyTypleStruct(true, 42)"),
Err(SpannedError {
code: Error::ExpectedDifferentStructName {
expected: "MyTupleStruct",
found: String::from("MyTypleStruct")
},
span: Span {
start: ron::error::Position { line: 1, col: 1 },
end: Position { line: 1, col: 14 }
}
}),
);
assert_eq!(
ron::from_str::<MyTupleStruct>("42"),
Err(SpannedError {
code: Error::ExpectedNamedStructLike("MyTupleStruct"),
span: Span {
start: ron::error::Position { line: 1, col: 1 },
end: Position { line: 1, col: 1 }
}
}),
);
}
#[test]
fn test_newtype_struct_name_mismatch() {
assert_eq!(
ron::from_str::<MyNewtypeStruct>("((true, 42))"),
Ok(MyNewtypeStruct(MyTupleStruct(true, 42))),
);
assert_eq!(
ron::from_str::<MyNewtypeStruct>("MyNewtypeStruct((true, 42))"),
Ok(MyNewtypeStruct(MyTupleStruct(true, 42))),
);
assert_eq!(
ron::from_str::<MyNewtypeStruct>("MyNewtypeStrucl((true, 42))"),
Err(SpannedError {
code: Error::ExpectedDifferentStructName {
expected: "MyNewtypeStruct",
found: String::from("MyNewtypeStrucl")
},
span: Span {
start: ron::error::Position { line: 1, col: 1 },
end: Position { line: 1, col: 16 }
}
}),
);
assert_eq!(
ron::from_str::<MyNewtypeStruct>("42"),
Err(SpannedError {
code: Error::ExpectedNamedStructLike("MyNewtypeStruct"),
span: Span {
start: ron::error::Position { line: 1, col: 1 },
end: Position { line: 1, col: 1 }
}
}),
);
}
#[test]
fn test_struct_name_mismatch() {
assert_eq!(
ron::from_str::<MyStruct>("(a: true, b: 42)"),
Ok(MyStruct { a: true, b: 42 }),
);
assert_eq!(
ron::from_str::<MyStruct>("MyStruct(a: true, b: 42)"),
Ok(MyStruct { a: true, b: 42 }),
);
assert_eq!(
ron::from_str::<MyStruct>("MuStryct(a: true, b: 42)"),
Err(SpannedError {
code: Error::ExpectedDifferentStructName {
expected: "MyStruct",
found: String::from("MuStryct")
},
span: Span {
start: ron::error::Position { line: 1, col: 1 },
end: Position { line: 1, col: 9 }
}
}),
);
assert_eq!(
ron::from_str::<MyStruct>("42"),
Err(SpannedError {
code: Error::ExpectedNamedStructLike("MyStruct"),
span: Span {
start: ron::error::Position { line: 1, col: 1 },
end: Position { line: 1, col: 1 }
}
}),
);
}
|