Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert artificial changes to the file
- Loading branch information
Showing
1 changed file
with
83 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,83 @@ | ||
#![cfg(feature = "json5")] | ||
|
||
extern crate config; | ||
extern crate float_cmp; | ||
extern crate serde; | ||
|
||
#[macro_use] | ||
extern crate serde_derive; | ||
|
||
use config::*; | ||
use float_cmp::ApproxEqUlps; | ||
use std::collections::HashMap; | ||
use std::path::PathBuf; | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct Place { | ||
name: String, | ||
longitude: f64, | ||
latitude: f64, | ||
favorite: bool, | ||
telephone: Option<String>, | ||
reviews: u64, | ||
creator: HashMap<String, Value>, | ||
rating: Option<f32>, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct Settings { | ||
debug: f64, | ||
production: Option<String>, | ||
place: Place, | ||
#[serde(rename = "arr")] | ||
elements: Vec<String>, | ||
} | ||
|
||
fn make() -> Config { | ||
Config::builder() | ||
.add_source(File::new("tests/Settings", FileFormat::Json5)) | ||
.build() | ||
.unwrap() | ||
} | ||
|
||
#[test] | ||
fn test_file() { | ||
let c = make(); | ||
|
||
// Deserialize the entire file as single struct | ||
let s: Settings = c.try_into().unwrap(); | ||
|
||
assert!(s.debug.approx_eq_ulps(&1.0, 2)); | ||
assert_eq!(s.production, Some("false".to_string())); | ||
assert_eq!(s.place.name, "Torre di Pisa"); | ||
assert!(s.place.longitude.approx_eq_ulps(&43.7224985, 2)); | ||
assert!(s.place.latitude.approx_eq_ulps(&10.3970522, 2)); | ||
assert_eq!(s.place.favorite, false); | ||
assert_eq!(s.place.reviews, 3866); | ||
assert_eq!(s.place.rating, Some(4.5)); | ||
assert_eq!(s.place.telephone, None); | ||
assert_eq!(s.elements.len(), 10); | ||
assert_eq!(s.elements[3], "4".to_string()); | ||
assert_eq!( | ||
s.place.creator["name"].clone().into_string().unwrap(), | ||
"John Smith".to_string() | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_error_parse() { | ||
let res = Config::builder() | ||
.add_source(File::new("tests/Settings-invalid", FileFormat::Json5)) | ||
.build(); | ||
|
||
let path_with_extension: PathBuf = ["tests", "Settings-invalid.json5"].iter().collect(); | ||
|
||
assert!(res.is_err()); | ||
assert_eq!( | ||
res.unwrap_err().to_string(), | ||
format!( | ||
" --> 2:7\n |\n2 | ok: true␊\n | ^---\n |\n = expected null in {}", | ||
path_with_extension.display() | ||
) | ||
); | ||
} | ||
#![cfg(feature = "json5")] | ||
|
||
extern crate config; | ||
extern crate float_cmp; | ||
extern crate serde; | ||
|
||
#[macro_use] | ||
extern crate serde_derive; | ||
|
||
use config::*; | ||
use float_cmp::ApproxEqUlps; | ||
use std::collections::HashMap; | ||
use std::path::PathBuf; | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct Place { | ||
name: String, | ||
longitude: f64, | ||
latitude: f64, | ||
favorite: bool, | ||
telephone: Option<String>, | ||
reviews: u64, | ||
creator: HashMap<String, Value>, | ||
rating: Option<f32>, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct Settings { | ||
debug: f64, | ||
production: Option<String>, | ||
place: Place, | ||
#[serde(rename = "arr")] | ||
elements: Vec<String>, | ||
} | ||
|
||
fn make() -> Config { | ||
Config::builder() | ||
.add_source(File::new("tests/Settings", FileFormat::Json5)) | ||
.build() | ||
.unwrap() | ||
} | ||
|
||
#[test] | ||
fn test_file() { | ||
let c = make(); | ||
|
||
// Deserialize the entire file as single struct | ||
let s: Settings = c.try_into().unwrap(); | ||
|
||
assert!(s.debug.approx_eq_ulps(&1.0, 2)); | ||
assert_eq!(s.production, Some("false".to_string())); | ||
assert_eq!(s.place.name, "Torre di Pisa"); | ||
assert!(s.place.longitude.approx_eq_ulps(&43.7224985, 2)); | ||
assert!(s.place.latitude.approx_eq_ulps(&10.3970522, 2)); | ||
assert_eq!(s.place.favorite, false); | ||
assert_eq!(s.place.reviews, 3866); | ||
assert_eq!(s.place.rating, Some(4.5)); | ||
assert_eq!(s.place.telephone, None); | ||
assert_eq!(s.elements.len(), 10); | ||
assert_eq!(s.elements[3], "4".to_string()); | ||
assert_eq!( | ||
s.place.creator["name"].clone().into_string().unwrap(), | ||
"John Smith".to_string() | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_error_parse() { | ||
let res = Config::builder() | ||
.add_source(File::new("tests/Settings-invalid", FileFormat::Json5)) | ||
.build(); | ||
|
||
let path_with_extension: PathBuf = ["tests", "Settings-invalid.json5"].iter().collect(); | ||
|
||
assert!(res.is_err()); | ||
assert_eq!( | ||
res.unwrap_err().to_string(), | ||
format!( | ||
" --> 2:7\n |\n2 | ok: true␊\n | ^---\n |\n = expected null in {}", | ||
path_with_extension.display() | ||
) | ||
); | ||
} |