NimbRo ROS Soccer Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test_utilities.h
1 // Utilities for unit testing
2 // File: test_utilities.h
3 // Author: Philipp Allgeuer <pallgeuer@ais.uni-bonn.de>
4 // Refer to http://en.wikipedia.org/wiki/ANSI_escape_code for information on the escape codes being used
5 
6 // Ensure header is only included once
7 #ifndef TEST_UTILITIES_H
8 #define TEST_UTILITIES_H
9 
10 // Includes
11 #include <iostream>
12 #include <string>
13 
14 // Macros
15 #ifndef DISPLAY_NEWLINE
16 #ifdef VERBOSE_TEST
17 #define DISPLAY(cmd) std::cout << " "; cmd // Note: Don't use inside a single-line if/for/while/etc as this expands into two lines of code
18 #define DISPLAY_NO_PAD(cmd) cmd
19 #define DISPLAY_NEWLINE std::cout << endl;
20 #else
21 #define DISPLAY(cmd)
22 #define DISPLAY_NO_PAD(cmd)
23 #define DISPLAY_NEWLINE
24 #endif /* VERBOSE_TEST */
25 #endif /* DISPLAY_NEWLINE */
26 
27 // Test utilities namespace
28 namespace testutilities
29 {
30  // Attributes namespace - enumerations are 'hidden' so as to avoid possible naming conflicts, and are not in the FText class as FText(FText::MAGENTA) looks awkward.
31  namespace Attr
32  {
33  // Enumerations
34  enum Attribute
35  {
36  RESET = 0,
37  BOLD,
38  ITALIC,
39  UNDERLINE,
40  BLINK,
41  INVERT = 7,
42  CONCEAL,
43  STRIKETHROUGH,
44  BOLD_OFF = 22,
45  ITALIC_OFF,
46  UNDERLINE_OFF,
47  BLINK_OFF,
48  INVERT_OFF = 27,
49  CONCEAL_OFF,
50  STRIKETHROUGH_OFF,
51  NOATTR = 110
52  };
53  enum Colour
54  {
55  BLACK = 0,
56  RED,
57  GREEN,
58  YELLOW,
59  BLUE,
60  MAGENTA,
61  CYAN,
62  WHITE,
63  DEFAULT = 9,
64  NONE = 110
65  };
66  }
67 
68  // Formatting functions
69  // These can be used with any output stream, not just cout
70  // Use like this: cout << "The following word is " << setColour(Attr::MAGENTA) << "magenta" << resetColour() << "!" << endl;
71  std::string setFormat(Attr::Colour foreground = Attr::NONE, Attr::Colour background = Attr::NONE, Attr::Attribute attribute = Attr::NOATTR);
72  std::string setColour(Attr::Colour foreground = Attr::NONE, Attr::Colour background = Attr::NONE);
73  std::string setAttribute(Attr::Attribute attribute);
74  std::string resetColour(); // Both reset functions reset all colours/attributes...
75  std::string resetFormat(); // The alternative naming is only included for symmetry reasons
76 
77  // Stream text formatting class
78  // This class automatically writes the output in the given format to cout, then resets the formatting and appends std::endl
79  // Use like this: FText(Attr::MAGENTA) << "This text is magenta!";
80  class FText
81  {
82  public:
83  // Constructors
84  FText(Attr::Colour foreground = Attr::NONE, Attr::Colour background = Attr::NONE, Attr::Attribute attribute = Attr::NOATTR)
85  {
86  std::cout << setFormat(foreground,background,attribute);
87  }
88  ~FText()
89  {
90  std::cout << resetFormat() << std::endl;
91  }
92 
93  // Stream operators
94  template <class T>
95  FText& operator<<(const T &v) // Everything else...
96  {
97  std::cout << v;
98  return *this;
99  }
100  FText& operator<<(std::ostream& (*pf)(std::ostream&)) // Type 1 manipulator
101  {
102  std::cout << pf;
103  return *this;
104  }
105  FText& operator<<(std::ios& (*pf)(std::ios&)) // Type 2 manipulator
106  {
107  std::cout << pf;
108  return *this;
109  }
110  FText& operator<<(std::ios_base& (*pf)(std::ios_base&)) // Type 3 manipulator
111  {
112  std::cout << pf;
113  return *this;
114  }
115  };
116 }
117 
118 #endif /* TEST_UTILITIES_H */
119 // EOF