~yaffut/yaffut/yaffut.bzr

« back to all changes in this revision

Viewing changes to factory.cpp

  • Committer: Jan Nieuwenhuizen
  • Date: 2006-11-14 11:52:02 UTC
  • Revision ID: janneke@abc-20061114115202-2a6d3fb8d9187f98
Initial.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2006 Rutger E.W. van Beusekom.
 
2
// Distributed under the Boost Software License, Version 1.0. (See
 
3
// accompanying file LICENSE_1_0.txt or copy at
 
4
// http://www.boost.org/LICENSE_1_0.txt)
 
5
 
 
6
#include "factory.h"
 
7
#include "result.h"
 
8
#include "test.h"
 
9
 
 
10
Factory::Factory()
 
11
: m_Stop(false)
 
12
, m_Stopped(true)
 
13
, m_TestResult(0,0,0,"")
 
14
{}
 
15
 
 
16
Factory& Factory::Instance()
 
17
{
 
18
  static Factory singleton;
 
19
  return singleton;
 
20
}
 
21
const std::vector<std::string>& Factory::Names()
 
22
{
 
23
  static std::vector<std::string> names;
 
24
  if(names.empty())
 
25
  {
 
26
    std::vector<Create_t>& tests = Instance().m_Tests;
 
27
    names.push_back("All");
 
28
    for(size_t i = 0; i < tests.size(); ++i)
 
29
    {
 
30
      std::auto_ptr<AbstractTest> test(tests[i]());
 
31
      names.push_back(test->name());
 
32
    }
 
33
  }
 
34
  return names;
 
35
}
 
36
const TestResult& Factory::Result()
 
37
 
38
  return m_TestResult;
 
39
}
 
40
void Factory::Register(Create_t create)
 
41
{
 
42
  m_Tests.push_back(create);
 
43
}
 
44
void Factory::Run(const std::string& name)
 
45
{
 
46
  m_Stop = false;
 
47
  m_Stopped = false;
 
48
      
 
49
  std::vector<Create_t>& tests = Instance().m_Tests;
 
50
  size_t success = 0;
 
51
  size_t fail    = 0;
 
52
  size_t total   = 0;
 
53
  std::string message;
 
54
  
 
55
  for(size_t i = 0; i < tests.size(); ++i)
 
56
  {
 
57
    try
 
58
    {
 
59
      std::auto_ptr<AbstractTest> test(tests[i]());
 
60
      if(test->name() == name or "All" == name)
 
61
      {
 
62
        total += test->TestFunctions().empty() ? 1 : 0;
 
63
        total += test->TestFunctions().size();
 
64
      }
 
65
    }
 
66
    catch(...){}
 
67
  }
 
68
 
 
69
  m_TestResult = TestResult(total, success, fail, message);
 
70
 
 
71
  bool show_progress = false;
 
72
  
 
73
  for(size_t i = 0; i < tests.size() && !m_Stop; ++i)
 
74
  {
 
75
    size_t j = 0;
 
76
    size_t sz = 0;
 
77
    do
 
78
    {
 
79
      std::auto_ptr<AbstractTest> test(tests[i]());
 
80
      try
 
81
      {
 
82
        if(test->name() == name or "All" == name)
 
83
        {
 
84
          if(!test->TestFunctions().empty())
 
85
          {
 
86
            test->TestFunctions()[j](*test);
 
87
          }
 
88
          ++success;
 
89
          show_progress = true;
 
90
          std::cout << "[OK]\n" << std::endl;
 
91
        }
 
92
        else
 
93
        {
 
94
          show_progress = false;
 
95
        }
 
96
      }
 
97
      catch(const std::exception& e)
 
98
      {
 
99
        message.append(test->name()).append(" [FAIL] ");
 
100
        message.append(e.what()).append("\n\n");
 
101
          
 
102
        std::cout << "[FAIL] " << e.what() << '\n' << std::endl;
 
103
        ++fail;
 
104
      }
 
105
      catch(...)
 
106
      {
 
107
        message.append(test->name()).append(" [FAIL] ");
 
108
        message.append(" unknown exception\n\n");
 
109
        
 
110
        std::cout << "[FAIL] unknown exception\n" << std::endl;
 
111
        ++fail;
 
112
      }
 
113
      
 
114
      m_TestResult = TestResult(total, success, fail, message);
 
115
      
 
116
      sz = test->TestFunctions().size();
 
117
      ++j;
 
118
    }
 
119
    while(j < sz && !m_Stop);
 
120
 
 
121
    if(show_progress)
 
122
    {
 
123
      std::cout << "\n[PROGRESS] tested(" << success + fail << '/' << total
 
124
                << ") succeeded(" << success << '/' << total
 
125
                << ") failed(" << fail << '/' << total << ")\n" << std::endl;
 
126
    }        
 
127
    m_Stopped = true;
 
128
  }
 
129
}
 
130
 
 
131
void Factory::Stop()
 
132
{
 
133
  m_Stop = true;
 
134
}
 
135
 
 
136
bool Factory::Stopped()
 
137
{
 
138
  return m_Stop and m_Stopped;
 
139
}