Repository: decorators-squad/eo-yaml (java)

Issue:
Introduce interface ScalarComment
Interface ScalarComment will extend interface Comment and provide methods above() and inline() to represent the 2 comments that a Scalar can have. 

We will have only 1 implementation, ReadScalarComment (no BuiltScalarComment, we will not allow the user to specify 2 comments). 

Pull request description:
#458 ReadScalarComment implemented and tested
Fixes #458 

Left puzzle to use this class inside ReadPlainScalar.comment()

Patch:
diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadScalarComment.java b/src/main/java/com/amihaiemil/eoyaml/ReadScalarComment.java
new file mode 100644
index 0000000..f5870c9
--- /dev/null
+++ b/src/main/java/com/amihaiemil/eoyaml/ReadScalarComment.java
@@ -0,0 +1,96 @@
+/**
+ * Copyright (c) 2016-2021, Mihai Emil Andronache
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ *  list of conditions and the following disclaimer.
+ *  Redistributions in binary form must reproduce the above copyright notice,
+ *  this list of conditions and the following disclaimer in the documentation
+ *  and/or other materials provided with the distribution.
+ * Neither the name of the copyright holder nor the names of its
+ *  contributors may be used to endorse or promote products derived from
+ *  this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+package com.amihaiemil.eoyaml;
+
+/**
+ * A ScalarComment read from somewhere.
+ * @author Mihai Andronache (amihaiemil@gmail.com)
+ * @version $Id$
+ * @since 5.2.1
+ * @todo #458:30min Use this class inside ReadPlainScalar.comment() to read
+ *  both the comment which is above and the one which is inline.
+ */
+final class ReadScalarComment implements ScalarComment {
+
+    /**
+     * Comment above the scalar.
+     */
+    private final Comment above;
+
+    /**
+     * Inline Comment.
+     */
+    private final Comment inline;
+
+    /**
+     * Ctor.
+     * @param above Comment above the scalar.
+     * @param inline Comment inline with the scalar.
+     */
+    ReadScalarComment(
+        final Comment above,
+        final Comment inline
+    ) {
+        this.above = above;
+        this.inline = inline;
+    }
+
+    @Override
+    public YamlNode yamlNode() {
+        return this.inline.yamlNode();
+    }
+
+    @Override
+    public String value() {
+        final StringBuilder comment = new StringBuilder();
+        final String aboveValue = this.above.value();
+        final String inlineValue = this.inline.value();
+
+        if(inlineValue.trim().isEmpty()){
+            comment.append(aboveValue);
+        } else if(aboveValue.trim().isEmpty()){
+            comment.append(inlineValue);
+        } else {
+            comment
+                .append(aboveValue)
+                .append(System.lineSeparator())
+                .append(inlineValue);
+        }
+        return comment.toString();
+    }
+
+    @Override
+    public Comment above() {
+        return this.above;
+    }
+
+    @Override
+    public Comment inline() {
+        return this.inline;
+    }
+}
diff --git a/src/main/java/com/amihaiemil/eoyaml/ScalarComment.java b/src/main/java/com/amihaiemil/eoyaml/ScalarComment.java
new file mode 100644
index 0000000..6a1dc3d
--- /dev/null
+++ b/src/main/java/com/amihaiemil/eoyaml/ScalarComment.java
@@ -0,0 +1,51 @@
+/**
+ * Copyright (c) 2016-2021, Mihai Emil Andronache
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ *  list of conditions and the following disclaimer.
+ *  Redistributions in binary form must reproduce the above copyright notice,
+ *  this list of conditions and the following disclaimer in the documentation
+ *  and/or other materials provided with the distribution.
+ * Neither the name of the copyright holder nor the names of its
+ *  contributors may be used to endorse or promote products derived from
+ *  this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+package com.amihaiemil.eoyaml;
+
+/**
+ * A scalar can have 2 comments: 1 on top of it and 1 inline. This interface
+ * represents both of them.
+ * @author Mihai Andronache (amihaiemil@gmail.com)
+ * @version $Id$
+ * @since 5.2.1
+ */
+public interface ScalarComment extends Comment {
+
+    /**
+     * Comment above the Scalar.
+     * @return Comment.
+     */
+    Comment above();
+
+    /**
+     * Comment inline with the Scalar.
+     * @return Comment.
+     */
+    Comment inline();
+
+}


Test patch:
diff --git a/src/test/java/com/amihaiemil/eoyaml/ReadScalarCommentTest.java b/src/test/java/com/amihaiemil/eoyaml/ReadScalarCommentTest.java
new file mode 100644
index 0000000..fd8d1f5
--- /dev/null
+++ b/src/test/java/com/amihaiemil/eoyaml/ReadScalarCommentTest.java
@@ -0,0 +1,156 @@
+/**
+ * Copyright (c) 2016-2021, Mihai Emil Andronache
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ *  list of conditions and the following disclaimer.
+ *  Redistributions in binary form must reproduce the above copyright notice,
+ *  this list of conditions and the following disclaimer in the documentation
+ *  and/or other materials provided with the distribution.
+ * Neither the name of the copyright holder nor the names of its
+ *  contributors may be used to endorse or promote products derived from
+ *  this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+package com.amihaiemil.eoyaml;
+
+import org.hamcrest.MatcherAssert;
+import org.hamcrest.Matchers;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+/**
+ * Unit tests for {@link ReadScalarComment}.
+ * @author Mihai Andronache (amihaiemil@gmail.com)
+ * @version $Id$
+ * @since 5.2.1
+ */
+public final class ReadScalarCommentTest {
+
+    /**
+     * ReadScalarComment can return the Comment above.
+     */
+    @Test
+    public void returnsCommentAbove() {
+        final Comment above = Mockito.mock(Comment.class);
+        final Comment inline = Mockito.mock(Comment.class);
+        MatcherAssert.assertThat(
+            new ReadScalarComment(above, inline).above(),
+            Matchers.is(above)
+        );
+    }
+
+    /**
+     * ReadScalarComment can return the inline Comment.
+     */
+    @Test
+    public void returnsInlineComment() {
+        final Comment above = Mockito.mock(Comment.class);
+        final Comment inline = Mockito.mock(Comment.class);
+        MatcherAssert.assertThat(
+            new ReadScalarComment(above, inline).inline(),
+            Matchers.is(inline)
+        );
+    }
+
+    /**
+     * ReadScalarComment can return the parent YamlNode.
+     */
+    @Test
+    public void returnsNode() {
+        final YamlNode parent = Mockito.mock(YamlNode.class);
+        final Comment above = Mockito.mock(Comment.class);
+        final Comment inline = Mockito.mock(Comment.class);
+        Mockito.when(inline.yamlNode()).thenReturn(parent);
+
+        MatcherAssert.assertThat(
+            new ReadScalarComment(above, inline).yamlNode(),
+            Matchers.is(parent)
+        );
+    }
+
+    /**
+     * ReadScalarComment returns the concatenated (with newline) value of
+     * both comments.
+     */
+    @Test
+    public void returnsConcatenatedValue(){
+        final Comment above = Mockito.mock(Comment.class);
+        Mockito.when(above.value()).thenReturn("Comment above on\nmore lines.");
+        final Comment inline = Mockito.mock(Comment.class);
+        Mockito.when(inline.value()).thenReturn("And inline comment.");
+
+        MatcherAssert.assertThat(
+            new ReadScalarComment(above, inline).value(),
+            Matchers.equalTo(
+                "Comment above on\n"
+                + "more lines.\n"
+                + "And inline comment."
+            )
+        );
+    }
+
+    /**
+     * ReadScalarComment.value() is only the inline comment if the above
+     * Comment is empty.
+     */
+    @Test
+    public void returnsValueOfInlineCommentOnly() {
+        final Comment above = Mockito.mock(Comment.class);
+        Mockito.when(above.value()).thenReturn("");
+        final Comment inline = Mockito.mock(Comment.class);
+        Mockito.when(inline.value()).thenReturn("Inline comment.");
+
+        MatcherAssert.assertThat(
+            new ReadScalarComment(above, inline).value(),
+            Matchers.equalTo("Inline comment.")
+        );
+    }
+
+    /**
+     * ReadScalarComment.value() is only the above comment if the inline
+     * Comment is empty.
+     */
+    @Test
+    public void returnsValueOfAboveCommentOnly() {
+        final Comment above = Mockito.mock(Comment.class);
+        Mockito.when(above.value()).thenReturn("Comment above on\nmore lines.");
+        final Comment inline = Mockito.mock(Comment.class);
+        Mockito.when(inline.value()).thenReturn("");
+
+        MatcherAssert.assertThat(
+            new ReadScalarComment(above, inline).value(),
+            Matchers.equalTo("Comment above on\nmore lines.")
+        );
+    }
+
+    /**
+     * ReadScalarComment.value() returns empty string if both comments are
+     * empty.
+     */
+    @Test
+    public void returnsEmptyComment() {
+        final Comment above = Mockito.mock(Comment.class);
+        Mockito.when(above.value()).thenReturn("");
+        final Comment inline = Mockito.mock(Comment.class);
+        Mockito.when(inline.value()).thenReturn("");
+
+        MatcherAssert.assertThat(
+            new ReadScalarComment(above, inline).value(),
+            Matchers.isEmptyString()
+        );
+    }
+}