-
Notifications
You must be signed in to change notification settings - Fork 142
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
Changes from 4 commits
eae19cc
60bdcf9
1520778
a8ddca4
f532ab2
c9285f9
385a033
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,10 +27,17 @@ | |
*/ | ||
package EOorg.EOeolang; | ||
|
||
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; | ||
|
||
|
@@ -55,4 +62,51 @@ void makesToxicObject() { | |
); | ||
} | ||
|
||
@Test | ||
void getsReadableError() { | ||
ExAbstract error = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @levBagryansky can we make this variable final? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @maxonfjvipon We cannot do it here, I get the error |
||
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 Data.ToPhi("qwerty") | ||
) | ||
) | ||
) | ||
); | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@levBagryansky if this class is an instance of
Phi
and it makes phi objects safer by properly printing theirtoString()
, why do we use only here, in this private method? Why don't we decorate all our phi objects with this decorator?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yegor256 This class is correctly to use in order to print bytes of
data
as UTF8 String. For example,PhContainingUTF8(EOint)
produces something strange. We can also use this class inData.ToPhi(java.util.String)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@levBagryansky maybe we shouldn't call this class
Phi...
and should not make it a decorator ofPhi
? It doesn't look like a decorator to me, but a "smart" class: https://www.yegor256.com/2016/04/26/why-inputstream-design-is-wrong.html It just takes an existing instance ofPhi
, calls its method and then post-process the returned data. It doesn't become phi, it only is a client of Phi.