File: cal_iter.rs

package info (click to toggle)
rust-datetime 0.5.2-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 728 kB
  • sloc: ruby: 18; makefile: 6
file content (134 lines) | stat: -rw-r--r-- 3,482 bytes parent folder | download | duplicates (3)
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
extern crate datetime;
pub use datetime::{YearMonth, Year};

mod months {
    use super::*;
    use datetime::Month::*;

    #[test]
    fn range_full() {
        let year = Year(2013);
        let months: Vec<_> = year.months(..).collect();
        assert_eq!(months, vec![
            year.month(January),
            year.month(February),
            year.month(March),
            year.month(April),
            year.month(May),
            year.month(June),
            year.month(July),
            year.month(August),
            year.month(September),
            year.month(October),
            year.month(November),
            year.month(December),
        ]);
    }

    #[test]
    fn range_from() {
        let year = Year(2013);
        let months: Vec<_> = year.months(July..).collect();
        assert_eq!(months, vec![
            year.month(July),
            year.month(August),
            year.month(September),
            year.month(October),
            year.month(November),
            year.month(December),
        ]);
    }

    #[test]
    fn range_to() {
        let year = Year(2013);
        let months: Vec<_> = year.months(..July).collect();
        assert_eq!(months, vec![
            year.month(January),
            year.month(February),
            year.month(March),
            year.month(April),
            year.month(May),
            year.month(June),
        ]);
    }

    #[test]
    fn range() {
        let year = Year(2013);
        let months: Vec<_> = year.months(April..July).collect();
        assert_eq!(months, vec![
            year.month(April),
            year.month(May),
            year.month(June),
        ]);
    }

    #[test]
    fn range_empty() {
        let year = Year(2013);
        let months: Vec<_> = year.months(August..August).collect();
        assert!(months.is_empty());
    }

    #[test]
    fn range_singular() {
        let year = Year(2013);
        let months = year.month(April);
        assert_eq!(months, year.month(April));
    }
}

mod days {
    use super::*;
    use datetime::LocalDate;
    use datetime::Month::*;

    #[test]
    fn range_full() {
        let year = Year(2013).month(February);
        let days: Vec<_> = year.days(..).collect();
        let results: Vec<_> = (1..29).map(|d| LocalDate::ymd(2013, February, d).unwrap()).collect();
        assert_eq!(days, results);
    }

    #[test]
    fn range_full_leap_year() {
        let year = Year(2000).month(February);
        let days: Vec<_> = year.days(..).collect();
        let results: Vec<_> = (1..30).map(|d| LocalDate::ymd(2000, February, d).unwrap()).collect();
        assert_eq!(days, results);
    }

    #[test]
    fn range() {
        let year = Year(2008).month(March);
        let days: Vec<_> = year.days(10..20).collect();
        let results: Vec<_> = (10..20).map(|d| LocalDate::ymd(2008, March, d).unwrap()).collect();
        assert_eq!(days, results);
    }

    #[test]
    fn just_for_one_day() {
        let day = Year(1066).month(October).day(14);
        assert_eq!(day, LocalDate::ymd(1066, October, 14));
    }
}

#[test]
fn entire_year() {
    let count = Year(1999).months(..)
                          .flat_map(|m| m.days(..))
                          .count();

    assert_eq!(count, 365);
}

#[test]
fn entire_leap_year() {
    let count = Year(2000).months(..)
                          .flat_map(|m| m.days(..))
                          .count();

    assert_eq!(count, 366);
}