Nektar++
MetricPyUnitTest.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: MetricPyUnitTest.cpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Implementation of a metric for Python's unittest.
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#include <MetricPyUnitTest.h>
36
37namespace Nektar
38{
40 "PYUNITTEST", MetricPyUnitTest::create);
41
42MetricPyUnitTest::MetricPyUnitTest(TiXmlElement *metric, bool generate)
43 : MetricRegex(metric, generate)
44{
45 // Set up the regular expression.
46 m_regex = "^(test.*) \\(.*\\) ... (ERROR|FAIL|ok).*";
47
48 // Python's unittest framework prints to stderr by default.
49 m_useStderr = true;
50
51 // Find the functions to match against.
52 TiXmlElement *func = metric->FirstChildElement("function");
54 "Missing function tag for Python unittest metric!");
55
56 while (func)
57 {
58 ASSERTL0(!EmptyString(func->GetText()),
59 "Missing function name in Python unittest metric.");
60
61 if (!m_generate)
62 {
63 std::vector<MetricRegexFieldValue> tmp(2);
64 tmp[0] = MetricRegexFieldValue(func->GetText());
65 tmp[1] = MetricRegexFieldValue("ok");
66 m_matches.push_back(tmp);
67 }
68
69 func = func->NextSiblingElement("function");
70 }
71}
72
73void MetricPyUnitTest::v_Generate(std::istream &pStdout, std::istream &pStderr)
74{
75 // Run MetricRegex to generate matches.
76 MetricRegex::v_Generate(pStdout, pStderr);
77
78 // First remove all existing values.
79 m_metric->Clear();
80
81 // Now create new values.
82 for (int i = 0; i < m_matches.size(); ++i)
83 {
84 ASSERTL0(m_matches[i].size() == 2,
85 "Wrong number of matches for regular expression.");
86 ASSERTL0(m_matches[i][1].m_value == "ok",
87 "Test " + m_matches[i][0].m_value + " has failed");
88
89 TiXmlElement *func = new TiXmlElement("function");
90 func->LinkEndChild(new TiXmlText(m_matches[i][0].m_value));
91 m_metric->LinkEndChild(func);
92 }
93}
94} // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
std::string RegisterCreatorFunction(std::string key, CreatorFunction func)
Definition: Metric.h:133
TiXmlElement * m_metric
Pointer to XML structure containing metric definition.
Definition: Metric.h:101
bool m_generate
Determines whether to generate this metric or not.
Definition: Metric.h:96
MetricPyUnitTest(TiXmlElement *metric, bool generate)
static std::string type
void v_Generate(std::istream &pStdout, std::istream &pStderr) override
Virtual function to generate the metric. Should be redefined in derived classes.
static MetricSharedPtr create(TiXmlElement *metric, bool generate)
void v_Generate(std::istream &pStdout, std::istream &pStderr) override
Test output against a regex expression and set of matches.
std::vector< std::vector< MetricRegexFieldValue > > m_matches
Stores the multiple matches defined in each <MATCH> tag.
Definition: MetricRegex.h:84
bool m_useStderr
If true, use stderr for testing/generation instead of stdout.
Definition: MetricRegex.h:88
std::regex m_regex
Storage for the boost regex.
Definition: MetricRegex.h:82
bool EmptyString(const char *s)
Check to see whether the given string s is empty (or null).
Definition: Metric.h:50
MetricFactory & GetMetricFactory()
Definition: Metric.cpp:42
Data structure for a Regex value to match.
Definition: MetricRegex.h:49