E-MailRelay
gtest.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file gtest.cpp
19///
20
21#include "gdef.h"
22#include "gtest.h"
23#include "glog.h"
24#include "genvironment.h"
25#include <string>
26#include <set>
27
28#if defined(_DEBUG) || defined(G_TEST_ENABLED)
29namespace G
30{
31 namespace TestImp
32 {
33 bool spec_set = false ;
34 std::string spec( bool set = false , const std::string & s = std::string() ) ;
35 }
36}
37std::string G::TestImp::spec( bool set , const std::string & s_in )
38{
39 static std::string s ;
40 if( set )
41 {
42 if( !s_in.empty() )
43 s = "," + s_in + "," ;
44 spec_set = true ;
45 }
46 return s ;
47}
48void G::Test::set( const std::string & s )
49{
50 TestImp::spec( true , s ) ;
51}
52bool G::Test::enabled( const char * name )
53{
54 if( !TestImp::spec_set )
55 {
56 TestImp::spec( true , Environment::get("G_TEST",std::string()) ) ;
57 }
58
59 bool result = TestImp::spec().empty() ? false : ( TestImp::spec().find(","+std::string(name)+",") != std::string::npos ) ;
60 if( result )
61 {
62 static std::set<std::string> warned ;
63 if( warned.find(name) == warned.end() )
64 {
65 warned.insert( name ) ;
66 G_WARNING( "G::Test::enabled: test case enabled: [" << name << "]" ) ;
67 }
68 }
69 return result ;
70}
71bool G::Test::enabled() noexcept
72{
73 return true ;
74}
75#else
76void G::Test::set( const std::string & )
77{
78}
79bool G::Test::enabled() noexcept
80{
81 return false ;
82}
83#endif
static std::string get(const std::string &name, const std::string &default_)
Returns the environment variable value or the given default.
static bool enabled() noexcept
Returns true if test features are enabled.
Definition: gtest.cpp:79
static void set(const std::string &)
Sets the test specification string.
Definition: gtest.cpp:76
Low-level classes.
Definition: garg.h:36