Project

General

Profile

C++TESK Cookbook » History » Version 5

Alexander Kamkin, 05/22/2014 10:49 AM

1 1 Alexander Kamkin
h1. C++TESK Cookbook
2
3 5 Alexander Kamkin
{{toc}}
4
5 1 Alexander Kamkin
h2. Developing a Report Printer
6
7
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.
8
9
h4. Step 1. Printer Definition
10
11
<pre><code class="cpp">
12
#include <hw/report/printer.hpp>
13
14
using namespace cpptesk::hw::report;
15
16
// You need to inherit from the base printer
17
structure MyPrinter: public ReportPrinter
18
{
19
    // ... and to define (override) the print method.
20
    virtual void print(std::ostream &out, const Report &report) { ... }
21
};
22
</code></pre>
23
24
h4. Step 2. Printer Registration
25
26
<pre><code class="cpp">
27
// If the printer is ready, it may be registered.
28
CPPTESK_MODEL(MyModel)
29
{
30
    MyModel()
31
    {
32
        // Printers are registered in a model.
33
        registerReportPrinter(new MyPrinter());
34
        ...
35
    }
36
};
37
</code></pre>
38
39 2 Alexander Kamkin
h4. Example. Simple Console Printer
40
41
<pre><code class="cpp">
42
void MyPrinter::print(std::ostream &out, const Report &report)
43
{
44
    const Failures &failures = report.failures;
45
46
    out << "================================================================================" << std::endl;
47
    out << failures.name << std::endl;
48
    out << "================================================================================" << std::endl;
49
50 4 Alexander Protsenko
    for(cpptesk::hw::report::Failures::const_iterator i = failures.begin(); i != failures.end(); i++)
51 1 Alexander Kamkin
    {
52 4 Alexander Protsenko
        const cpptesk::hw::report::FailureGroup &failure_group = *i;
53
    
54
        const cpptesk::hw::report::Failure &failure = *failure_group.begin();
55 2 Alexander Kamkin
56
        out << "--------------------------------------------------------------------------------" << std::endl;
57 4 Alexander Protsenko
        out << failure.name        << std::endl;
58 2 Alexander Kamkin
        out << "--------------------------------------------------------------------------------" << std::endl;
59
60
        out << failure.type    << std::endl;
61 1 Alexander Kamkin
        out << failure.value   << std::endl;
62
        out << failure.unit    << std::endl;
63
        out << failure.info    << std::endl;
64
        out << failure.failure << std::endl;
65
66 4 Alexander Protsenko
        for(cpptesk::hw::report::DataFailure::const_iterator j = failure.data.begin(); j != failure.data.end(); j++)
67 1 Alexander Kamkin
        {
68 4 Alexander Protsenko
            const cpptesk::hw::report::FieldFailure &field = *j;
69 1 Alexander Kamkin
70 4 Alexander Protsenko
            FieldList field_impl = field.impl;
71
            out << "Impl = ";
72
            for(cpptesk::hw::report::FieldList::const_iterator i = field_impl.begin(); i != field_impl.end(); i++)
73
            {
74
                const cpptesk::hw::report::ItemField &item = *i;
75
                
76
                out << item.value;
77
            }   
78
            out << std::endl;
79 2 Alexander Kamkin
80 4 Alexander Protsenko
            FieldList field_spec = field.impl;
81
            out << "Spec = ";
82
            for(cpptesk::hw::report::FieldList::const_iterator i = field_spec.begin(); i != field_spec.end(); i++)
83 2 Alexander Kamkin
            {
84 4 Alexander Protsenko
                const cpptesk::hw::report::ItemField &item = *i;
85
                
86
                out << item.value;
87
            }   
88
            out << std::endl;
89
        }  
90 2 Alexander Kamkin
91
        const TimeFailure &time = failure.time;
92
93
        out << "--------------------------------------------------------------------------------" << std::endl;
94 1 Alexander Kamkin
        out << time.name    << std::endl;
95
        out << "--------------------------------------------------------------------------------" << std::endl;
96
97 2 Alexander Kamkin
        out << time.spec    << std::endl;
98
        out << time.impl    << std::endl;
99
        out << time.failure << std::endl;
100
101
        const IfaceFailure &iface = failure.iface;
102
103
        out << "--------------------------------------------------------------------------------" << std::endl;
104
        out << iface.name    << std::endl;
105
        out << "--------------------------------------------------------------------------------" << std::endl;
106
107
        out << iface.spec    << std::endl;
108
        out << iface.impl    << std::endl;
109
        out << iface.failure << std::endl;
110
    }
111
112
    const Simulation &simulation = report.simulation;
113
114
    out << "================================================================================" << std::endl;
115
    out << simulation.name << std::endl;
116
    out << "================================================================================" << std::endl;
117
118 4 Alexander Protsenko
    out << simulation.cycle.name << " = " << simulation.cycle.value << " " << simulation.cycle.unit << std::endl;
119
    out << simulation.time.name << " = " << simulation.time.value << " " << simulation.time.unit << std::endl;
120
    out << simulation.frequency.name << " = " << simulation.frequency.value << " " << simulation.frequency.unit << std::endl;
121 2 Alexander Kamkin
122
    const Statistics &statistics = report.statistics;
123
124
    out << "================================================================================" << std::endl;
125 4 Alexander Protsenko
    out << statistics.name << " = " << statistics.value << " " << statistics.unit << std::endl;
126 2 Alexander Kamkin
    out << "================================================================================" << std::endl;
127
128 4 Alexander Protsenko
    out << statistics.stimuli.name << " = " << statistics.stimuli.value << std::endl;
129
    out << statistics.reactions.name << " = " << statistics.reactions.value << std::endl;
130
    out << statistics.normal.name << " = " << statistics.normal.value << std::endl;
131
    out << statistics.incorrect.name << " = " << statistics.incorrect.value << std::endl;
132
    out << statistics.missing.name << " = " << statistics.missing.value << std::endl;
133
    out << statistics.unexpected.name << " = " << statistics.unexpected.value << std::endl;
134 2 Alexander Kamkin
}
135
</code></pre>
136
137 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.