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