forked from Arduino-CI/arduino_ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeviceusingbytes.cpp
94 lines (75 loc) · 2.32 KB
/
deviceusingbytes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <Arduino.h>
#include <ArduinoUnitTests.h>
#include <SoftwareSerial.h>
#include <ci/DeviceUsingBytes.h>
// DeviceUsingBytes extends DataStreamObserver,
// so we will be able to attach this class to an
// ObservableDataStream object, of which the pin
// history (soft-serial) and HardwareSerial
// objects are.
class FakeHayesModem : public DeviceUsingBytes {
public:
String mLast;
bool mMatchedInput;
FakeHayesModem() : DeviceUsingBytes() {
mLast = "";
mMatchedInput = false;
addResponseLine("AT", "OK");
addResponseLine("ATV1", "NO CARRIER");
}
virtual ~FakeHayesModem() {}
virtual void onMatchInput(String output) {
mLast = output;
mMatchedInput = true;
}
};
GodmodeState* state = GODMODE();
unittest_setup()
{
state->reset();
}
unittest(modem_hardware)
{
String cmd = "AT\n";
FakeHayesModem m;
m.attach(&Serial);
assertEqual(0, Serial.available());
assertFalse(m.mMatchedInput);
assertEqual("", m.mMessage);
for (int i = 0; i < cmd.length(); ++i) {
assertEqual(i, m.mMessage.length()); // before we write, length should equal i
Serial.write(cmd[i]);
}
assertEqual(0, m.mMessage.length()); // should have matched and reset
assertEqual("", state->serialPort[0].dataIn);
assertEqual("AT\n", state->serialPort[0].dataOut);
assureTrue(m.mMatchedInput);
//assertEqual(3, Serial.available());
assertEqual("OK\n", m.mLast);
}
unittest(modem_software)
{
bool bigEndian = false;
bool flipLogic = false;
SoftwareSerial ss(1, 2, flipLogic);
ss.listen();
String cmd = "AT\n";
FakeHayesModem m;
m.attach(&state->digitalPin[2]);
assertEqual(0, ss.available());
assertFalse(m.mMatchedInput);
assertEqual("", m.mMessage);
for (int i = 0; i < cmd.length(); ++i) {
assertEqual(i, m.mMessage.length()); // before we write, length should equal i
assertEqual(cmd.substr(0, i), state->digitalPin[2].toAscii(1, bigEndian));
assertEqual(cmd.substr(0, i), m.mMessage);
ss.write(cmd[i]);
}
assertEqual(0, m.mMessage.length()); // should have matched and reset
assertEqual("", state->digitalPin[1].incomingToAscii(1, bigEndian));
assertEqual("AT\n", state->digitalPin[2].toAscii(1, bigEndian));
assureTrue(m.mMatchedInput);
//assertEqual(3, Serial.available());
assertEqual("OK\n", m.mLast);
}
unittest_main()