Slicer  4.8
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
vtkTestingOutputWindow.h
Go to the documentation of this file.
1 /*==============================================================================
2 
3  Program: 3D Slicer
4 
5  See COPYRIGHT.txt
6  or http://www.slicer.org/copyright/copyright.txt for details.
7 
8  Unless required by applicable law or agreed to in writing, software
9  distributed under the License is distributed on an "AS IS" BASIS,
10  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  See the License for the specific language governing permissions and
12  limitations under the License.
13 
14 ==============================================================================*/
15 
16 #ifndef __vtkTestingOutputWindow_h
17 #define __vtkTestingOutputWindow_h
18 
19 #include "vtkAddon.h"
20 
21 #include "vtkObject.h"
22 #include "vtkOutputWindow.h"
23 #include "vtkLoggingMacros.h" // for vtkInfoWithoutObjectMacro
24 
45 class VTK_ADDON_EXPORT vtkTestingOutputWindow : public vtkOutputWindow
46 {
47 public:
48  vtkTypeMacro(vtkTestingOutputWindow, vtkOutputWindow);
49  static vtkTestingOutputWindow* New();
50 
51  // Gets a pointer to the singleton testing output window instance.
52  // If the current VTK output window is not vtkTestingOutputWindow type then
53  // it changes the output window to that.
54  static vtkTestingOutputWindow* GetInstance();
55 
56  virtual void PrintSelf(ostream& os, vtkIndent indent) VTK_OVERRIDE;
57 
58  virtual void DisplayText(const char* text) VTK_OVERRIDE;
59  virtual void DisplayErrorText(const char* text) VTK_OVERRIDE;
60  virtual void DisplayWarningText(const char* text) VTK_OVERRIDE;
61  virtual void DisplayGenericWarningText(const char* text) VTK_OVERRIDE;
62  virtual void DisplayDebugText(const char* text) VTK_OVERRIDE;
63 
64  // Sets number of warning and error messages to zero
65  virtual void ResetNumberOfLoggedMessages();
66 
67  // Number of any logged messages
68  vtkGetMacro(NumberOfLoggedMessages, int);
69  vtkSetMacro(NumberOfLoggedMessages, int);
70 
71  // Number of logged warning or generic warning messages
72  vtkGetMacro(NumberOfLoggedWarningMessages, int);
73  vtkSetMacro(NumberOfLoggedWarningMessages, int);
74 
75  // Number of logged error messages
76  vtkGetMacro(NumberOfLoggedErrorMessages, int);
77  vtkSetMacro(NumberOfLoggedErrorMessages, int);
78 
79  // Returns the sum of warning and error messages logged
80  int GetNumberOfLoggedWarningErrorMessages();
81 
82 protected:
84  virtual ~vtkTestingOutputWindow();
85 
89 
90 private:
91  vtkTestingOutputWindow(const vtkTestingOutputWindow&); // Not implemented.
92  void operator=(const vtkTestingOutputWindow&); // Not implemented.
93 };
94 
95 
96 // Convenience macros:
97 
99 #define TESTING_OUTPUT_INIT() vtkTestingOutputWindow::GetInstance();
100 
102 #define TESTING_OUTPUT_RESET() vtkTestingOutputWindow::GetInstance()->ResetNumberOfLoggedMessages();
103 
105 #define TESTING_OUTPUT_ASSERT_MESSAGES(expectedNumberOfMessages) \
106  { \
107  int actualNumberOfMessages = vtkTestingOutputWindow::GetInstance()->GetNumberOfLoggedMessages(); \
108  if (actualNumberOfMessages != expectedNumberOfMessages) \
109  { \
110  std::cerr << "Assertion failed in " << __FILE__ << ":" << __LINE__ << " - expected " << expectedNumberOfMessages \
111  << " messages, got " << actualNumberOfMessages << std::endl; \
112  exit(EXIT_FAILURE); \
113  } \
114  };
115 
117 #define TESTING_OUTPUT_ASSERT_WARNINGS(expectedNumberOfMessages) \
118  { \
119  int actualNumberOfMessages = vtkTestingOutputWindow::GetInstance()->GetNumberOfLoggedWarningMessages(); \
120  if (actualNumberOfMessages != expectedNumberOfMessages) \
121  { \
122  std::cerr << "Assertion failed in " << __FILE__ << ":" << __LINE__ << " - expected " << expectedNumberOfMessages \
123  << " warnings messages, got " << actualNumberOfMessages << std::endl; \
124  exit(EXIT_FAILURE); \
125  } \
126  };
127 
129 #define TESTING_OUTPUT_ASSERT_ERRORS(expectedNumberOfMessages) \
130  { \
131  int actualNumberOfMessages = vtkTestingOutputWindow::GetInstance()->GetNumberOfLoggedErrorMessages(); \
132  if (actualNumberOfMessages != expectedNumberOfMessages) \
133  { \
134  std::cerr << "Assertion failed in " << __FILE__ << ":" << __LINE__ << " - expected " << expectedNumberOfMessages \
135  << " error messages, got " << actualNumberOfMessages << std::endl; \
136  exit(EXIT_FAILURE); \
137  } \
138  };
139 
141 #define TESTING_OUTPUT_ASSERT_WARNINGS_ERRORS(expectedNumberOfMessages) \
142  { \
143  int actualNumberOfMessages = vtkTestingOutputWindow::GetInstance()->GetNumberOfLoggedWarningErrorMessages(); \
144  if (actualNumberOfMessages != expectedNumberOfMessages) \
145  { \
146  std::cerr << "Assertion failed in " << __FILE__ << ":" << __LINE__ << " - expected " << expectedNumberOfMessages \
147  << " error or warning messages, got " << actualNumberOfMessages << std::endl; \
148  exit(EXIT_FAILURE); \
149  } \
150  };
151 
153 #define TESTING_OUTPUT_ASSERT_WARNINGS_MINIMUM(expectedNumberOfMessages) \
154  { \
155  int actualNumberOfMessages = vtkTestingOutputWindow::GetInstance()->GetNumberOfLoggedWarningMessages(); \
156  if (actualNumberOfMessages < expectedNumberOfMessages) \
157  { \
158  std::cerr << "Assertion failed in " << __FILE__ << ":" << __LINE__ << " - expected minimum " << expectedNumberOfMessages \
159  << " warning messages, got " << actualNumberOfMessages << std::endl; \
160  exit(EXIT_FAILURE); \
161  } \
162  };
163 
165 #define TESTING_OUTPUT_ASSERT_ERRORS_MINIMUM(expectedNumberOfMessages) \
166  { \
167  int actualNumberOfMessages = vtkTestingOutputWindow::GetInstance()->GetNumberOfLoggedErrorMessages(); \
168  if (actualNumberOfMessages < expectedNumberOfMessages) \
169  { \
170  std::cerr << "Assertion failed in " << __FILE__ << ":" << __LINE__ << " - expected minimum " << expectedNumberOfMessages \
171  << " error messages, got " << actualNumberOfMessages << std::endl; \
172  exit(EXIT_FAILURE); \
173  } \
174  };
175 
177 #define TESTING_OUTPUT_ASSERT_WARNINGS_BEGIN() \
178  { \
179  /* Make sure there were no errors or warnings so far */ \
180  TESTING_OUTPUT_ASSERT_WARNINGS_ERRORS(0); \
181  vtkInfoWithoutObjectMacro("Expecting warning message(s)..."); \
182  }
183 
185 #define TESTING_OUTPUT_ASSERT_WARNINGS_END() \
186  { \
187  TESTING_OUTPUT_ASSERT_WARNINGS_MINIMUM(1); \
188  vtkInfoWithoutObjectMacro("Expected warning message(s) successfully received"); \
189  TESTING_OUTPUT_ASSERT_ERRORS(0); \
190  TESTING_OUTPUT_RESET(); \
191  }
192 
194 #define TESTING_OUTPUT_ASSERT_ERRORS_BEGIN() \
195  { \
196  /* Make sure there were no errors or warnings so far */ \
197  TESTING_OUTPUT_ASSERT_WARNINGS_ERRORS(0); \
198  vtkInfoWithoutObjectMacro("Expecting warning or error message(s)..."); \
199  }
200 
202 #define TESTING_OUTPUT_ASSERT_ERRORS_END() \
203  { \
204  TESTING_OUTPUT_ASSERT_ERRORS_MINIMUM(1); \
205  vtkInfoWithoutObjectMacro("Expected error message(s) successfully received"); \
206  TESTING_OUTPUT_RESET(); \
207  }
208 
210 #define TESTING_OUTPUT_IGNORE_WARNINGS_ERRORS_BEGIN() \
211  { \
212  /* Make sure there were no errors or warnings so far */ \
213  TESTING_OUTPUT_ASSERT_WARNINGS_ERRORS(0); \
214  vtkInfoWithoutObjectMacro("Ignoring expected warning or error message(s)..."); \
215  }
216 
218 #define TESTING_OUTPUT_IGNORE_WARNINGS_ERRORS_END() \
219  { \
220  vtkInfoWithoutObjectMacro("Finished ignoring warning or error message"); \
221  TESTING_OUTPUT_RESET(); \
222  }
223 
224 
225 #endif
VTK message output window class for automated testing.