From f6aecdf3a552775904726599f2ebedd7009caac2 Mon Sep 17 00:00:00 2001 From: amihaiemil Date: Fri, 12 Apr 2024 21:40:23 +0300 Subject: [PATCH] PlainStringScalar.unescape --- .../amihaiemil/eoyaml/PlainStringScalar.java | 36 +++++++++++++++---- .../eoyaml/ReadFlowMappingTestCase.java | 6 ++-- .../eoyaml/ReadFlowSequenceTestCase.java | 14 +++----- 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/amihaiemil/eoyaml/PlainStringScalar.java b/src/main/java/com/amihaiemil/eoyaml/PlainStringScalar.java index 291f3297..fd4f9c57 100644 --- a/src/main/java/com/amihaiemil/eoyaml/PlainStringScalar.java +++ b/src/main/java/com/amihaiemil/eoyaml/PlainStringScalar.java @@ -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 (amihaiemil@gmail.com) * @version $Id$ * @since 1.0.0 @@ -94,7 +89,13 @@ 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 @@ -102,4 +103,25 @@ 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; + } } diff --git a/src/test/java/com/amihaiemil/eoyaml/ReadFlowMappingTestCase.java b/src/test/java/com/amihaiemil/eoyaml/ReadFlowMappingTestCase.java index f1185d6c..0be460cf 100644 --- a/src/test/java/com/amihaiemil/eoyaml/ReadFlowMappingTestCase.java +++ b/src/test/java/com/amihaiemil/eoyaml/ReadFlowMappingTestCase.java @@ -89,7 +89,7 @@ public void returnsKeys() { final Iterator iterator = keys.iterator(); MatcherAssert.assertThat( iterator.next().toString(), - Matchers.equalTo("- 'a'") + Matchers.equalTo("- a") ); MatcherAssert.assertThat( iterator.next().toString(), @@ -170,7 +170,7 @@ public void returnsAllValues() { Matchers.equalTo( "---" + System.lineSeparator() - + "'b'" + + "b" + System.lineSeparator() + "..." ) @@ -214,7 +214,7 @@ public void returnsAllValues() { Matchers.equalTo( "- a" + System.lineSeparator() - + "- '0,3'" + + "- \"0,3\"" + System.lineSeparator() + "- \"2, 3, 4\"" + System.lineSeparator() diff --git a/src/test/java/com/amihaiemil/eoyaml/ReadFlowSequenceTestCase.java b/src/test/java/com/amihaiemil/eoyaml/ReadFlowSequenceTestCase.java index e17b5841..ca4b720b 100644 --- a/src/test/java/com/amihaiemil/eoyaml/ReadFlowSequenceTestCase.java +++ b/src/test/java/com/amihaiemil/eoyaml/ReadFlowSequenceTestCase.java @@ -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() { @@ -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); @@ -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));