lcov_parser/report/
mod.rs

1// Copyright (c) 2015-2016 lcov-parser developers
2//
3// Licensed under the Apache License, Version 2.0
4// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
5// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. All files in the project carrying such notice may not be copied,
7// modified, or distributed except according to those terms.
8
9use std::fmt;
10use std::fs:: { OpenOptions, File as OutputFile };
11use std::convert::{ AsRef };
12use std::io:: { Result as IOResult };
13use std::io::prelude::*;
14use std::path::Path;
15use report::summary:: { Summary };
16use report::file:: { File, Files };
17use record:: { RecordWrite };
18
19pub mod attribute;
20pub mod summary;
21pub mod file;
22pub mod branch;
23pub mod line;
24pub mod function;
25pub mod test;
26pub mod counter;
27
28#[derive(Debug)]
29pub struct Report {
30    files: Files
31}
32
33impl Report {
34    pub fn new(files: Files) -> Self {
35        Report {
36            files: files
37        }
38    }
39    pub fn get(&self, key: &str) -> Option<&File> {
40        self.files.get(&key.to_string())
41    }
42    pub fn files(&self) -> &Files {
43        &self.files
44    }
45    pub fn len(&self) -> usize {
46        self.files.len()
47    }
48    pub fn save_as<T: AsRef<Path>>(&self, path: T) -> IOResult<()> {
49        let mut output = OpenOptions::new().create(true).write(true).open(path)?;
50        self.write_records::<OutputFile>(&mut output)
51    }
52}
53
54impl RecordWrite for Report {
55    fn write_records<T: Write>(&self, output: &mut T) -> IOResult<()> {
56        writeln!(output, "{}", self)
57    }
58}
59
60impl fmt::Display for Report {
61    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62        for (source_name, file) in self.files.iter() {
63            for (test_name, test) in file.tests().iter() {
64                writeln!(f, "TN:{}", test_name)?;
65                writeln!(f, "SF:{}", source_name)?;
66                write!(f, "{}", test.functions())?;
67                write!(f, "{}", test.branches())?;
68                write!(f, "{}", test.lines())?;
69                writeln!(f, "end_of_record")?;
70            }
71        }
72        Ok(())
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    extern crate tempdir;
79
80    use self::tempdir::TempDir;
81    use record::{ LineData, FunctionData, BranchData };
82    use report::test::{ Tests };
83    use report::file;
84    use report::{ Report };
85    use merger::ops:: { TryMerge };
86    use std::fs::File;
87    use std::io::*;
88
89    fn build_report() -> Report {
90        let mut tests = Tests::new();
91        let line_data = &LineData { line: 1, count: 1, checksum: None };
92        let function_data = &FunctionData { name: "main".to_string(), count: 1 };
93        let branch_data = &BranchData { line: 1, block: 1, branch: 1, taken: 1 };
94        let test_name = "test1".to_string();
95
96        tests.try_merge((&test_name, line_data)).unwrap();
97        tests.try_merge((&test_name, function_data)).unwrap();
98        tests.try_merge((&test_name, branch_data)).unwrap();
99
100        let file = file::File::new(tests);
101        let mut files = file::Files::new();
102        files.try_merge((&"a.c".to_string(), &file)).unwrap();
103
104        Report::new(files)
105    }
106
107    #[test]
108    fn save_as() {
109        let report = build_report();
110        let tmp_dir = TempDir::new("report").expect("create temp dir");
111        let file_path = tmp_dir.path().join("report.lcov");
112        let _ = report.save_as(file_path.clone()).unwrap();
113
114        assert_eq!(file_path.as_path().exists(), true);
115    }
116
117    #[test]
118    fn display() {
119        let report = build_report();
120        let report_path = "tests/fixtures/report/report.info";
121        let readed_file_content = {
122            let mut output = String::new();
123            let mut f = File::open(report_path).unwrap();
124            let _ = f.read_to_string(&mut output);
125            output
126        };
127        assert_eq!(report.to_string(), readed_file_content);
128    }
129}