Repository: decorators-squad/eo-yaml
Pull request #290 (2020-04-06 14:06:04)

Implemented Scalar.comment()
PR for #288 
Implemented and tested method ``comment()`` in all scalars.
Next step: implement building of comments into scalars (modify the scalar builder to also accept comments)


Patch:
diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md
deleted file mode 100644
index 82d5b02..0000000
--- a/CODE_OF_CONDUCT.md
+++ /dev/null
@@ -1,4 +0,0 @@
-1. This repo should be an intelectual warfare zone, particularly focused on Object-Oriented Programming.
-2. Be nice to each other. Don't take rejected PRs personally.
-3. Try not to spam Issues or PRs with useless comments.
-3. Enjoy yourself.
diff --git a/src/main/java/com/amihaiemil/eoyaml/AllYamlLines.java b/src/main/java/com/amihaiemil/eoyaml/AllYamlLines.java
index 1bff40a..adc256e 100644
--- a/src/main/java/com/amihaiemil/eoyaml/AllYamlLines.java
+++ b/src/main/java/com/amihaiemil/eoyaml/AllYamlLines.java
@@ -115,7 +115,7 @@ final class AllYamlLines implements YamlLines {
         } else if (first.trimmed().contains(":")){
             node = new ReadYamlMapping(prev, this);
         } else if(this.original().size() == 1) {
-            node = new ReadPlainScalar(first);
+            node = new ReadPlainScalar(this, first);
         } else {
             throw new YamlReadingException(
                 "Could not parse YAML starting at line " + (first.number() + 1)
diff --git a/src/main/java/com/amihaiemil/eoyaml/BaseScalar.java b/src/main/java/com/amihaiemil/eoyaml/BaseScalar.java
index db9d1ca..a90180a 100644
--- a/src/main/java/com/amihaiemil/eoyaml/BaseScalar.java
+++ b/src/main/java/com/amihaiemil/eoyaml/BaseScalar.java
@@ -130,11 +130,4 @@ abstract class BaseScalar extends BaseYamlNode implements Scalar {
         return printed.append(this.value()).toString();
     }
 
-    /**
-     * Get the Comment referring to this YamlNode.
-     * @return Comment.
-     */
-    public Comment comment() {
-        return null;
-    }
 }
diff --git a/src/main/java/com/amihaiemil/eoyaml/FirstCommentFound.java b/src/main/java/com/amihaiemil/eoyaml/FirstCommentFound.java
index b2d3961..4621df1 100644
--- a/src/main/java/com/amihaiemil/eoyaml/FirstCommentFound.java
+++ b/src/main/java/com/amihaiemil/eoyaml/FirstCommentFound.java
@@ -35,7 +35,7 @@ import java.util.List;
 /**
  * Iterate over the lines which are YAML comments (begin with "#") and break
  * iteration when a non-comment line is found. In essence, this reads the lines
- * of the first comment from a given YamlLines.
+ * of the first comment from a given YamlLines, if it exists.
  * @author Mihai Andronache (amihaiemil@gmail.com)
  * @version $Id$
  * @since 4.2.0
diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadFoldedBlockScalar.java b/src/main/java/com/amihaiemil/eoyaml/ReadFoldedBlockScalar.java
index e5f3f1d..72c90cd 100644
--- a/src/main/java/com/amihaiemil/eoyaml/ReadFoldedBlockScalar.java
+++ b/src/main/java/com/amihaiemil/eoyaml/ReadFoldedBlockScalar.java
@@ -55,18 +55,23 @@ final class ReadFoldedBlockScalar extends BaseScalar {
      * In the above example the scalar consists of line1 and line2, while
      * "previous" is line 0.
      */
-    private YamlLine previous;
+    private final YamlLine previous;
 
     /**
-     * Lines to be represented as a wrapped scalar.
+     * All lines of the YAML document.
      */
-    private YamlLines lines;
+    private final AllYamlLines all;
+
+    /**
+     * The significant lines of this folded block scalar.
+     */
+    private final YamlLines significant;
 
     /**
      * Ctor.
-     * @param lines Given lines to represent.
+     * @param lines All lines.
      */
-    ReadFoldedBlockScalar(final YamlLines lines) {
+    ReadFoldedBlockScalar(final AllYamlLines lines) {
         this(new YamlLine.NullYamlLine(), lines);
     }
 
@@ -75,9 +80,10 @@ final class ReadFoldedBlockScalar extends BaseScalar {
      * @param previous Previous YAML line.
      * @param lines Given lines to represent.
      */
-    ReadFoldedBlockScalar(final YamlLine previous, final YamlLines lines) {
+    ReadFoldedBlockScalar(final YamlLine previous, final AllYamlLines lines) {
         this.previous = previous;
-        this.lines = new GreaterIndentation(
+        this.all = lines;
+        this.significant = new GreaterIndentation(
             previous,
             new Skip(
                 lines,
@@ -100,7 +106,7 @@ final class ReadFoldedBlockScalar extends BaseScalar {
             spaces--;
         }
         StringBuilder printed = new StringBuilder();
-        for(final YamlLine line: this.lines) {
+        for(final YamlLine line: this.significant) {
             printed.append(alignment);
             printed.append(line.trimmed());
             printed.append(System.lineSeparator());
@@ -125,7 +131,7 @@ final class ReadFoldedBlockScalar extends BaseScalar {
     public String value() {
         StringBuilder builder = new StringBuilder();
         final String newLine = System.lineSeparator();
-        for(final YamlLine line: this.lines) {
+        for(final YamlLine line: this.significant) {
             if(line.trimmed().length() == 0 || line.indentation() > 0) {
                 if(this.doNotEndWithNewLine(builder)) {
                     builder.append(newLine);
@@ -146,6 +152,38 @@ final class ReadFoldedBlockScalar extends BaseScalar {
         return builder.toString();
     }
 
+    @Override
+    public Comment comment() {
+        return new ReadComment(
+            new FirstCommentFound(
+                new Backwards(
+                    new Skip(
+                        this.all,
+                        line -> {
+                            final boolean skip;
+                            if(this.previous.number() < 0) {
+                                if(this.significant.iterator().hasNext()) {
+                                    skip = line.number() >= this.significant
+                                            .iterator().next().number();
+                                } else {
+                                    skip = false;
+                                }
+                            } else {
+                                skip = line.number() >= this.previous.number();
+                            }
+                            return skip;
+                        },
+                        line -> line.trimmed().startsWith("---"),
+                        line -> line.trimmed().startsWith("..."),
+                        line -> line.trimmed().startsWith("%"),
+                        line -> line.trimmed().startsWith("!!")
+                    )
+                )
+            ),
+            this
+        );
+    }
+
     /**
      * When printing a folded scalar, we have to wrap it
      * inside proper YAML elements, otherwise it won't make
diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadLiteralBlockScalar.java b/src/main/java/com/amihaiemil/eoyaml/ReadLiteralBlockScalar.java
index c471834..e601899 100644
--- a/src/main/java/com/amihaiemil/eoyaml/ReadLiteralBlockScalar.java
+++ b/src/main/java/com/amihaiemil/eoyaml/ReadLiteralBlockScalar.java
@@ -54,29 +54,35 @@ final class ReadLiteralBlockScalar extends BaseScalar {
      * In the above example the scalar consists of line1 and line2, while
      * "previous" is line 0.
      */
-    private YamlLine previous;
+    private final YamlLine previous;
 
     /**
-     * Lines to be represented as a wrapped scalar.
+     * All lines of the YAML document.
      */
-    private YamlLines lines;
+    private final AllYamlLines all;
+
+    /**
+     * The significant lines of this literal block scalar.
+     */
+    private final YamlLines significant;
 
     /**
      * Ctor.
-     * @param lines Given lines to represent.
+     * @param lines All lines.
      */
-    ReadLiteralBlockScalar(final YamlLines lines) {
+    ReadLiteralBlockScalar(final AllYamlLines lines) {
         this(new YamlLine.NullYamlLine(), lines);
     }
 
     /**
      * Ctor.
      * @param previous Previous YAML line.
-     * @param lines Given lines to represent.
+     * @param lines All yaml lines.
      */
-    ReadLiteralBlockScalar(final YamlLine previous, final YamlLines lines) {
+    ReadLiteralBlockScalar(final YamlLine previous, final AllYamlLines lines) {
         this.previous = previous;
-        this.lines = new GreaterIndentation(
+        this.all = lines;
+        this.significant = new GreaterIndentation(
             previous,
             new Skip(
                 lines,
@@ -99,7 +105,7 @@ final class ReadLiteralBlockScalar extends BaseScalar {
             spaces--;
         }
         StringBuilder printed = new StringBuilder();
-        for(final YamlLine line: this.lines) {
+        for(final YamlLine line: this.significant) {
             printed.append(alignment);
             printed.append(line.trimmed());
             printed.append(System.lineSeparator());
@@ -114,7 +120,7 @@ final class ReadLiteralBlockScalar extends BaseScalar {
      */
     public String value() {
         StringBuilder builder = new StringBuilder();
-        for(final YamlLine line: this.lines) {
+        for(final YamlLine line: this.significant) {
             builder.append(line.trimmed());
             builder.append(System.lineSeparator());
         }
@@ -124,6 +130,38 @@ final class ReadLiteralBlockScalar extends BaseScalar {
         return builder.toString();
     }
 
+    @Override
+    public Comment comment() {
+        return new ReadComment(
+            new FirstCommentFound(
+                new Backwards(
+                    new Skip(
+                        this.all,
+                        line -> {
+                            final boolean skip;
+                            if(this.previous.number() < 0) {
+                                if(this.significant.iterator().hasNext()) {
+                                    skip = line.number() >= this.significant
+                                            .iterator().next().number();
+                                } else {
+                                    skip = false;
+                                }
+                            } else {
+                                skip = line.number() >= this.previous.number();
+                            }
+                            return skip;
+                        },
+                        line -> line.trimmed().startsWith("---"),
+                        line -> line.trimmed().startsWith("..."),
+                        line -> line.trimmed().startsWith("%"),
+                        line -> line.trimmed().startsWith("!!")
+                    )
+                )
+            ),
+            this
+        );
+    }
+
     /**
      * When printing a literal scalar, we have to wrap it
      * inside proper YAML elements, otherwise it won't make
diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadPlainScalar.java b/src/main/java/com/amihaiemil/eoyaml/ReadPlainScalar.java
index 60726cd..55459f8 100644
--- a/src/main/java/com/amihaiemil/eoyaml/ReadPlainScalar.java
+++ b/src/main/java/com/amihaiemil/eoyaml/ReadPlainScalar.java
@@ -35,6 +35,11 @@ package com.amihaiemil.eoyaml;
  */
 final class ReadPlainScalar extends BaseScalar {
 
+    /**
+     * All YAML Lines of the document.
+     */
+    private final AllYamlLines all;
+
     /**
      * Line where the plain scalar value is supposed to be found.
      * The Scalar can be either after the ":" character, if this
@@ -42,20 +47,22 @@ final class ReadPlainScalar extends BaseScalar {
      * this line is from a sequence, or it represents the whole line,
      * if no "-" or ":" are found.
      */
-    private YamlLine line;
+    private final YamlLine scalar;
 
     /**
      * Constructor.
-     * @param line Read YamlLine.
+     * @param all All lines of the document.
+     * @param scalar YamlLine containing the scalar.
      */
-    ReadPlainScalar(final YamlLine line) {
-        this.line = line;
+    ReadPlainScalar(final AllYamlLines all, final YamlLine scalar) {
+        this.all = all;
+        this.scalar = scalar;
     }
 
     @Override
     public String value() {
         final String value;
-        final String trimmed = this.line.trimmed();
+        final String trimmed = this.scalar.trimmed();
         if(trimmed.startsWith("-") && trimmed.length() > 1) {
             value = trimmed.substring(trimmed.indexOf('-')+1).trim();
         } else if(trimmed.contains(":") && !trimmed.endsWith(":")) {
@@ -66,6 +73,31 @@ final class ReadPlainScalar extends BaseScalar {
         return this.unescape(value);
     }
 
+    @Override
+    public Comment comment() {
+        final Comment comment;
+        if(this.scalar instanceof YamlLine.NullYamlLine) {
+            comment = new BuiltComment(this, "");
+        } else {
+            comment = new ReadComment(
+                new FirstCommentFound(
+                    new Backwards(
+                        new Skip(
+                            this.all,
+                            line -> line.number() >= this.scalar.number(),
+                            line -> line.trimmed().startsWith("---"),
+                            line -> line.trimmed().startsWith("..."),
+                            line -> line.trimmed().startsWith("%"),
+                            line -> line.trimmed().startsWith("!!")
+                        )
+                    )
+                ),
+                this
+            );
+        }
+        return comment;
+    }
+
     /**
      * Remove the possible escaping quotes or apostrophes surrounding the
      * given value.
diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadYamlMapping.java b/src/main/java/com/amihaiemil/eoyaml/ReadYamlMapping.java
index dd501b9..8ab0403 100644
--- a/src/main/java/com/amihaiemil/eoyaml/ReadYamlMapping.java
+++ b/src/main/java/com/amihaiemil/eoyaml/ReadYamlMapping.java
@@ -59,14 +59,14 @@ final class ReadYamlMapping extends BaseYamlMapping {
     private YamlLine previous;
 
     /**
-     * Lines of this mapping also containing the comments.
+     * All the lines of this YAML document.
      */
-    private YamlLines comments;
+    private final AllYamlLines all;
 
     /**
      * Only the significant lines of this YamlMapping.
      */
-    private YamlLines significant;
+    private final YamlLines significant;
 
     /**
      * Ctor.
@@ -83,6 +83,7 @@ final class ReadYamlMapping extends BaseYamlMapping {
      */
     ReadYamlMapping(final YamlLine previous, final AllYamlLines lines) {
         this.previous = previous;
+        this.all = lines;
         this.significant = new SameIndentationLevel(
             new WellIndented(
                 new Skip(
@@ -96,29 +97,6 @@ final class ReadYamlMapping extends BaseYamlMapping {
                 )
             )
         );
-        this.comments = new WellIndented(
-            new Skip(
-                lines,
-                line -> {
-                    final boolean skip;
-                    if(previous.number() < 0) {
-                        if(this.significant.iterator().hasNext()) {
-                            skip = line.number() >= this.significant
-                                    .iterator().next().number();
-                        } else {
-                            skip = false;
-                        }
-                    } else {
-                        skip = line.number() >= previous.number();
-                    }
-                    return skip;
-                },
-                line -> line.trimmed().startsWith("---"),
-                line -> line.trimmed().startsWith("..."),
-                line -> line.trimmed().startsWith("%"),
-                line -> line.trimmed().startsWith("!!")
-            )
-        );
     }
 
     @Override
@@ -183,7 +161,27 @@ final class ReadYamlMapping extends BaseYamlMapping {
         return new ReadComment(
             new FirstCommentFound(
                 new Backwards(
-                    this.comments
+                    new Skip(
+                        this.all,
+                        line -> {
+                            final boolean skip;
+                            if(this.previous.number() < 0) {
+                                if(this.significant.iterator().hasNext()) {
+                                    skip = line.number() >= this.significant
+                                            .iterator().next().number();
+                                } else {
+                                    skip = false;
+                                }
+                            } else {
+                                skip = line.number() >= this.previous.number();
+                            }
+                            return skip;
+                        },
+                        line -> line.trimmed().startsWith("---"),
+                        line -> line.trimmed().startsWith("..."),
+                        line -> line.trimmed().startsWith("%"),
+                        line -> line.trimmed().startsWith("!!")
+                    )
                 )
             ),
             this
@@ -271,7 +269,7 @@ final class ReadYamlMapping extends BaseYamlMapping {
             } else if(trimmed.startsWith(key + ":")
                 && trimmed.length() > 1
             ) {
-                value = new ReadPlainScalar(line);
+                value = new ReadPlainScalar(this.all, line);
             }
         }
         return value;
@@ -301,7 +299,7 @@ final class ReadYamlMapping extends BaseYamlMapping {
                     } else if(colonLine.trimmed().startsWith(":")
                         && (colonLine.trimmed().length() > 1)
                     ){
-                        value = new ReadPlainScalar(colonLine);
+                        value = new ReadPlainScalar(this.all, colonLine);
                     } else {
                         throw new YamlReadingException(
                             "No value found for existing complex key: "
diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadYamlSequence.java b/src/main/java/com/amihaiemil/eoyaml/ReadYamlSequence.java
index 9aa5f00..57d42f8 100644
--- a/src/main/java/com/amihaiemil/eoyaml/ReadYamlSequence.java
+++ b/src/main/java/com/amihaiemil/eoyaml/ReadYamlSequence.java
@@ -52,17 +52,17 @@ final class ReadYamlSequence extends BaseYamlSequence {
      * 1  - elem2
      * </pre>
      */
-    private YamlLine previous;
+    private final YamlLine previous;
 
     /**
-     * Lines of this mapping also containing the comments.
+     * All lines of the YAML document.
      */
-    private YamlLines comments;
+    private final AllYamlLines all;
 
     /**
      * Only the significant lines of this sequence.
      */
-    private YamlLines significant;
+    private final YamlLines significant;
 
     /**
      * Ctor.
@@ -79,6 +79,7 @@ final class ReadYamlSequence extends BaseYamlSequence {
      */
     ReadYamlSequence(final YamlLine previous, final AllYamlLines lines) {
         this.previous = previous;
+        this.all = lines;
         this.significant = new SameIndentationLevel(
             new WellIndented(
                 new Skip(
@@ -92,29 +93,6 @@ final class ReadYamlSequence extends BaseYamlSequence {
                 )
             )
         );
-        this.comments = new WellIndented(
-            new Skip(
-                lines,
-                line -> {
-                    final boolean skip;
-                    if(previous.number() < 0) {
-                        if(this.significant.iterator().hasNext()) {
-                            skip = line.number() >= this.significant
-                                    .iterator().next().number();
-                        } else {
-                            skip = false;
-                        }
-                    } else {
-                        skip = line.number() >= previous.number();
-                    }
-                    return skip;
-                },
-                line -> line.trimmed().startsWith("---"),
-                line -> line.trimmed().startsWith("..."),
-                line -> line.trimmed().startsWith("%"),
-                line -> line.trimmed().startsWith("!!")
-            )
-        );
     }
 
     @Override
@@ -128,7 +106,7 @@ final class ReadYamlSequence extends BaseYamlSequence {
             ) {
                 kids.add(this.significant.toYamlNode(line));
             } else {
-                kids.add(new ReadPlainScalar(line));
+                kids.add(new ReadPlainScalar(this.all, line));
             }
         }
         return kids;
@@ -232,7 +210,27 @@ final class ReadYamlSequence extends BaseYamlSequence {
         return new ReadComment(
             new FirstCommentFound(
                 new Backwards(
-                    this.comments
+                    new Skip(
+                        this.all,
+                        line -> {
+                            final boolean skip;
+                            if(this.previous.number() < 0) {
+                                if(this.significant.iterator().hasNext()) {
+                                    skip = line.number() >= this.significant
+                                            .iterator().next().number();
+                                } else {
+                                    skip = false;
+                                }
+                            } else {
+                                skip = line.number() >= this.previous.number();
+                            }
+                            return skip;
+                        },
+                        line -> line.trimmed().startsWith("---"),
+                        line -> line.trimmed().startsWith("..."),
+                        line -> line.trimmed().startsWith("%"),
+                        line -> line.trimmed().startsWith("!!")
+                    )
                 )
             ),
             this
diff --git a/src/main/java/com/amihaiemil/eoyaml/RtYamlInput.java b/src/main/java/com/amihaiemil/eoyaml/RtYamlInput.java
index 53349d1..ab40dbb 100644
--- a/src/main/java/com/amihaiemil/eoyaml/RtYamlInput.java
+++ b/src/main/java/com/amihaiemil/eoyaml/RtYamlInput.java
@@ -74,8 +74,9 @@ final class RtYamlInput implements YamlInput {
     @Override
     public Scalar readPlainScalar() throws IOException {
         final ReadPlainScalar read;
+        final AllYamlLines all = this.readInput();
         final Iterator<YamlLine> iterator = new Skip(
-            this.readInput(),
+            all,
             line -> line.trimmed().startsWith("#"),
             line -> line.trimmed().startsWith("---"),
             line -> line.trimmed().startsWith("..."),
@@ -83,9 +84,9 @@ final class RtYamlInput implements YamlInput {
             line -> line.trimmed().startsWith("!!")
         ).iterator();
         if(!iterator.hasNext()) {
-            read = new ReadPlainScalar(new YamlLine.NullYamlLine());
+            read = new ReadPlainScalar(all, new YamlLine.NullYamlLine());
         } else {
-            read = new ReadPlainScalar(iterator.next());
+            read = new ReadPlainScalar(all, iterator.next());
         }
         return read;
     }


Test patch:
diff --git a/src/test/java/com/amihaiemil/eoyaml/ReadFoldedBlockScalarTest.java b/src/test/java/com/amihaiemil/eoyaml/ReadFoldedBlockScalarTest.java
index e3c4903..c7e665a 100644
--- a/src/test/java/com/amihaiemil/eoyaml/ReadFoldedBlockScalarTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/ReadFoldedBlockScalarTest.java
@@ -131,6 +131,28 @@ public final class ReadFoldedBlockScalarTest {
         );
     }
 
+    /**
+     * ReadFoldedBlockScalar can return the value referring to it.
+     */
+    @Test
+    public void returnsComment() {
+        final List<YamlLine> lines = new ArrayList<>();
+        lines.add(new RtYamlLine("key: value", 0));
+        lines.add(new RtYamlLine("# Folded scalar as value in map", 1));
+        lines.add(new RtYamlLine("folded: >", 2));
+        lines.add(new RtYamlLine("  Mark McGwire's", 3));
+        lines.add(new RtYamlLine("  year was crippled", 4));
+        lines.add(new RtYamlLine("  by a knee injury.", 5));
+        final ReadFoldedBlockScalar scalar =
+            new ReadFoldedBlockScalar(lines.get(2), new AllYamlLines(lines));
+        final Comment comment = scalar.comment();
+        MatcherAssert.assertThat(
+            comment.value(),
+            Matchers.equalTo("Folded scalar as value in map")
+        );
+        MatcherAssert.assertThat(comment.yamlNode(), Matchers.is(scalar));
+    }
+
     /**
      * ReadPointedScalar works with Spec Example 2.15:
      * Folded newlines are preserved for "more indented"
diff --git a/src/test/java/com/amihaiemil/eoyaml/ReadLiteralBlockScalarTest.java b/src/test/java/com/amihaiemil/eoyaml/ReadLiteralBlockScalarTest.java
index 72ff539..666e023 100644
--- a/src/test/java/com/amihaiemil/eoyaml/ReadLiteralBlockScalarTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/ReadLiteralBlockScalarTest.java
@@ -62,6 +62,28 @@ public final class ReadLiteralBlockScalarTest {
         );
     }
 
+    /**
+     * ReadLiteralBlockScalar can return the value referring to it.
+     */
+    @Test
+    public void returnsComment() {
+        final List<YamlLine> lines = new ArrayList<>();
+        lines.add(new RtYamlLine("key: value", 0));
+        lines.add(new RtYamlLine("# Literal scalar as value in map", 1));
+        lines.add(new RtYamlLine("literal: |", 2));
+        lines.add(new RtYamlLine("  line 1", 3));
+        lines.add(new RtYamlLine("  line 2", 4));
+        lines.add(new RtYamlLine("  line 3", 5));
+        final ReadLiteralBlockScalar scalar =
+            new ReadLiteralBlockScalar(lines.get(2), new AllYamlLines(lines));
+        final Comment comment = scalar.comment();
+        MatcherAssert.assertThat(
+            comment.value(),
+            Matchers.equalTo("Literal scalar as value in map")
+        );
+        MatcherAssert.assertThat(comment.yamlNode(), Matchers.is(scalar));
+    }
+
     /**
      * ReadPipeScalar can compare itself to a Mapping.
      */
diff --git a/src/test/java/com/amihaiemil/eoyaml/ReadPlainScalarValueTest.java b/src/test/java/com/amihaiemil/eoyaml/ReadPlainScalarTest.java
similarity index 62%
rename from src/test/java/com/amihaiemil/eoyaml/ReadPlainScalarValueTest.java
rename to src/test/java/com/amihaiemil/eoyaml/ReadPlainScalarTest.java
index f5d9198..5320e1b 100644
--- a/src/test/java/com/amihaiemil/eoyaml/ReadPlainScalarValueTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/ReadPlainScalarTest.java
@@ -31,13 +31,16 @@ import org.hamcrest.MatcherAssert;
 import org.hamcrest.Matchers;
 import org.junit.Test;
 
+import java.util.ArrayList;
+import java.util.List;
+
 /**
- * Unit tests for {@ling ReadPlainScalarValue}.
+ * Unit tests for {@ling ReadPlainScalar}.
  * @author Mihai Andronache (amihaiemil@gmail.com)
  * @version $Id$
  * @since 3.1.3
  */
-public final class ReadPlainScalarValueTest {
+public final class ReadPlainScalarTest {
 
     /**
      * ReadPlainScalar can return the scalar's value from a mapping line.
@@ -45,6 +48,7 @@ public final class ReadPlainScalarValueTest {
     @Test
     public void returnsValueFromMappingLine() {
         final Scalar scalar = new ReadPlainScalar(
+            new AllYamlLines(new ArrayList<>()),
             new RtYamlLine("key: value", 0)
         );
         MatcherAssert.assertThat(scalar.value(), Matchers.equalTo("value"));
@@ -56,18 +60,66 @@ public final class ReadPlainScalarValueTest {
     @Test
     public void returnsValueFromSequenceLine() {
         final Scalar scalar = new ReadPlainScalar(
+            new AllYamlLines(new ArrayList<>()),
             new RtYamlLine("- value", 0)
         );
         MatcherAssert.assertThat(scalar.value(), Matchers.equalTo("value"));
     }
 
+    /**
+     * ReadPlainScalar can return the comment referring to a scalar
+     * in a sequence.
+     */
+    @Test
+    public void returnsCommentFromSequenceLine() {
+        final List<YamlLine> lines = new ArrayList<>();
+        lines.add(new RtYamlLine("- value1", 0));
+        lines.add(new RtYamlLine("# Comment about value 2", 1));
+        lines.add(new RtYamlLine("- value2", 2));
+        lines.add(new RtYamlLine("- value3", 3));
+        final Scalar scalar = new ReadPlainScalar(
+            new AllYamlLines(lines), lines.get(2)
+        );
+        final Comment comment = scalar.comment();
+        MatcherAssert.assertThat(
+            comment.value(),
+            Matchers.equalTo("Comment about value 2")
+        );
+        MatcherAssert.assertThat(comment.yamlNode(), Matchers.is(scalar));
+    }
+
+    /**
+     * ReadPlainScalar can return the comment referring to a scalar
+     * in a mapping.
+     */
+    @Test
+    public void returnsCommentFromMappingLine() {
+        final List<YamlLine> lines = new ArrayList<>();
+        lines.add(new RtYamlLine("key1: value1", 0));
+        lines.add(new RtYamlLine("# Comment about value 2", 1));
+        lines.add(new RtYamlLine("key2: value2", 2));
+        lines.add(new RtYamlLine("key3: value3", 3));
+        final Scalar scalar = new ReadPlainScalar(
+            new AllYamlLines(lines), lines.get(2)
+        );
+        final Comment comment = scalar.comment();
+        MatcherAssert.assertThat(
+            comment.value(),
+            Matchers.equalTo("Comment about value 2")
+        );
+        MatcherAssert.assertThat(comment.yamlNode(), Matchers.is(scalar));
+    }
+
     /**
      * ReadPlainScalar will return the plain scalar.
      */
     @Test
     public void returnsPlainScalar() {
         final YamlLine line = new RtYamlLine("value", 0);
-        final Scalar scalar = new ReadPlainScalar(line);
+        final Scalar scalar = new ReadPlainScalar(
+            new AllYamlLines(new ArrayList<>()),
+            line
+        );
         MatcherAssert.assertThat(scalar.value(), Matchers.equalTo("value"));
     }
 
@@ -79,7 +131,10 @@ public final class ReadPlainScalarValueTest {
     @Test
     public void returnsEmptyScalar() {
         final YamlLine line = new RtYamlLine("", 0);
-        final Scalar scalar = new ReadPlainScalar(line);
+        final Scalar scalar = new ReadPlainScalar(
+            new AllYamlLines(new ArrayList<>()),
+            line
+        );
         MatcherAssert.assertThat(scalar.value(), Matchers.isEmptyString());
     }
 
@@ -89,6 +144,7 @@ public final class ReadPlainScalarValueTest {
     @Test
     public void toStringWorks() {
         final Scalar scalar = new ReadPlainScalar(
+            new AllYamlLines(new ArrayList<>()),
             new RtYamlLine("key: value", 0)
         );
         MatcherAssert.assertThat(