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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#243: Introduce IsBytes #244

Merged
merged 3 commits into from
May 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
137 changes: 137 additions & 0 deletions src/main/java/org/llorllale/cactoos/matchers/IsBytes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* The MIT License (MIT)
*
* Copyright (c) for portions of project cactoos-matchers are held by
* Yegor Bugayenko, 2017-2018, as part of project cactoos.
* All other copyright for project cactoos-matchers are held by
* George Aristy, 2018-2020.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.llorllale.cactoos.matchers;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.stream.IntStream;
import org.cactoos.Bytes;
import org.cactoos.bytes.BytesOf;
import org.cactoos.bytes.UncheckedBytes;
import org.cactoos.iterable.IterableOfBytes;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;

/**
* Matcher for {@link Bytes}.
*
* @since 1.0.0
* @checkstyle ProtectedMethodInFinalClassCheck (200 lines)
*/
public final class IsBytes extends TypeSafeDiagnosingMatcher<Bytes> {

/**
* Expected bytes.
*/
private final UncheckedBytes bytes;

/**
* Ctor.
* @param bytes The bytes to match against
*/
public IsBytes(final byte... bytes) {
this(new BytesOf(bytes));
}

/**
* Ctor.
* @param chars The chars to match against
*/
public IsBytes(final char... chars) {
this(new BytesOf(chars));
}

/**
* Ctor.
* @param ints The bytes to match against
*/
public IsBytes(final int... ints) {
this(
IntStream
.of(ints)
.collect(
() -> ByteBuffer.allocate(ints.length),
(bts, value) -> bts.put((byte) value),
ByteBuffer::put
).array()
);
}

/**
* Ctor.
* @param string The string to match against
*/
public IsBytes(final String string) {
this(new BytesOf(string));
}

/**
* Ctor.
* @param bytes The bytes to match against
*/
public IsBytes(final Bytes bytes) {
this.bytes = new UncheckedBytes(bytes);
}

@Override
public void describeTo(final Description description) {
description.appendValue(new IterableOfBytes(this.bytes));
}

@Override
@SuppressWarnings(
{"PMD.AvoidCatchingGenericException", "PMD.AvoidCatchingThrowable"}
)
protected boolean matchesSafely(
final Bytes item, final Description description
) {
// @checkstyle IllegalCatchCheck (100 lines)
boolean res = true;
byte[] presented = new byte[0];
try {
presented = item.asBytes();
} catch (final Exception ex) {
description
.appendText("thrown ")
.appendValue(ex);
res = false;
andreoss marked this conversation as resolved.
Show resolved Hide resolved
}
if (res) {
final byte[] expected = this.bytes.asBytes();
res = Arrays.equals(
presented, expected
);
if (!res) {
description.appendValue(
new IterableOfBytes(presented)
);
}
}
return res;
}

}
91 changes: 91 additions & 0 deletions src/test/java/org/llorllale/cactoos/matchers/IsBytesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* The MIT License (MIT)
*
* Copyright (c) for portions of project cactoos-matchers are held by
* Yegor Bugayenko, 2017-2018, as part of project cactoos.
* All other copyright for project cactoos-matchers are held by
* George Aristy, 2018-2020.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.llorllale.cactoos.matchers;

import org.cactoos.Text;
import org.cactoos.bytes.BytesOf;
import org.cactoos.bytes.HexOf;
import org.cactoos.text.TextOf;
import org.junit.jupiter.api.Test;

/**
* Test for {@link IsBytes}.
* @since 1.0.0
* @checkstyle MagicNumberCheck (500 lines)
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
final class IsBytesTest {

@Test
void matchesExactly() {
new Assertion<>(
"Must match",
new BytesOf("ABC"),
new IsBytes('A', 'B', 'C')
).affirm();
}

@Test
void mismatchesWhenThrows() {
new Assertion<>(
"Must mismatch",
new IsBytes(65, 66, 67),
new Mismatches<>(
new BytesOf(
(Text) () -> {
throw new UnsupportedOperationException("ok");
}
),
"<65, 66, 67>",
"thrown <java.lang.UnsupportedOperationException: ok>"
)
).affirm();
}

@Test
void mismatches() {
new Assertion<>(
"Must mismatch",
new IsBytes((byte) 65, (byte) 66, (byte) 67),
Copy link
Collaborator

Choose a reason for hiding this comment

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

@andreoss why do we need to cast here? if this is because of the char... constructor, then let's remove it, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@victornoel it's narrowing conversion, casting should be explicit

new Mismatches<>(
new BytesOf("abc"),
"<65, 66, 67>",
"<97, 98, 99>"
)
).affirm();
}

@Test
void matchesAsString() {
new Assertion<>(
"Must match",
new HexOf(new TextOf("6465616462656166")),
new IsBytes("deadbeaf")
).affirm();
}

}