From 5e7bd298deb9611de62888683632588fb2ef5f9a Mon Sep 17 00:00:00 2001 From: Jacob Salvi Date: Tue, 17 Mar 2020 17:44:57 +0100 Subject: [PATCH] Added some tests for Regular Light and for Switch --- .../smarthut/RegularLightTests.java | 47 +++++++++ .../sa4/sanmarinoes/smarthut/SwitchTests.java | 96 +++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RegularLightTests.java create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RegularLightTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RegularLightTests.java new file mode 100644 index 0000000..c4f4694 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RegularLightTests.java @@ -0,0 +1,47 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut; + +import static org.junit.jupiter.api.Assertions.*; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLight; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +@DisplayName("A RegularLight") +public class RegularLightTests { + + RegularLight regularLight; + + @BeforeEach + public void createRegularLight() { + this.regularLight = new RegularLight(); + } + + @Test + @DisplayName("State when just created") + public void beginningState() { + assertFalse(regularLight.isOn()); + } + + @Test + @DisplayName("Changing state to on after creating the light") + public void createAndSetOn() { + regularLight.setOn(true); + assertTrue(regularLight.isOn()); + } + + @Test + @DisplayName("Change state of the light to off after creating it") + public void createAndSetOff() { + regularLight.setOn(false); + assertFalse(regularLight.isOn()); + } + + @Test + @DisplayName("Checks whether a turned on light getting turned on is still in the on State") + public void setOn() { + regularLight.setOn(true); + regularLight.setOn(true); + assertTrue(regularLight.isOn()); + } +} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchTests.java new file mode 100644 index 0000000..9f937d3 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchTests.java @@ -0,0 +1,96 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut; + +import static org.junit.jupiter.api.Assertions.*; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +@DisplayName("A switch") +public class SwitchTests { + + Switch aSwitch; + + @BeforeEach + public void createNewSwitch() { + + this.aSwitch = new Switch(); + RegularLight regularLight = new RegularLight(); + DimmableLight dimmableLight = new DimmableLight(); + SmartPlug smartPlug = new SmartPlug(); + this.aSwitch.getOutputs().add(regularLight); + this.aSwitch.getOutputs().add(dimmableLight); + this.aSwitch.getOutputs().add(smartPlug); + } + + @Test + @DisplayName("check state when switch created") + public void createdSwitch() { + assertFalse(aSwitch.isOn()); + } + + @Test + @DisplayName("Check toggle on a switch in its off state") + public void offToggle() { + aSwitch.toggle(); + assertTrue(aSwitch.isOn()); + } + + @Test + @DisplayName("Checks whether setting on a off switch works as intended") + public void offSetOn() { + aSwitch.setOn(true); + assertTrue(aSwitch.isOn()); + } + + @Test + @DisplayName("Checks whether setting off a on switch works as intended") + public void onSetOff() { + aSwitch.toggle(); + aSwitch.setOn(false); + assertFalse(aSwitch.isOn()); + } + + @Test + @DisplayName("Checks that setting off an off switch results in a off state") + public void offSetOff() { + aSwitch.setOn(false); + assertFalse(aSwitch.isOn()); + } + + @Test + @DisplayName("Checks that setting on an on switch results in a on state") + public void onSetOn() { + aSwitch.toggle(); + aSwitch.setOn(true); + assertTrue(aSwitch.isOn()); + } + + @Test + @DisplayName("Checks wheter toggling a on switch set its state to off") + public void onToggle() { + aSwitch.setOn(true); + aSwitch.toggle(); + assertFalse(aSwitch.isOn()); + } + + @Test + @DisplayName("Checks that toggling on sets all elements of the Set on as well") + public void toggleEffctOnSet() { + aSwitch.toggle(); + for (final Switchable s : aSwitch.getOutputs()) { + assertTrue(s.isOn()); + } + } + + @Test + @DisplayName("Checks that toggling the switch off also sets all elements of its set off") + public void toggleOffEffectOnElementes() { + aSwitch.setOn(true); + aSwitch.toggle(); + for (final Switchable s : aSwitch.getOutputs()) { + assertFalse(s.isOn()); + } + } +}