@@ -5,7 +5,6 @@ use std::collections::HashMap;
5
5
use std:: collections:: { btree_map, BTreeMap } ;
6
6
use std:: fs:: { self , File } ;
7
7
use std:: io:: { BufRead , BufReader , Write } ;
8
- use std:: ops:: { Div , Mul } ;
9
8
use std:: path:: { Path , PathBuf } ;
10
9
use std:: sync:: { Arc , Mutex } ;
11
10
use tera:: try_get_value;
@@ -153,18 +152,13 @@ fn get_stats(result: &CovResult) -> HtmlStats {
153
152
}
154
153
155
154
#[ inline( always) ]
156
- fn get_percentage <
157
- T : Div + Mul + PartialOrd + std:: ops:: Mul < Output = T > + std:: ops:: Div < Output = T > + From < u8 > ,
158
- > (
159
- x : T ,
160
- y : T ,
161
- ) -> T {
162
- if y != T :: from ( 0 ) {
163
- x / y * T :: from ( 100 )
155
+ fn get_percentage_of_covered_lines ( covered_lines : usize , total_lines : usize ) -> f64 {
156
+ if total_lines != 0 {
157
+ covered_lines as f64 / total_lines as f64 * 100.0
164
158
} else {
165
159
// If the file is empty (no lines) then the coverage
166
160
// must be 100% (0% means "bad" which is not the case).
167
- T :: from ( 100 )
161
+ 100.0
168
162
}
169
163
}
170
164
@@ -174,7 +168,7 @@ fn percent(args: &HashMap<String, Value>) -> tera::Result<Value> {
174
168
from_value :: < usize > ( n. clone ( ) ) ,
175
169
from_value :: < usize > ( d. clone ( ) ) ,
176
170
) {
177
- Ok ( to_value ( get_percentage ( num as f64 , den as f64 ) ) . unwrap ( ) )
171
+ Ok ( to_value ( get_percentage_of_covered_lines ( num, den) ) . unwrap ( ) )
178
172
} else {
179
173
Err ( tera:: Error :: msg ( "Invalid arguments" ) )
180
174
}
@@ -466,7 +460,7 @@ pub fn gen_badge(tera: &Tera, stats: &HtmlStats, conf: &Config, output: &Path, s
466
460
let mut ctx = make_context ( ) ;
467
461
ctx. insert (
468
462
"current" ,
469
- & get_percentage ( stats. covered_lines , stats. total_lines ) ,
463
+ & ( get_percentage_of_covered_lines ( stats. covered_lines , stats. total_lines ) as usize ) ,
470
464
) ;
471
465
ctx. insert ( "hi_limit" , & conf. hi_limit ) ;
472
466
ctx. insert ( "med_limit" , & conf. med_limit ) ;
@@ -511,7 +505,7 @@ pub fn gen_coverage_json(stats: &HtmlStats, conf: &Config, output: &Path) {
511
505
Ok ( f) => f,
512
506
} ;
513
507
514
- let coverage = get_percentage ( stats. covered_lines , stats. total_lines ) ;
508
+ let coverage = get_percentage_of_covered_lines ( stats. covered_lines , stats. total_lines ) as usize ;
515
509
516
510
let res = serde_json:: to_writer (
517
511
& mut output_stream,
@@ -533,3 +527,17 @@ pub fn gen_coverage_json(stats: &HtmlStats, conf: &Config, output: &Path) {
533
527
eprintln ! ( "cannot write the file {:?}" , output_file) ;
534
528
}
535
529
}
530
+
531
+ #[ cfg( test) ]
532
+ mod tests {
533
+ use super :: get_percentage_of_covered_lines;
534
+
535
+ #[ test]
536
+ fn test_get_percentage_of_covered_lines ( ) {
537
+ assert_eq ! ( get_percentage_of_covered_lines( 5 , 5 ) , 100.0 ) ;
538
+ assert_eq ! ( get_percentage_of_covered_lines( 1 , 2 ) , 50.0 ) ;
539
+ assert_eq ! ( get_percentage_of_covered_lines( 200 , 500 ) , 40.0 ) ;
540
+ assert_eq ! ( get_percentage_of_covered_lines( 0 , 0 ) , 100.0 ) ;
541
+ assert_eq ! ( get_percentage_of_covered_lines( 5 , 0 ) , 100.0 ) ;
542
+ }
543
+ }
0 commit comments