Skip to content

Commit

Permalink
tests and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Apr 12, 2024
1 parent 4d1d184 commit 2e3ee98
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 8 deletions.
2 changes: 1 addition & 1 deletion checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
Maximum number of lines in any .java file is limited.
-->
<module name="FileLength">
<property name="max" value="2000"/>
<property name="max" value="4000"/>
<property name="fileExtensions" value="java"/>
</module>

Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/amihaiemil/eoyaml/ReadFlowMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ final class ReadFlowMapping extends BaseYamlMapping {
*/
ReadFlowMapping(final YamlLine folded) {
this.entries = new StringEntries(folded);
System.out.println("FLOW LINE: " + folded.value());
this.folded = folded;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/amihaiemil/eoyaml/RtYamlInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public YamlMapping readYamlMapping() throws IOException {
).iterator();
final YamlMapping read;
if(iterator.hasNext()) {
if ("{".equals(iterator.next().trimmed())) {
if (iterator.next().trimmed().startsWith("{")) {
read = new ReadFlowMapping(all);
} else {
read = new ReadYamlMapping(all);
Expand All @@ -93,7 +93,7 @@ public YamlSequence readYamlSequence() throws IOException {
).iterator();
final YamlSequence read;
if(iterator.hasNext()) {
if ("[".equals(iterator.next().trimmed())) {
if (iterator.next().trimmed().startsWith("[")) {
read = new ReadFlowSequence(all);
} else {
read = new ReadYamlSequence(all);
Expand Down
241 changes: 237 additions & 4 deletions src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -1844,6 +1843,240 @@ public void shouldReadListOfMapProperly()
);
}

/**
* We should be able to read a file containing only a flow mapping.
* @throws IOException If something is wrong.
*/
@Test
public void readsFlowMapping() throws IOException {
final String filename = "flowMapping.yml";
final YamlMapping mapping = new RtYamlInput(
Files.newBufferedReader(Paths.get("src/test/resources/" + filename))
).readYamlMapping();
System.out.println(mapping);
MatcherAssert.assertThat(
mapping.keys().size(), Matchers.equalTo(4)
);
MatcherAssert.assertThat(
mapping.values().size(), Matchers.equalTo(4)
);
MatcherAssert.assertThat(
mapping.string("name"), Matchers.equalTo("eo-yaml")
);
MatcherAssert.assertThat(
mapping.string("architect"), Matchers.equalTo("amihaiemil")
);
MatcherAssert.assertThat(
mapping.yamlSequence("developers").string(0),
Matchers.equalTo("amihaiemil")
);
MatcherAssert.assertThat(
mapping.yamlSequence("developers").string(1),
Matchers.equalTo("sherif")
);
MatcherAssert.assertThat(
mapping.yamlSequence("developers").string(2),
Matchers.equalTo("rultor")
);
MatcherAssert.assertThat(
mapping.yamlMapping("tools").string("devops"),
Matchers.equalTo("rultor")
);
MatcherAssert.assertThat(
mapping.yamlMapping("tools").string("pm"),
Matchers.equalTo("self-xdsd")
);
}

/**
* We should be able to read a file containing only a flow mapping
* on one line.
* @throws IOException If something is wrong.
*/
@Test
public void readsFlowMappingOneLine() throws IOException {
final String filename = "flowMapping_one_line.yml";
final YamlMapping mapping = new RtYamlInput(
Files.newBufferedReader(Paths.get("src/test/resources/" + filename))
).readYamlMapping();
System.out.println(mapping);
MatcherAssert.assertThat(
mapping.keys().size(), Matchers.equalTo(4)
);
MatcherAssert.assertThat(
mapping.values().size(), Matchers.equalTo(4)
);
MatcherAssert.assertThat(
mapping.string("name"), Matchers.equalTo("eo-yaml")
);
MatcherAssert.assertThat(
mapping.string("architect"), Matchers.equalTo("amihaiemil")
);
MatcherAssert.assertThat(
mapping.yamlSequence("developers").string(0),
Matchers.equalTo("amihaiemil")
);
MatcherAssert.assertThat(
mapping.yamlSequence("developers").string(1),
Matchers.equalTo("sherif")
);
MatcherAssert.assertThat(
mapping.yamlSequence("developers").string(2),
Matchers.equalTo("rultor")
);
MatcherAssert.assertThat(
mapping.yamlMapping("tools").string("devops"),
Matchers.equalTo("rultor")
);
MatcherAssert.assertThat(
mapping.yamlMapping("tools").string("pm"),
Matchers.equalTo("self-xdsd")
);
}

/**
* We should be able to read a file containing only a flow sequence.
* @throws IOException If something is wrong.
*/
@Test
public void readsFlowSequence() throws IOException {
final String filename = "flowSequence.yml";
final YamlSequence sequence = new RtYamlInput(
Files.newBufferedReader(Paths.get("src/test/resources/" + filename))
).readYamlSequence();
System.out.println(sequence);
final YamlMapping first = sequence.yamlMapping(0);
MatcherAssert.assertThat(
first.keys().size(), Matchers.equalTo(4)
);
MatcherAssert.assertThat(
first.values().size(), Matchers.equalTo(4)
);
MatcherAssert.assertThat(
first.string("name"), Matchers.equalTo("eo-yaml")
);
MatcherAssert.assertThat(
first.string("architect"), Matchers.equalTo("amihaiemil")
);
MatcherAssert.assertThat(
first.yamlSequence("developers").string(0),
Matchers.equalTo("amihaiemil")
);
MatcherAssert.assertThat(
first.yamlSequence("developers").string(1),
Matchers.equalTo("sherif")
);
MatcherAssert.assertThat(
first.yamlSequence("developers").string(2),
Matchers.equalTo("rultor")
);
MatcherAssert.assertThat(
first.yamlMapping("tools").string("devops"),
Matchers.equalTo("rultor")
);
MatcherAssert.assertThat(
first.yamlMapping("tools").string("pm"),
Matchers.equalTo("self-xdsd")
);
final YamlMapping second = sequence.yamlMapping(1);
MatcherAssert.assertThat(
second.keys().size(), Matchers.equalTo(4)
);
MatcherAssert.assertThat(
second.values().size(), Matchers.equalTo(4)
);
MatcherAssert.assertThat(
second.string("name"), Matchers.equalTo("queenlang")
);
MatcherAssert.assertThat(
second.string("architect"), Matchers.equalTo("amihaiemil")
);
MatcherAssert.assertThat(
second.yamlSequence("developers").string(0),
Matchers.equalTo("amihaiemil")
);
MatcherAssert.assertThat(
second.yamlMapping("tools").string("devops"),
Matchers.equalTo("rultor")
);
MatcherAssert.assertThat(
second.yamlMapping("tools").string("pm"),
Matchers.equalTo("self-xdsd")
);
}

/**
* We should be able to read a file containing only a flow sequence on one
* line.
* @throws IOException If something is wrong.
*/
@Test
public void readsFlowSequenceOneLine() throws IOException {
final String filename = "flowSequence_one_line.yml";
final YamlSequence sequence = new RtYamlInput(
Files.newBufferedReader(Paths.get("src/test/resources/" + filename))
).readYamlSequence();
System.out.println(sequence);
final YamlMapping first = sequence.yamlMapping(0);
MatcherAssert.assertThat(
first.keys().size(), Matchers.equalTo(4)
);
MatcherAssert.assertThat(
first.values().size(), Matchers.equalTo(4)
);
MatcherAssert.assertThat(
first.string("name"), Matchers.equalTo("eo-yaml")
);
MatcherAssert.assertThat(
first.string("architect"), Matchers.equalTo("amihaiemil")
);
MatcherAssert.assertThat(
first.yamlSequence("developers").string(0),
Matchers.equalTo("amihaiemil")
);
MatcherAssert.assertThat(
first.yamlSequence("developers").string(1),
Matchers.equalTo("sherif")
);
MatcherAssert.assertThat(
first.yamlSequence("developers").string(2),
Matchers.equalTo("rultor")
);
MatcherAssert.assertThat(
first.yamlMapping("tools").string("devops"),
Matchers.equalTo("rultor")
);
MatcherAssert.assertThat(
first.yamlMapping("tools").string("pm"),
Matchers.equalTo("self-xdsd")
);
final YamlMapping second = sequence.yamlMapping(1);
MatcherAssert.assertThat(
second.keys().size(), Matchers.equalTo(4)
);
MatcherAssert.assertThat(
second.values().size(), Matchers.equalTo(4)
);
MatcherAssert.assertThat(
second.string("name"), Matchers.equalTo("queenlang")
);
MatcherAssert.assertThat(
second.string("architect"), Matchers.equalTo("amihaiemil")
);
MatcherAssert.assertThat(
second.yamlSequence("developers").string(0),
Matchers.equalTo("amihaiemil")
);
MatcherAssert.assertThat(
second.yamlMapping("tools").string("devops"),
Matchers.equalTo("rultor")
);
MatcherAssert.assertThat(
second.yamlMapping("tools").string("pm"),
Matchers.equalTo("self-xdsd")
);
}

/**
* Read a test resource file's contents.
* @param fileName File to read.
Expand All @@ -1852,11 +2085,11 @@ public void shouldReadListOfMapProperly()
* @throws IOException If something is wrong.
*/
private String readTestResource(final String fileName)
throws FileNotFoundException, IOException {
throws IOException {
return new String(
IOUtils.toByteArray(
new FileInputStream(
new File("src/test/resources/" + fileName)
Files.newInputStream(
new File("src/test/resources/" + fileName).toPath()
)
)
);
Expand Down
6 changes: 6 additions & 0 deletions src/test/resources/flowMapping.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
name: eo-yaml,
architect: amihaiemil,
developers: [amihaiemil, sherif, rultor],
tools: {devops: rultor, pm: self-xdsd}
}
1 change: 1 addition & 0 deletions src/test/resources/flowMapping_one_line.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{name: eo-yaml,architect: amihaiemil,developers: [amihaiemil, sherif, rultor],tools: {devops: rultor, pm: self-xdsd}}
14 changes: 14 additions & 0 deletions src/test/resources/flowSequence.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
name: eo-yaml,
architect: amihaiemil,
developers: [ amihaiemil, sherif, rultor ],
tools: { devops: rultor, pm: self-xdsd }
},
{
name: queenlang,
architect: amihaiemil,
developers: [ amihaiemil],
tools: { devops: rultor, pm: self-xdsd }
},
]
1 change: 1 addition & 0 deletions src/test/resources/flowSequence_one_line.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{name: eo-yaml,architect: amihaiemil,developers: [ amihaiemil, sherif, rultor ],tools: { devops: rultor, pm: self-xdsd }},{name: queenlang,architect: amihaiemil,developers: [ amihaiemil], tools: { devops: rultor, pm: self-xdsd }},]

0 comments on commit 2e3ee98

Please sign in to comment.