Project

General

Profile

C++TESK Cookbook » History » Version 2

Alexander Kamkin, 08/01/2013 11:06 AM

1 1 Alexander Kamkin
h1. C++TESK Cookbook
2
3
h2. Developing a Report Printer
4
5
If you want to change the way the tool describes failures (you don’t like the printing style or you get used to work with a different format or you need to integrate your test system with a bug tracker or whatever), the best solution is probably to write your own report printer. It can be easily done by defining one method in a printer class and registering the printer in C++TESK Report Subsystem. The steps are as follows.
6
7
h4. Step 1. Printer Definition
8
9
<pre><code class="cpp">
10
#include <hw/report/printer.hpp>
11
12
using namespace cpptesk::hw::report;
13
14
// You need to inherit from the base printer
15
structure MyPrinter: public ReportPrinter
16
{
17
    // ... and to define (override) the print method.
18
    virtual void print(std::ostream &out, const Report &report) { ... }
19
};
20
</code></pre>
21
22
h4. Step 2. Printer Registration
23
24
<pre><code class="cpp">
25
// If the printer is ready, it may be registered.
26
CPPTESK_MODEL(MyModel)
27
{
28
    MyModel()
29
    {
30
        // Printers are registered in a model.
31
        registerReportPrinter(new MyPrinter());
32
        ...
33
    }
34
};
35
</code></pre>
36
37 2 Alexander Kamkin
h4. Example. Simple Console Printer
38
39
<pre><code class="cpp">
40
void MyPrinter::print(std::ostream &out, const Report &report)
41
{
42
    const Failures &failures = report.failures;
43
44
    out << "================================================================================" << std::endl;
45
    out << failures.name << std::endl;
46
    out << "================================================================================" << std::endl;
47
48
    for(Failures::const_iterator i = failures.begin(); i != failures.end(); i++)
49
    {
50
        const Failure &failure = *i;
51
52
        out << "--------------------------------------------------------------------------------" << std::endl;
53
        out << failure.name        << std::endl;
54
        out << "--------------------------------------------------------------------------------" << std::endl;
55
56
        out << failure.type    << std::endl;
57
        out << failure.value   << std::endl;
58
        out << failure.unit    << std::endl;
59
        out << failure.info    << std::endl;
60
        out << failure.failure << std::endl;
61
62
        const DataFailure &data = failure.data;
63
64
        out << data.name    << std::endl;
65
        out << data.failure << std::endl;
66
67
        for(DataFailure::const_iterator j = data.begin(); j != data.end(); j++)
68
        {
69
            const FieldFailure &field = *j;
70
71
            out << field.name    << std::endl;
72
            out << field.failure << std::endl;
73
74
            for(FieldFailure::const_iterator k = field.begin(); k != field.end(); k++)
75
            {
76
                const ItemFailure &item = *k;
77
78
                out << item.name    << std::endl;
79
                out << item.spec    << std::endl;
80
                out << item.impl    << std::endl;
81
                out << item.failure << std::endl;
82
            }
83
        }
84
85
        const TimeFailure &time = failure.time;
86
87
        out << "--------------------------------------------------------------------------------" << std::endl;
88
        out << time.name    << std::endl;
89
        out << "--------------------------------------------------------------------------------" << std::endl;
90
91
        out << time.spec    << std::endl;
92
        out << time.impl    << std::endl;
93
        out << time.failure << std::endl;
94
95
        const IfaceFailure &iface = failure.iface;
96
97
        out << "--------------------------------------------------------------------------------" << std::endl;
98
        out << iface.name    << std::endl;
99
        out << "--------------------------------------------------------------------------------" << std::endl;
100
101
        out << iface.spec    << std::endl;
102
        out << iface.impl    << std::endl;
103
        out << iface.failure << std::endl;
104
    }
105
106
    const Simulation &simulation = report.simulation;
107
108
    out << "================================================================================" << std::endl;
109
    out << simulation.name << std::endl;
110
    out << "================================================================================" << std::endl;
111
112
    for(Simulation::const_iterator i = simulation.begin(); i != simulation.end(); i++)
113
    {
114
        const Cycle &cycle = *i;
115
116
        out << cycle.name  << std::endl;
117
        out << cycle.value << std::endl;
118
    }
119
120
    const Statistics &statistics = report.statistics;
121
122
    out << "================================================================================" << std::endl;
123
    out << statistics.name << std::endl;
124
    out << "================================================================================" << std::endl;
125
126
    for(Statistics::const_iterator i = statistics.begin(); i != statistics.end(); i++)
127
    {
128
        const Count &count = *i;
129
130
        out << count.name  << std::endl;
131
        out << count.value << std::endl;
132
    }
133
}
134
</code></pre>
135
136 1 Alexander Kamkin
Before developing your report printer make sure that printer you need to develop (or a similar one) doesn’t exist in the library.