This repository has been archived by the owner on Apr 6, 2020. It is now read-only.
/
testing_with_ron.rs
137 lines (108 loc) · 4.23 KB
/
testing_with_ron.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
extern crate settingsfile;
use settingsfile::{ShadowSettings, Settings, SupportedType, SettingsRaw, Format, Type};
#[macro_use] extern crate failure; use failure::Error;
extern crate ron;
extern crate serde;
extern crate tempfile;
use std::fs::File;
use std::io::{Seek, SeekFrom};
// setting up the configuration, this will tell `Settingsfile-rs`
// how you want the physical file saved / setup.
#[derive(Clone)]
struct Configuration { }
impl Format for Configuration {
fn filename(&self) -> String { "settings".to_string() }
fn folder(&self) -> String { "program_app_folder".to_string() }
fn from_str<T>(&self,buffer:&str) -> Result<SettingsRaw,Error>
where T : Format + Clone
{
let result : Result<SettingsRaw,ron::de::Error> = ron::de::from_str(&buffer);
println!("from_str result: {:?}",result);
match result {
Ok(result) => Ok(result),
Err(error) => Err(format_err!("{}",error)),
}
}
fn to_string<T:Sized>(&self,object:&T) -> Result<String,Error>
where T : SupportedType + serde::ser::Serialize,
{
let result : Result<String,ron::ser::Error> = ron::ser::to_string(object);
println!("to_string result: {:?}",result);
match result {
Ok(result) => Ok(result),
Err(error) => Err(format_err!("{}",error)),
}
}
}
// testing functionality.
#[test]
fn decoding_and_reencoding() {
let mut test = Settings::new(Configuration{});
assert!(test.set_value("user.name", "snsvrno").is_ok());
// using a tempfile for testing, so we don't have to worry
// about where or what to create it as.
let mut tempfile : File = tempfile::tempfile().unwrap();
// uses `save_to` here so we can tell it what buffer to use,
// if you use `save` it will automatically save it to the file
// defined in the configuration
assert!(test.save_to(&mut tempfile).is_ok());
// needed to reset the cursor to the front,
// otherwise we will read from the end of the file
// and won't get any data.
tempfile.seek(SeekFrom::Start(0)).unwrap();
// now loading that temporary file that we just created
let loaded_setting = Settings::create_from(&mut tempfile,Configuration{}).unwrap();
assert_eq!(loaded_setting.get_value("user.name"),test.get_value("user.name"));
}
#[test]
#[ignore]
fn file_reading_and_writing_settings() {
let mut test = Settings::new(Configuration{});
// cleanup if test was run before
test.delete_file();
assert!(test.set_value("user.name", "snsvrno").is_ok());
if let Err(error) = test.save() {
println!("{:?}",error);
assert!(false);
}
let mut other_test = Settings::new(Configuration{});
if let Err(error) = other_test.load() {
println!("{:?}",error);
assert!(false);
}
assert_eq!(other_test.get_value("user.name"),test.get_value("user.name"));
}
#[test]
#[ignore]
fn file_reading_and_writing_shadow_settings() {
let mut test = ShadowSettings::new(Configuration{});
// cleanup from older tests / if still on disk
test.delete_file_global();
test.delete_file_local();
// setup first case
assert!(test.set_value_global("user.name", "other username").is_ok());
if let Err(error) = test.save() {
println!("{:?}",error);
assert!(false);
}
// loads the first case
let mut other_test = ShadowSettings::new(Configuration{});
if let Err(error) = other_test.load() {
println!("{:?}",error);
assert!(false);
};
assert_eq!(other_test.get_value("user.name"),test.get_value("user.name"));
// set up second case
assert!(test.set_value_local("user.name", "debug tester").is_ok());
if let Err(error) = test.save() {
println!("{:?}",error);
assert!(false);
}
if let Err(error) = other_test.load() {
println!("{:?}",error);
assert!(false);
};
assert_eq!(other_test.get_value_local("user.name"),Some(Type::Text("debug tester".to_string())));
assert_eq!(other_test.get_value_global("user.name"),Some(Type::Text("other username".to_string())));
assert_eq!(other_test.get_value("user.name"),Some(Type::Text("debug tester".to_string())));
}