From 986dd0d8152db805e57c9dad8ce5388dede0db4d Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 6 Jan 2025 09:21:05 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=9A=B8=20Add=20InputStream=20matcher=20for?= =?utf8?q?=20byte=20arrays?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../fcp/test/InputStreamMatchers.java | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/test/java/net/pterodactylus/fcp/test/InputStreamMatchers.java b/src/test/java/net/pterodactylus/fcp/test/InputStreamMatchers.java index 3e47a0b..7507799 100644 --- a/src/test/java/net/pterodactylus/fcp/test/InputStreamMatchers.java +++ b/src/test/java/net/pterodactylus/fcp/test/InputStreamMatchers.java @@ -40,4 +40,35 @@ public class InputStreamMatchers { }; } + public static Matcher equalTo(byte... content) { + return new TypeSafeDiagnosingMatcher() { + @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); + } + }; + } + } -- 2.7.4