Add test for span

This commit is contained in:
howardwu 2020-07-28 22:54:32 -07:00
parent 695ca8d998
commit 6a199d1256

View File

@ -31,3 +31,23 @@ impl<'i> From<SpanDef<'i>> for Span<'i> {
Span::new(def.input, def.start, def.end).unwrap()
}
}
#[test]
fn test_span_def() {
// Wrapper serializable JSON struct
#[derive(Serialize)]
struct Element<'ast> {
#[serde(with = "SpanDef")]
span: Span<'ast>,
}
// Starting value
let span = Span::new("hello world", 1, 5).unwrap();
let element = Element { span };
// Attempt to serialize span to string.
let output = serde_json::to_string(&element).unwrap();
let expected_output = "{\"span\":{\"input\":\"ello\",\"start\":1,\"end\":5}}";
assert_eq!(expected_output, output);
}