Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Commit

Permalink
settings: add tests for SettingsExtManual
Browse files Browse the repository at this point in the history
  • Loading branch information
Cogitri committed Aug 14, 2019
1 parent 12e500f commit ea6bcbf
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,65 @@ impl<O: IsA<Settings>> SettingsExtManual for O {
self.set_value(key, &value.to_variant())
}
}

#[cfg(test)]
mod test {
use super::*;
use std::env::set_var;
use std::path::Path;
use std::process::Command;
use std::str::from_utf8;

fn set_env() {
let path_string = format!("{}/tests/gschema.compile", env!("CARGO_MANIFEST_DIR"));
let gschema_path = Path::new(&path_string);

let output = Command::new("glib-compile-schemas")
.args(&[
gschema_path.to_str().unwrap(),
"--targetdir",
env!("OUT_DIR"),
])
.output()
.unwrap();

if !output.status.success() {
println!("Failed to generate GSchema!");
println!(
"glib-compile-schemas stdout: {}",
from_utf8(&output.stdout).unwrap()
);
println!(
"glib-compile-schemas stderr: {}",
from_utf8(&output.stderr).unwrap()
);
panic!("Can't test without GSchemas!");
}

set_var("GSETTINGS_SCHEMA_DIR", env!("OUT_DIR"));
set_var("GSETTINGS_BACKEND", "memory");
}

#[test]
fn string_get() {
set_env();
let settings = Settings::new("com.github.gtk-rs.test");
assert_eq!(settings.get::<String>("test-string").as_str(), "Good");
}

#[test]
fn bool_set_get() {
set_env();
let settings = Settings::new("com.github.gtk-rs.test");
settings.set("test-bool", &false).unwrap();
assert!(!settings.get::<bool>("test-bool"));
}

#[test]
#[should_panic]
fn wrong_type() {
set_env();
let settings = Settings::new("com.github.gtk-rs.test");
settings.get::<u8>("test-string");
}
}
17 changes: 17 additions & 0 deletions tests/com.github.gtk-rs.test.gschema.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>

<schemalist>

<schema path="/com/github/gtk-rs/" id="com.github.gtk-rs.test">

<key name="test-string" type="s">
<default>"Good"</default>
</key>

<key name="test-bool" type="b">
<default>true</default>
</key>

</schema>

</schemalist>

0 comments on commit ea6bcbf

Please sign in to comment.