summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyan Leckey <ryan@launchbadge.com>2017-06-22 23:10:56 -0700
committerRyan Leckey <ryan@launchbadge.com>2017-06-22 23:10:56 -0700
commitc798fd881fa8652e5d9ea2873babed55da1ad05f (patch)
tree85819daff6b8f715cc11a5ede003881cdd034b94
parentde0de66f1cd01c5e6184b9e326ca58e201289326 (diff)
Add a test to ensure we can get dates out of config
-rw-r--r--Cargo.toml1
-rw-r--r--src/file/format/mod.rs5
-rw-r--r--src/file/format/toml.rs5
-rw-r--r--tests/datetime.rs70
4 files changed, 77 insertions, 4 deletions
diff --git a/Cargo.toml b/Cargo.toml
index d8785aa..3a61e3d 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -30,3 +30,4 @@ yaml-rust = { version = "^0.3.5", optional = true }
[dev-dependencies]
serde_derive = "^1.0.8"
float-cmp = "0.2.3"
+chrono = { version = "0.4", features = ["serde"] }
diff --git a/src/file/format/mod.rs b/src/file/format/mod.rs
index 1988513..366fa45 100644
--- a/src/file/format/mod.rs
+++ b/src/file/format/mod.rs
@@ -1,3 +1,7 @@
+// If no features are used, there is an "unused mut" warning in `ALL_EXTENSIONS`
+// BUG: ? For some reason this doesn't do anything if I try and function scope this
+#![allow(unused_mut)]
+
use source::Source;
use value::Value;
use std::error::Error;
@@ -29,6 +33,7 @@ pub enum FileFormat {
lazy_static! {
#[doc(hidden)]
+ // #[allow(unused_mut)] ?
pub static ref ALL_EXTENSIONS: HashMap<FileFormat, Vec<&'static str>> = {
let mut formats: HashMap<FileFormat, Vec<_>> = HashMap::new();
diff --git a/src/file/format/toml.rs b/src/file/format/toml.rs
index 6c835a4..cecad75 100644
--- a/src/file/format/toml.rs
+++ b/src/file/format/toml.rs
@@ -42,9 +42,6 @@ fn from_toml_value(uri: Option<&String>, value: &toml::Value) -> Value {
Value::new(uri, l)
}
- _ => {
- // TODO: DateTime
- unimplemented!();
- }
+ toml::Value::Datetime(ref datetime) => Value::new(uri, datetime.to_string()),
}
}
diff --git a/tests/datetime.rs b/tests/datetime.rs
new file mode 100644
index 0000000..f2dc9fe
--- /dev/null
+++ b/tests/datetime.rs
@@ -0,0 +1,70 @@
+extern crate config;
+extern crate chrono;
+
+use config::*;
+use chrono::{DateTime, Utc, TimeZone};
+
+fn make() -> Config {
+ Config::default()
+ .merge(File::from_str(
+ r#"
+ {
+ "json_datetime": "2017-05-10T02:14:53Z"
+ }
+ "#,
+ FileFormat::Json,
+ ))
+ .merge(File::from_str(
+ r#"
+ yaml_datetime: 2017-06-12T10:58:30Z
+ "#,
+ FileFormat::Yaml,
+ ))
+ .merge(File::from_str(
+ r#"
+ toml_datetime = 2017-05-11T14:55:15Z
+ "#,
+ FileFormat::Toml,
+ ))
+ .unwrap()
+}
+
+#[test]
+fn test_datetime_string() {
+ let s = make();
+
+ // JSON
+ let date: String = s.get("json_datetime").unwrap();
+
+ assert_eq!(&date, "2017-05-10T02:14:53Z");
+
+ // TOML
+ let date: String = s.get("toml_datetime").unwrap();
+
+ assert_eq!(&date, "2017-05-11T14:55:15Z");
+
+ // YAML
+ let date: String = s.get("yaml_datetime").unwrap();
+
+ assert_eq!(&date, "2017-06-12T10:58:30Z");
+}
+
+#[test]
+fn test_datetime() {
+ let s = make();
+
+ // JSON
+ let date: DateTime<Utc> = s.get("json_datetime").unwrap();
+
+ assert_eq!(date, Utc.ymd(2017, 5, 10).and_hms(2, 14, 53));
+
+ // TOML
+ let date: DateTime<Utc> = s.get("toml_datetime").unwrap();
+
+ assert_eq!(date, Utc.ymd(2017, 5, 11).and_hms(14, 55, 15));
+
+ // YAML
+ let date: DateTime<Utc> = s.get("yaml_datetime").unwrap();
+
+ assert_eq!(date, Utc.ymd(2017, 6, 12).and_hms(10, 58, 30));
+}