test/src/pEpTestOutput.cc
author Krista Bennett <krista@pep-project.org>
Mon, 10 Sep 2018 15:11:12 +0200
changeset 2937 441d5dceee62
parent 2928 6d8e51098e7a
child 3164 5c4b1bd2c638
permissions -rw-r--r--
Fixed test output bug
krista@2928
     1
#include <cpptest.h>
krista@2928
     2
#include <iostream>
krista@2928
     3
#include <iomanip>
krista@2928
     4
#include <vector>
krista@2928
     5
#include <string>
krista@2928
     6
#include <cstring>
krista@2928
     7
krista@2928
     8
#include "pEpTestOutput.h"
krista@2928
     9
using namespace std;
krista@2928
    10
krista@2928
    11
namespace Test {
krista@2928
    12
	pEpTestOutput::pEpTestOutput() : _total_failed(0), _total_tests(0) {}
krista@2928
    13
    
krista@2928
    14
    void pEpTestOutput::outputCorrectPercentage(int num_tests, int failures, int width) {
krista@2928
    15
        cout << setw(width);
krista@2928
    16
        if (num_tests) {
krista@2928
    17
            double percentage = ((num_tests - failures) / (double)num_tests) * 100;
krista@2928
    18
            cout << std::fixed << setprecision(1) << percentage << "\%"; 
krista@2928
    19
        }
krista@2928
    20
        else 
krista@2928
    21
            cout << "N/A";
krista@2928
    22
    }
krista@2928
    23
    	
krista@2928
    24
	void pEpTestOutput::finished(int tests, const Test::Time& time) {
krista@2928
    25
        cout << huge_sepline << endl;
krista@2928
    26
        string header = "FULL TEST RUN RESULTS:";
krista@2928
    27
        cout << alt_sepline << left << setw(header.size()) << header;
krista@2928
    28
        cout << right << setw(56 - header.size()) << "+" << endl;
krista@2928
    29
        cout << right << setw(30) << "Number of tests run: " << setw(7) << tests << setw(19) << "+" << endl;
krista@2928
    30
        cout << right << setw(30) << "Tests failed: " << setw(7) << _total_failed << setw(19) << "+" << endl;
krista@2928
    31
        cout << right << setw(30) << "Pass percentage: ";
krista@2928
    32
        outputCorrectPercentage(_total_tests, _total_failed, 7);
krista@2928
    33
        cout << setw(18) << "+" << endl;
krista@2928
    34
        cout << setw(56) << "+" << endl;
krista@2928
    35
        string finalstr = std::to_string(tests) + " tests run in " + std::to_string(time.seconds()) + "." + std::to_string(time.microseconds()) + " seconds.";
krista@2928
    36
        int remlen = 56 - finalstr.size();
krista@2928
    37
        cout << left << setw(finalstr.size()) << finalstr << right << setw(remlen) << "+" << endl;
krista@2928
    38
        cout << alt_sepline << endl;
krista@2928
    39
	}
krista@2928
    40
	
krista@2928
    41
	void pEpTestOutput::suite_start(int tests, const string& name) {
krista@2928
    42
        _suite_failed = 0;
krista@2928
    43
        _suite_name = name;
krista@2928
    44
        _suite_total = 0;
krista@2928
    45
        if (tests > 0) {
krista@2928
    46
            cout << endl << huge_sepline;
krista@2928
    47
            cout << "BEGIN TEST SUITE: " << name << endl << endl;
krista@2928
    48
        }
krista@2928
    49
	}
krista@2928
    50
	
krista@2928
    51
	void pEpTestOutput::suite_end(int tests, const string& name, const Test::Time& time)
krista@2928
    52
	{
krista@2928
    53
        if (tests > 0) {
krista@2928
    54
            cout << endl << "Suite results:" << endl; 
krista@2928
    55
            cout << right << setw(30) << "Number of tests run: " << setw(7) << tests << endl;
krista@2937
    56
            cout << right << setw(30) << "Tests failed: " << setw(7) << _suite_failed << endl;
krista@2928
    57
            cout << right << setw(30) << "Pass percentage: ";
krista@2928
    58
            outputCorrectPercentage(_suite_total, _suite_failed, 7);
krista@2928
    59
            cout << endl << endl;
krista@2928
    60
            cout << tests << " tests run in " << time << " seconds." << endl;
krista@2928
    61
            cout << endl;
krista@2928
    62
            cout << "END TEST SUITE: " << name << endl;
krista@2928
    63
        }
krista@2928
    64
	}
krista@2928
    65
    
krista@2928
    66
	void pEpTestOutput::test_start(const std::string& name) {
krista@2928
    67
        _test_name = name;
krista@2928
    68
        cout << med_sepline;
krista@2928
    69
        cout << "Begin test " << name << endl;
krista@2928
    70
        cout << lil_sepline;
krista@2928
    71
        _test_errors.clear();
krista@2928
    72
    }
krista@2928
    73
    void pEpTestOutput::test_end(const string& name, bool ok, const Test::Time&) {
krista@2928
    74
	    if (!ok) {
krista@2928
    75
            _suite_failed++;
krista@2928
    76
            _total_failed++;
krista@2928
    77
            cout << endl << endl << alt_sepline;
krista@2928
    78
            cout << "*** Test " << name << " failed!" << endl;
krista@2928
    79
            vector<Source>::iterator it;
krista@2928
    80
            for (it = _test_errors.begin(); it != _test_errors.end(); it++) {
krista@2928
    81
                Source src = *it;
krista@2928
    82
                cout << lil_sepline;
krista@2928
    83
                cout << left << setw(25) << "*** Assert location: " << src.file() << ":" << src.line() << endl;
krista@2928
    84
                cout << left << setw(25) << "*** Message: " << src.message() << endl;                
krista@2928
    85
            }
krista@2928
    86
            cout << alt_sepline << endl;
krista@2928
    87
        }
krista@2928
    88
        _total_tests++;
krista@2928
    89
        _suite_total++;
krista@2928
    90
krista@2928
    91
        cout << "End test " << name << endl;
krista@2928
    92
        cout << med_sepline;        
krista@2928
    93
	}
krista@2928
    94
	
krista@2928
    95
	void pEpTestOutput::assertment(const Source& s) {
krista@2928
    96
		_test_errors.push_back(s);
krista@2928
    97
	}
krista@2928
    98
}