1 mike 1.1 /*
2 **==============================================================================
3 **
4 ** Open Management Infrastructure (OMI)
5 **
6 ** Copyright (c) Microsoft Corporation
7 **
8 ** Licensed under the Apache License, Version 2.0 (the "License"); you may not
9 ** use this file except in compliance with the License. You may obtain a copy
10 ** of the License at
11 **
12 ** http://www.apache.org/licenses/LICENSE-2.0
13 **
14 ** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 ** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 ** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 ** MERCHANTABLITY OR NON-INFRINGEMENT.
18 **
19 ** See the Apache 2 License for the specific language governing permissions
20 ** and limitations under the License.
21 **
22 mike 1.1 **==============================================================================
23 */
24
25 //==============================================================================
26 //
27 // ut.h : provides helper macro/classes for unit-test framework
28 //
29 //==============================================================================
30
31 #ifndef _ut_ut_h
32 #define _ut_ut_h
33
|
34 krisbash 1.3 #if !defined(TEST_BUILD)
35 # define TEST_BUILD
36 #endif
37
|
38 mike 1.1 #include <common.h>
39 #include <string>
40 #include <cassert>
41 #include <cstdio>
42 #include <sstream>
43
|
44 krisbash 1.3 #include <nits/base/nits.h>
45
46
|
47 mike 1.1 // A constant 'false' value that can be used in a condition with getting the
|
48 krisbash 1.3 // 'conditional expression is constant' warning from Windows.
|
49 mike 1.1 inline bool __UT_False() { return false; }
50 #define UT_FALSE __UT_False()
51
|
52 krisbash 1.3 #define TEST_ASSERT(x) NitsAssert(x, PAL_T(""))
53
|
54 mike 1.1 //==============================================================================
55 //
|
56 krisbash 1.3 // NITS version of these macros:
|
57 mike 1.1 //
58 //==============================================================================
59
|
60 krisbash 1.3 # define UT_ASSERT(cond) \
|
61 mike 1.1 do \
62 { \
|
63 krisbash 1.3 NitsAssertExA((cond) != 0, "", NitsHere(), NitsAutomatic); \
|
64 mike 1.1 } \
65 while (__UT_False())
66
|
67 krisbash 1.3 # define UT_ASSERT_EQUAL(v1, v2) \
|
68 mike 1.1 do \
69 { \
|
70 krisbash 1.3 NitsAssertExA((v1) == (v2), "", NitsHere(), NitsAutomatic); \
|
71 mike 1.1 } \
72 while (__UT_False())
73
74
|
75 krisbash 1.3 # define UT_ASSERT_NOT_EQUAL(v1, v2) \
|
76 mike 1.1 do \
77 { \
|
78 krisbash 1.3 NitsAssertExA((v1) != (v2), "", NitsHere(), NitsAutomatic); \
|
79 mike 1.1 } \
80 while (__UT_False())
81
|
82 krisbash 1.3 # define UT_ASSERT_FAILED_MSG(message) \
83 do {\
84 NitsAssertExA(0, message, NitsHere(), NitsAutomatic); \
85 } \
86 while (__UT_False())
|
87 mike 1.1
|
88 krisbash 1.3 # define UT_WARNING(text) \
89 do { \
90 NitsTraceExA(text, NitsHere(), NitsAutomatic); \
91 } \
92 while (__UT_False())
|
93 mike 1.1
94 //==============================================================================
95 //
96 // Test framework functions:
97 //
98 //==============================================================================
99
100 namespace ut
101 {
102 // internal part
103 struct UnittestException
104 {
105 std::string m_file;
106 std::string m_function;
107 std::string m_text;
108 std::string m_testcase;
109 int m_line;
110
111 UnittestException(const char* file, int line, const char* function,
112 const char* text ) : m_file(file), m_function(function),
113 m_text(text), m_line(line)
114 mike 1.1 {
115 }
116 };
117
|
118 krisbash 1.3 INLINE int Assert(
119 int cond,
120 const char* condStr,
121 const char* file,
122 int line,
123 const char* function)
124 {
125 if (!cond)
126 throw(ut::UnittestException(file, line, function, condStr));
127 return cond;
128 }
129
|
130 mike 1.1 typedef void (*MODULE_TEST_CALLBACK) ();
131
132 // interanl callbacks - used by macros
133 void registerCallback(MODULE_TEST_CALLBACK pfn);
134 unsigned int testStarted(const char* name);
135 void testCompleted(const char* name);
136 void testFailed(const UnittestException& ex);
137
138 void testWarning(const char* text);
139
140 bool testGetAttr(const std::string& name, std::string& value);
141 };
142
143 #include "utility.h"
144 #include "strutil.h"
145
146 #endif /* _ut_ut_h */
|