Pi4J / pi4j-v2

Pi4J Version 2.0
Apache License 2.0
266 stars 54 forks source link

Feature request: Method for MockSpi class that allows reading of the entire buffer for testing purposes #300

Open MMMMMNG opened 9 months ago

MMMMMNG commented 9 months ago

THIS IS ISSUE #300! 🀩πŸ₯³πŸ₯³πŸ₯³πŸŽ‚

Congrats pi4j people!

Consider this quote: grafik I think the same can be said for libraries like pi4j. So I think this calls for a celebration! 🍾🍺🍻 Also I think the quote would've been funnier if the creator of JavaScript was the one associated with it haha

...but anywho, I digress.

https://github.com/Pi4J/pi4j-v2/blob/8d8f1574f926dfd2961356cf015dcbd58e50664e/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/provider/spi/MockSpi.java#L49

I would love a method like MockSpi.readEntireMockBuffer() that could be used during testing to get all the bytes that were written to the mock object by calls to write() or transfer() and weren't yet consumed by calls to read() or transfer().

I tried to use a while loop and the read() method that returns -1 if the buffer is empty: https://github.com/Pi4J/pi4j-v2/blob/8d8f1574f926dfd2961356cf015dcbd58e50664e/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/provider/spi/MockSpi.java#L145C1-L156C6

But I realised pretty quickly that this would cause a problem when the actual data was -1 (that's maybe also worth investigating).

In any case, It has been a pleasure to write Issue #300 for this awsm project!

Cheers -MNG