7 #ifndef IGLOO_CONTEXTREGISTRY_H 8 #define IGLOO_CONTEXTREGISTRY_H 25 template <
typename ContextToCall>
28 typedef void (ContextToCall::*SpecPtr)();
44 typedef std::pair<std::string, SpecInfo> NamedSpec;
45 typedef std::map<std::string, SpecInfo> Specs;
52 static void RegisterSpec(
const std::string& name,
void (ContextToCall::*spec)(),
53 bool skip =
false,
bool only =
false)
56 spec_info.spec_ptr = spec;
57 spec_info.skip = skip;
58 spec_info.only = only;
59 GetSpecs().insert(std::make_pair(name, spec_info));
62 static void ClearRegisteredSpecs()
67 template <
typename ContextToCreate>
68 static void Run(
const std::string& contextName,
TestResults& results,
73 CallSpecs<ContextToCreate>(specs, contextName, results, testListener);
77 template <
typename ContextToCreate>
78 static void CallSpecs(
const Specs& specs,
const std::string& contextName,
81 ContextToCreate::SetUpContext();
84 c.SetName(contextName);
86 testListener.ContextRunStarting(c);
88 typename Specs::const_iterator it;
89 for (it = specs.begin(); it != specs.end(); it++)
91 const std::string& specName = (*it).first;
92 SpecInfo spec_info = (*it).second;
94 ContextToCreate context;
95 context.SetName(contextName);
97 testListener.SpecRunStarting(context, specName);
101 if(CallSpec(context, specName, spec_info.spec_ptr, results))
103 testListener.SpecSucceeded(context, specName);
107 testListener.SpecFailed(context, specName);
112 ContextToCreate::TearDownContext();
114 testListener.ContextRunEnded(c);
117 static bool CallSpec(ContextToCall& context,
const std::string& specName,
124 context.IglooFrameworkSetUp();
129 results.AddResult(
TestResultFactory(context.Name(), specName).CreateFromException(e));
134 results.AddResult(
FailedTestResult(context.Name(), specName,
"Caught unknown exception"));
140 context.IglooFrameworkTearDown();
144 results.AddResult(
TestResultFactory(context.Name(), specName).CreateFromException(e));
149 results.AddResult(
FailedTestResult(context.Name(), specName,
"Caught unknown exception"));
155 results.AddResult(
TestResultFactory(context.Name(), specName).CreateSuccessful());
161 static Specs& GetSpecs()
167 static bool is_spec_not_marked_as_only(
const NamedSpec& spec)
169 return !is_spec_marked_as_only(spec);
172 static bool is_spec_marked_as_only(
const NamedSpec& spec)
174 return spec.second.only;
177 static void GetSpecsMarkedAsOnly(Specs& specs)
179 std::remove_copy_if(GetSpecs().begin(), GetSpecs().end(), std::inserter(specs, specs.end()), is_spec_not_marked_as_only);
182 static bool HasSpecsMarkedAsOnly()
184 return std::find_if(GetSpecs().begin(), GetSpecs().end(), is_spec_marked_as_only) != GetSpecs().end();
187 static void GetSpecsToRun(Specs& specs)
189 if(HasSpecsMarkedAsOnly())
191 GetSpecsMarkedAsOnly(specs);
195 std::copy(GetSpecs().begin(), GetSpecs().end(), std::inserter(specs, specs.end()));
Definition: testresult.h:52
Definition: testlistener.h:14
Definition: assertionexception.h:11
Definition: contextregistry.h:26
Definition: testresults.h:12
Definition: testresultfactory.h:12