xaizek / uncov (License: AGPLv3+) (since 2018-12-07)
Uncov(er) is a tool that collects and processes code coverage reports.
<root> / src / coverage.hpp (3ce8529ff869846274e31e32d4e7ea374b219b8e) (4,001B) (mode 100644) [raw]
// Copyright (C) 2016 xaizek <xaizek@posteo.net>
//
// This file is part of uncov.
//
// uncov is free software: you can redistribute it and/or modify
// it under the terms of version 3 of the GNU Affero General Public License as
// published by the Free Software Foundation.
//
// uncov is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with uncov.  If not, see <http://www.gnu.org/licenses/>.

#ifndef UNCOV_COVERAGE_HPP_
#define UNCOV_COVERAGE_HPP_

#include <string>

/**
 * @file coverage.hpp
 *
 * @brief Coverage computation and formatting.
 */

/**
 * @brief Computes and formats coverage conformation.
 */
class CovInfo
{
    //! To give access to @c coveredCount and @c missedCount.
    friend class CovChange;

public:
    /**
     * @brief Constructs empty coverage information.
     */
    CovInfo() = default;
    /**
     * @brief Constructs coverage information from a Coverable.
     *
     * Coverable is any class that implements @c getCoveredCount() and
     * @c getMissedCount() functions that return @c int.
     *
     * @tparam T Type of the Coverable.
     *
     * @param coverable Source of the coverage information.
     */
    template <typename T>
    explicit CovInfo(const T &coverable)
        : coveredCount(coverable.getCoveredCount()),
          missedCount(coverable.getMissedCount())
    {
    }

public:
    /**
     * @brief Adds coverage information.
     *
     * @param other Another coverage information.
     */
    void add(const CovInfo &other);
    /**
     * @brief Formats coverage rate in the new state as a string.
     *
     * @returns Formatted string.
     */
    std::string formatCoverageRate() const;
    /**
     * @brief Formats coverage statistics in lines.
     *
     * Format is covered/missed/relevant.
     *
     * @param separator Separator of statistics.
     *
     * @returns Formatted string.
     */
    std::string formatLines(const std::string &separator) const;

private:
    /**
     * @brief Retrieves rate of source coverage.
     *
     * @returns The rate.
     */
    float getCoverage() const;
    /**
     * @brief Retrieves number of lines that are relevant for code coverage.
     *
     * @returns The number.
     */
    int getRelevantLines() const;

private:
    int coveredCount = 0; //!< Number of covered lines.
    int missedCount = 0;  //!< Number of missed lines.
};

/**
 * @brief Computes and formats coverage change.
 */
class CovChange
{
public:
    /**
     * @brief Computes coverage change between two states.
     *
     * @param oldCov Old coverage information.
     * @param newCov New coverage information.
     */
    CovChange(const CovInfo &oldCov, const CovInfo &newCov);

public:
    /**
     * @brief Whether new coverage information differs from the old one.
     *
     * @returns @c true if so, @c false otherwise.
     */
    bool isChanged() const;
    /**
     * @brief Formats change of coverage rate in the new state as a string.
     *
     * @returns Formatted string.
     */
    std::string formatCoverageRate() const;
    /**
     * @brief Formats changes coverage statistics in lines.
     *
     * Format is covered/missed/relevant.
     *
     * @param separator Separator of statistics.
     * @param width     Minimal width for first and second statistics.
     *
     * @returns Formatted string.
     */
    std::string formatLines(const std::string &separator, int width = 0) const;

private:
    float coverageChange; //!< Change of covered lines in percents.
    int coveredChange;    //!< Change of covered lines in lines.
    int missedChange;     //!< Change of missed lines in lines.
    int relevantChange;   //!< Change of relevant lines in lines.
};

#endif // UNCOV_COVERAGE_HPP_
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/uncov

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/uncov

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master