-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OrderedYamlLines, fixes and unit tests
- Loading branch information
1 parent
391a70c
commit 05ff0c7
Showing
5 changed files
with
219 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* Copyright (c) 2016-2017, 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.camel; | ||
|
||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
/** | ||
* Ordered YamlLines. | ||
* @author Mihai Andronache (amihaiemil@gmail.com) | ||
* @version $Id$ | ||
* @since 1.0.0 | ||
*/ | ||
final class OrderedYamlLines extends AbstractYamlLines { | ||
|
||
/** | ||
* Lines to order. | ||
*/ | ||
private AbstractYamlLines unordered; | ||
|
||
/** | ||
* Ctor. | ||
* @param unordered Decorated lines. | ||
*/ | ||
OrderedYamlLines(final AbstractYamlLines unordered) { | ||
this.unordered = unordered; | ||
} | ||
|
||
/** | ||
* Iterates over the lines with the same indentation. The lines | ||
* are ordered. | ||
* @return Iterator over the ordered lines. | ||
*/ | ||
@Override | ||
public Iterator<YamlLine> iterator() { | ||
final Iterator<YamlLine> lines = this.unordered.iterator(); | ||
final List<YamlLine> ordered = new LinkedList<>(); | ||
while (lines.hasNext()) { | ||
ordered.add(lines.next()); | ||
} | ||
Collections.sort(ordered); | ||
return ordered.iterator(); | ||
} | ||
|
||
/** | ||
* Returns the lines which are nested after the given line. | ||
* The lines are not necessarily ordered. If the resulting lines | ||
* should be ordered (be iterated in order), then they have | ||
* to be wrapped inside a new OrderedYamlLines. | ||
* @return AbstractYamlLines | ||
* @param after The number of the parent line | ||
*/ | ||
@Override | ||
AbstractYamlLines nested(final int after) { | ||
return this.unordered.nested(after); | ||
} | ||
|
||
@Override | ||
int count() { | ||
return this.unordered.count(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
src/test/java/com/amihaiemil/camel/OrderedYamlLinesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/** | ||
* Copyright (c) 2016-2017, 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.camel; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import org.hamcrest.MatcherAssert; | ||
import org.hamcrest.Matchers; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Unit tests for {@link OrderedYamlLines}. | ||
* @author Mihai Andronache (amihaiemil@gmail.com) | ||
* @version $Id$ | ||
* @since 1.0.0 | ||
*/ | ||
public final class OrderedYamlLinesTest { | ||
|
||
/** | ||
* OrderedYamlLines can iterate over the ordered lines properly. | ||
* It should iterate only over the lines which are at the | ||
* same indentation level. | ||
*/ | ||
@Test | ||
public void iteratesRight() { | ||
final List<YamlLine> lines = new ArrayList<>(); | ||
lines.add(new RtYamlLine("first: ", 0)); | ||
lines.add(new RtYamlLine(" - fourth", 1)); | ||
lines.add(new RtYamlLine(" - fifth", 2)); | ||
lines.add(new RtYamlLine("bay: something", 3)); | ||
lines.add(new RtYamlLine("alba: somethingElse", 4)); | ||
final Iterator<YamlLine> iterator = new OrderedYamlLines( | ||
new RtYamlLines(lines) | ||
).iterator(); | ||
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(4)); | ||
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(3)); | ||
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(0)); | ||
MatcherAssert.assertThat(iterator.hasNext(), Matchers.is(false)); | ||
} | ||
|
||
/** | ||
* OrderedYamlLines can return nested lines (in initial order) for a given | ||
* line. | ||
*/ | ||
@Test | ||
public void returnsNestedLinesRight() { | ||
final List<YamlLine> lines = new ArrayList<>(); | ||
lines.add(new RtYamlLine("first: ", 0)); | ||
lines.add(new RtYamlLine(" - bay", 1)); | ||
lines.add(new RtYamlLine(" - alba", 2)); | ||
lines.add(new RtYamlLine("second: something", 3)); | ||
lines.add(new RtYamlLine("third: somethingElse", 4)); | ||
lines.add(new RtYamlLine(" - sixth", 5)); | ||
AbstractYamlLines yamlLines = new RtYamlLines(lines); | ||
|
||
Iterator<YamlLine> iterator = yamlLines.nested(0).iterator(); | ||
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(1)); | ||
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(2)); | ||
MatcherAssert.assertThat(iterator.hasNext(), Matchers.is(false)); | ||
|
||
iterator = yamlLines.nested(1).iterator(); | ||
MatcherAssert.assertThat(iterator.hasNext(), Matchers.is(false)); | ||
|
||
iterator = yamlLines.nested(3).iterator(); | ||
MatcherAssert.assertThat(iterator.hasNext(), Matchers.is(false)); | ||
|
||
iterator = yamlLines.nested(4).iterator(); | ||
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(5)); | ||
MatcherAssert.assertThat(iterator.hasNext(), Matchers.is(false)); | ||
} | ||
|
||
/** | ||
* Test to check that YamlLine.compareTo(...) works fine. | ||
*/ | ||
@Test | ||
public void yamlLinesAreOrdered() { | ||
final List<YamlLine> lines = new ArrayList<>(); | ||
lines.add(new RtYamlLine("Einestien", 0)); | ||
lines.add(new RtYamlLine(" Ben", 1)); | ||
lines.add(new RtYamlLine("Charles", 2)); | ||
lines.add(new RtYamlLine(" Denise", 3)); | ||
lines.add(new RtYamlLine(" Albert", 4)); | ||
lines.add(new RtYamlLine("Sherif", 5)); | ||
Collections.sort(lines); | ||
|
||
Iterator<YamlLine> iterator = lines.iterator(); | ||
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(4)); | ||
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(1)); | ||
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(2)); | ||
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(3)); | ||
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(0)); | ||
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(5)); | ||
MatcherAssert.assertThat(iterator.hasNext(), Matchers.is(false)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters