Skip to content

Commit

Permalink
Merge #197
Browse files Browse the repository at this point in the history
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
bors[bot] and Peter C. S. Scholtens committed Nov 4, 2019
2 parents 41263b1 + c0d52d2 commit a891543
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
61 changes: 61 additions & 0 deletions tests/preserve_sequence.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use ron::ser::{to_string_pretty, PrettyConfig};
use ron::de::from_str;
use serde::Deserialize;
use serde::Serialize;
use std::{collections::HashMap, fs::File};
use std::env;
use std::fs;

#[derive(Debug, Deserialize, Serialize)]
struct Config {
boolean: bool,
float: f32,
map: HashMap<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)
);
}
20 changes: 20 additions & 0 deletions tests/preserve_sequence_ex1.ron
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,
),
)
15 changes: 15 additions & 0 deletions tests/preserve_sequence_ex2.ron
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,
),
)

0 comments on commit a891543

Please sign in to comment.