Skip to content

Commit

Permalink
Merge pull request #134 from hoodie/bug/emitting_hexlike_strings
Browse files Browse the repository at this point in the history
Fix emitting hexlike strings without quotes
  • Loading branch information
chyh1990 committed Sep 5, 2019
2 parents 1d29d21 + 0a2df81 commit 360a34d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 11 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ matrix:
- rust: stable
- rust: beta
- rust: nightly
- rust: 1.17.0
script: cargo build
- rust: 1.24.1
- rust: 1.28.0
- rust: nightly
env: CLIPPY
script: |
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ so it may not be a huge problem for most users.
* Tag directive
* Alias while desearilization

## Minimum Rust version policy

This crate's minimum supported `rustc` version is 1.28, as this is the currently known minimum version for [`regex`](https://crates.io/crates/regex#minimum-rust-version-policy) as well.

## License

Licensed under either of
Expand Down
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
install:
- ps: Start-FileDownload 'https://static.rust-lang.org/dist/rust-1.24.1-i686-pc-windows-gnu.exe'
- rust-1.24.1-i686-pc-windows-gnu.exe /VERYSILENT /NORESTART /DIR="C:\Program Files (x86)\Rust"
- ps: Start-FileDownload 'https://static.rust-lang.org/dist/rust-1.28.0-i686-pc-windows-gnu.exe'
- rust-1.28.0-i686-pc-windows-gnu.exe /VERYSILENT /NORESTART /DIR="C:\Program Files (x86)\Rust"
- SET PATH=%PATH%;C:\Program Files (x86)\Rust\bin
- SET PATH=%PATH%;C:\MinGW\bin
- rustc -V
Expand Down
1 change: 1 addition & 0 deletions src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ fn need_quotes(string: &str) -> bool {
]
.contains(&string)
|| string.starts_with('.')
|| string.starts_with("0x")
|| string.parse::<i64>().is_ok()
|| string.parse::<f64>().is_ok()
}
Expand Down
54 changes: 48 additions & 6 deletions tests/test_round_trip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,64 @@ extern crate yaml_rust;

use yaml_rust::{Yaml, YamlEmitter, YamlLoader};

fn test_round_trip(original: &Yaml) {
let mut out = String::new();
YamlEmitter::new(&mut out).dump(original).unwrap();
let documents = YamlLoader::load_from_str(&out).unwrap();
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());
test_round_trip(&y);
roundtrip(&y);
}

#[test]
fn test_colon_in_string() {
let y = Yaml::String("x: %".to_owned());
test_round_trip(&y);
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

}

0 comments on commit 360a34d

Please sign in to comment.