Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
UnitTestServiceAdapter.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2022 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* UnitTestServiceAdapter.h (C) 2000-2020 */
9/* */
10/* Adapte un service qui déclare des tests a l'interface IUnitTest. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13#ifndef ARCANE_UNITTESTADAPTER_H
14#define ARCANE_UNITTESTADAPTER_H
15/*---------------------------------------------------------------------------*/
16/*---------------------------------------------------------------------------*/
17
18#include "arcane/IUnitTest.h"
19#include "arcane/ArcaneException.h"
20#include "arcane/Assertion.h"
21#include "arcane/XmlNode.h"
22
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
26namespace Arcane
27{
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
35template<typename T>
37: public IXmlUnitTest
38, public Assertion
39{
40 public:
41
42 typedef void (T::*FuncPtr)();
43
44 public:
45
46 explicit UnitTestServiceAdapter(T* service)
47 : m_service(service) {}
48
49 public:
50
51 void setClassSetUpFunction(FuncPtr f) { m_class_set_up_function = f; }
52 void setTestSetUpFunction(FuncPtr f) { m_set_up_function = f; }
53 void setClassTearDownFunction(FuncPtr f) { m_class_tear_down_function = f; }
54 void setTestTearDownFunction(FuncPtr f) { m_tear_down_function = f; }
55 void addTestFunction(FuncPtr f, String name, String method_name)
56 {
57 TestFuncInfo info(f, name, method_name);
58 m_test_functions.add(info);
59 }
60
61 public:
62
64 void initializeTest() override
65 {
67 (m_service->*m_class_set_up_function)();
68 }
69
71 bool executeTest(XmlNode& report) override
72 {
73 bool success = true;
74 report.setAttrValue("name", m_service->serviceInfo()->localName());
76 XmlNode xunittest = report.createAndAppendElement("unit-test");
77 try {
78 xunittest.setAttrValue("name", func_info.m_name);
79 xunittest.setAttrValue("method-name", func_info.m_method_name);
81 (m_service->*m_set_up_function)();
82 (m_service->*func_info.m_test_func)();
84 (m_service->*m_tear_down_function)();
85 xunittest.setAttrValue("result", "success");
86 m_service->info() << "[OK ] " << func_info.m_name;
87 }
88 catch (const AssertionException& e) {
89 xunittest.setAttrValue("result", "failure");
90 XmlNode xexception = xunittest.createAndAppendElement("exception");
91 xexception.setAttrValue("where", e.where());
92 xexception.setAttrValue("file", e.file());
93 xexception.setAttrValue("line", Arcane::String::fromNumber(e.line()));
94 xexception.setAttrValue("message", e.message());
95 m_service->info() << "[ECHEC] " << func_info.m_name << " (line " << e.line() << " in " << e.where() << ")";
96 m_service->info() << " " << e.message();
97 success = false;
98 }
99 }
100 return success;
101 }
102
104 void finalizeTest() override
105 {
107 (m_service->*m_class_tear_down_function)();
108 }
109
110 private:
111
113 {
115 : m_test_func(test_func), m_name(name), m_method_name(method_name) {}
116
117 FuncPtr m_test_func;
118 String m_name;
119 String m_method_name;
120 };
121
122 private:
123
135 T* m_service;
136};
137
138/*---------------------------------------------------------------------------*/
139/*---------------------------------------------------------------------------*/
140
141} // End namespace Arcane
142
143/*---------------------------------------------------------------------------*/
144/*---------------------------------------------------------------------------*/
145
146#endif
Exception dans une assertion.
const String & where() const
Localisation de l'exception.
const char * file() const
Fichier de l'exception.
const String & message() const
Message de l'exception.
int line() const
Ligne de l'exception.
Classe de base pour assertions dans les tests unitaires.
Definition Assertion.h:43
Interface d'un service de test unitaire fournissant un rapport de test sous forme d'un noeud XML.
Definition IUnitTest.h:62
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120
Adapte un service qui déclare des tests a l'interface IUnitTest.
void initializeTest() override
Implémentation de l'interface IUnitTest.
bool executeTest(XmlNode &report) override
Implémentation de l'interface IUnitTest.
void finalizeTest() override
Implémentation de l'interface IUnitTest.
UniqueArray< TestFuncInfo > m_test_functions
Service associé.
FuncPtr m_set_up_function
Pointeur vers la méthode d'initialisation de chaque test.
FuncPtr m_tear_down_function
Pointeur vers la méthode de fin de chaque test.
FuncPtr m_class_tear_down_function
Pointeur vers la méthode de fin des tests de la classe.
FuncPtr m_class_set_up_function
Pointeur vers la méthode d'initialisation de la classe.
void(T::* FuncPtr)()
Type du pointeur sur les méthodes de test.
Noeud d'un arbre DOM.
Definition XmlNode.h:51
Chaîne de caractères unicode.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-