Skip to content

Commit 52648a1

Browse files
authored
feat: Isolate/test debug interface (#50)
* fix: Refactor mock Stream * feat: Isolate/test NoteLog interface
1 parent 019b3ee commit 52648a1

13 files changed

+253
-50
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Blues Wireless Notecard
2-
version=1.3.5
2+
version=1.3.6
33
author=Blues Wireless
44
maintainer=Blues Wireless <[email protected]>
55
sentence=An easy to use Notecard Library for Arduino.

src/NoteI2c.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
#include <stddef.h>
55
#include <stdint.h>
66

7-
#include "Notecard.h"
8-
97
class NoteI2c {
108
public:
119
/**************************************************************************/

src/NoteI2c_Arduino.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include "NoteI2c.hpp"
55

6+
#include "Notecard.h"
7+
68
#ifndef MOCK
79
#include <Wire.h>
810
#else

src/NoteLog.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef NOTE_LOG_HPP
2+
#define NOTE_LOG_HPP
3+
4+
#include <stddef.h>
5+
6+
class NoteLog {
7+
public:
8+
/**************************************************************************/
9+
/*!
10+
@brief Type used to abstract specific hardware implementation types.
11+
*/
12+
/**************************************************************************/
13+
typedef void * channel_t;
14+
15+
virtual ~NoteLog(void) {}
16+
17+
/**************************************************************************/
18+
/*!
19+
@brief Writes a message to the Notecard debug port.
20+
@param message
21+
A null-terminated, message string.
22+
@return The number of bytes transmitted.
23+
*/
24+
/**************************************************************************/
25+
virtual size_t print(const char * message) = 0;
26+
};
27+
28+
/******************************************************************************/
29+
/*!
30+
@brief Helper function to abstract, create and maintain a single instance
31+
of the NoteLog interface implementation, as required by the underlying
32+
`note-c` library.
33+
@param[in] log_channel Pointer to the hardware specific serial stream
34+
implementation.
35+
*/
36+
/******************************************************************************/
37+
NoteLog * make_note_log (
38+
NoteLog::channel_t log_channel
39+
);
40+
41+
#endif // NOTE_LOG_HPP

src/NoteLog_Arduino.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "NoteLog_Arduino.hpp"
2+
3+
NoteLog *
4+
make_note_log (
5+
NoteLog::channel_t log_channel_
6+
)
7+
{
8+
static NoteLog * note_log = nullptr;
9+
if (note_log) {
10+
delete note_log;
11+
}
12+
note_log = new NoteLog_Arduino(reinterpret_cast<Stream *>(log_channel_));
13+
return note_log;
14+
}
15+
16+
NoteLog_Arduino::NoteLog_Arduino
17+
(
18+
Stream * log_stream_
19+
) :
20+
_notecardLog(log_stream_)
21+
{ }
22+
23+
size_t
24+
NoteLog_Arduino::print (
25+
const char * str_
26+
)
27+
{
28+
size_t result;
29+
30+
result = _notecardLog->print(str_);
31+
32+
return result;
33+
}

src/NoteLog_Arduino.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef NOTE_LOG_ARDUINO_HPP
2+
#define NOTE_LOG_ARDUINO_HPP
3+
4+
#include "NoteLog.hpp"
5+
6+
#ifndef MOCK
7+
#include <Arduino.h>
8+
#else
9+
#include "mock/mock-arduino.hpp"
10+
#endif
11+
12+
class NoteLog_Arduino final : public NoteLog
13+
{
14+
public:
15+
NoteLog_Arduino(Stream * log_stream_);
16+
size_t print(const char * message) override;
17+
18+
private:
19+
Stream * const _notecardLog;
20+
};
21+
22+
#endif // NOTE_LOG_ARDUINO_HPP

src/Notecard.cpp

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,28 @@
4242
#include <stdio.h>
4343
#include <stdlib.h>
4444

45+
#include "NoteLog.hpp"
4546
#include "NoteI2c.hpp"
4647
#include "NoteSerial.hpp"
4748

48-
Stream *Notecard::_debugSerial;
49-
bool Notecard::_debugSerialInitialized;
50-
5149
namespace
5250
{
51+
NoteLog *noteLog(nullptr);
52+
53+
size_t noteLogPrint(const char * str_)
54+
{
55+
size_t result;
56+
if (noteLog)
57+
{
58+
result = noteLog->print(str_);
59+
}
60+
else
61+
{
62+
result = 0;
63+
}
64+
return result;
65+
}
66+
5367
NoteI2c *noteI2c(nullptr);
5468

5569
const char *noteI2cReceive(uint16_t device_address_, uint8_t *buffer_, uint16_t size_, uint32_t *available_)
@@ -208,9 +222,8 @@ void Notecard::begin(HardwareSerial &selectedSerialPort, int selectedSpeed)
208222
/**************************************************************************/
209223
void Notecard::setDebugOutputStream(Stream &dbgserial)
210224
{
211-
_debugSerial = &dbgserial;
212-
_debugSerialInitialized = true;
213-
NoteSetFnDebugOutput(Notecard::debugSerialOutput);
225+
noteLog = make_note_log(&dbgserial);
226+
NoteSetFnDebugOutput(noteLogPrint);
214227
}
215228

216229
/**************************************************************************/
@@ -220,7 +233,6 @@ void Notecard::setDebugOutputStream(Stream &dbgserial)
220233
/**************************************************************************/
221234
void Notecard::clearDebugOutputStream()
222235
{
223-
_debugSerialInitialized = false;
224236
NoteSetFnDebugOutput(nullptr);
225237
}
226238

@@ -356,24 +368,3 @@ bool Notecard::responseError(J *rsp)
356368
{
357369
return NoteResponseError(rsp);
358370
}
359-
360-
/***************************************************************************
361-
PRIVATE FUNCTIONS
362-
***************************************************************************/
363-
364-
/**************************************************************************/
365-
/*!
366-
@brief Writes a message to the debug Serial stream.
367-
@param message
368-
The message to log.
369-
@return The number of bytes written.
370-
*/
371-
/**************************************************************************/
372-
size_t Notecard::debugSerialOutput(const char *message)
373-
{
374-
if (!_debugSerialInitialized)
375-
{
376-
return 0;
377-
}
378-
return (_debugSerial->print(message));
379-
}

src/Notecard.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
#ifndef MOCK
3131
#include <Arduino.h>
32-
#include <HardwareSerial.h>
3332
#include <Wire.h>
3433
#include <note-c/note.h>
3534
#else
@@ -135,12 +134,6 @@ class Notecard
135134
void logDebugf(const char *format, ...);
136135
bool debugSyncStatus(int pollFrequencyMs, int maxLevel);
137136
bool responseError(J *rsp);
138-
139-
private:
140-
static Stream *_debugSerial;
141-
static bool _debugSerialInitialized;
142-
143-
static size_t debugSerialOutput(const char *message);
144137
};
145138

146139
#endif

test/NoteLog_Arduino.test.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include "NoteLog_Arduino.hpp"
2+
#include "TestFunction.hpp"
3+
#include "mock/mock-arduino.hpp"
4+
#include "mock/mock-parameters.hpp"
5+
6+
#include <string.h>
7+
8+
// Compile command: g++ -Wall -Wextra -Wpedantic mock/mock-arduino.cpp ../src/NoteLog_Arduino.cpp NoteLog_Arduino.test.cpp -std=c++11 -I. -I../src -DMOCK && ./a.out || echo "Tests Result: $?"
9+
10+
int test_notelog_arduino_print_does_not_modify_str_parameter_value_before_passing_to_stream_print()
11+
{
12+
int result;
13+
14+
// Arrange
15+
NoteLog_Arduino notelog(&Serial);
16+
const char EXPECTED_RESULT[] = "Hello, Test!";
17+
18+
streamPrint_Parameters.reset();
19+
20+
// Action
21+
notelog.print(EXPECTED_RESULT);
22+
23+
// Assert
24+
if (!strcmp(EXPECTED_RESULT, streamPrint_Parameters.str_cache.c_str()))
25+
{
26+
result = 0;
27+
}
28+
else
29+
{
30+
result = static_cast<int>('d' + 'e' + 'b' + 'u' + 'g');
31+
std::cout << "FAILED] " << __FILE__ << ":" << __LINE__ << std::endl;
32+
std::cout << "\tstreamPrint_Parameters.str_cache.c_str() == \"" << streamPrint_Parameters.str_cache.c_str() << "\", EXPECTED: \"" << EXPECTED_RESULT << "\"" << std::endl;
33+
std::cout << "[";
34+
}
35+
36+
return result;
37+
}
38+
39+
int test_notelog_arduino_print_does_not_modify_stream_print_result_value_before_returning_to_caller()
40+
{
41+
int result;
42+
43+
// Arrange
44+
NoteLog_Arduino notelog(&Serial);
45+
const size_t EXPECTED_RESULT = 13;
46+
47+
streamPrint_Parameters.reset();
48+
streamPrint_Parameters.result = EXPECTED_RESULT;
49+
50+
// Action
51+
const size_t ACTUAL_RESULT = notelog.print("Hello, Test!");
52+
53+
// Assert
54+
if (ACTUAL_RESULT == EXPECTED_RESULT)
55+
{
56+
result = 0;
57+
}
58+
else
59+
{
60+
result = static_cast<int>('d' + 'e' + 'b' + 'u' + 'g');
61+
std::cout << "FAILED] " << __FILE__ << ":" << __LINE__ << std::endl;
62+
std::cout << "\tnotelog.print(\"Hello, Test!\") == " << ACTUAL_RESULT << ", EXPECTED: " << EXPECTED_RESULT << std::endl;
63+
std::cout << "[";
64+
}
65+
66+
return result;
67+
}
68+
69+
int main(void)
70+
{
71+
TestFunction tests[] = {
72+
{test_notelog_arduino_print_does_not_modify_str_parameter_value_before_passing_to_stream_print, "test_notelog_arduino_print_does_not_modify_buffer_parameter_value_before_passing_to_stream_print"},
73+
{test_notelog_arduino_print_does_not_modify_stream_print_result_value_before_returning_to_caller, "test_notelog_arduino_print_does_not_modify_stream_print_result_value_before_returning_to_caller"},
74+
};
75+
76+
return TestFunction::runTests(tests, (sizeof(tests) / sizeof(TestFunction)));
77+
}

test/Notecard.test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ int test_notecard_setDebugOutputStream_shares_a_debug_log_functon_pointer()
586586
noteSetFnDebugOutput_Parameters.reset();
587587

588588
// Action
589-
notecard.setDebugOutputStream(dbgserial);
589+
notecard.setDebugOutputStream(Serial);
590590

591591
// Assert
592592
if (noteSetFnDebugOutput_Parameters.fn)

test/mock/mock-arduino.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ HardwareSerialBegin_Parameters hardwareSerialBegin_Parameters;
55
HardwareSerialFlush_Parameters hardwareSerialFlush_Parameters;
66
HardwareSerialRead_Parameters hardwareSerialRead_Parameters;
77
HardwareSerialWrite_Parameters hardwareSerialWrite_Parameters;
8+
StreamPrint_Parameters streamPrint_Parameters;
89
TwoWireBegin_Parameters twoWireBegin_Parameters;
910
TwoWireBeginTransmission_Parameters twoWireBeginTransmission_Parameters;
1011
TwoWireEnd_Parameters twoWireEnd_Parameters;
@@ -17,7 +18,6 @@ TwoWireWriteBuffer_Parameters twoWireWriteBuffer_Parameters;
1718
// Global Arduino Objects (Singletons)
1819
HardwareSerial Serial;
1920
TwoWire Wire;
20-
Stream dbgserial;
2121

2222
void
2323
delay (
@@ -91,11 +91,19 @@ HardwareSerial::write (
9191
return hardwareSerialWrite_Parameters.result;
9292
}
9393

94-
long unsigned int
94+
size_t
9595
Stream::print (
96-
const char *
96+
const char * str
9797
) {
98-
return 0;
98+
// Record invocation(s)
99+
++streamPrint_Parameters.invoked;
100+
101+
// Stash parameter(s)
102+
streamPrint_Parameters.str = str;
103+
streamPrint_Parameters.str_cache = str;
104+
105+
// Return user-supplied result
106+
return streamPrint_Parameters.result;
99107
}
100108

101109
void

0 commit comments

Comments
 (0)