Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing for RON files #197

Merged
merged 2 commits into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
),
)