diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/ron/tests/value.rs | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/ron/tests/value.rs')
-rw-r--r-- | third_party/rust/ron/tests/value.rs | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/third_party/rust/ron/tests/value.rs b/third_party/rust/ron/tests/value.rs new file mode 100644 index 0000000000..8256d3cf95 --- /dev/null +++ b/third_party/rust/ron/tests/value.rs @@ -0,0 +1,131 @@ +use ron::value::{Map, Number, Value}; +use serde::Serialize; +use std::f64; + +#[test] +fn bool() { + assert_eq!("true".parse(), Ok(Value::Bool(true))); + assert_eq!("false".parse(), Ok(Value::Bool(false))); +} + +#[test] +fn char() { + assert_eq!("'a'".parse(), Ok(Value::Char('a'))); +} + +#[test] +fn map() { + let mut map = Map::new(); + map.insert(Value::Char('a'), Value::Number(Number::new(1))); + map.insert(Value::Char('b'), Value::Number(Number::new(2f64))); + assert_eq!("{ 'a': 1, 'b': 2.0 }".parse(), Ok(Value::Map(map))); +} + +#[test] +fn number() { + assert_eq!("42".parse(), Ok(Value::Number(Number::new(42)))); + assert_eq!( + "3.141592653589793".parse(), + Ok(Value::Number(Number::new(f64::consts::PI))) + ); +} + +#[test] +fn option() { + let opt = Some(Box::new(Value::Char('c'))); + assert_eq!("Some('c')".parse(), Ok(Value::Option(opt))); +} + +#[test] +fn string() { + let normal = "\"String\""; + assert_eq!(normal.parse(), Ok(Value::String("String".into()))); + + let raw = "r\"Raw String\""; + assert_eq!(raw.parse(), Ok(Value::String("Raw String".into()))); + + let raw_hashes = "r#\"Raw String\"#"; + assert_eq!(raw_hashes.parse(), Ok(Value::String("Raw String".into()))); + + let raw_escaped = "r##\"Contains \"#\"##"; + assert_eq!( + raw_escaped.parse(), + Ok(Value::String("Contains \"#".into())) + ); + + let raw_multi_line = "r\"Multi\nLine\""; + assert_eq!( + raw_multi_line.parse(), + Ok(Value::String("Multi\nLine".into())) + ); +} + +#[test] +fn seq() { + let seq = vec![ + Value::Number(Number::new(1)), + Value::Number(Number::new(2f64)), + ]; + assert_eq!("[1, 2.0]".parse(), Ok(Value::Seq(seq))); +} + +#[test] +fn unit() { + use ron::error::{Error, Position, SpannedError}; + + assert_eq!("()".parse(), Ok(Value::Unit)); + assert_eq!("Foo".parse(), Ok(Value::Unit)); + + assert_eq!( + "".parse::<Value>(), + Err(SpannedError { + code: Error::Eof, + position: Position { col: 1, line: 1 } + }) + ); +} + +#[derive(Serialize)] +struct Scene(Option<(u32, u32)>); + +#[derive(Serialize)] +struct Scene2 { + foo: Option<(u32, u32)>, +} + +#[test] +fn roundtrip() { + use ron::{de::from_str, ser::to_string}; + + { + let s = to_string(&Scene2 { + foo: Some((122, 13)), + }) + .unwrap(); + println!("{}", s); + let scene: Value = from_str(&s).unwrap(); + println!("{:?}", scene); + } + { + let s = to_string(&Scene(Some((13, 122)))).unwrap(); + println!("{}", s); + let scene: Value = from_str(&s).unwrap(); + println!("{:?}", scene); + } +} + +#[test] +fn map_roundtrip_338() { + // https://github.com/ron-rs/ron/issues/338 + + let v: Value = ron::from_str("{}").unwrap(); + println!("{:?}", v); + + let ser = ron::to_string(&v).unwrap(); + println!("{:?}", ser); + + let roundtrip = ron::from_str(&ser).unwrap(); + println!("{:?}", roundtrip); + + assert_eq!(v, roundtrip); +} |