Skip to content

Commit

Permalink
Improve Javadoc and JavadocDescription to allow modification
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardtke committed Jan 19, 2018
1 parent 8906098 commit 358c86c
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 19 deletions.
Expand Up @@ -106,8 +106,16 @@ public JavadocDescription getDescription() {
return description; return description;
} }


public List<JavadocBlockTag> getBlockTags() { public JavadocBlockTag getBlockTag(int i) {
return blockTags; return this.blockTags.get(i);
}

public int getBlockTagCount() {
return this.blockTags.size();
}

public JavadocBlockTag removeBlockTag(int i) {
return this.blockTags.get(i);
} }


@Override @Override
Expand Down
Expand Up @@ -69,8 +69,26 @@ public JavadocDescription() {
elements = new LinkedList<>(); elements = new LinkedList<>();
} }


public void addElement(JavadocDescriptionElement element) { public JavadocDescription(List<JavadocDescriptionElement> elements) {
this.elements.add(element); this();

this.elements.addAll(elements);
}

public boolean addElement(JavadocDescriptionElement element) {
return this.elements.add(element);
}

public JavadocDescriptionElement getElement(int i) {
return this.elements.get(i);
}

public JavadocDescriptionElement removeElement(int i) {
return this.elements.remove(i);
}

public int getElementCount() {
return this.elements.size();
} }


public String toText() { public String toText() {
Expand Down
Expand Up @@ -103,7 +103,8 @@ public void parseBlockTagsAndProvideTagName() {




assertEquals(underTest, JavadocParser.parse(expectedText)); assertEquals(underTest, JavadocParser.parse(expectedText));
assertEquals(underTest.getBlockTags().get(0).getTagName(), "unofficial"); assertEquals(1, underTest.getBlockTagCount());
assertEquals("unofficial", underTest.getBlockTag(0).getTagName());
} }


@Test @Test
Expand Down Expand Up @@ -137,10 +138,10 @@ public void parseMultilineParamBlockTags() {
" "; " ";
Javadoc res = JavadocParser.parse(text); Javadoc res = JavadocParser.parse(text);
assertEquals(new Javadoc(JavadocDescription.parseText("Add a field to this and automatically add the import of the type if needed")) assertEquals(new Javadoc(JavadocDescription.parseText("Add a field to this and automatically add the import of the type if needed"))
.addBlockTag(JavadocBlockTag.createParamBlockTag("typeClass", "the type of the field continued in a second line")) .addBlockTag(JavadocBlockTag.createParamBlockTag("typeClass", "the type of the field continued in a second line"))
.addBlockTag(JavadocBlockTag.createParamBlockTag("name", "the name of the field")) .addBlockTag(JavadocBlockTag.createParamBlockTag("name", "the name of the field"))
.addBlockTag(JavadocBlockTag.createParamBlockTag("modifiers", "the modifiers like {@link Modifier#PUBLIC}")) .addBlockTag(JavadocBlockTag.createParamBlockTag("modifiers", "the modifiers like {@link Modifier#PUBLIC}"))
.addBlockTag(new JavadocBlockTag(JavadocBlockTag.Type.RETURN, "the {@link FieldDeclaration} created")), res); .addBlockTag(new JavadocBlockTag(JavadocBlockTag.Type.RETURN, "the {@link FieldDeclaration} created")), res);
} }




Expand Down
Expand Up @@ -24,48 +24,48 @@
import com.github.javaparser.JavaParser; import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.comments.JavadocComment; import com.github.javaparser.ast.comments.JavadocComment;
import com.github.javaparser.javadoc.description.JavadocDescription; import com.github.javaparser.javadoc.description.JavadocDescription;
import com.github.javaparser.javadoc.description.JavadocDescriptionElement;
import com.github.javaparser.javadoc.description.JavadocInlineTag;
import org.junit.Test; import org.junit.Test;


import java.io.FileNotFoundException;

import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;


public class JavadocTest { public class JavadocTest {


@Test @Test
public void toTextForEmptyJavadoc() throws FileNotFoundException { public void toTextForEmptyJavadoc() {
Javadoc javadoc = new Javadoc(new JavadocDescription()); Javadoc javadoc = new Javadoc(new JavadocDescription());
assertEquals("", javadoc.toText()); assertEquals("", javadoc.toText());
} }


@Test @Test
public void toTextForJavadocWithTwoLinesOfJustDescription() throws FileNotFoundException { public void toTextForJavadocWithTwoLinesOfJustDescription() {
Javadoc javadoc = new Javadoc(JavadocDescription.parseText("first line\nsecond line")); Javadoc javadoc = new Javadoc(JavadocDescription.parseText("first line\nsecond line"));
assertEquals("first line\nsecond line\n", javadoc.toText()); assertEquals("first line\nsecond line\n", javadoc.toText());
} }


@Test @Test
public void toTextForJavadocWithTwoLinesOfJustDescriptionAndOneBlockTag() throws FileNotFoundException { public void toTextForJavadocWithTwoLinesOfJustDescriptionAndOneBlockTag() {
Javadoc javadoc = new Javadoc(JavadocDescription.parseText("first line\nsecond line")); Javadoc javadoc = new Javadoc(JavadocDescription.parseText("first line\nsecond line"));
javadoc.addBlockTag("foo", "something useful"); javadoc.addBlockTag("foo", "something useful");
assertEquals("first line\nsecond line\n\n@foo something useful\n", javadoc.toText()); assertEquals("first line\nsecond line\n\n@foo something useful\n", javadoc.toText());
} }


@Test @Test
public void toCommentForEmptyJavadoc() throws FileNotFoundException { public void toCommentForEmptyJavadoc() {
Javadoc javadoc = new Javadoc(new JavadocDescription()); Javadoc javadoc = new Javadoc(new JavadocDescription());
assertEquals(new JavadocComment("\n\t\t "), javadoc.toComment("\t\t")); assertEquals(new JavadocComment("\n\t\t "), javadoc.toComment("\t\t"));
} }


@Test @Test
public void toCommentorJavadocWithTwoLinesOfJustDescription() throws FileNotFoundException { public void toCommentorJavadocWithTwoLinesOfJustDescription() {
Javadoc javadoc = new Javadoc(JavadocDescription.parseText("first line\nsecond line")); Javadoc javadoc = new Javadoc(JavadocDescription.parseText("first line\nsecond line"));
assertEquals(new JavadocComment("\n\t\t * first line\n\t\t * second line\n\t\t "), javadoc.toComment("\t\t")); assertEquals(new JavadocComment("\n\t\t * first line\n\t\t * second line\n\t\t "), javadoc.toComment("\t\t"));
} }


@Test @Test
public void toCommentForJavadocWithTwoLinesOfJustDescriptionAndOneBlockTag() throws FileNotFoundException { public void toCommentForJavadocWithTwoLinesOfJustDescriptionAndOneBlockTag() {
Javadoc javadoc = new Javadoc(JavadocDescription.parseText("first line\nsecond line")); Javadoc javadoc = new Javadoc(JavadocDescription.parseText("first line\nsecond line"));
javadoc.addBlockTag("foo", "something useful"); javadoc.addBlockTag("foo", "something useful");
assertEquals(new JavadocComment("\n\t\t * first line\n\t\t * second line\n\t\t * \n\t\t * @foo something useful\n\t\t "), javadoc.toComment("\t\t")); assertEquals(new JavadocComment("\n\t\t * first line\n\t\t * second line\n\t\t * \n\t\t * @foo something useful\n\t\t "), javadoc.toComment("\t\t"));
Expand All @@ -75,7 +75,7 @@ public void toCommentForJavadocWithTwoLinesOfJustDescriptionAndOneBlockTag() thr
public void descriptionAndBlockTagsAreRetrievable() { public void descriptionAndBlockTagsAreRetrievable() {
Javadoc javadoc = JavaParser.parseJavadoc("first line\nsecond line\n\n@param node a node\n@return result the result"); Javadoc javadoc = JavaParser.parseJavadoc("first line\nsecond line\n\n@param node a node\n@return result the result");
assertEquals(javadoc.getDescription().toText(), "first line\nsecond line"); assertEquals(javadoc.getDescription().toText(), "first line\nsecond line");
assertEquals(javadoc.getBlockTags().size(), 2); assertEquals(2, javadoc.getBlockTagCount());
} }


@Test @Test
Expand All @@ -101,7 +101,38 @@ public void emptyLinesBetweenBlockTagsGetsFiltered() {
" * \n" + " * \n" +
" * @param <T>\n"; " * @param <T>\n";
Javadoc javadoc = JavaParser.parseJavadoc(comment); Javadoc javadoc = JavaParser.parseJavadoc(comment);
assertEquals(javadoc.getBlockTags().size(), 2); assertEquals(2, javadoc.getBlockTagCount());
}

@Test
public void blockTagModificationWorks() {
Javadoc javadoc = new Javadoc(new JavadocDescription());

assertEquals(0, javadoc.getBlockTagCount());
JavadocBlockTag blockTag = new JavadocBlockTag(JavadocBlockTag.Type.RETURN, "a value");
javadoc.addBlockTag(blockTag);

assertEquals(1, javadoc.getBlockTagCount());
assertEquals(blockTag, javadoc.getBlockTag(0));

assertEquals(blockTag, javadoc.removeBlockTag(0));
assertEquals(1, javadoc.getBlockTagCount());
}

@Test
public void descriptionModificationWorks() {
JavadocDescription description = new JavadocDescription();

assertEquals(0, description.getElementCount());

JavadocDescriptionElement inlineTag = new JavadocInlineTag("inheritDoc", JavadocInlineTag.Type.INHERIT_DOC, "");
assertTrue(description.addElement(inlineTag));

assertEquals(1, description.getElementCount());
assertEquals(inlineTag, description.getElement(0));

assertEquals(inlineTag, description.removeElement(0));
assertEquals(0, description.getElementCount());
} }


} }

0 comments on commit 358c86c

Please sign in to comment.