summaryrefslogtreecommitdiffstats
path: root/tests/datetime.rs
blob: 9c182643402d5ebcd71fbe6d258a780d07dcc97c (plain)
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
#![cfg(all(
    feature = "toml",
    feature = "json",
    feature = "hjson",
    feature = "yaml",
    feature = "ini",
))]

extern crate chrono;
extern crate config_maint;

use chrono::{DateTime, TimeZone, Utc};
use config_maint::*;

fn make() -> Config {
    Config::default()
        .merge(File::from_str(
            r#"
            {
                "json_datetime": "2017-05-10T02:14:53Z"
            }
            "#,
            FileFormat::Json,
        ))
        .unwrap()
        .merge(File::from_str(
            r#"
            yaml_datetime: 2017-06-12T10:58:30Z
            "#,
            FileFormat::Yaml,
        ))
        .unwrap()
        .merge(File::from_str(
            r#"
            toml_datetime = 2017-05-11T14:55:15Z
            "#,
            FileFormat::Toml,
        ))
        .unwrap()
        .merge(File::from_str(
            r#"
            {
                "hjson_datetime": "2017-05-10T02:14:53Z"
            }
            "#,
            FileFormat::Hjson,
        ))
        .unwrap()
        .merge(File::from_str(
            r#"
                ini_datetime = 2017-05-10T02:14:53Z
            "#,
            FileFormat::Ini,
        ))
        .unwrap()
        .clone()
}

#[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");

    // HJSON
    let date: String = s.get("hjson_datetime").unwrap();

    assert_eq!(&date, "2017-05-10T02:14:53Z");

    // INI
    let date: String = s.get("ini_datetime").unwrap();

    assert_eq!(&date, "2017-05-10T02:14:53Z");
}

#[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));

    // HJSON
    let date: DateTime<Utc> = s.get("hjson_datetime").unwrap();

    assert_eq!(date, Utc.ymd(2017, 5, 10).and_hms(2, 14, 53));

    // INI
    let date: DateTime<Utc> = s.get("ini_datetime").unwrap();

    assert_eq!(date, Utc.ymd(2017, 5, 10).and_hms(2, 14, 53));
}