-
-
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.
- Loading branch information
Showing
3 changed files
with
303 additions
and
0 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
src/main/java/com/amihaiemil/eoyaml/ReadScalarComment.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,96 @@ | ||
/** | ||
* Copyright (c) 2016-2021, 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; | ||
|
||
/** | ||
* A ScalarComment read from somewhere. | ||
* @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 { | ||
|
||
/** | ||
* Comment above the scalar. | ||
*/ | ||
private final Comment above; | ||
|
||
/** | ||
* Inline Comment. | ||
*/ | ||
private final Comment inline; | ||
|
||
/** | ||
* Ctor. | ||
* @param above Comment above the scalar. | ||
* @param inline Comment inline with the scalar. | ||
*/ | ||
ReadScalarComment( | ||
final Comment above, | ||
final Comment inline | ||
) { | ||
this.above = above; | ||
this.inline = inline; | ||
} | ||
|
||
@Override | ||
public YamlNode yamlNode() { | ||
return this.inline.yamlNode(); | ||
} | ||
|
||
@Override | ||
public String value() { | ||
final StringBuilder comment = new StringBuilder(); | ||
final String aboveValue = this.above.value(); | ||
final String inlineValue = this.inline.value(); | ||
|
||
if(inlineValue.trim().isEmpty()){ | ||
comment.append(aboveValue); | ||
} else if(aboveValue.trim().isEmpty()){ | ||
comment.append(inlineValue); | ||
} else { | ||
comment | ||
.append(aboveValue) | ||
.append(System.lineSeparator()) | ||
.append(inlineValue); | ||
} | ||
return comment.toString(); | ||
} | ||
|
||
@Override | ||
public Comment above() { | ||
return this.above; | ||
} | ||
|
||
@Override | ||
public Comment inline() { | ||
return this.inline; | ||
} | ||
} |
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,51 @@ | ||
/** | ||
* Copyright (c) 2016-2021, 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; | ||
|
||
/** | ||
* A scalar can have 2 comments: 1 on top of it and 1 inline. This interface | ||
* represents both of them. | ||
* @author Mihai Andronache (amihaiemil@gmail.com) | ||
* @version $Id$ | ||
* @since 5.2.1 | ||
*/ | ||
public interface ScalarComment extends Comment { | ||
|
||
/** | ||
* Comment above the Scalar. | ||
* @return Comment. | ||
*/ | ||
Comment above(); | ||
|
||
/** | ||
* Comment inline with the Scalar. | ||
* @return Comment. | ||
*/ | ||
Comment inline(); | ||
|
||
} |
156 changes: 156 additions & 0 deletions
156
src/test/java/com/amihaiemil/eoyaml/ReadScalarCommentTest.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,156 @@ | ||
/** | ||
* Copyright (c) 2016-2021, 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 org.mockito.Mockito; | ||
|
||
/** | ||
* Unit tests for {@link ReadScalarComment}. | ||
* @author Mihai Andronache (amihaiemil@gmail.com) | ||
* @version $Id$ | ||
* @since 5.2.1 | ||
*/ | ||
public final class ReadScalarCommentTest { | ||
|
||
/** | ||
* ReadScalarComment can return the Comment above. | ||
*/ | ||
@Test | ||
public void returnsCommentAbove() { | ||
final Comment above = Mockito.mock(Comment.class); | ||
final Comment inline = Mockito.mock(Comment.class); | ||
MatcherAssert.assertThat( | ||
new ReadScalarComment(above, inline).above(), | ||
Matchers.is(above) | ||
); | ||
} | ||
|
||
/** | ||
* ReadScalarComment can return the inline Comment. | ||
*/ | ||
@Test | ||
public void returnsInlineComment() { | ||
final Comment above = Mockito.mock(Comment.class); | ||
final Comment inline = Mockito.mock(Comment.class); | ||
MatcherAssert.assertThat( | ||
new ReadScalarComment(above, inline).inline(), | ||
Matchers.is(inline) | ||
); | ||
} | ||
|
||
/** | ||
* ReadScalarComment can return the parent YamlNode. | ||
*/ | ||
@Test | ||
public void returnsNode() { | ||
final YamlNode parent = Mockito.mock(YamlNode.class); | ||
final Comment above = Mockito.mock(Comment.class); | ||
final Comment inline = Mockito.mock(Comment.class); | ||
Mockito.when(inline.yamlNode()).thenReturn(parent); | ||
|
||
MatcherAssert.assertThat( | ||
new ReadScalarComment(above, inline).yamlNode(), | ||
Matchers.is(parent) | ||
); | ||
} | ||
|
||
/** | ||
* ReadScalarComment returns the concatenated (with newline) value of | ||
* both comments. | ||
*/ | ||
@Test | ||
public void returnsConcatenatedValue(){ | ||
final Comment above = Mockito.mock(Comment.class); | ||
Mockito.when(above.value()).thenReturn("Comment above on\nmore lines."); | ||
final Comment inline = Mockito.mock(Comment.class); | ||
Mockito.when(inline.value()).thenReturn("And inline comment."); | ||
|
||
MatcherAssert.assertThat( | ||
new ReadScalarComment(above, inline).value(), | ||
Matchers.equalTo( | ||
"Comment above on\n" | ||
+ "more lines.\n" | ||
+ "And inline comment." | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* ReadScalarComment.value() is only the inline comment if the above | ||
* Comment is empty. | ||
*/ | ||
@Test | ||
public void returnsValueOfInlineCommentOnly() { | ||
final Comment above = Mockito.mock(Comment.class); | ||
Mockito.when(above.value()).thenReturn(""); | ||
final Comment inline = Mockito.mock(Comment.class); | ||
Mockito.when(inline.value()).thenReturn("Inline comment."); | ||
|
||
MatcherAssert.assertThat( | ||
new ReadScalarComment(above, inline).value(), | ||
Matchers.equalTo("Inline comment.") | ||
); | ||
} | ||
|
||
/** | ||
* ReadScalarComment.value() is only the above comment if the inline | ||
* Comment is empty. | ||
*/ | ||
@Test | ||
public void returnsValueOfAboveCommentOnly() { | ||
final Comment above = Mockito.mock(Comment.class); | ||
Mockito.when(above.value()).thenReturn("Comment above on\nmore lines."); | ||
final Comment inline = Mockito.mock(Comment.class); | ||
Mockito.when(inline.value()).thenReturn(""); | ||
|
||
MatcherAssert.assertThat( | ||
new ReadScalarComment(above, inline).value(), | ||
Matchers.equalTo("Comment above on\nmore lines.") | ||
); | ||
} | ||
|
||
/** | ||
* ReadScalarComment.value() returns empty string if both comments are | ||
* empty. | ||
*/ | ||
@Test | ||
public void returnsEmptyComment() { | ||
final Comment above = Mockito.mock(Comment.class); | ||
Mockito.when(above.value()).thenReturn(""); | ||
final Comment inline = Mockito.mock(Comment.class); | ||
Mockito.when(inline.value()).thenReturn(""); | ||
|
||
MatcherAssert.assertThat( | ||
new ReadScalarComment(above, inline).value(), | ||
Matchers.isEmptyString() | ||
); | ||
} | ||
} |