]> git.tdb.fi Git - ext/sigc++-2.0.git/blob - tests/testutilities.cc
Adjust the name of the library to match upstream
[ext/sigc++-2.0.git] / tests / testutilities.cc
1 /* Copyright (C) 2012 The libsigc++ Development Team
2  *
3  * This file is part of libsigc++.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "testutilities.h"
20
21 #include <iostream>
22 #include <cstring>
23
24 TestUtilities* TestUtilities::instance_ = nullptr;
25
26 TestUtilities::TestUtilities()
27 : verbose_(false), result_ok_(true), test_number_(0)
28 {
29 }
30
31 //static
32 TestUtilities* TestUtilities::get_instance()
33 {
34   if (!instance_)
35     instance_ = new TestUtilities;
36
37   return instance_;
38 }
39
40 bool TestUtilities::check_command_args(int argc, char* argv[])
41 {
42   bool go_on = true; // Whether the caller shall continue program execution.
43   bool print_help = false;
44   for (int argi = 1; argi < argc; ++argi)
45   {
46     if (std::strcmp(argv[argi], "-v") == 0 || std::strcmp(argv[argi], "--verbose") == 0)
47     {
48       verbose_ = true;
49     }
50     else
51     {
52       print_help = true;
53       go_on = false;
54       if (!(std::strcmp(argv[argi], "-h") == 0 || std::strcmp(argv[argi], "--help") == 0))
55       {
56         result_ok_ = false;
57         std::cout << "Unknown command argument: " << argv[argi] << std::endl;
58       }
59     }
60   }
61
62   if (print_help)
63     std::cout << "Usage: " << argv[0] << " [-h|--help] [-v|--verbose]" << std::endl;
64
65   return go_on;
66 }
67
68 void TestUtilities::check_result(std::ostringstream& result_stream,
69                                  const std::string& expected_result)
70 {
71   if (verbose_)
72     std::cout << result_stream.str() << std::endl;
73
74   ++test_number_;
75   if (expected_result != result_stream.str())
76   {
77     std::cerr << "   Test " << test_number_ << std::endl;
78     std::cerr << "Expected \"" << expected_result << "\"" << std::endl;
79     std::cerr << "Got      \"" << result_stream.str() << "\"" << std::endl;
80     result_ok_ = false;
81   }
82   result_stream.str("");
83 }
84
85 //static
86 bool TestUtilities::get_result_and_delete_instance()
87 {
88   const bool result = instance_ ? instance_->result_ok_ : true;
89   delete instance_;
90   instance_ = nullptr;
91   return result;
92 }