Skip to content

Commit

Permalink
#109: modify temp tests & add test macros
Browse files Browse the repository at this point in the history
  • Loading branch information
jack5661 authored and tarek20501 committed Aug 7, 2021
1 parent 9c1abab commit 9a54b8f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 29 deletions.
40 changes: 15 additions & 25 deletions Particle/src/tests/TempTests.cpp
Original file line number Diff line number Diff line change
@@ -1,36 +1,26 @@
#include "TempTests.h"

static Temperature *temp = NULL;
Temperature temp(TEMP_PIN, digital, "Temperature", "Celsius", 't');

void initTemperature()
{
temp = new Temperature(TEMP_PIN, digital, "Temperature", "Celsius", 't');
}

void cleanUp_Temperature()
bool test_Temp_read()
{
if (temp == NULL)
return;
delete temp;
temp = NULL;
}
String read = temp.read();
String err = "0.00,";
String result = "Temperature: " + read;
Serial.println(result);
if (err == read)
TEST_FAIL_MESSAGE("Temperature Reading Failed");

void test_Temp_read()
{
if (temp == NULL)
TEST_FAIL_MESSAGE("Temp Sensor did not initialize properly");

String read = temp->read();
String err = "nan,";
String print = "Temperature: " + read;
Serial.println(print);
if (read == err)
TEST_FAIL_MESSAGE("Cannot read from Temperature Sensor");
return true;
}

void testTemp()
{
initTemperature();
test_Temp_read();
cleanUp_Temperature();
String tag = "Temperature Sensor";
printTestStart(tag);

RUN_TEST(test_Temp_read);

printTestEnd(tag);
}
9 changes: 5 additions & 4 deletions Particle/src/tests/TempTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#include "PinConfig.h"
#include "tests.h"

void initTemperature();
void cleanUp_Temperature();
void test_Temp_read();
void testTemp();
bool initTemperature();
bool cleanUp_Temperature();
bool test_Temp_read();

void testTemp();
11 changes: 11 additions & 0 deletions Particle/src/tests/tests.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
#include "tests.h"
#include "GSMTests.h"
#include "TempTests.h"

void tests() {
Serial.println("Hello Unit Tests");
// Test that extern objects are available
Serial.printlnf("%s", rtc.getTimeStamp().c_str());
#if EN_GSM
testGsm();
#endif
#if TEMPERATURE
testTemp();
#endif
Serial.println("----- TESTING COMPLETE -----");
}

void run_test(bool (*test)(), char *test_name)
{
Serial.printf("UNIT TEST: %s\n", test_name);
if (test())
Serial.printf("%s [PASSED]\n", test_name);
}

void printTestStart(String tag) {
Serial.printlnf("----- START OF %s TEST -----", tag.c_str());
}
Expand Down
9 changes: 9 additions & 0 deletions Particle/src/tests/tests.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#include "PinConfig.h"
#include <Arduino.h>


#define RUN_TEST(test_func) run_test(test_func, #test_func)
#define TEST_FAIL_MESSAGE(msg) return (Serial.printf("TEST FAILED: %s\n", msg), false)

#define TEST_ASSERT_NULL(ptr) if (ptr != NULL) TEST_FAIL_MESSAGE("Pointer not NULL")
#define TEST_ASSERT_EQUAL_STRING(str1, str2) if (strcmp(str1, str2)) TEST_FAIL_MESSAGE("Strings not equal: " str1 "!=" str2)
#define TEST_ASSERT_TRUE(cond) if (!cond) TEST_FAIL_MESSAGE(#cond " IS FALSE")

#if EN_GSM
#include "Gsm.h"
extern Gsm gsm;
Expand Down Expand Up @@ -35,6 +43,7 @@ extern SdCard memory;
#endif

void tests();
void run_test(bool (*test)(), char *test_name);
void printTestStart(String tag);
void printTestEnd(String tag);
void printTestFailed(String tag);
Expand Down

0 comments on commit 9a54b8f

Please sign in to comment.