Skip to content

Commit

Permalink
PlainStringScalar.unescape
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Apr 12, 2024
1 parent b0f187a commit f6aecdf
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
36 changes: 29 additions & 7 deletions src/main/java/com/amihaiemil/eoyaml/PlainStringScalar.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@
package com.amihaiemil.eoyaml;

/**
* YAML Plain scalar from String. Use this class when dealing with
* built YAML or in the unit tests.
*
* DO NOT use it when READING yaml. For reading use
* {@link ReadPlainScalar}!
*
* YAML Plain Scalar from String.
* @author Mihai Andronache ([email protected])
* @version $Id$
* @since 1.0.0
Expand Down Expand Up @@ -94,12 +89,39 @@ final class PlainStringScalar extends BaseScalar {
*/
@Override
public String value() {
return this.value;
final String unescaped;
if("null".equals(this.value)) {
unescaped = null;
} else {
unescaped = this.unescape(this.value);
}
return unescaped;
}

@Override
public Comment comment() {
return this.comment;
}

/**
* Remove the possible escaping quotes or apostrophes surrounding the
* given value.
* @param escaped The value to unescape.
* @return The value without quotes or apostrophes.
*/
private String unescape(final String escaped) {
final String unescaped;
if(escaped == null) {
unescaped = escaped;
} else {
if (escaped.startsWith("\"") && escaped.endsWith("\"")) {
unescaped = escaped.substring(1, escaped.length() - 1);
} else if (escaped.startsWith("'") && escaped.endsWith("'")) {
unescaped = escaped.substring(1, escaped.length() - 1);
} else {
unescaped = escaped;
}
}
return unescaped;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void returnsKeys() {
final Iterator<YamlNode> iterator = keys.iterator();
MatcherAssert.assertThat(
iterator.next().toString(),
Matchers.equalTo("- 'a'")
Matchers.equalTo("- a")
);
MatcherAssert.assertThat(
iterator.next().toString(),
Expand Down Expand Up @@ -170,7 +170,7 @@ public void returnsAllValues() {
Matchers.equalTo(
"---"
+ System.lineSeparator()
+ "'b'"
+ "b"
+ System.lineSeparator()
+ "..."
)
Expand Down Expand Up @@ -214,7 +214,7 @@ public void returnsAllValues() {
Matchers.equalTo(
"- a"
+ System.lineSeparator()
+ "- '0,3'"
+ "- \"0,3\""
+ System.lineSeparator()
+ "- \"2, 3, 4\""
+ System.lineSeparator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ public void hasOnlyScalars() {

/**
* A ReadFlowSequence works if it has only sequences in it.
* @todo #368:30min Make sure the escaped scalars read from a
* flow sequence are unescaped when they are returned to the user.
* The bellow assertions, for [a] and b][ should work without being
* surrounded by quotes/apostrophes.
*/
@Test
public void hasOnlySequences() {
Expand Down Expand Up @@ -141,11 +137,11 @@ public void hasOnlySequences() {
);
MatcherAssert.assertThat(
second.string(1),
Matchers.equalTo("\"[a]\"")
Matchers.equalTo("[a]")
);
MatcherAssert.assertThat(
second.string(2),
Matchers.equalTo("'b]['")
Matchers.equalTo("b][")
);

final YamlSequence third = seq.yamlSequence(2);
Expand Down Expand Up @@ -178,15 +174,15 @@ public void hasSequencesAndScalars() {
);
MatcherAssert.assertThat(
seq.string(1),
Matchers.equalTo("\"u][\"")
Matchers.equalTo("u][")
);
MatcherAssert.assertThat(
seq.string(2),
Matchers.equalTo("'v}{'")
Matchers.equalTo("v}{")
);
MatcherAssert.assertThat(
seq.string(3),
Matchers.equalTo("'escalar'")
Matchers.equalTo("escalar")
);
final YamlSequence firstSeq = seq.yamlSequence(4);
MatcherAssert.assertThat(firstSeq, Matchers.iterableWithSize(2));
Expand Down

1 comment on commit f6aecdf

@zoeself
Copy link
Collaborator

Choose a reason for hiding this comment

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

@amihaiemil I've closed the Issues [#602] since their to-dos disappeared from the code.

The to-dos may have been removed in an earlier commit, but I've found it just now.

Please sign in to comment.