AUnit  1.2.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 
39 class FakePrint: public Print {
40  public:
47  static const uint8_t kBufSize = 8 * sizeof(long long) + 2 + 1;
48 
49  size_t write(uint8_t c) override {
50  if (mIndex < kBufSize - 1) {
51  mBuf[mIndex] = c;
52  mIndex++;
53  return 1;
54  } else {
55  return 0;
56  }
57  }
58 
59  size_t write(const uint8_t *buffer, size_t size) override {
60  if (buffer == nullptr) return 0;
61 
62  while (size > 0 && mIndex < kBufSize - 1) {
63  write(*buffer++);
64  size--;
65  }
66  return size;
67  }
68 
69 // ESP32 version of Print class does not define a virtual flush() method.
70 #ifdef ESP32
71  void flush() {
72  mIndex = 0;
73  }
74 #else
75  void flush() override {
76  mIndex = 0;
77  }
78 #endif
79 
85  const char* getBuffer() const {
86  mBuf[mIndex] = '\0';
87  return mBuf;
88  }
89 
90  private:
91  mutable char mBuf[kBufSize];
92  uint8_t mIndex = 0;
93 };
94 
95 }
96 }
97 
98 #endif
const char * getBuffer() const
Return the NUL terminated string buffer.
Definition: FakePrint.h:85
static const uint8_t kBufSize
Size of the internal buffer.
Definition: FakePrint.h:47
An implementation of Print that writes to an in-memory buffer.
Definition: FakePrint.h:39