|
| 1 | +/* |
| 2 | + * Configurate |
| 3 | + * Copyright (C) zml and Configurate contributors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.spongepowered.configurate.yaml |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals |
| 20 | +import static org.junit.jupiter.api.Assertions.assertFalse |
| 21 | +import static org.junit.jupiter.api.Assertions.assertNull |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Disabled |
| 24 | +import org.junit.jupiter.api.Test |
| 25 | +import org.spongepowered.configurate.CommentedConfigurationNode |
| 26 | + |
| 27 | +class CommentTest implements YamlTest { |
| 28 | + |
| 29 | + @Test |
| 30 | + void testLoadScalarComment() { |
| 31 | + final CommentedConfigurationNode node = parseString(""" |
| 32 | + # Hello world |
| 33 | + "i'm a string" |
| 34 | + """.stripIndent()) |
| 35 | + |
| 36 | + assertEquals("Hello world", node.comment()) |
| 37 | + assertEquals("i'm a string", node.raw()) |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void testLoadBlockMappingComment() { |
| 42 | + final CommentedConfigurationNode node = parseString(""" |
| 43 | + test: |
| 44 | + # meow |
| 45 | + cat: purrs |
| 46 | + """.stripIndent()) |
| 47 | + |
| 48 | + assertEquals("purrs", node.node("test", "cat").raw()) |
| 49 | + assertEquals("meow", node.node("test", "cat").comment()) |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void testLoadBlockScalarSequenceComment() { |
| 54 | + final CommentedConfigurationNode test = parseString(""" |
| 55 | + - first |
| 56 | + # i matter less |
| 57 | + - second |
| 58 | + - third |
| 59 | + # we skipped one |
| 60 | + - fourth |
| 61 | + """.stripIndent()) |
| 62 | + |
| 63 | + assertNull(test.node(0).comment()) |
| 64 | + assertEquals("i matter less", test.node(1).comment()) |
| 65 | + assertEquals("we skipped one", test.node(3).comment()) |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + @Disabled("This doesn't seem to associate comments with the first map entry properly") |
| 70 | + void testLoadScalarCommentsInBlockMapping() { |
| 71 | + final CommentedConfigurationNode test = parseString(""" |
| 72 | + blah: |
| 73 | + # beginning sequence |
| 74 | + - # first on map entry |
| 75 | + test: hello |
| 76 | + - # on second mapping |
| 77 | + test2: goodbye |
| 78 | + """.stripIndent(true)) |
| 79 | + |
| 80 | + final CommentedConfigurationNode child = test.node("blah", 0) |
| 81 | + assertFalse(child.virtual()) |
| 82 | + assertEquals("beginning sequence", child.comment()) |
| 83 | + assertEquals("first on map entry", child.node("test").comment()) |
| 84 | + assertEquals("on second mapping", child.node("test2").comment()) |
| 85 | + } |
| 86 | + |
| 87 | + // flow collections are a bit trickier |
| 88 | + // we can't really do comments on one line, so these all have to have a line per element |
| 89 | + |
| 90 | + @Test |
| 91 | + void testLoadCommentInFlowMapping() { |
| 92 | + final CommentedConfigurationNode test = parseString(""" |
| 93 | + { |
| 94 | + # hello |
| 95 | + test: value, |
| 96 | + uncommented: thing, |
| 97 | + #hi there |
| 98 | + last: bye |
| 99 | + } |
| 100 | + """.stripIndent()) |
| 101 | + |
| 102 | + assertEquals("hello", test.node("test").comment()) |
| 103 | + assertNull(test.node("uncommented").comment()) |
| 104 | + assertEquals("hi there", test.node("last").comment()) |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void testLoadCommentInFlowSequence() { |
| 109 | + final CommentedConfigurationNode test = parseString(""" |
| 110 | + # on list |
| 111 | + [ |
| 112 | + # first |
| 113 | + 'first entry', |
| 114 | + # second |
| 115 | + 'second entry' |
| 116 | + ] |
| 117 | + """.stripIndent()) |
| 118 | + |
| 119 | + assertEquals("on list", test.comment()) |
| 120 | + assertEquals("first", test.node(0).comment()) |
| 121 | + assertEquals("second", test.node(1).comment()) |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void testLoadMixedStructure() { |
| 126 | + final CommentedConfigurationNode test = parseResource(getClass().getResource("/comments-complex.yml")) |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + void testWriteScalarCommented() { |
| 132 | + final CommentedConfigurationNode node = CommentedConfigurationNode.root() |
| 133 | + .raw("test") |
| 134 | + .comment("i have a comment") |
| 135 | + |
| 136 | + assertEquals(""" |
| 137 | + # i have a comment |
| 138 | + test |
| 139 | + """.stripIndent(), |
| 140 | + dump(node)) |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + void testWriteBlockMappingCommented() { |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + void testWriteBlockSequenceCommented() { |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + void testWriteFlowMappingCommented() { |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + void testWriteFlowSequenceCommented() { |
| 160 | + |
| 161 | + } |
| 162 | + |
| 163 | +} |
0 commit comments