summaryrefslogtreecommitdiff
path: root/tests/testutilities.cc
blob: 845dcef02846315bb7ef7ed05118fcc08df1b25c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* Copyright (C) 2012 The libsigc++ Development Team
 *
 * This file is part of libsigc++.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "testutilities.h"

#include <iostream>
#include <cstring>

TestUtilities* TestUtilities::instance_ = nullptr;

TestUtilities::TestUtilities() : verbose_(false), result_ok_(true), test_number_(0)
{
}

// static
TestUtilities*
TestUtilities::get_instance()
{
  if (!instance_)
    instance_ = new TestUtilities;

  return instance_;
}

bool
TestUtilities::check_command_args(int argc, char* argv[])
{
  bool go_on = true; // Whether the caller shall continue program execution.
  bool print_help = false;
  for (int argi = 1; argi < argc; ++argi)
  {
    if (std::strcmp(argv[argi], "-v") == 0 || std::strcmp(argv[argi], "--verbose") == 0)
    {
      verbose_ = true;
    }
    else
    {
      print_help = true;
      go_on = false;
      if (!(std::strcmp(argv[argi], "-h") == 0 || std::strcmp(argv[argi], "--help") == 0))
      {
        result_ok_ = false;
        std::cout << "Unknown command argument: " << argv[argi] << std::endl;
      }
    }
  }

  if (print_help)
    std::cout << "Usage: " << argv[0] << " [-h|--help] [-v|--verbose]" << std::endl;

  return go_on;
}

void
TestUtilities::check_result(std::ostringstream& result_stream, const std::string& expected_result)
{
  if (verbose_)
    std::cout << result_stream.str() << std::endl;

  ++test_number_;
  if (expected_result != result_stream.str())
  {
    std::cerr << "   Test " << test_number_ << std::endl;
    std::cerr << "Expected \"" << expected_result << "\"" << std::endl;
    std::cerr << "Got      \"" << result_stream.str() << "\"" << std::endl;
    result_ok_ = false;
  }
  result_stream.str("");
}

// static
bool
TestUtilities::get_result_and_delete_instance()
{
  const bool result = instance_ ? instance_->result_ok_ : true;
  delete instance_;
  instance_ = nullptr;
  return result;
}