Project

General

Profile

C++TESK Cookbook » History » Version 4

Alexander Protsenko, 11/14/2013 11:49 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 4 Alexander Protsenko
    for(cpptesk::hw::report::Failures::const_iterator i = failures.begin(); i != failures.end(); i++)
49 1 Alexander Kamkin
    {
50 4 Alexander Protsenko
        const cpptesk::hw::report::FailureGroup &failure_group = *i;
51
    
52
        const cpptesk::hw::report::Failure &failure = *failure_group.begin();
53 2 Alexander Kamkin
54
        out << "--------------------------------------------------------------------------------" << std::endl;
55 4 Alexander Protsenko
        out << failure.name        << std::endl;
56 2 Alexander Kamkin
        out << "--------------------------------------------------------------------------------" << std::endl;
57
58
        out << failure.type    << std::endl;
59 1 Alexander Kamkin
        out << failure.value   << std::endl;
60
        out << failure.unit    << std::endl;
61
        out << failure.info    << std::endl;
62
        out << failure.failure << std::endl;
63
64 4 Alexander Protsenko
        for(cpptesk::hw::report::DataFailure::const_iterator j = failure.data.begin(); j != failure.data.end(); j++)
65 1 Alexander Kamkin
        {
66 4 Alexander Protsenko
            const cpptesk::hw::report::FieldFailure &field = *j;
67 1 Alexander Kamkin
68 4 Alexander Protsenko
            FieldList field_impl = field.impl;
69
            out << "Impl = ";
70
            for(cpptesk::hw::report::FieldList::const_iterator i = field_impl.begin(); i != field_impl.end(); i++)
71
            {
72
                const cpptesk::hw::report::ItemField &item = *i;
73
                
74
                out << item.value;
75
            }   
76
            out << std::endl;
77 2 Alexander Kamkin
78 4 Alexander Protsenko
            FieldList field_spec = field.impl;
79
            out << "Spec = ";
80
            for(cpptesk::hw::report::FieldList::const_iterator i = field_spec.begin(); i != field_spec.end(); i++)
81 2 Alexander Kamkin
            {
82 4 Alexander Protsenko
                const cpptesk::hw::report::ItemField &item = *i;
83
                
84
                out << item.value;
85
            }   
86
            out << std::endl;
87
        }  
88 2 Alexander Kamkin
89
        const TimeFailure &time = failure.time;
90
91
        out << "--------------------------------------------------------------------------------" << std::endl;
92 1 Alexander Kamkin
        out << time.name    << std::endl;
93
        out << "--------------------------------------------------------------------------------" << std::endl;
94
95 2 Alexander Kamkin
        out << time.spec    << std::endl;
96
        out << time.impl    << std::endl;
97
        out << time.failure << std::endl;
98
99
        const IfaceFailure &iface = failure.iface;
100
101
        out << "--------------------------------------------------------------------------------" << std::endl;
102
        out << iface.name    << std::endl;
103
        out << "--------------------------------------------------------------------------------" << std::endl;
104
105
        out << iface.spec    << std::endl;
106
        out << iface.impl    << std::endl;
107
        out << iface.failure << std::endl;
108
    }
109
110
    const Simulation &simulation = report.simulation;
111
112
    out << "================================================================================" << std::endl;
113
    out << simulation.name << std::endl;
114
    out << "================================================================================" << std::endl;
115
116 4 Alexander Protsenko
    out << simulation.cycle.name << " = " << simulation.cycle.value << " " << simulation.cycle.unit << std::endl;
117
    out << simulation.time.name << " = " << simulation.time.value << " " << simulation.time.unit << std::endl;
118
    out << simulation.frequency.name << " = " << simulation.frequency.value << " " << simulation.frequency.unit << std::endl;
119 2 Alexander Kamkin
120
    const Statistics &statistics = report.statistics;
121
122
    out << "================================================================================" << std::endl;
123 4 Alexander Protsenko
    out << statistics.name << " = " << statistics.value << " " << statistics.unit << std::endl;
124 2 Alexander Kamkin
    out << "================================================================================" << std::endl;
125
126 4 Alexander Protsenko
    out << statistics.stimuli.name << " = " << statistics.stimuli.value << std::endl;
127
    out << statistics.reactions.name << " = " << statistics.reactions.value << std::endl;
128
    out << statistics.normal.name << " = " << statistics.normal.value << std::endl;
129
    out << statistics.incorrect.name << " = " << statistics.incorrect.value << std::endl;
130
    out << statistics.missing.name << " = " << statistics.missing.value << std::endl;
131
    out << statistics.unexpected.name << " = " << statistics.unexpected.value << std::endl;
132 2 Alexander Kamkin
}
133
</code></pre>
134
135 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.