🚸 Add InputStream matcher for byte arrays
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Mon, 6 Jan 2025 08:21:05 +0000 (09:21 +0100)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Mon, 6 Jan 2025 08:21:05 +0000 (09:21 +0100)
src/test/java/net/pterodactylus/fcp/test/InputStreamMatchers.java

index 3e47a0b..7507799 100644 (file)
@@ -40,4 +40,35 @@ public class InputStreamMatchers {
                };
        }
 
+       public static Matcher<InputStream> equalTo(byte... content) {
+               return new TypeSafeDiagnosingMatcher<InputStream>() {
+                       @Override
+                       protected boolean matchesSafely(InputStream inputStream, Description mismatchDescription) {
+                               try {
+                                       for (int index = 0; index < content.length; index++) {
+                                               int readByte = inputStream.read();
+                                               if ((readByte == -1) || ((byte) readByte != content[index])) {
+                                                       mismatchDescription.appendText("was ").appendValue(readByte).appendText(" at offset ").appendValue(index);
+                                                       return false;
+                                               }
+                                       }
+                                       int eof = inputStream.read();
+                                       if (eof != -1) {
+                                               mismatchDescription.appendText("contained more than ").appendValue(content.length).appendText(" bytes");
+                                               return false;
+                                       }
+                               } catch (IOException e) {
+                                       mismatchDescription.appendText("could not be read (").appendValue(e).appendText(")");
+                                       return false;
+                               }
+                               return true;
+                       }
+
+                       @Override
+                       public void describeTo(Description description) {
+                               description.appendText("is input stream containing ").appendValue(content);
+                       }
+               };
+       }
+
 }