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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use std::fs;
use std::io::{BufRead, BufReader, BufWriter, Write};
use std::path::{Path, PathBuf};

use crate::error::*;

/// Coverage information for a single line
#[derive(Clone, Debug, PartialEq)]
pub struct LineCoverage {
    /// 1-indexed line in the source file
    pub line_number: usize,
    /// execution count of line. `None` means this line is not executable.
    /// `None` value is used when the fixer detects non-executable line.
    pub count: Option<u32>,
}

/// Coverage information for a single branch
#[derive(Clone, Debug, PartialEq)]
pub struct BranchCoverage {
    /// 1-indexed line in the source file
    pub line_number: usize,
    /// block id which contains this branch
    pub block_number: Option<usize>,
    /// whether this branch was executed.
    /// `None` value is used when the fixer detects non-executable branch.
    pub taken: Option<bool>,
}

/// Coverage information for a single file
///
/// `FileCoverage` holds coverage information for lines and branches in the source file.
#[derive(Debug, PartialEq)]
pub struct FileCoverage {
    path: PathBuf,
    #[doc(hidden)]
    pub line_coverages: Vec<LineCoverage>,
    #[doc(hidden)]
    pub branch_coverages: Vec<BranchCoverage>,
}

impl FileCoverage {
    pub fn new<P: Into<PathBuf>>(
        path: P,
        line_coverages: Vec<LineCoverage>,
        branch_coverages: Vec<BranchCoverage>,
    ) -> Self {
        Self {
            path: path.into(),
            line_coverages,
            branch_coverages,
        }
    }

    pub fn path(&self) -> &Path {
        &self.path
    }

    pub fn line_coverages(&self) -> &[LineCoverage] {
        &self.line_coverages
    }

    pub fn branch_coverages(&self) -> &[BranchCoverage] {
        &self.branch_coverages
    }
}

/// Coverage information for package
#[derive(Debug, PartialEq)]
pub struct PackageCoverage {
    name: String,
    #[doc(hidden)]
    pub file_coverages: Vec<FileCoverage>,
}

impl PackageCoverage {
    pub fn new(file_coverages: Vec<FileCoverage>) -> Self {
        Self::with_test_name("", file_coverages)
    }

    pub fn with_test_name<T: Into<String>>(name: T, file_coverages: Vec<FileCoverage>) -> Self {
        Self {
            name: name.into(),
            file_coverages,
        }
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn file_coverages(&self) -> &[FileCoverage] {
        &self.file_coverages
    }
}

#[doc(hidden)]
pub trait TotalCoverage {
    fn line_executed(&self) -> usize;
    fn line_total(&self) -> usize;
    fn branch_executed(&self) -> usize;
    fn branch_total(&self) -> usize;
}

#[doc(hidden)]
impl TotalCoverage for FileCoverage {
    fn line_executed(&self) -> usize {
        self.line_coverages
            .iter()
            .filter(|&v| v.count.map_or(false, |c| c > 0))
            .count()
    }

    fn line_total(&self) -> usize {
        self.line_coverages
            .iter()
            .filter(|&v| v.count.is_some())
            .count()
    }

    fn branch_executed(&self) -> usize {
        self.branch_coverages
            .iter()
            .filter(|&v| v.taken.unwrap_or(false))
            .count()
    }

    fn branch_total(&self) -> usize {
        self.branch_coverages
            .iter()
            .filter(|&v| v.taken.is_some())
            .count()
    }
}

#[doc(hidden)]
impl TotalCoverage for PackageCoverage {
    fn line_executed(&self) -> usize {
        self.file_coverages
            .iter()
            .fold(0, |sum, a| sum + a.line_executed())
    }

    fn line_total(&self) -> usize {
        self.file_coverages
            .iter()
            .fold(0, |sum, a| sum + a.line_total())
    }

    fn branch_executed(&self) -> usize {
        self.file_coverages
            .iter()
            .fold(0, |sum, a| sum + a.branch_executed())
    }

    fn branch_total(&self) -> usize {
        self.file_coverages
            .iter()
            .fold(0, |sum, a| sum + a.branch_total())
    }
}

pub trait CoverageReader {
    /// fetch the coverage information from the reader
    fn read<R: BufRead>(&self, reader: &mut R) -> Result<PackageCoverage, Error>;

    /// fetch the coverage information from file
    fn read_from_file(&self, path: &Path) -> Result<PackageCoverage, Error> {
        let f = fs::File::open(path)
            .chain_err(|| format!("Failed to open coverage file {:?}", path))?;
        let capacity = f.metadata().map(|m| m.len() as usize + 1).unwrap_or(8192);
        let mut reader = BufReader::with_capacity(capacity, f);
        self.read(&mut reader)
    }
}

pub trait CoverageWriter {
    /// save coverage information into the writer
    fn write<W: Write>(&self, data: &PackageCoverage, writer: &mut W) -> Result<(), Error>;

    /// save coverage information into the file
    fn write_to_file(&self, data: &PackageCoverage, path: &Path) -> Result<(), Error> {
        let f = fs::File::create(path).chain_err(|| format!("Failed to open file {:?}", path))?;
        let mut writer = BufWriter::new(f);
        self.write(&data, &mut writer)
    }
}