Skip to content

Commit

Permalink
Add CodeBlock.Builder#clear() method
Browse files Browse the repository at this point in the history
Analogous to the change added in KotlinPoet
  • Loading branch information
ZacSweers committed Aug 11, 2019
1 parent d70a303 commit 4e8f72f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/squareup/javapoet/CodeBlock.java
Expand Up @@ -424,6 +424,12 @@ public Builder unindent() {
return this;
}

public Builder clear() {
formatParts.clear();
args.clear();
return this;
}

public CodeBlock build() {
return new CodeBlock(this);
}
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/com/squareup/javapoet/CodeBlockTest.java
Expand Up @@ -339,4 +339,13 @@ public final class CodeBlockTest {
CodeBlock joined = codeBlocks.stream().collect(CodeBlock.joining(" || ", "start {", "} end"));
assertThat(joined.toString()).isEqualTo("start {\"hello\" || world.World || need tacos} end");
}

@Test public void clear() {
CodeBlock block = CodeBlock.builder()
.addStatement("$S", "Test string")
.clear()
.build();

assertThat(block.toString()).isEmpty();
}
}

0 comments on commit 4e8f72f

Please sign in to comment.