Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HACKTOBERFEST] MockSpi Class revision #301

Merged
merged 4 commits into from
Oct 11, 2023

Conversation

MMMMMNG
Copy link
Contributor

@MMMMMNG MMMMMNG commented Oct 6, 2023

This PR is supposed to fix Issues #298, #299 and last but certainly not least Issue #300!

It is also supposed to be part of Hacktoberfest.
According to the FAQ, if you add the hacktoberfest-accepted label to my PR, it will count - that is, should you choose to accept it ofc. ;)
grafik
You could also open the entire repo to Hacktoberfest, could be worthwhile. Here's the relevant info.


I was made aware of these shortcomings of the MockSpi class by my working extensively with Roberts class for interfacing with the MCP23S17 Integrated Circuit.

Here's an example of how I use this revised version of MockSpi:

var turnEverySecondPinOn = MCPData.builder().read().response(1, 0, 1, 0, 1, 0, 1, 0).next().build();
//-----------------------------------------------------------------------------------------------------//
//   I assemble the data bytes (MCPData is just a convenience class to create MCP23S17-SPI-packets)    //
//-----------------------------------------------------------------------------------------------------//
        //before we pretend every second pin is on verify that all pins are off
        for (var pin : pins) {
            assertFalse(pin.get());
        }

        //when
        mockSpi.write(turnEverySecondPinOn);
//-----------------------------------------------------------------------------------------------------//
//   I write the bytes to the internal buffer of the MockSpi class, where they will later be           //
//   be consumed by the transfer() calls from the MCP23S17 class                                       //
//-----------------------------------------------------------------------------------------------------//
        try {                     //-----------------------------------------------//
            cut.readGPIOA();      // these functions trigger  the transfer() calls //
            cut.readGPIOB();      //-----------------------------------------------//
        } catch (Exception e) {
            //don't care
        }
        //then
        for (int i = 0; i < pins.size(); i++) {
            assertEquals(i % 2 == 1, pins.get(i).get());
//-----------------------------------------------------------------------------------------------------//
//   Now the actual test to check if the state was computed correctly according                        //
//   to the mock data I fed                                                                            //
//-----------------------------------------------------------------------------------------------------//
        }

There are more working examples in my fork of Roberts project.

Feel free to give feedback and I will try to adapt the code as ASAP as possible.
Also, if you accept the PR, please don't forget to label it hacktoberfest-accepted 馃檹.

Cheers!
-MNG

with the old implementation it was impossible to provide mock SPI data or check written SPI data during automated testing when using the transfer() method
@FDelporte
Copy link
Member

FDelporte commented Oct 7, 2023

Hi MNG, thanks for your contribution! Our first Hacktoberfest ever. I love your humor regarding #300 and indeed a project without issues is a dead project so thanks for reporting and proposing a fix. Looks good at first quick sight, will dive in a bit deeper later and come back to this.

Copy link
Member

@eitch eitch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @MMMMMNG thanks for your PR! It looks good. I like that you cleaned up the duplicate code for the log preamble. The rest of the changes look fine.

It would be nice though if you could follow Java formatting conventions and add spaces after commas, etc.

@eitch eitch merged commit 87c73c0 into Pi4J:develop Oct 11, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants