support Unity testing

This commit is contained in:
proddy
2024-08-01 22:13:52 +02:00
parent 4ec5739b67
commit b38ec2f25e
16 changed files with 598 additions and 285 deletions

View File

@@ -1583,11 +1583,16 @@ EMSESP::EMSESP()
// start all the core services
// the services must be loaded in the correct order
void EMSESP::start() {
// don't need shell if running unit tests
#if !defined(UNITY_INCLUDE_CONFIG_H)
serial_console_.begin(SERIAL_CONSOLE_BAUD_RATE);
shell_ = std::make_shared<EMSESPConsole>(*this, serial_console_, true);
shell_->maximum_log_messages(100);
shell_->start();
#if defined(EMSESP_DEBUG)
shell_->log_level(uuid::log::Level::DEBUG);
#else
@@ -1598,6 +1603,8 @@ void EMSESP::start() {
shell_->add_flags(CommandFlags::ADMIN); // always start in su/admin mode when running tests
#endif
#endif
// start the file system
#ifndef EMSESP_STANDALONE
if (!LittleFS.begin(true)) {

View File

@@ -265,6 +265,8 @@ bool Test::test(const std::string & cmd, int8_t id1, int8_t id2) {
// These next tests are run from the Consol via the test command, so inherit the Shell
void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const std::string & id1_s, const std::string & id2_s) {
bool ok = false; // default tests fail
shell.add_flags(CommandFlags::ADMIN); // switch to su
// init stuff
@@ -297,8 +299,6 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const
id2 = Helpers::atoint(id2_s.c_str());
}
bool ok = false;
// e.g. "test add 0x10 172"
if (command == "add") {
if (id1 == -1 || id2 == -1) {
@@ -638,11 +638,11 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const
Serial.print(COLOR_BRIGHT_MAGENTA);
serializeJson(doc, Serial);
Serial.print(COLOR_RESET);
Serial.println();
Serial.print(" measureMsgPack=");
Serial.print(measureMsgPack(doc));
Serial.print(" measureJson=");
Serial.print(measureJson(doc));
// Serial.println();
// Serial.print(" measureMsgPack=");
// Serial.print(measureMsgPack(doc));
// Serial.print(" measureJson=");
// Serial.print(measureJson(doc));
Serial.println(" **");
}
}

View File

@@ -64,7 +64,7 @@ void WebAPIService::webAPIService(AsyncWebServerRequest * request, JsonVariant j
}
#ifdef EMSESP_TEST
// for test.cpp so we can invoke GETs to test the API
// for test.cpp and unit tests so we can invoke GETs to test the API
void WebAPIService::webAPIService(AsyncWebServerRequest * request) {
JsonDocument input_doc;
parse(request, input_doc.to<JsonObject>());
@@ -134,6 +134,10 @@ void WebAPIService::parse(AsyncWebServerRequest * request, JsonObject input) {
const char * api_data = output["api_data"];
if (api_data) {
request->send(200, "text/plain; charset=utf-8", api_data);
#if defined(EMSESP_TEST)
// store the result so we can test with Unity later
storeResponse(output);
#endif
#if defined(EMSESP_STANDALONE)
Serial.printf("%sweb output: %s[%s] %s(200)%s ", COLOR_WHITE, COLOR_BRIGHT_CYAN, request->url().c_str(), COLOR_BRIGHT_GREEN, COLOR_MAGENTA);
serializeJson(output, Serial);
@@ -157,6 +161,10 @@ void WebAPIService::parse(AsyncWebServerRequest * request, JsonObject input) {
request->send(response);
api_count_++;
#if defined(EMSESP_TEST)
// store the result so we can test with Unity later
storeResponse(output);
#endif
#if defined(EMSESP_STANDALONE)
Serial.printf("%sweb output: %s[%s]", COLOR_WHITE, COLOR_BRIGHT_CYAN, request->url().c_str());
Serial.printf(" %s(%d)%s ", ret_codes[return_code] == 200 ? COLOR_BRIGHT_GREEN : COLOR_BRIGHT_RED, ret_codes[return_code], COLOR_YELLOW);
@@ -221,4 +229,19 @@ void WebAPIService::getEntities(AsyncWebServerRequest * request) {
request->send(response);
}
#if defined(EMSESP_TEST)
// store the result so we can test with Unity later
static JsonDocument storeResponseDoc_;
void WebAPIService::storeResponse(JsonObject response) {
storeResponseDoc_.clear(); // clear it, so can only recall once
storeResponseDoc_.add(response); // add the object to our doc
}
const char * WebAPIService::getResponse() {
static std::string buffer;
serializeJson(storeResponseDoc_, buffer);
return buffer.c_str();
}
#endif
} // namespace emsesp

View File

@@ -34,9 +34,11 @@ class WebAPIService {
void webAPIService(AsyncWebServerRequest * request, JsonVariant json);
#ifdef EMSESP_TEST
// for test.cpp
void webAPIService(AsyncWebServerRequest * request);
#if defined(EMSESP_TEST)
// for test.cpp and running unit tests
void webAPIService(AsyncWebServerRequest * request);
void storeResponse(JsonObject response);
const char * getResponse();
#endif
static uint32_t api_count() {