CommentDumper extends CommentReader { public String dump() throws IOException { if (this.commentMapper == null) { return this.reader.lines().collect(Collectors.joining("\n")); } this.builder = new StringBuilder(); while (this.nextLine()) { if (!this.isComment()) { final String path = this.track().getPath(); final KeyTree.Node node = this.getNode(path); this.append(node, KeyTree.Node::getComment); this.builder.append(this.currentLine); this.append(node, KeyTree.Node::getSideComment); this.builder.append('\n'); } } this.append(this.getNode(null), KeyTree.Node::getComment); this.reader.close(); return this.builder.toString(); } CommentDumper(final YamlConfigurationOptions options, final CommentMapper commentMapper, final Reader reader); String dump();  }
@Test void dump() throws IOException { final StringReader reader = new StringReader("test: 'test'\n" + "test-2: 'test-2'"); final YamlConfiguration configuration = new YamlConfiguration(); final YamlConfigurationOptions options = new TestYamlConfigurationOptions(configuration); final CommentMapper mapper = new CommentMapper(options); mapper.setComment("test", "test comment"); mapper.setComment("test", "test comment", CommentType.SIDE); mapper.setComment("test-2", "test comment"); mapper.setComment("test-2", "test comment", CommentType.SIDE); final CommentDumper dumper = new CommentDumper(options, mapper, reader); MatcherAssert.assertThat( "Comments are wrong!", dumper.dump(), new IsEqual<>("# test comment\n" + "test: 'test' # test comment\n" + "# test comment\n" + "test-2: 'test-2' # test comment\n") ); }