Skip to content

Commit

Permalink
objectionary#2772: PhContainingUTF8 to print strings correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
levBagryansky committed Jan 14, 2024
1 parent 63cb73c commit eae19cc
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 1 deletion.
3 changes: 2 additions & 1 deletion eo-runtime/src/main/java/EOorg/EOeolang/EOerror.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.eolang.Attr;
import org.eolang.ExAbstract;
import org.eolang.ExFailure;
import org.eolang.PhContainingUTF8;
import org.eolang.PhDefault;
import org.eolang.Phi;
import org.eolang.Versionized;
Expand Down Expand Up @@ -115,7 +116,7 @@ public static final class ExError extends ExAbstract {
* @param enclosure Enclosure inside the error
*/
public ExError(final Phi enclosure) {
super(enclosure.toString());
super(new PhContainingUTF8(enclosure).toString());
this.enc = enclosure;
}

Expand Down
89 changes: 89 additions & 0 deletions eo-runtime/src/main/java/org/eolang/PhContainingUTF8.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 Objectionary.com
*
* 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.eolang;

import java.nio.charset.StandardCharsets;

/**
* Class to print {@link Phi} objects with UTF8 String data correctly.
* @since 0.35
* @checkstyle AbbreviationAsWordInNameCheck (5 lines)
*/
public final class PhContainingUTF8 implements Phi {

/**
* Origin.
*/
private final Phi origin;

/**
* Ctor.
* @param origin Origin.
*/
public PhContainingUTF8(final Phi origin) {
this.origin = origin;
}

@Override
public String toString() {
return String.format(
"{%s: Δ as String = \"%s\"}",
this.origin.toString(),
new String(
new Dataized(this.origin).take(),
StandardCharsets.UTF_8
)
);
}

@Override
public Phi copy() {
return this.origin.copy();
}

@Override
public Attr attr(final int pos) {
return this.origin.attr(pos);
}

@Override
public Attr attr(final String name) {
return this.origin.attr(name);
}

@Override
public String locator() {
return this.origin.locator();
}

@Override
public String forma() {
return this.origin.forma();
}

@Override
public String φTerm() {
return this.origin.φTerm();
}
}
68 changes: 68 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,10 +27,18 @@
*/
package EOorg.EOeolang;

import org.eolang.AtComposite;
import org.eolang.AtOnce;
import org.eolang.BytesOf;
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;

Expand All @@ -55,4 +63,64 @@ void makesToxicObject() {
);
}

@Test
void getsReadableError() {
ExAbstract error = null;
try {
new Dataized(new MyError()).take();
} catch (final ExAbstract exc) {
error = exc;
}
assert error != null;
MatcherAssert.assertThat(
error.toString(),
Matchers.containsString("qwerty")
);
}

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

/**
* Ctor.
*/
MyError() {
this.add(
"φ",
new AtOnce(
new AtComposite(
this,
rho -> new PhWith(
new PhCopy(
Phi.Φ.attr("org").get().attr("eolang").get().attr("error").get()
),
"α",
new PhWith(
new PhCopy(
Phi.Φ
.attr("org").get()
.attr("eolang").get()
.attr("string").get()
),
0,
new PhWith(
new EObytes(Phi.Φ),
"Δ",
new Data.Value<>(new BytesOf("qwerty").take())
)
)
)
)
)
);
}
}

}

0 comments on commit eae19cc

Please sign in to comment.