-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
197: Testing for RON files r=kvark a=peter-scholtens As a solution to the proposed test as mentioned in #192 I created a two functions: read_original() and test_sequence(). Using an assert equal macro various files can be tested. Two examples are given: the first one fails as the mapping sequence changes, the secvond one is correct, as the map only contains one item. Co-authored-by: Peter C. S. Scholtens <peter.scholtens@xs4all.nl>
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use ron::ser::{to_string_pretty, PrettyConfig}; | ||
use ron::de::from_str; | ||
use serde::Deserialize; | ||
use serde::Serialize; | ||
use std::collections::BTreeMap; | ||
|
||
#[derive(Debug, Deserialize, Serialize)] | ||
struct Config { | ||
boolean: bool, | ||
float: f32, | ||
map: BTreeMap<u8, char>, | ||
nested: Nested, | ||
tuple: (u32, u32), | ||
} | ||
|
||
#[derive(Debug, Deserialize, Serialize)] | ||
struct Nested { | ||
a: String, | ||
b: char, | ||
} | ||
|
||
fn read_original(source: &str) -> (String) { | ||
source.to_string() | ||
} | ||
|
||
fn make_roundtrip(source: &str) -> (String) { | ||
let config: Config = match from_str(source) { | ||
Ok(x) => x, | ||
Err(e) => { | ||
println!("Failed to load config: {}", e); | ||
std::process::exit(1); | ||
} | ||
}; | ||
let pretty = PrettyConfig::new() | ||
.with_depth_limit(3) | ||
.with_separate_tuple_members(true) | ||
.with_enumerate_arrays(true); | ||
to_string_pretty(&config, pretty).expect("Serialization failed") | ||
} | ||
|
||
#[test] | ||
fn test_sequence_ex1() | ||
{ | ||
let file = include_str!("preserve_sequence_ex1.ron"); | ||
assert_eq!( | ||
read_original(file), | ||
make_roundtrip(file) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_sequence_ex2() | ||
{ | ||
let file = include_str!("preserve_sequence_ex2.ron"); | ||
assert_eq!( | ||
read_original(file), | ||
make_roundtrip(file) | ||
); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
( | ||
boolean: true, | ||
float: 8.2, | ||
map: { | ||
1: '1', | ||
2: '4', | ||
3: '9', | ||
4: '1', | ||
5: '2', | ||
6: '3', | ||
}, | ||
nested: ( | ||
a: "Decode me!", | ||
b: 'z', | ||
), | ||
tuple: ( | ||
3, | ||
7, | ||
), | ||
) |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
( | ||
boolean: true, | ||
float: 8.2, | ||
map: { | ||
1: '1', | ||
}, | ||
nested: ( | ||
a: "Decode me!", | ||
b: 'z', | ||
), | ||
tuple: ( | ||
3, | ||
7, | ||
), | ||
) |