Repository: decorators-squad/eo-yaml
Pull request #514 (2022-08-27 09:52:39)

#512 Added MutableYamlMappingBuilder and MutableYamlSequenceBuilder
PR for #512 


Patch:
diff --git a/src/main/java/com/amihaiemil/eoyaml/MutableYamlMappingBuilder.java b/src/main/java/com/amihaiemil/eoyaml/MutableYamlMappingBuilder.java
new file mode 100644
index 0000000..5cd58ad
--- /dev/null
+++ b/src/main/java/com/amihaiemil/eoyaml/MutableYamlMappingBuilder.java
@@ -0,0 +1,98 @@
+/**
+ * Copyright (c) 2016-2022, 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 java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * YamlMappingBuilder mutable implementation, for better memory cosumption.
+ * This class is <b>mutable and NOT thread-safe</b>.
+ * @author Mihai Andronache (amihaiemil@gmail.com)
+ * @version $Id$
+ * @since 6.1.0
+ */
+final class MutableYamlMappingBuilder implements YamlMappingBuilder {
+    /**
+     * Added pairs.
+     */
+    private final Map<YamlNode, YamlNode> pairs;
+
+    /**
+     * Default ctor.
+     */
+    MutableYamlMappingBuilder() {
+        this(new LinkedHashMap<>());
+    }
+
+    /**
+     * Constructor.
+     * @param pairs Pairs used in building the YamlMapping.
+     */
+    MutableYamlMappingBuilder(final Map<YamlNode, YamlNode> pairs) {
+        this.pairs = pairs;
+    }
+
+    @Override
+    public YamlMappingBuilder add(final String key, final String value) {
+        return this.add(
+            new PlainStringScalar(key),
+            new PlainStringScalar(value)
+        );
+    }
+
+    @Override
+    public YamlMappingBuilder add(final YamlNode key, final String value) {
+        return this.add(key, new PlainStringScalar(value));
+    }
+
+    @Override
+    public YamlMappingBuilder add(final String key, final YamlNode value) {
+        return this.add(new PlainStringScalar(key), value);
+    }
+
+    @Override
+    public YamlMappingBuilder add(final YamlNode key, final YamlNode value) {
+        if(key == null || key.isEmpty()) {
+            throw new IllegalArgumentException(
+                "The key in YamlMapping cannot be null or empty!"
+            );
+        }
+        this.pairs.put(key, value);
+        return this;
+    }
+
+    @Override
+    public YamlMapping build(final String comment) {
+        YamlMapping mapping = new RtYamlMapping(this.pairs, comment);
+        if (pairs.isEmpty()) {
+            mapping = new EmptyYamlMapping(mapping);
+        }
+        return mapping;
+    }
+}
diff --git a/src/main/java/com/amihaiemil/eoyaml/MutableYamlSequenceBuilder.java b/src/main/java/com/amihaiemil/eoyaml/MutableYamlSequenceBuilder.java
new file mode 100644
index 0000000..370a284
--- /dev/null
+++ b/src/main/java/com/amihaiemil/eoyaml/MutableYamlSequenceBuilder.java
@@ -0,0 +1,80 @@
+/**
+ * Copyright (c) 2016-2022, 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 java.util.LinkedList;
+import java.util.List;
+
+/**
+ * YamlSequenceBuilder mutable implementation, for better memory cosumption.
+ * This class is <b>mutable and NOT thread-safe</b>.
+ * @author Mihai Andronache (amihaiemil@gmail.com)
+ * @version $Id$
+ * @since 6.1.0
+ */
+final class MutableYamlSequenceBuilder implements YamlSequenceBuilder {
+    /**
+     * Added nodes.
+     */
+    private final List<YamlNode> nodes;
+
+    /**
+     * Default ctor.
+     */
+    MutableYamlSequenceBuilder() {
+        this(new LinkedList<>());
+    }
+
+    /**
+     * Constructor.
+     * @param nodes Nodes used in building the YamlSequence
+     */
+    MutableYamlSequenceBuilder(final List<YamlNode> nodes) {
+        this.nodes = nodes;
+    }
+
+    @Override
+    public YamlSequenceBuilder add(final String value) {
+        return this.add(new PlainStringScalar(value));
+    }
+
+    @Override
+    public YamlSequenceBuilder add(final YamlNode node) {
+        this.nodes.add(node);
+        return this;
+    }
+
+    @Override
+    public YamlSequence build(final String comment) {
+        YamlSequence sequence = new RtYamlSequence(this.nodes, comment);
+        if (this.nodes.isEmpty()) {
+            sequence = new EmptyYamlSequence(sequence);
+        }
+        return sequence;
+    }
+}
diff --git a/src/main/java/com/amihaiemil/eoyaml/Yaml.java b/src/main/java/com/amihaiemil/eoyaml/Yaml.java
index 05fe6d1..2745a9d 100644
--- a/src/main/java/com/amihaiemil/eoyaml/Yaml.java
+++ b/src/main/java/com/amihaiemil/eoyaml/Yaml.java
@@ -47,7 +47,7 @@ public final class Yaml {
     private Yaml(){}
 
     /**
-     * Create a {@link YamlMappingBuilder}.
+     * Create an immutable, thread-safe, {@link YamlMappingBuilder}.
      * @return Builder of YamlMapping.
      */
     public static YamlMappingBuilder createYamlMappingBuilder() {
@@ -55,13 +55,29 @@ public final class Yaml {
     }
 
     /**
-     * Create a {@link YamlSequenceBuilder}.
+     * Create a mutable, NOT thread-safe, {@link YamlMappingBuilder}.
+     * @return Builder of YamlMapping.
+     */
+    public static YamlMappingBuilder createMutableYamlMappingBuilder() {
+        return new MutableYamlMappingBuilder();
+    }
+
+    /**
+     * Create an immutable, thread-safe, {@link YamlSequenceBuilder}.
      * @return Builder of YamlMapping.
      */
     public static YamlSequenceBuilder createYamlSequenceBuilder() {
         return new RtYamlSequenceBuilder();
     }
 
+    /**
+     * Create a mutable, NOT thread-safe, {@link YamlSequenceBuilder}.
+     * @return Builder of YamlMapping.
+     */
+    public static YamlSequenceBuilder createMutableYamlSequenceBuilder() {
+        return new MutableYamlSequenceBuilder();
+    }
+
     /**
      * Create a {@link YamlScalarBuilder}.
      * @return Builder of Yaml Scalars.
diff --git a/src/main/java/com/amihaiemil/eoyaml/YamlMappingBuilder.java b/src/main/java/com/amihaiemil/eoyaml/YamlMappingBuilder.java
index 255595a..c3c98e2 100644
--- a/src/main/java/com/amihaiemil/eoyaml/YamlMappingBuilder.java
+++ b/src/main/java/com/amihaiemil/eoyaml/YamlMappingBuilder.java
@@ -30,7 +30,7 @@ package com.amihaiemil.eoyaml;
 import java.util.Collection;
 
 /**
- * Builder of YamlMapping. Implementations should be immutable and thread-safe.
+ * Builder of YamlMapping.
  * @author Mihai Andronache (amihaiemil@gmail.com)
  * @version $Id$
  * @since 1.0.0
@@ -40,7 +40,7 @@ public interface YamlMappingBuilder {
      * Add a pair to the mapping.
      * @param key String
      * @param value String
-     * @return This builder
+     * @return Builder
      */
     YamlMappingBuilder add(final String key, final String value);
 
@@ -48,7 +48,7 @@ public interface YamlMappingBuilder {
      * Add a pair to the mapping.
      * @param key YamlNode (sequence or mapping)
      * @param value String
-     * @return This builder
+     * @return Builder
      */
     YamlMappingBuilder add(final YamlNode key, final String value);
 
@@ -56,7 +56,7 @@ public interface YamlMappingBuilder {
      * Add a pair to the mapping.
      * @param key YamlNode (sequence or mapping)
      * @param value YamlNode (sequence or mapping)
-     * @return This builder
+     * @return Builder
      */
     YamlMappingBuilder add(final YamlNode key, final YamlNode value);
 
@@ -64,7 +64,7 @@ public interface YamlMappingBuilder {
      * Add a pair to the mapping.
      * @param key String
      * @param value YamlNode (sequence or mapping)
-     * @return This builder
+     * @return Builder
      */
     YamlMappingBuilder add(final String key, final YamlNode value);
 
diff --git a/src/main/java/com/amihaiemil/eoyaml/YamlSequenceBuilder.java b/src/main/java/com/amihaiemil/eoyaml/YamlSequenceBuilder.java
index dfb7681..14548da 100644
--- a/src/main/java/com/amihaiemil/eoyaml/YamlSequenceBuilder.java
+++ b/src/main/java/com/amihaiemil/eoyaml/YamlSequenceBuilder.java
@@ -30,7 +30,7 @@ package com.amihaiemil.eoyaml;
 import java.util.Collection;
 
 /**
- * Builder of YamlSequence. Implementations should be immutable and thread-safe.
+ * Builder of YamlSequence.
  * @author Salavat.Yalalov (s.yalalov@gmail.com)
  * @version $Id$
  * @since 1.0.0
@@ -40,14 +40,14 @@ public interface YamlSequenceBuilder {
     /**
      * Add a value to the sequence.
      * @param value String
-     * @return This builder
+     * @return Builder
      */
     YamlSequenceBuilder add(final String value);
 
     /**
      * Add a value to the sequence.
      * @param node YamlNode
-     * @return This builder
+     * @return Builder
      */
     YamlSequenceBuilder add(final YamlNode node);
 


Test patch:
diff --git a/src/test/java/com/amihaiemil/eoyaml/MutableYamlMappingBuilderTest.java b/src/test/java/com/amihaiemil/eoyaml/MutableYamlMappingBuilderTest.java
new file mode 100644
index 0000000..9457ce0
--- /dev/null
+++ b/src/test/java/com/amihaiemil/eoyaml/MutableYamlMappingBuilderTest.java
@@ -0,0 +1,187 @@
+/**
+ * Copyright (c) 2016-2022, 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.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Unit tests for {@link MutableYamlMappingBuilder}.
+ * @author Mihai Andronache (amihaiemil@gmail.com)
+ * @version $Id$
+ * @since 6.1.0
+ */
+public final class MutableYamlMappingBuilderTest {
+    /**
+     * MutableYamlMappingBuilder can add a K:V pair of Strings.
+     */
+    @Test
+    public void addsPairOfStrings() {
+        YamlMappingBuilder mappingBuilder = new MutableYamlMappingBuilder();
+        YamlMappingBuilder withAdded = mappingBuilder.add("key", "value");
+        MatcherAssert.assertThat(withAdded, Matchers.notNullValue());
+        MatcherAssert.assertThat(
+            mappingBuilder, Matchers.is(withAdded)
+        );
+    }
+
+    /**
+     * MutableYamlMappingBuilder can add a K:V pair of String and YamlNode.
+     */
+    @Test
+    public void addsPairOfStringYamlNode() {
+        YamlMappingBuilder mappingBuilder = new MutableYamlMappingBuilder();
+        YamlMappingBuilder withAdded = mappingBuilder.add(
+            "key", new PlainStringScalar("value")
+        );
+        MatcherAssert.assertThat(withAdded, Matchers.notNullValue());
+        MatcherAssert.assertThat(
+            mappingBuilder, Matchers.is(withAdded)
+        );
+    }
+
+    /**
+     * MutableYamlMappingBuilder can add a K:V pair of String and YamlNode.
+     */
+    @Test
+    public void addsPairOfYamlNodeString() {
+        YamlMappingBuilder mappingBuilder = new MutableYamlMappingBuilder();
+        YamlMappingBuilder withAdded = mappingBuilder.add(
+            new PlainStringScalar("key"), "value"
+        );
+        MatcherAssert.assertThat(withAdded, Matchers.notNullValue());
+        MatcherAssert.assertThat(
+            mappingBuilder, Matchers.is(withAdded)
+        );
+    }
+
+    /**
+     * MutableYamlMappingBuilder can add a K:V pair of YamlNode.
+     */
+    @Test
+    public void addsPairOfYamlNodes() {
+        YamlMappingBuilder mappingBuilder = new MutableYamlMappingBuilder();
+        YamlMappingBuilder withAdded = mappingBuilder.add(
+            new PlainStringScalar("key"), new PlainStringScalar("value")
+        );
+        MatcherAssert.assertThat(withAdded, Matchers.notNullValue());
+        MatcherAssert.assertThat(
+            mappingBuilder, Matchers.is(withAdded)
+        );
+    }
+
+    /**
+     * MutableYamlMappingBuilder can build a YamlMapping.
+     */
+    @Test
+    public void buildsYamlMapping() {
+        YamlMappingBuilder mappingBuilder = new MutableYamlMappingBuilder();
+        List<YamlNode> devs = new ArrayList<>();
+        devs.add(new PlainStringScalar("amihaiemil"));
+        devs.add(new PlainStringScalar("salikjan"));
+        YamlMapping mapping = mappingBuilder
+            .add("architect", "amihaiemil")
+            .add("developers", new RtYamlSequence(devs))
+            .build();
+        MatcherAssert.assertThat(mapping, Matchers.notNullValue());
+        MatcherAssert.assertThat(
+            mapping.string("architect"), Matchers.equalTo("amihaiemil")
+        );
+        MatcherAssert.assertThat(
+            mapping.yamlSequence("developers").values().size(),
+            Matchers.equalTo(2)
+        );
+    }
+
+    /**
+     * MutableYamlMappingBuilder can build a YamlMapping with a comment
+     * referring to it.
+     */
+    @Test
+    public void buildsYamlMappingWithComment() {
+        final YamlMapping mapping = new MutableYamlMappingBuilder()
+            .add("key", "value")
+            .add("key1", "value1")
+            .build("some test mapping");
+        final Comment com = mapping.comment();
+        MatcherAssert.assertThat(com.yamlNode(), Matchers.is(mapping));
+        MatcherAssert.assertThat(
+            com.value(),
+            Matchers.equalTo("some test mapping")
+        );
+    }
+
+    /**
+     * MutableYamlMappingBuilder should complain when a null key is provided.
+     */
+    @Test
+    public void complainsOnNullKey() {
+        final YamlNode key = null;
+        try {
+            final YamlMapping mapping = new MutableYamlMappingBuilder()
+                .add(key, "value")
+                .build("some test mapping");
+            Assert.fail("IAE was expected!");
+        } catch (final IllegalArgumentException ex) {
+            MatcherAssert.assertThat(
+                ex.getMessage(),
+                Matchers.equalTo(
+                    "The key in YamlMapping cannot be null or empty!"
+                )
+            );
+        }
+    }
+
+    /**
+     * MutableYamlMappingBuilder should complain when an empty key is provided.
+     */
+    @Test
+    public void complainsOnEmptyKey() {
+        final BaseYamlNode key = Mockito.mock(BaseYamlNode.class);
+        Mockito.when(key.isEmpty()).thenReturn(true);
+        try {
+            final YamlMapping mapping = new MutableYamlMappingBuilder()
+                .add(key, "value")
+                .build("some test mapping");
+            Assert.fail("IAE was expected!");
+        } catch (final IllegalArgumentException ex) {
+            MatcherAssert.assertThat(
+                ex.getMessage(),
+                Matchers.equalTo(
+                    "The key in YamlMapping cannot be null or empty!"
+                )
+            );
+        }
+    }
+}
diff --git a/src/test/java/com/amihaiemil/eoyaml/MutableYamlSequenceBuilderTest.java b/src/test/java/com/amihaiemil/eoyaml/MutableYamlSequenceBuilderTest.java
new file mode 100644
index 0000000..0eb8c7b
--- /dev/null
+++ b/src/test/java/com/amihaiemil/eoyaml/MutableYamlSequenceBuilderTest.java
@@ -0,0 +1,112 @@
+/**
+ * Copyright (c) 2016-2022, 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 java.util.LinkedList;
+import java.util.List;
+
+/**
+ * Unit tests for {@link MutableYamlSequenceBuilder}.
+ * @author Mihai Andronache (amihaiemil@gmail.com)
+ * @version $Id$
+ * @since 6.1.0
+ */
+public final class MutableYamlSequenceBuilderTest {
+    /**
+     * MutableYamlSequenceBuilder can add a String.
+     */
+    @Test
+    public void addsString() {
+        YamlSequenceBuilder sequenceBuilder = new MutableYamlSequenceBuilder();
+        YamlSequenceBuilder withAdded = sequenceBuilder.add("value");
+        MatcherAssert.assertThat(withAdded, Matchers.notNullValue());
+        MatcherAssert.assertThat(
+            sequenceBuilder, Matchers.is(withAdded)
+        );
+    }
+
+    /**
+     * MutableYamlSequenceBuilder can add a YamlNode.
+     */
+    @Test
+    public void addsYamlNode() {
+        YamlSequenceBuilder sequenceBuilder = new MutableYamlSequenceBuilder();
+        YamlSequenceBuilder withAdded =
+            sequenceBuilder.add(new PlainStringScalar("value"));
+        MatcherAssert.assertThat(withAdded, Matchers.notNullValue());
+        MatcherAssert.assertThat(
+            sequenceBuilder, Matchers.is(withAdded)
+        );
+    }
+
+    /**
+     * MutableYamlSequenceBuilder can build a YamlSequence.
+     */
+    @Test
+    public void buildsYamlSequence() {
+        YamlSequenceBuilder sequenceBuilder = new MutableYamlSequenceBuilder();
+        List<YamlNode> devs = new LinkedList<>();
+        devs.add(new PlainStringScalar("amihaiemil"));
+        devs.add(new PlainStringScalar("salikjan"));
+        YamlSequence sequence = sequenceBuilder
+            .add("amihaiemil")
+            .add(new RtYamlSequence(devs))
+            .build();
+
+        MatcherAssert.assertThat(sequence, Matchers.notNullValue());
+        MatcherAssert.assertThat(
+            sequence.string(0), Matchers.equalTo("amihaiemil")
+        );
+
+        MatcherAssert.assertThat(
+            sequence.yamlSequence(1).values().size(),
+            Matchers.equalTo(2)
+        );
+    }
+    /**
+     * MutableYamlSequenceBuilder can build a YamlSequence with a comment
+     * referring to it.
+     */
+    @Test
+    public void buildsYamlSequenceWithComment() {
+        final YamlSequence seq = new MutableYamlSequenceBuilder()
+            .add("element 1")
+            .add("element 2")
+            .build("some test sequence");
+        final Comment com = seq.comment();
+        MatcherAssert.assertThat(com.yamlNode(), Matchers.is(seq));
+        MatcherAssert.assertThat(
+            com.value(),
+            Matchers.equalTo("some test sequence")
+        );
+    }
+}
diff --git a/src/test/java/com/amihaiemil/eoyaml/RtYamlSequenceBuilderTest.java b/src/test/java/com/amihaiemil/eoyaml/RtYamlSequenceBuilderTest.java
index e23701c..93408fe 100644
--- a/src/test/java/com/amihaiemil/eoyaml/RtYamlSequenceBuilderTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/RtYamlSequenceBuilderTest.java
@@ -97,7 +97,7 @@ public final class RtYamlSequenceBuilderTest {
      * referring to it.
      */
     @Test
-    public void buildsYamlMappingWithComment() {
+    public void buildsYamlSequenceWithComment() {
         final YamlSequence seq = new RtYamlSequenceBuilder()
             .add("element 1")
             .add("element 2")
diff --git a/src/test/java/com/amihaiemil/eoyaml/YamlTest.java b/src/test/java/com/amihaiemil/eoyaml/YamlTest.java
index e396f2a..d50e0ef 100644
--- a/src/test/java/com/amihaiemil/eoyaml/YamlTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/YamlTest.java
@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2016-2020, Mihai Emil Andronache
+ * Copyright (c) 2016-2022, Mihai Emil Andronache
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -46,22 +46,58 @@ import javax.json.JsonValue;
 public final class YamlTest {
 
     /**
-     * Yaml can create a YamlMappingBuilder.
+     * Yaml can create an immutable YamlMappingBuilder.
      */
     @Test
     public void createsYamlMappingBuilder() {
         MatcherAssert.assertThat(
-            Yaml.createYamlMappingBuilder(), Matchers.notNullValue()
+            Yaml.createYamlMappingBuilder(),
+            Matchers.allOf(
+                Matchers.notNullValue(),
+                Matchers.instanceOf(RtYamlMappingBuilder.class)
+            )
+        );
+    }
+
+    /**
+     * Yaml can create a mutable YamlMappingBuilder.
+     */
+    @Test
+    public void createsMutableYamlMappingBuilder() {
+        MatcherAssert.assertThat(
+            Yaml.createMutableYamlMappingBuilder(),
+            Matchers.allOf(
+                Matchers.notNullValue(),
+                Matchers.instanceOf(MutableYamlMappingBuilder.class)
+            )
         );
     }
 
     /**
-     * Yaml can create a YamlSequenceBuilder.
+     * Yaml can create an immutable YamlSequenceBuilder.
      */
     @Test
     public void createsYamlSequenceBuilder() {
         MatcherAssert.assertThat(
-            Yaml.createYamlSequenceBuilder(), Matchers.notNullValue()
+            Yaml.createYamlSequenceBuilder(),
+            Matchers.allOf(
+                Matchers.notNullValue(),
+                Matchers.instanceOf(RtYamlSequenceBuilder.class)
+            )
+        );
+    }
+
+    /**
+     * Yaml can create a mutable YamlSequenceBuilder.
+     */
+    @Test
+    public void createsMutablYamlSequenceBuilder() {
+        MatcherAssert.assertThat(
+            Yaml.createMutableYamlSequenceBuilder(),
+            Matchers.allOf(
+                Matchers.notNullValue(),
+                Matchers.instanceOf(MutableYamlSequenceBuilder.class)
+            )
         );
     }