Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
UnitTestServiceAdapter.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 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-2025 */
9/* */
10/* Adapts a service that declares tests to the IUnitTest interface. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13#ifndef ARCANE_CORE_UNITTESTADAPTER_H
14#define ARCANE_CORE_UNITTESTADAPTER_H
15/*---------------------------------------------------------------------------*/
16/*---------------------------------------------------------------------------*/
17
18#include "arcane/core/IUnitTest.h"
19#include "arcane/core/ArcaneException.h"
21#include "arcane/core/XmlNode.h"
22
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
26namespace Arcane
27{
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
36template <typename T>
37class UnitTestServiceAdapter
38: public IXmlUnitTest
39, public Assertion
40{
41 public:
42
43 typedef void (T::*FuncPtr)();
44
45 public:
46
47 explicit UnitTestServiceAdapter(T* service)
48 : m_service(service)
49 {}
50
51 public:
52
53 void setClassSetUpFunction(FuncPtr f) { m_class_set_up_function = f; }
54 void setTestSetUpFunction(FuncPtr f) { m_set_up_function = f; }
55 void setClassTearDownFunction(FuncPtr f) { m_class_tear_down_function = f; }
56 void setTestTearDownFunction(FuncPtr f) { m_tear_down_function = f; }
57 void addTestFunction(FuncPtr f, String name, String method_name)
58 {
59 TestFuncInfo info(f, name, method_name);
60 m_test_functions.add(info);
61 }
62
63 public:
64
66 void initializeTest() override
67 {
69 (m_service->*m_class_set_up_function)();
70 }
71
73 bool executeTest(XmlNode& report) override
74 {
75 bool success = true;
76 report.setAttrValue("name", m_service->serviceInfo()->localName());
77 for (TestFuncInfo func_info : m_test_functions) {
78 XmlNode xunittest = report.createAndAppendElement("unit-test");
79 try {
80 xunittest.setAttrValue("name", func_info.m_name);
81 xunittest.setAttrValue("method-name", func_info.m_method_name);
83 (m_service->*m_set_up_function)();
84 (m_service->*func_info.m_test_func)();
86 (m_service->*m_tear_down_function)();
87 xunittest.setAttrValue("result", "success");
88 m_service->info() << "[OK ] " << func_info.m_name;
89 }
90 catch (const AssertionException& e) {
91 xunittest.setAttrValue("result", "failure");
92 XmlNode xexception = xunittest.createAndAppendElement("exception");
93 xexception.setAttrValue("where", e.where());
94 xexception.setAttrValue("file", e.file());
95 xexception.setAttrValue("line", Arcane::String::fromNumber(e.line()));
96 xexception.setAttrValue("message", e.message());
97 m_service->info() << "[FAILURE] " << func_info.m_name << " (line " << e.line() << " in " << e.where() << ")";
98 m_service->info() << " " << e.message();
99 success = false;
100 }
101 }
102 return success;
103 }
104
106 void finalizeTest() override
107 {
109 (m_service->*m_class_tear_down_function)();
110 }
111
112 private:
113
114 struct TestFuncInfo
115 {
116 TestFuncInfo(FuncPtr test_func, String name, String method_name)
117 : m_test_func(test_func)
118 , m_name(name)
119 , m_method_name(method_name)
120 {}
121
122 FuncPtr m_test_func;
123 String m_name;
124 String m_method_name;
125 };
126
127 private:
128
140 T* m_service;
141};
142
143/*---------------------------------------------------------------------------*/
144/*---------------------------------------------------------------------------*/
145
146} // End namespace Arcane
147
148/*---------------------------------------------------------------------------*/
149/*---------------------------------------------------------------------------*/
150
151#endif
Exception in an assertion.
const String & where() const
Location of the exception.
const char * file() const
File of the exception.
const String & message() const
Exception message.
int line() const
Line of the exception.
Base class for assertions in unit tests.
Definition Assertion.h:45
Interface of a unit test service providing a test report in the form of an XML node.
Definition IUnitTest.h:64
1D data vector with value semantics (STL style).
void initializeTest() override
Implementation of the IUnitTest interface.
bool executeTest(XmlNode &report) override
Implementation of the IUnitTest interface.
void finalizeTest() override
Implementation of the IUnitTest interface.
UniqueArray< TestFuncInfo > m_test_functions
Associated service.
FuncPtr m_set_up_function
Pointer to the initialization method of each test.
FuncPtr m_tear_down_function
Pointer to the teardown method of each test.
FuncPtr m_class_tear_down_function
Pointer to the class teardown method.
FuncPtr m_class_set_up_function
Pointer to the class initialization method.
void(T::* FuncPtr)()
Type of the pointer to the test methods.
Node of a DOM tree.
Definition XmlNode.h:51
void setAttrValue(const String &name, const String &value)
Sets the attribute name to the value value.
Definition XmlNode.cc:248
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --