Skip to content

Commit

Permalink
#2772: byte array tests
Browse files Browse the repository at this point in the history
  • Loading branch information
levBagryansky committed Jan 17, 2024
1 parent 9d584fd commit c8d1ddf
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
24 changes: 14 additions & 10 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOerror.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
package EOorg.EOeolang;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import org.eolang.AtFree;
import org.eolang.AtLambda;
import org.eolang.Attr;
Expand Down Expand Up @@ -143,38 +145,40 @@ private static String safeMessage(final Phi enclosure) {
result = "null Phi";
} else {
try {
final byte[] data = new Dataized(enclosure).take();
switch (data.length) {
final byte[] raw = new Dataized(enclosure).take();
final Bytes bytes = new BytesOf(raw);
switch (raw.length) {
case 0:
result = String.format(
"%s(螖 = --)",
enclosure
"%s(螖 = %s",
enclosure,
bytes
);
break;
case 1:
result = String.format(
"%s(螖 = %s)",
"%s(螖 = %s or %s)",
enclosure,
data[0] != 0
bytes,
raw[0] != 0
);
break;
case 8:
final Bytes bytes = new BytesOf(data);
result = String.format(
"%s(螖 = %s = %s, or %s, or %s)",
enclosure,
bytes,
bytes.asNumber(Long.class),
bytes.asNumber(Double.class),
new String(data, StandardCharsets.UTF_8)
new String(raw, StandardCharsets.UTF_8)
);
break;
default:
result = String.format(
"%s(螖 = %s or %s)",
enclosure,
new BytesOf(data),
new String(data, StandardCharsets.UTF_8)
bytes,
new String(raw, StandardCharsets.UTF_8)
);
}
} catch (final Throwable first) {
Expand Down
33 changes: 32 additions & 1 deletion eo-runtime/src/test/java/EOorg/EOeolang/EOerrorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
*/
package EOorg.EOeolang;

import java.util.Arrays;
import java.util.stream.Stream;
import org.eolang.AtComposite;
import org.eolang.AtOnce;
import org.eolang.BytesOf;
import org.eolang.Data;
import org.eolang.Dataized;
import org.eolang.ExAbstract;
Expand Down Expand Up @@ -81,8 +83,24 @@ void getsReadableError(final Object cnst) {
);
}

@ParameterizedTest
@MethodSource("getTestByteArraySources")
void getsReadableByteArrayError(final byte[] cnst) {
ExAbstract error = null;
try {
new Dataized(new MyError(cnst)).take();
} catch (final ExAbstract exc) {
error = exc;
}
assert error != null;
MatcherAssert.assertThat(
error.toString(),
Matchers.containsString(Arrays.toString(cnst))
);
}

/**
* Input arguments for getsReadableError unit test.
* Input arguments for getsReadableError unit tests.
* @return Stream of arguments.
*/
private static Stream<Object> getTestSources() {
Expand All @@ -95,6 +113,19 @@ private static Stream<Object> getTestSources() {
);
}

/**
* Input arguments for getsReadableByteArrayError unit tests.
* @return Stream of arguments.
*/
private static Stream<byte[]> getTestByteArraySources() {
return Stream.of(
new byte[]{},
new byte[]{12},
new byte[]{10, 11, 12, 13, 14, 15, 16, 17},
new byte[]{10, 11, 12, 13, 14, 15, 16, 17, -18, -19, -20, -21, 22}
);
}

/**
* The object below.
* [] > my-error
Expand Down

0 comments on commit c8d1ddf

Please sign in to comment.