Skip to content

Commit

Permalink
Rewored the thermostat tests
Browse files Browse the repository at this point in the history
  • Loading branch information
whatisbyandby committed Jul 6, 2024
1 parent 3bb09d3 commit b2fd40f
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 174 deletions.
1 change: 0 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ int main()
context.repl = &repl;
context.thermostat = &thermostat;


ThermostatError err = context.initialize();


Expand Down
18 changes: 0 additions & 18 deletions src/subscriber/subscriber.cpp

This file was deleted.

20 changes: 0 additions & 20 deletions src/subscriber/subscriber.hpp

This file was deleted.

1 change: 0 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ add_subdirectory(logging)
add_subdirectory(json)
add_subdirectory(repl)
add_subdirectory(producer)
add_subdirectory(subscriber)
add_subdirectory(wifi)
4 changes: 3 additions & 1 deletion test/mocks/mock_mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ ThermostatError Mqtt::publish(const char *topic, char *message)
.returnIntValueOrDefault(THERMOSTAT_OK);
}

ThermostatError Mqtt::subscribe(const char *topic)
ThermostatError Mqtt::subscribe(const char *topic, CommandCallback callback, void *arg)
{
return (ThermostatError) mock()
.actualCall("Mqtt::subscribe")
.withStringParameter("topic", topic)
.withPointerParameter("callback", (void *)(ThermostatCommand*(*)(ThermostatCommand*, void *))callback)
.withPointerParameter("arg", arg)
.returnIntValueOrDefault(THERMOSTAT_OK);
}
54 changes: 0 additions & 54 deletions test/subscriber/CMakeLists.txt

This file was deleted.

36 changes: 0 additions & 36 deletions test/subscriber/test_subscriber.cpp

This file was deleted.

23 changes: 0 additions & 23 deletions test/subscriber/test_subscriber_main.cpp

This file was deleted.

20 changes: 9 additions & 11 deletions test/thermostat/test_thermostat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@
#include <iostream>

static Thermostat *thermostat;
static ThermostatContext *context;
static TemperatureController *tempController;
static EnvironmentSensor *sensor;
static Hvac *hvac;

TEST_GROUP(ThermostatTestGroup){
void setup(){
thermostat = new Thermostat();
context = new ThermostatContext();
tempController = new TemperatureController();
sensor = new EnvironmentSensor();
hvac = new Hvac();

context->sensor = sensor;
context->tempController = tempController;

thermostat->initialize(context);
thermostat->initialize(sensor, tempController, hvac);


}
Expand All @@ -32,9 +30,9 @@ void teardown()
mock().checkExpectations();
mock().clear();
delete thermostat;
delete context;
delete tempController;
delete sensor;
delete hvac;

}
}
Expand Down Expand Up @@ -178,7 +176,7 @@ TEST(ThermostatTestGroup, UpdateThermostat_ExpectHeaterOn_UnderTemp)
{

thermostat->setMode(HEAT);
context->tempController->setTargetTemperature(25.0);
tempController->setTargetTemperature(25.0);

double temperature = 20.0;
double humidity = 50.0;
Expand All @@ -205,7 +203,7 @@ TEST(ThermostatTestGroup, UpdateThermostat_ExpectHeaterOn_InRange)
{

thermostat->setMode(HEAT);
context->tempController->setTargetTemperature(20);
tempController->setTargetTemperature(20);

double temperature = 19.9;
double humidity = 50.0;
Expand All @@ -232,7 +230,7 @@ TEST(ThermostatTestGroup, UpdateThermostat_ExpectAllOff_OverTemp)
{

thermostat->setMode(HEAT);
context->tempController->setTargetTemperature(20);
tempController->setTargetTemperature(20);

double temperature = 20.1;
double humidity = 50.0;
Expand Down Expand Up @@ -261,8 +259,8 @@ TEST(ThermostatTestGroup, GetState)
thermostat->setTemperatureUnits(FAHRENHEIT);
thermostat->setTargetTemperature(68.0);

context->tempController->setTargetTemperature(20);
context->tempController->setTemperatureRange(1.0);
tempController->setTargetTemperature(20);
tempController->setTemperatureRange(1.0);

mock().expectOneCall("HVAC::getCurrentState").andReturnValue(HEATING);

Expand Down
6 changes: 1 addition & 5 deletions test/thermostat/test_thermostat_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

static Thermostat *thermostat;
static TemperatureController *tempController;
static ThermostatContext *context;

TEST_GROUP(ThermostatControllerTestGroup)
{
Expand All @@ -14,10 +13,8 @@ TEST_GROUP(ThermostatControllerTestGroup)
tempController = new TemperatureController();
thermostat = new Thermostat();

context = new ThermostatContext();
context->tempController = tempController;

thermostat->initialize(context);
thermostat->initialize(NULL, tempController, NULL);
}

void teardown()
Expand All @@ -26,7 +23,6 @@ TEST_GROUP(ThermostatControllerTestGroup)
mock().clear();
delete thermostat;
delete tempController;
delete context;
}
};

Expand Down
18 changes: 14 additions & 4 deletions test/thermostat/test_thermostat_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
#include <iostream>

static Thermostat *thermostat;
static ThermostatContext *context;
static EnvironmentSensor *environmentSensor;
static TemperatureController *tempController;
static Hvac *hvac;

TEST_GROUP(ThermostatTestGroupInit){
void setup(){
ThermostatContext newCtx;
context = &newCtx;
environmentSensor = new EnvironmentSensor();
tempController = new TemperatureController();
hvac = new Hvac();
thermostat = new Thermostat();
thermostat->initialize(environmentSensor, tempController, hvac);


}

Expand All @@ -21,6 +27,10 @@ void teardown()

mock().checkExpectations();
mock().clear();
delete thermostat;
delete environmentSensor;
delete tempController;
delete hvac;
}
}
;
Expand All @@ -29,7 +39,7 @@ TEST(ThermostatTestGroupInit, ThermostatInitalize)
{

Thermostat *testInit = new Thermostat();
ENUMS_EQUAL_INT(THERMOSTAT_OK, testInit->initialize(context));
ENUMS_EQUAL_INT(THERMOSTAT_OK, testInit->initialize(environmentSensor, tempController, hvac));
CHECK_TRUE(testInit->isInitialized());
delete testInit;
}

0 comments on commit b2fd40f

Please sign in to comment.