Skip to content

Commit

Permalink
Refactored MockDigitalInput and moved PushButtonHelperTests to the co…
Browse files Browse the repository at this point in the history
…rrect package
  • Loading branch information
Ed0827 committed Sep 22, 2024
1 parent d505f65 commit ef09bb0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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() {

}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package com.opensourcewithslu.inputdevices;
package com.opensourcewithslu.mock;
import com.pi4j.common.Metadata;
import com.pi4j.context.Context;
import com.pi4j.exception.InitializeException;
import com.pi4j.exception.ShutdownException;
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(){
Expand Down Expand Up @@ -71,7 +78,7 @@ public String id() {

@Override
public String name() {
return "";
return "MockDigitalInput";
}

@Override
Expand Down

0 comments on commit ef09bb0

Please sign in to comment.