2020-03-30 19:35:11 +11:00
|
|
|
//! This tests the `defaults` module to ensure things behave as they should.
|
|
|
|
|
2020-03-31 14:23:54 +11:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2021-08-08 15:31:48 +10:00
|
|
|
use cacao::appkit::{App, AppDelegate};
|
2020-04-01 09:22:00 +11:00
|
|
|
use cacao::defaults::{UserDefaults, Value};
|
2020-03-30 19:35:11 +11:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct DefaultsTest;
|
|
|
|
|
|
|
|
impl AppDelegate for DefaultsTest {
|
|
|
|
fn did_finish_launching(&self) {
|
|
|
|
let mut defaults = UserDefaults::standard();
|
|
|
|
|
2020-03-31 14:23:54 +11:00
|
|
|
defaults.register({
|
|
|
|
let mut map = HashMap::new();
|
2020-04-01 14:59:08 +11:00
|
|
|
map.insert("testbool", Value::Bool(true));
|
|
|
|
map.insert("testint", Value::Integer(42));
|
|
|
|
map.insert("testfloat", Value::Float(42.));
|
|
|
|
map.insert("teststring", Value::string("Testing"));
|
|
|
|
|
|
|
|
let bytes = "BYTES TEST".to_string().into_bytes();
|
|
|
|
map.insert("testdata", Value::Data(bytes));
|
|
|
|
|
2020-03-31 14:23:54 +11:00
|
|
|
map
|
|
|
|
});
|
2020-03-30 19:35:11 +11:00
|
|
|
|
2020-04-01 14:59:08 +11:00
|
|
|
let testbool = defaults.get("testbool").unwrap().as_bool().unwrap();
|
|
|
|
assert_eq!(testbool, true);
|
|
|
|
|
|
|
|
let testint = defaults.get("testint").unwrap().as_i64().unwrap();
|
|
|
|
assert_eq!(testint, 42);
|
2020-04-01 09:22:00 +11:00
|
|
|
|
2020-04-01 14:59:08 +11:00
|
|
|
let testfloat = defaults.get("testfloat").unwrap().as_f64().unwrap();
|
|
|
|
assert_eq!(testfloat, 42.);
|
|
|
|
|
|
|
|
let teststring = defaults.get("teststring").unwrap();
|
|
|
|
assert_eq!(teststring.as_str().unwrap(), "Testing");
|
2022-07-11 01:15:29 +10:00
|
|
|
|
2020-04-01 14:59:08 +11:00
|
|
|
let bytes = defaults.get("testdata").unwrap();
|
|
|
|
let s = match std::str::from_utf8(bytes.as_data().unwrap()) {
|
2020-04-01 09:22:00 +11:00
|
|
|
Ok(s) => s,
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("Error converting bytes {}", e);
|
|
|
|
"Error converting bytes"
|
2022-08-27 00:40:31 +10:00
|
|
|
}
|
2020-04-01 09:22:00 +11:00
|
|
|
};
|
|
|
|
|
2020-04-01 14:59:08 +11:00
|
|
|
assert_eq!(s, "BYTES TEST");
|
|
|
|
println!("All UserDefaults tests pass");
|
2020-03-30 19:35:11 +11:00
|
|
|
|
|
|
|
App::terminate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
App::new("com.cacao.defaults-test", DefaultsTest::default()).run();
|
|
|
|
}
|