AUnit  1.7.1
Unit testing framework for Arduino platforms inspired by ArduinoUnit and Google Test.
FakePrint.h
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 #ifndef AUNIT_FAKE_PRINT_H
26 #define AUNIT_FAKE_PRINT_H
27 
28 #include <stddef.h> // size_t
29 #include <Print.h>
30 
31 namespace aunit {
32 namespace fake {
33 
60 class FakePrint: public Print {
61  public:
68  static const uint8_t kBufSize = 8 * sizeof(long long) + 2 + 1;
69 
70  size_t write(uint8_t c) override {
71  if (mIndex < kBufSize - 1) {
72  mBuf[mIndex] = c;
73  mIndex++;
74  return 1;
75  } else {
76  return 0;
77  }
78  }
79 
80  size_t write(const uint8_t *buffer, size_t size) override {
81  if (buffer == nullptr) return 0;
82 
83  while (size > 0 && mIndex < kBufSize - 1) {
84  write(*buffer++);
85  size--;
86  }
87  return size;
88  }
89 
90 // ESP32 and STM32duino do not provide a virtual Print::flush() method.
91 #if defined(ESP32) || defined(ARDUINO_ARCH_STM32)
92  void flush() {
93 #else
94  void flush() override {
95 #endif
96  mIndex = 0;
97  }
98 
104  const char* getBuffer() const {
105  mBuf[mIndex] = '\0';
106  return mBuf;
107  }
108 
109  private:
110  mutable char mBuf[kBufSize];
111  uint8_t mIndex = 0;
112 };
113 
114 }
115 }
116 
117 #endif
An implementation of Print that writes to an in-memory buffer.
Definition: FakePrint.h:60
static const uint8_t kBufSize
Size of the internal buffer.
Definition: FakePrint.h:68
const char * getBuffer() const
Return the NUL terminated string buffer.
Definition: FakePrint.h:104