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

#2772: PhContainingUTF8 to print strings correctly #2776

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion eo-runtime/src/main/java/EOorg/EOeolang/EOerror.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@
*/
package EOorg.EOeolang;

import java.nio.charset.StandardCharsets;
import org.eolang.AtFree;
import org.eolang.AtLambda;
import org.eolang.Attr;
import org.eolang.Bytes;
import org.eolang.BytesOf;
import org.eolang.Dataized;
import org.eolang.ExAbstract;
import org.eolang.ExFailure;
import org.eolang.PhDefault;
Expand Down Expand Up @@ -115,7 +119,7 @@ public static final class ExError extends ExAbstract {
* @param enclosure Enclosure inside the error
*/
public ExError(final Phi enclosure) {
super(enclosure.toString());
super(EOerror.ExError.safeMessage(enclosure));
this.enc = enclosure;
}

Expand All @@ -126,5 +130,62 @@ public ExError(final Phi enclosure) {
public Phi enclosure() {
return this.enc;
}

/**
* Retrieve message from enclosure safely.
* @param enclosure Enclosure.
* @return String message.
* @checkstyle IllegalCatchCheck (45 lines)
*/
private static String safeMessage(final Phi enclosure) {
String result;
if (enclosure == null) {
result = "null Phi";
} else {
try {
final byte[] data = new Dataized(enclosure).take();
switch (data.length) {
case 0:
result = String.format(
"%s(Δ = --)",
enclosure
);
break;
case 1:
result = String.format(
"%s(Δ = %s)",
enclosure,
data[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)
);
break;
default:
result = String.format(
"%s(Δ = %s or %s)",
enclosure,
new BytesOf(data),
new String(data, StandardCharsets.UTF_8)
);
}
} catch (final Throwable first) {
try {
result = enclosure.toString();
} catch (final Throwable second) {
result = enclosure.getClass().toString();
}
}
}
return result;
}
}
}
73 changes: 73 additions & 0 deletions eo-runtime/src/test/java/EOorg/EOeolang/EOerrorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,22 @@
*/
package EOorg.EOeolang;

import java.util.stream.Stream;
import org.eolang.AtComposite;
import org.eolang.AtOnce;
import org.eolang.Data;
import org.eolang.Dataized;
import org.eolang.ExAbstract;
import org.eolang.PhCopy;
import org.eolang.PhDefault;
import org.eolang.PhWith;
import org.eolang.Phi;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Test case for {@link EOerror}.
Expand All @@ -55,4 +65,67 @@ void makesToxicObject() {
);
}

@ParameterizedTest
@MethodSource("getTestSources")
void getsReadableError(final Object cnst) {
ExAbstract error = null;
Copy link
Member

Choose a reason for hiding this comment

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

@levBagryansky can we make this variable final?
For example set it to null in try block and set to exc in catch block?

Copy link
Member Author

Choose a reason for hiding this comment

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

@maxonfjvipon We cannot do it here, I get the error Variable 'error' might already have been assigned to: 73

try {
new Dataized(new MyError(cnst)).take();
} catch (final ExAbstract exc) {
error = exc;
}
assert error != null;
MatcherAssert.assertThat(
error.toString(),
Matchers.containsString(cnst.toString())
);
}

/**
* Input arguments for getsReadableError unit test.
* @return Stream of arguments.
*/
private static Stream<Object> getTestSources() {
return Stream.of(
12345L,
"qwerty",
12.34567D,
true,
false
);
}

/**
* The object below.
* [] > my-error
* error > @
* "qwerty"
* @since 0.35
* @checkstyle JavadocStyleCheck
*/
private static final class MyError extends PhDefault {

/**
* Ctor.
* @param data Data inside error.
*/
MyError(final Object data) {
this.add(
"φ",
new AtOnce(
new AtComposite(
this,
rho -> new PhWith(
new PhCopy(
Phi.Φ.attr("org").get().attr("eolang").get().attr("error").get()
),
"α",
new Data.ToPhi(data)
)
)
)
);
}
}

}
Loading