summaryrefslogtreecommitdiffstats
path: root/third_party/rust/yaml-rust/tests/test_round_trip.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/yaml-rust/tests/test_round_trip.rs
parentInitial commit. (diff)
downloadfirefox-esr-upstream.tar.xz
firefox-esr-upstream.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/yaml-rust/tests/test_round_trip.rs')
-rw-r--r--third_party/rust/yaml-rust/tests/test_round_trip.rs65
1 files changed, 65 insertions, 0 deletions
diff --git a/third_party/rust/yaml-rust/tests/test_round_trip.rs b/third_party/rust/yaml-rust/tests/test_round_trip.rs
new file mode 100644
index 0000000000..dc5e85ef39
--- /dev/null
+++ b/third_party/rust/yaml-rust/tests/test_round_trip.rs
@@ -0,0 +1,65 @@
+extern crate yaml_rust;
+
+use yaml_rust::{Yaml, YamlEmitter, YamlLoader};
+
+fn roundtrip(original: &Yaml) {
+ let mut emitted = String::new();
+ YamlEmitter::new(&mut emitted).dump(original).unwrap();
+
+ let documents = YamlLoader::load_from_str(&emitted).unwrap();
+ println!("emitted {}", emitted);
+
+ assert_eq!(documents.len(), 1);
+ assert_eq!(documents[0], *original);
+}
+
+fn double_roundtrip(original: &str) {
+ let parsed = YamlLoader::load_from_str(&original).unwrap();
+
+ let mut serialized = String::new();
+ YamlEmitter::new(&mut serialized).dump(&parsed[0]).unwrap();
+
+ let reparsed = YamlLoader::load_from_str(&serialized).unwrap();
+
+ assert_eq!(parsed, reparsed);
+}
+
+#[test]
+fn test_escape_character() {
+ let y = Yaml::String("\x1b".to_owned());
+ roundtrip(&y);
+}
+
+#[test]
+fn test_colon_in_string() {
+ let y = Yaml::String("x: %".to_owned());
+ roundtrip(&y);
+}
+
+#[test]
+fn test_numberlike_strings() {
+ let docs = [
+ r#"x: "1234""#, r#"x: "01234""#, r#""1234""#,
+ r#""01234""#, r#"" 01234""#, r#""0x1234""#,
+ r#"" 0x1234""#,
+ ];
+
+ for doc in &docs {
+ roundtrip(&Yaml::String(doc.to_string()));
+ double_roundtrip(&doc);
+ }
+}
+
+/// Example from https://github.com/chyh1990/yaml-rust/issues/133
+#[test]
+fn test_issue133() {
+
+ let doc = YamlLoader::load_from_str("\"0x123\"").unwrap().pop().unwrap();
+ assert_eq!(doc, Yaml::String("0x123".to_string()));
+
+ let mut out_str = String::new();
+ YamlEmitter::new(&mut out_str).dump(&doc).unwrap();
+ let doc2 = YamlLoader::load_from_str(&out_str).unwrap().pop().unwrap();
+ assert_eq!(doc, doc2); // This failed because the type has changed to a number now
+
+}