Skip to content

Commit 28b0525

Browse files
author
James Foster
committed
WIP: runs unit tests.
1 parent 5351b02 commit 28b0525

17 files changed

+163
-12
lines changed
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
unittest:
2+
platforms:
3+
- mega2560
4+
# libraries:
5+
# - "Ethernet"
6+
7+
compile:
8+
platforms:
9+
- mega2560
10+
# libraries:
11+
# - "Ethernet"

SampleProjects/NetworkLib/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.bundle

SampleProjects/NetworkLib/Gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
source 'https://rubygems.org'
2+
gem 'arduino_ci', path: '../../'

SampleProjects/NetworkLib/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# NetworkLib
2+
3+
This is an example of a library that depends on Ethernet.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=Ethernet
2+
version=0.1.0
3+
author=James Foster <[email protected]>
4+
maintainer=James Foster <[email protected]>
5+
sentence=Sample Ethernet library to validate Client/Server mocks
6+
paragraph=Sample Ethernet library to validate Client/Server mocks
7+
category=Other
8+
url=https://github.com/Arduino-CI/arduino_ci/SampleProjects/Ethernet
9+
architectures=avr,esp8266
10+
includes=NetworkLib.h
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
cd "$( dirname "${BASH_SOURCE[0]}" )"
4+
cd ../src
5+
rm -rf Ethernet > /dev/null 2>&1
6+
# get Ethernet library
7+
git clone https://github.com/arduino-libraries/Ethernet.git
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "Ethernet.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
#include <Arduino.h>
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
cd SampleProjects/Ethernet
3+
bundle config --local path vendor/bundle
4+
bundle install
5+
bundle exec arduino_ci_remote.rb --skip-compilation
6+
# bundle exec arduino_ci_remote.rb --skip-examples-compilation
7+
*/
8+
9+
#include <ArduinoUnitTests.h>
10+
#include <Arduino.h>
11+
12+
unittest(test) {
13+
assertTrue(true);
14+
}
15+
16+
unittest_main()

cpp/arduino/Arduino.h

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Where possible, variable names from the Arduino library are used to avoid confli
99

1010
#include "ArduinoDefines.h"
1111

12+
#include "IPAddress.h"
1213
#include "WCharacter.h"
1314
#include "WString.h"
1415
#include "Print.h"

cpp/arduino/Client.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#include <Stream.h>
4+
5+
// https://github.com/arduino-libraries/Ethernet/blob/master/src/utility/w5100.h:341
6+
extern volatile uint8_t mmapPorts[MOCK_PINS_COUNT];
7+
#define portOutputRegister(port) (uint8_t *) (&mmapPorts[port])
8+
#define digitalPinToPort(pin) (pin)
9+
#define digitalPinToBitMask(pin) (1)
10+
11+
class Client : public Stream {
12+
protected:
13+
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
14+
};

cpp/arduino/Godmode.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,6 @@ SPIClass SPI = SPIClass(&GODMODE()->spi.dataIn, &GODMODE()->spi.dataOut);
113113

114114
// defined in Wire.h
115115
TwoWire Wire = TwoWire();
116+
117+
// Memory-mapped ports defined in Client.h
118+
uint8_t volatile mmapPorts[MOCK_PINS_COUNT];

cpp/arduino/IPAddress.h

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#pragma once
2+
3+
#include <stdint.h>
4+
5+
class IPAddress {
6+
private:
7+
union {
8+
uint8_t bytes[4];
9+
uint32_t dword;
10+
operator uint8_t* () const { return (uint8_t*) bytes; }
11+
} _address;
12+
13+
public:
14+
// Constructors
15+
IPAddress() { IPAddress(0, 0, 0, 0); }
16+
IPAddress(uint8_t octet1, uint8_t octet2, uint8_t octet3, uint8_t octet4) {
17+
_address.bytes[0] = octet1;
18+
_address.bytes[1] = octet2;
19+
_address.bytes[2] = octet3;
20+
_address.bytes[3] = octet4;
21+
}
22+
IPAddress(uint32_t dword) { _address.dword = dword; }
23+
IPAddress(const uint8_t bytes[]) {
24+
_address.bytes[0] = bytes[0];
25+
_address.bytes[1] = bytes[1];
26+
_address.bytes[2] = bytes[2];
27+
_address.bytes[3] = bytes[3];
28+
}
29+
30+
IPAddress(unsigned long dword) { _address.dword = (uint32_t) dword; }
31+
32+
// Accessors
33+
uint32_t asWord() const { return _address.dword; }
34+
uint8_t *raw_address() { return _address.bytes; };
35+
36+
// Comparisons
37+
bool operator==(const IPAddress &rhs) const {
38+
return _address.dword == rhs.asWord();
39+
}
40+
41+
bool operator!=(const IPAddress &rhs) const {
42+
return _address.dword != rhs.asWord();
43+
}
44+
45+
// Indexing
46+
uint8_t operator[](int index) const { return _address.bytes[index]; }
47+
uint8_t &operator[](int index) { return _address.bytes[index]; }
48+
49+
// Conversions
50+
operator uint32_t() const { return _address.dword; };
51+
52+
friend class EthernetClass;
53+
friend class UDP;
54+
friend class Client;
55+
friend class Server;
56+
friend class DhcpClass;
57+
friend class DNSClient;
58+
};
59+
60+
const IPAddress INADDR_NONE(0, 0, 0, 0);

cpp/arduino/Print.h

+9-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <stdio.h>
44
#include <avr/pgmspace.h>
5+
6+
#include "Printable.h"
57
#include "WString.h"
68

79
#define DEC 10
@@ -12,22 +14,17 @@
1214
#endif
1315
#define BIN 2
1416

15-
class Print;
16-
17-
class Printable
18-
{
19-
public:
20-
virtual size_t printTo(Print& p) const = 0;
21-
};
22-
2317
class Print
2418
{
19+
private:
20+
int write_error;
21+
protected:
22+
void setWriteError(int err = 1) { write_error = err; }
2523
public:
26-
Print() {}
24+
Print() : write_error(0) {}
2725

28-
// Arduino's version of this is richer but until I see an actual error case I'm not sure how to mock
29-
int getWriteError() { return 0; }
30-
void clearWriteError() { }
26+
int getWriteError() { return write_error; }
27+
void clearWriteError() { setWriteError(0); }
3128
virtual int availableForWrite() { return 0; }
3229

3330
virtual size_t write(uint8_t) = 0;

cpp/arduino/Printable.h

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
class Print;
4+
5+
class Printable {
6+
public:
7+
virtual size_t printTo(Print &p) const = 0;
8+
};

cpp/arduino/Server.h

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#pragma once
2+
3+
#include <Stream.h>
4+
5+
class Server : public Print {};

cpp/arduino/Udp.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
#include <Stream.h>
4+
#include <IPAddress.h>
5+
6+
class UDP : public Stream {
7+
protected:
8+
uint8_t *rawIPAddress(IPAddress &addr) { return addr.raw_address(); };
9+
};

0 commit comments

Comments
 (0)