webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
jsontest.h
Go to the documentation of this file.
1 // Copyright 2007-2010 Baptiste Lepilleur
2 // Distributed under MIT license, or public domain if desired and
3 // recognized in your jurisdiction.
4 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
5 
6 #ifndef JSONTEST_H_INCLUDED
7 # define JSONTEST_H_INCLUDED
8 
9 # include <json/config.h>
10 # include <json/value.h>
11 # include <json/writer.h>
12 # include <stdio.h>
13 # include <deque>
14 # include <sstream>
15 # include <string>
16 
17 // //////////////////////////////////////////////////////////////////
18 // //////////////////////////////////////////////////////////////////
19 // Mini Unit Testing framework
20 // //////////////////////////////////////////////////////////////////
21 // //////////////////////////////////////////////////////////////////
22 
23 
24 
31 namespace JsonTest {
32 
33 
34  class Failure
35  {
36  public:
37  const char *file_;
38  unsigned int line_;
41  unsigned int nestingLevel_;
42  };
43 
44 
49  {
50  typedef unsigned int Id;
51  Id id_;
52  const char *file_;
53  unsigned int line_;
54  const char *expr_;
59  };
60 
61  class TestResult
62  {
63  public:
64  TestResult();
65 
71 
74 
75  void setTestName( const std::string &name );
76 
78  TestResult &addFailure( const char *file, unsigned int line,
79  const char *expr = 0 );
80 
84  TestResult &popPredicateContext();
85 
86  bool failed() const;
87 
88  void printFailure( bool printTestName ) const;
89 
90  // Generic operator that will work with anything ostream can deal with.
91  template <typename T>
93  std::ostringstream oss;
94  oss.precision( 16 );
95  oss.setf( std::ios_base::floatfield );
96  oss << value;
97  return addToLastFailure(oss.str());
98  }
99 
100  // Specialized versions.
101  TestResult &operator << ( bool value );
102  // std:ostream does not support 64bits integers on all STL implementation
105 
106  private:
107  TestResult &addToLastFailure( const std::string &message );
108  unsigned int getAssertionNestingLevel() const;
110  void addFailureInfo( const char *file, unsigned int line,
111  const char *expr, unsigned int nestingLevel );
112  static std::string indentText( const std::string &text,
113  const std::string &indent );
114 
115  typedef std::deque<Failure> Failures;
116  Failures failures_;
117  std::string name_;
118  PredicateContext rootPredicateNode_;
119  PredicateContext::Id lastUsedPredicateId_;
121  Failure *messageTarget_;
122  };
123 
124 
125  class TestCase
126  {
127  public:
128  TestCase();
129 
130  virtual ~TestCase();
131 
132  void run( TestResult &result );
133 
134  virtual const char *testName() const = 0;
135 
136  protected:
138 
139  private:
140  virtual void runTestCase() = 0;
141  };
142 
144  typedef TestCase *(*TestCaseFactory)();
145 
146  class Runner
147  {
148  public:
149  Runner();
150 
153 
158  int runCommandLine( int argc, const char *argv[] ) const;
159 
161  bool runAllTest( bool printSummary ) const;
162 
164  unsigned int testCount() const;
165 
167  std::string testNameAt( unsigned int index ) const;
168 
170  void runTestAt( unsigned int index, TestResult &result ) const;
171 
172  static void printUsage( const char *appName );
173 
174  private: // prevents copy construction and assignment
175  Runner( const Runner &other );
176  Runner &operator =( const Runner &other );
177 
178  private:
179  void listTests() const;
180  bool testIndex( const std::string &testName, unsigned int &index ) const;
181  static void preventDialogOnCrash();
182 
183  private:
184  typedef std::deque<TestCaseFactory> Factories;
185  Factories tests_;
186  };
187 
188  template<typename T, typename U>
189  TestResult &
190  checkEqual( TestResult &result, const T &expected, const U &actual,
191  const char *file, unsigned int line, const char *expr )
192  {
193  if ( expected != actual )
194  {
195  result.addFailure( file, line, expr );
196  result << "Expected: " << expected << "\n";
197  result << "Actual : " << actual;
198  }
199  return result;
200  }
201 
202 
203  TestResult &
205  const std::string &expected, const std::string &actual,
206  const char *file, unsigned int line, const char *expr );
207 
208 } // namespace JsonTest
209 
210 
214 #define JSONTEST_ASSERT( expr ) \
215  if ( expr ) \
216  { \
217  } \
218  else \
219  result_->addFailure( __FILE__, __LINE__, #expr )
220 
223 #define JSONTEST_ASSERT_PRED( expr ) \
224  { \
225  JsonTest::PredicateContext _minitest_Context = { \
226  result_->predicateId_, __FILE__, __LINE__, #expr }; \
227  result_->predicateStackTail_->next_ = &_minitest_Context; \
228  result_->predicateId_ += 1; \
229  result_->predicateStackTail_ = &_minitest_Context; \
230  (expr); \
231  result_->popPredicateContext(); \
232  } \
233  *result_
234 
236 #define JSONTEST_ASSERT_EQUAL( expected, actual ) \
237  JsonTest::checkEqual( *result_, expected, actual, \
238  __FILE__, __LINE__, \
239  #expected " == " #actual )
240 
242 #define JSONTEST_ASSERT_STRING_EQUAL( expected, actual ) \
243  JsonTest::checkStringEqual( *result_, \
244  std::string(expected), std::string(actual), \
245  __FILE__, __LINE__, \
246  #expected " == " #actual )
247 
249 #define JSONTEST_FIXTURE( FixtureType, name ) \
250  class Test##FixtureType##name : public FixtureType \
251  { \
252  public: \
253  static JsonTest::TestCase *factory() \
254  { \
255  return new Test##FixtureType##name(); \
256  } \
257  public: /* overidden from TestCase */ \
258  virtual const char *testName() const \
259  { \
260  return #FixtureType "/" #name; \
261  } \
262  virtual void runTestCase(); \
263  }; \
264  \
265  void Test##FixtureType##name::runTestCase()
266 
267 #define JSONTEST_FIXTURE_FACTORY( FixtureType, name ) \
268  &Test##FixtureType##name::factory
269 
270 #define JSONTEST_REGISTER_FIXTURE( runner, FixtureType, name ) \
271  (runner).add( JSONTEST_FIXTURE_FACTORY( FixtureType, name ) )
272 
273 #endif // ifndef JSONTEST_H_INCLUDED
line
Definition: buildtests.py:37
const char * expr_
Definition: jsontest.h:54
Definition: XMLHttpRequest.idl:39
Definition: jsontest.h:34
unsigned int line_
Definition: jsontest.h:53
Unit testing framework.
Definition: jsontest.cpp:71
double U(int64_t x, double alpha)
Definition: metric_recorder.cc:414
TestResult & checkStringEqual(TestResult &result, const std::string &expected, const std::string &actual, const char *file, unsigned int line, const char *expr)
Definition: jsontest.cpp:561
const char * file_
Definition: jsontest.h:52
unsigned int line_
Definition: jsontest.h:38
std::string expr_
Definition: jsontest.h:39
TestCase *(* TestCaseFactory)()
Function pointer type for TestCase factory.
Definition: jsontest.h:144
PredicateContext::Id predicateId_
Definition: jsontest.h:70
PredicateContext * next_
Definition: jsontest.h:55
VoEFile * file
Definition: voe_cmd_test.cc:59
Definition: jsontest.h:61
const char * file_
Definition: jsontest.h:37
TestSubObjConstructor T
Definition: TestTypedefs.idl:84
rtc::scoped_refptr< PeerConnectionFactoryInterface > factory(webrtc::CreatePeerConnectionFactory(network_thread.get(), worker_thread.get(), signaling_thread.get(), nullptr, encoder_factory, decoder_factory))
Definition: peerconnection_jni.cc:1838
GLuint index
Definition: gl2.h:383
Failure * failure_
Definition: jsontest.h:58
EGLAttrib * value
Definition: eglext.h:120
PredicateContext * predicateStackTail_
Definition: jsontest.h:73
EGLImageKHR EGLint * name
Definition: eglext.h:851
const char * argv[]
Definition: DumpRenderTree.cpp:1631
unsigned int Id
Definition: jsontest.h:50
std::string message_
Definition: jsontest.h:40
string expected
Definition: buildtests.py:33
unsigned long long int UInt64
Definition: config.h:89
GLsizei const GLchar *const * string
Definition: gl2.h:479
result
Definition: target-blank-opener-post-window.php:5
long long int Int64
Definition: config.h:88
void run(Test f, element_t *first, element_t *last, int number_of_times)
Definition: container.cpp:101
unsigned int nestingLevel_
Definition: jsontest.h:41
TestResult * result_
Definition: jsontest.h:137
Definition: jsontest.h:125
Definition: jsontest.h:146
TestCase
Definition: gtest_test_utils.py:62
TestResult & addFailure(const char *file, unsigned int line, const char *expr=0)
Adds an assertion failure.
Definition: jsontest.cpp:96
Definition: jsontest.h:48
std::ostream & operator<<(std::ostream &os, const SdpAudioFormat &saf)
Definition: audio_format.cc:66
GLuint GLsizei const GLchar * message
Definition: gl2ext.h:137
int failed
Definition: testapi.c:58
Id id_
Definition: jsontest.h:51
testName
Definition: plotTimingTest.m:13
TestResult & checkEqual(TestResult &result, const T &expected, const U &actual, const char *file, unsigned int line, const char *expr)
Definition: jsontest.h:190