@@ -18,17 +18,15 @@ impl Merger {
1818
1919 /// Merge a single timeline
2020 pub fn merge_timeline ( & mut self , timeline : & Timeline ) -> Result < ( ) > {
21+ ///TODO: make sure that all days are filled with 0 between start and end
2122 for contribution in & timeline. contributions {
2223 let date = contribution. date . clone ( ) ;
2324 let date = git:: parse_date ( & date) ?;
2425 let count = contribution. count ;
2526
2627 if let Some ( date) = date {
27- // Kinda ineffective to call these update functions in a loop
28- for _ in 0 ..count {
29- self . state . update_years ( date) ;
30- self . state . update_days ( date) ;
31- }
28+ self . state . update_years ( date, count) ;
29+ self . state . update_days ( date, count) ;
3230 }
3331 }
3432 Ok ( ( ) )
@@ -103,4 +101,51 @@ mod test {
103101 assert_eq ! ( year. range. start, "2020-01-01" ) ;
104102 assert_eq ! ( year. range. end, "2020-01-02" ) ;
105103 }
104+
105+ #[ test]
106+ fn test_merge_multiple ( ) {
107+ let mut timeline1 = Timeline :: default ( ) ;
108+ let mut timeline2 = Timeline :: default ( ) ;
109+
110+ let range1 = Range {
111+ start : "2020-01-01" . into ( ) ,
112+ end : "2020-01-02" . into ( ) ,
113+ } ;
114+ let range2 = Range {
115+ start : "2020-01-01" . into ( ) ,
116+ end : "2020-01-03" . into ( ) ,
117+ } ;
118+
119+ let year2 = Year { year : "2020" . into ( ) , total : 0 , range : range2 } ;
120+ let year1 = Year { year : "2020" . into ( ) , total : 123 , range : range1 } ;
121+ timeline1. years = vec ! [ year1] ;
122+ timeline2. years = vec ! [ year2] ;
123+
124+ let contributions = vec ! [
125+ Contribution {
126+ date: "2020-01-01" . into( ) ,
127+ count: 1000 ,
128+ color: "" . into( ) ,
129+ intensity: 4 ,
130+ } ,
131+ Contribution {
132+ date: "2020-01-02" . into( ) ,
133+ count: 234 ,
134+ color: "" . into( ) ,
135+ intensity: 4 ,
136+ } ,
137+ ] ;
138+
139+ timeline1. contributions = contributions. clone ( ) ;
140+ timeline2. contributions = contributions. clone ( ) ;
141+
142+ let mut merger = Merger :: new ( ) ;
143+ let merged = merger. merge ( & [ timeline1. clone ( ) , timeline2. clone ( ) ] ) . unwrap ( ) ;
144+ assert_eq ! ( merged. years. len( ) , 1 ) ;
145+ let year = & merged. years [ 0 ] ;
146+ assert_eq ! ( year. year, "2020" ) ;
147+ assert_eq ! ( year. total, 2468 ) ;
148+ assert_eq ! ( year. range. start, "2020-01-01" ) ;
149+ assert_eq ! ( year. range. end, "2020-01-03" ) ;
150+ }
106151}
0 commit comments