Skip to content

Commit

Permalink
Include mocks and use standard make recipe for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rubberduck203 committed Oct 11, 2017
1 parent 596d0c6 commit 45eadea
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ Running `make` will build and run the tests, then, if successful, compile, link,
- upload: Use avrdude to upload the hex to the microcontroller
- install: standard target alias for upload

## Mocks

Mocks are stored in the `test/mocks` directory and made available as system includes in the test builds.
This allows you to reference `<avr/io.h>` and `<util/delay.h>` in your code under test without referencing the real AVR include files, which is problematic as the avr-asm has some instructions that are unavailable to the regular gcc compiler.

The `PORT_t` definition in `<avr/io.h>` should be updated according to your particular target device.
The real headers for these are available in the avr-gcc installation directory under `avr/include/avr`.

More mocks may be added in this directory as needed. The test makefile will automatically put them on the path as system (angle bracket) includes.

## License

The template and makefiles are licensed under the [MIT License](LICENSE) so you're free to include the template in your own projects. The MIT License is used in order to limit the licensing restrictions that other licenses would impose on your project. I want you to be free to use this in your own work without worrying about the implications of viral licenses. Just give credit where credit is due and pay it forward.
6 changes: 4 additions & 2 deletions test/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ CXXFLAGS += -include CppUTest/MemoryLeakDetectorNewMacros.h
CFLAGS += -include CppUTest/MemoryLeakDetectorMallocMacros.h
LD_LIBRARIES = $(shell pkg-config --libs cpputest)

CPPFLAGS += -Imocks

OBJ = obj
BIN = bin
SRC = ../src
Expand All @@ -38,12 +40,12 @@ all: check
### Compile code under test
$(OBJ)/%.o: $(SRC)/%.c
@mkdir -p $(OBJ)
gcc -Wall -c $(CFLAGS) $^ -o $@
gcc -Wall -c $(CFLAGS) $(CPPFLAGS) $^ -o $@

### Create test runner executable
$(BIN)/AllTests: $(objects) $(testSource)
@mkdir -p $(BIN)
g++ -Wall $(CPPFLAGS) $^ -o $@ $(LD_LIBRARIES)
g++ -Wall $(CXXFLAGS) $(CPPFLAGS) $^ -o $@ $(LD_LIBRARIES)

.PHONY: check
check: $(BIN)/AllTests
Expand Down
40 changes: 40 additions & 0 deletions test/mocks/avr/io.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef _AVR_IO_H_
#define _AVR_IO_H_

#include <stdint.h>

typedef volatile uint8_t register8_t;

/* I/O Port: Define mock as appropriate for your device */
typedef struct PORT_struct
{
register8_t DIR; /* I/O Port Data Direction */
register8_t DIRSET; /* I/O Port Data Direction Set */
register8_t DIRCLR; /* I/O Port Data Direction Clear */
register8_t DIRTGL; /* I/O Port Data Direction Toggle */
register8_t OUT; /* I/O Port Output */
register8_t OUTSET; /* I/O Port Output Set */
register8_t OUTCLR; /* I/O Port Output Clear */
register8_t OUTTGL; /* I/O Port Output Toggle */
register8_t IN; /* I/O port Input */
register8_t INTCTRL; /* Interrupt Control Register */
register8_t INT0MASK; /* Port Interrupt 0 Mask */
register8_t INT1MASK; /* Port Interrupt 1 Mask */
register8_t INTFLAGS; /* Interrupt Flag Register */
register8_t reserved_0x0D;
register8_t reserved_0x0E;
register8_t reserved_0x0F;
register8_t PIN0CTRL; /* Pin 0 Control Register */
register8_t PIN1CTRL; /* Pin 1 Control Register */
register8_t PIN2CTRL; /* Pin 2 Control Register */
register8_t PIN3CTRL; /* Pin 3 Control Register */
register8_t PIN4CTRL; /* Pin 4 Control Register */
register8_t PIN5CTRL; /* Pin 5 Control Register */
register8_t PIN6CTRL; /* Pin 6 Control Register */
register8_t PIN7CTRL; /* Pin 7 Control Register */
} PORT_t;

extern PORT_t FakePort;
#define PORTE (*(PORT_t *) &FakePort)

#endif
1 change: 1 addition & 0 deletions test/mocks/util/delay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void _delay_ms(double __ms) {}

0 comments on commit 45eadea

Please sign in to comment.