From ef09bb08f6bde66056f8dd3eeb1ded938ab1c4f9 Mon Sep 17 00:00:00 2001 From: Ed0827 Date: Sat, 21 Sep 2024 22:26:00 -0500 Subject: [PATCH] Refactored MockDigitalInput and moved PushButtonHelperTests to the correct package --- .../inputdevices/PushButtonHelperTests.java | 28 +++++++++++++++---- .../MockDigitalInput.java | 11 ++++++-- 2 files changed, 31 insertions(+), 8 deletions(-) rename pi4micronaut-utils/src/test/java/com/opensourcewithslu/{inputdevices => mock}/MockDigitalInput.java (80%) diff --git a/pi4micronaut-utils/src/test/java/com/opensourcewithslu/inputdevices/PushButtonHelperTests.java b/pi4micronaut-utils/src/test/java/com/opensourcewithslu/inputdevices/PushButtonHelperTests.java index c9d5887d..b9000bc2 100644 --- a/pi4micronaut-utils/src/test/java/com/opensourcewithslu/inputdevices/PushButtonHelperTests.java +++ b/pi4micronaut-utils/src/test/java/com/opensourcewithslu/inputdevices/PushButtonHelperTests.java @@ -1,27 +1,43 @@ package com.opensourcewithslu.inputdevices; import static org.junit.jupiter.api.Assertions.*; + +import com.opensourcewithslu.mock.MockDigitalInput; import com.pi4j.io.gpio.digital.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; class PushButtonHelperTests { @org.junit.jupiter.api.Test + /* This test simulates the push button in a LOW state without connecting to the actual hardware. + It ensures that when the button input is LOW, the 'isPressed' state is correctly identified as false. + */ void initializeStateLow() { - MockDigitalInput input = new MockDigitalInput(); - input.SetState(DigitalState.LOW); - PushButtonHelper helper = new PushButtonHelper(input); - assertFalse(helper.isPressed); + Logger log = LoggerFactory.getLogger(PushButtonHelper.class); // + MockDigitalInput input = new MockDigitalInput(); // Mock input for the button + input.SetState(DigitalState.LOW); // Simulate button in the LOW state + PushButtonHelper helper = new PushButtonHelper(input); // + assertFalse(helper.isPressed); // Button is not pressed when input is LOW } @org.junit.jupiter.api.Test + /* + This test simulates the push button in a HIGH state. + It verifies that when the button input is HIGH, the 'IsPressed' state is correctly identified as true. + */ void initializeStateHigh() { MockDigitalInput input = new MockDigitalInput(); - input.SetState(DigitalState.HIGH); + input.SetState(DigitalState.HIGH); // Simulate button in the HIGH state PushButtonHelper helper = new PushButtonHelper(input); - assertTrue(helper.isPressed); + assertTrue(helper.isPressed); // Button is pressed when input is HIGH } @org.junit.jupiter.api.Test + /* + Placeholder for testing even listener functionality. + */ void addEventListener() { + } } \ No newline at end of file diff --git a/pi4micronaut-utils/src/test/java/com/opensourcewithslu/inputdevices/MockDigitalInput.java b/pi4micronaut-utils/src/test/java/com/opensourcewithslu/mock/MockDigitalInput.java similarity index 80% rename from pi4micronaut-utils/src/test/java/com/opensourcewithslu/inputdevices/MockDigitalInput.java rename to pi4micronaut-utils/src/test/java/com/opensourcewithslu/mock/MockDigitalInput.java index 9996027f..b8edee75 100644 --- a/pi4micronaut-utils/src/test/java/com/opensourcewithslu/inputdevices/MockDigitalInput.java +++ b/pi4micronaut-utils/src/test/java/com/opensourcewithslu/mock/MockDigitalInput.java @@ -1,4 +1,4 @@ -package com.opensourcewithslu.inputdevices; +package com.opensourcewithslu.mock; import com.pi4j.common.Metadata; import com.pi4j.context.Context; import com.pi4j.exception.InitializeException; @@ -6,6 +6,13 @@ import com.pi4j.io.binding.DigitalBinding; import com.pi4j.io.gpio.digital.*; +/* +The ockDigitalInput class implements the DigitalInput interface, which requires +all methods to be implemented, even if they are not used in the mock. +These methods have default or no operation implementations to fulfill the interface contract. +they provided an empty method("return null") because the methods are required by the interface, +yet for the mock implementation, it doesn't need to do anything. +*/ public class MockDigitalInput implements DigitalInput { DigitalState mockState; public MockDigitalInput(){ @@ -71,7 +78,7 @@ public String id() { @Override public String name() { - return ""; + return "MockDigitalInput"; } @Override