3 This directory contains fake versions of some Arduino classes for the purposes
4 of unit testing. These header files are *not* included automatically by the
5 `#include <AUnit.h>` preprocessor directive. The various fake classes must be
6 included manually just after the `<AUnit.h>` is included. The fake classes
7 live in the `aunit::fake` namespace.
11 The `FakePrint` class is an implementation of the
12 [Print](https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/Print.h)
13 class which is the base class of the
14 [HardwareSerial](https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/HardwareSerial.h)
15 and other output classes. The `Serial` global object is an instance of the
16 `HardwareSerial` class. If a user-defined class or method is defined to use an
17 instance of the `Print` object, instead of hardcoding a dependency to the
18 `Serial` instance, then an instance of the `FakePrint` class can be substituted
19 and used to write unit tests for the user-defined class or method.
21 Let's say the user-defined class is called `Greeter` and a method called
22 `greet()` prints something out to `Serial`:
27 Greeter(Print& printer):
31 mPrinter.print("hello ");
33 mPrinter.println(" world(s)");
41 If we want to verify that the `Greeter::greet()` method prints what is expected,
42 we inject an instance of `FakePrint` (instead of `Serial`) in a test called
46 #line 2 "GreeterTest.ino"
49 #include <aunit/fake/FakePrint.h>
51 using namespace aunit;
52 using namespace aunit::fake;
54 test(GreeterTest, greet) {
56 Greeter greeter(fakePrint);
59 assertEqual("hello 1 world(s)\r\n", fakePrint.getBuffer());
63 assertEqual("hello 2 world(s)\r\n", fakePrint.getBuffer());
70 while (! Serial); // Wait until Serial is ready - Leonardo/Micro