Reputation: 3
I have been playing around with a small Arduino robot I purchased and found that there are several bugs running around in the code (that I may have placed there haha) and wanted to apply gtest to the code to locate the bug(s). The issue is #include "Arduino.h" is not something that I can run on my PC. And it is something I am having difficulty decoupling from my code.
I would like to build a test executable in my test folder that can run on my PC but still keep the majority of the functional code in the directory above so the Arduino IDE can build as well. Any help would be greatly appreciated.
Structure:
<Folder>
-ArduinoBuild.ino
-Class.h
-Class.cpp
-<test folder>
--test.cpp
--CMakeList.txt
This is where the problem begins, Arduino.h is introduced to the build
Class.h
#ifndef CLASS_H_
#define CLASS_H_
#include "Arduino.h"
class Class{
public:
int Some_Attribute = 0;
void member_function();
};
#endif
Class.cpp
#include "Class.h"
void Class::member_function(){
Some_Attribute = Some_ArduinoFunc(this->Some_Attribute);
}
Now, if I could get Arduino.h out of my build I could place dummy functions in my test script to cover the gap, but I would like to keep Arduino.h somewhere in the build process so that way I don't have to go adding it in every time I want to build for my Arduino. I have tried several things, one of them being moving the #include "Arduino.h" to the .ino file in the hopes that it would get flowed down to the Class.h/cpp file when building for Arduino, and that failed.
test.cpp
// Class_test.cpp
#include <gtest/gtest.h>
#include "../Class.cpp"
TEST(buildbin, member_function) {
Class car;
car.member_function(1);
ASSERT_EQ(1, car.Some_Attribute);
}
Again, any help with this would be greatly appreciated, thank you for your time.
POST SOLUTION Updates: Here is a sample header to what I had to enter into the scripts in the parent folder:
#ifndef ARDUINO
#include<cmath>
#define floor std::round
void delay(int i){}
#endif
and to access files to mimic library files all you need to do is:
#include "test/FakeLib.XX"
Upvotes: 0
Views: 1235
Reputation: 58848
Use a macro.
You can selectively include code using #ifdef
... #endif
. Example:
#ifdef ARDUINO_VERSION
#include "Arduino.h"
#endif
void setup() {
// no setup
}
void loop() {
#ifdef ARDUINO_VERSION
digitalWrite(1, HIGH);
#else
printf("Setting pin 1 high\n");
#endif
}
There's also #ifndef
which does the opposite, it only includes the code if ARDUINO_VERSION isn't defined.
Now you're probably asking, how does the compiler know whether it's the ARDUINO_VERSION or not? Good question. Based on this thread it looks like Arduino will define ARDUINO
for you, so you can use #ifdef ARDUINO
. Alternatively, since you should have more control over how tests are compiled, you could use #ifdef TEST_VERSION
and then compile your tests with the -DTEST_VERSION
option.
Upvotes: 1