Repository: decorators-squad/eo-yaml
Pull request #461 (2021-06-06 13:12:49)

#460 ReadPlainScalar.comment() returns ReadScalarComment
PR for #460 

ReadPlainScalar.comment() uses ReadScalarComment to concatenate the comment above it and the one inline with it. 
Updated tests.


Patch:
diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadPlainScalar.java b/src/main/java/com/amihaiemil/eoyaml/ReadPlainScalar.java
index cbd7b8b..b3088c0 100644
--- a/src/main/java/com/amihaiemil/eoyaml/ReadPlainScalar.java
+++ b/src/main/java/com/amihaiemil/eoyaml/ReadPlainScalar.java
@@ -133,18 +133,14 @@ final class ReadPlainScalar extends BaseScalar {
     public Comment comment() {
         final Comment comment;
         if(this.scalar instanceof YamlLine.NullYamlLine) {
-            comment = new BuiltComment(this, "");
-        } else {
-            final Comment inline = new ReadComment(
-                    new Skip(
-                        this.all,
-                        line -> line.number() != this.scalar.number()
-                    ),
-                this
+            comment = new ReadScalarComment(
+                new BuiltComment(this, ""),
+                new BuiltComment(this, "")
             );
-            if(inline.value().trim().isEmpty()) {
-                final int lineNumber = this.scalar.number();
-                comment = new ReadComment(
+        } else {
+            final int lineNumber = this.scalar.number();
+            comment = new ReadScalarComment(
+                new ReadComment(
                     new Backwards(
                         new FirstCommentFound(
                             new Backwards(
@@ -160,10 +156,15 @@ final class ReadPlainScalar extends BaseScalar {
                         )
                     ),
                     this
-                );
-            } else {
-                comment = inline;
-            }
+                ),
+                new ReadComment(
+                    new Skip(
+                        this.all,
+                        line -> line.number() != lineNumber
+                    ),
+                    this
+                )
+            );
         }
         return comment;
     }
diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadScalarComment.java b/src/main/java/com/amihaiemil/eoyaml/ReadScalarComment.java
index f5870c9..9cbe7da 100644
--- a/src/main/java/com/amihaiemil/eoyaml/ReadScalarComment.java
+++ b/src/main/java/com/amihaiemil/eoyaml/ReadScalarComment.java
@@ -32,8 +32,6 @@ package com.amihaiemil.eoyaml;
  * @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 {
 


Test patch:
diff --git a/src/test/java/com/amihaiemil/eoyaml/YamlMappingCommentsPrintTest.java b/src/test/java/com/amihaiemil/eoyaml/YamlMappingCommentsPrintTest.java
index 928b15b..472b87d 100644
--- a/src/test/java/com/amihaiemil/eoyaml/YamlMappingCommentsPrintTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/YamlMappingCommentsPrintTest.java
@@ -233,7 +233,11 @@ public final class YamlMappingCommentsPrintTest {
         );
         MatcherAssert.assertThat(
             read.value("tech").comment().value(),
-            Matchers.equalTo("planet java")
+            Matchers.equalTo(
+                "eo-yaml is written in Java\n"
+                + "and it has no dependencies\n"
+                + "Java SE 8+"
+            )
         );
     }
 
diff --git a/src/test/java/com/amihaiemil/eoyaml/YamlSequenceCommentsPrintTest.java b/src/test/java/com/amihaiemil/eoyaml/YamlSequenceCommentsPrintTest.java
index 272d30d..d14e46f 100644
--- a/src/test/java/com/amihaiemil/eoyaml/YamlSequenceCommentsPrintTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/YamlSequenceCommentsPrintTest.java
@@ -198,7 +198,11 @@ public final class YamlSequenceCommentsPrintTest {
         );
         MatcherAssert.assertThat(
             values.next().comment().value(),
-            Matchers.equalTo("inline comment about element6")
+            Matchers.equalTo(
+                "this comment refers\n"
+                + "to the element6 scalar\n"
+                + "element6 also has an inline comment"
+            )
         );
     }
 
diff --git a/src/test/resources/scalarCommentsInMapping.yml b/src/test/resources/scalarCommentsInMapping.yml
index f070577..b570f4e 100644
--- a/src/test/resources/scalarCommentsInMapping.yml
+++ b/src/test/resources/scalarCommentsInMapping.yml
@@ -9,6 +9,6 @@ name: "eo-yaml" # name of the project
 # git web service
 provider: github
 devops: rultor
-# this comment is ignored because the scalar has
-# an inline comment (closer to it)
-tech: java # planet java
\ No newline at end of file
+# eo-yaml is written in Java
+# and it has no dependencies
+tech: java # Java SE 8+
\ No newline at end of file
diff --git a/src/test/resources/scalarCommentsInSequence.yml b/src/test/resources/scalarCommentsInSequence.yml
index 86a5bc9..3bac657 100644
--- a/src/test/resources/scalarCommentsInSequence.yml
+++ b/src/test/resources/scalarCommentsInSequence.yml
@@ -9,6 +9,6 @@
 # it's multiline
 - element4
 - element5
-# this comment is ignored because there is
-# an inline (closer comment)
-- element6 # inline comment about element6
\ No newline at end of file
+# this comment refers
+# to the element6 scalar
+- element6 # element6 also has an inline comment
\ No newline at end of file