AUnit  0.3.1
Unit testing framework for Arduino platforms inspired by ArduinoUnit.
Assertion.h
Go to the documentation of this file.
1 /*
2 MIT License
3 
4 Copyright (c) 2018 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 // Significant portions of the design and implementation of this file came from
26 // https://github.com/mmurdoch/arduinounit/blob/master/src/ArduinoUnit.h
27 
34 #ifndef AUNIT_ASSERTION_H
35 #define AUNIT_ASSERTION_H
36 
37 #include <Arduino.h> // definition of Print
38 #include "Printer.h"
39 #include "Verbosity.h"
40 #include "TestRunner.h"
41 
42 // Various assertXxx() macros, implemented using the assertOp() macro.
43 
45 #define assertEqual(arg1,arg2) assertOp(arg1,aunit::compareEqual,"==",arg2)
46 
48 #define assertNotEqual(arg1,arg2) \
49  assertOp(arg1,aunit::compareNotEqual,"!=",arg2)
50 
52 #define assertLess(arg1,arg2) assertOp(arg1,aunit::compareLess,"<",arg2)
53 
55 #define assertMore(arg1,arg2) assertOp(arg1,aunit::compareMore,">",arg2)
56 
58 #define assertLessOrEqual(arg1,arg2) \
59  assertOp(arg1,aunit::compareLessOrEqual,"<=",arg2)
60 
62 #define assertMoreOrEqual(arg1,arg2) \
63  assertOp(arg1,aunit::compareMoreOrEqual,">=",arg2)
64 
66 #define assertTrue(arg) assertEqual(arg,true)
67 
69 #define assertFalse(arg) assertEqual(arg,false)
70 
72 #define assertOp(arg1,op,opName,arg2) do {\
73  if (!aunit::assertion(__FILE__,__LINE__,(arg1),opName,op,(arg2)))\
74  return;\
75 } while (false)
76 
77 namespace aunit {
78 
79 // For the same reason as the compareXxx() methods, we use explicit overloaded
80 // functions, instead of using template specialization. And just as before, I
81 // was unable to use a template function for primitive integer types, because it
82 // interfered with the resolution of assertion(char*, char*). The wrong function
83 // would be called.
84 //
85 // These are all internal helpers, should not be called directly by users.
86 
87 bool assertion(const char* file, uint16_t line, bool lhs,
88  const char* opName, bool (*op)(bool lhs, bool rhs),
89  bool rhs);
90 
91 bool assertion(const char* file, uint16_t line, char lhs,
92  const char* opName, bool (*op)(char lhs, char rhs),
93  char rhs);
94 
95 bool assertion(const char* file, uint16_t line, int lhs,
96  const char* opName, bool (*op)(int lhs, int rhs),
97  int rhs);
98 
99 bool assertion(const char* file, uint16_t line, unsigned int lhs,
100  const char* opName, bool (*op)(unsigned int lhs, unsigned int rhs),
101  unsigned int rhs);
102 
103 bool assertion(const char* file, uint16_t line, long lhs,
104  const char* opName, bool (*op)(long lhs, long rhs),
105  long rhs);
106 
107 bool assertion(const char* file, uint16_t line, unsigned long lhs,
108  const char* opName, bool (*op)(unsigned long lhs, unsigned long rhs),
109  unsigned long rhs);
110 
111 bool assertion(const char* file, uint16_t line, double lhs,
112  const char* opName, bool (*op)(double lhs, double rhs),
113  double rhs);
114 
115 bool assertion(const char* file, uint16_t line, const char* lhs,
116  const char* opName, bool (*op)(const char* lhs, const char* rhs),
117  const char* rhs);
118 
119 bool assertion(const char* file, uint16_t line, const char* lhs,
120  const char *opName, bool (*op)(const char* lhs, const String& rhs),
121  const String& rhs);
122 
123 bool assertion(const char* file, uint16_t line, const char* lhs,
124  const char *opName,
125  bool (*op)(const char* lhs, const __FlashStringHelper* rhs),
126  const __FlashStringHelper* rhs);
127 
128 bool assertion(const char* file, uint16_t line, const String& lhs,
129  const char *opName, bool (*op)(const String& lhs, const char* rhs),
130  const char* rhs);
131 
132 bool assertion(const char* file, uint16_t line, const String& lhs,
133  const char *opName, bool (*op)(const String& lhs, const String& rhs),
134  const String& rhs);
135 
136 bool assertion(const char* file, uint16_t line, const String& lhs,
137  const char *opName,
138  bool (*op)(const String& lhs, const __FlashStringHelper* rhs),
139  const __FlashStringHelper* rhs);
140 
141 bool assertion(const char* file, uint16_t line,
142  const __FlashStringHelper* lhs, const char *opName,
143  bool (*op)(const __FlashStringHelper* lhs, const char* rhs),
144  const char* rhs);
145 
146 bool assertion(const char* file, uint16_t line,
147  const __FlashStringHelper* lhs, const char *opName,
148  bool (*op)(const __FlashStringHelper* lhs, const String& rhs),
149  const String& rhs);
150 
151 bool assertion(const char* file, uint16_t line,
152  const __FlashStringHelper* lhs, const char *opName,
153  bool (*op)(const __FlashStringHelper* lhs, const __FlashStringHelper* rhs),
154  const __FlashStringHelper* rhs);
155 
156 }
157 
158 #endif