Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Fix #852 : API for creating a CodeBlock Statement directly #875

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/com/squareup/javapoet/CodeBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ public static CodeBlock of(String format, Object... args) {
return new Builder().add(format, args).build();
}

public static CodeBlock ofStatement(String format, Object... args){
return CodeBlock.builder().addStatement(format, args).build();
}
/**
* Joins {@code codeBlocks} into a single {@link CodeBlock}, each separated by {@code separator}.
* For example, joining {@code String s}, {@code Object o} and {@code int i} using {@code ", "}
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/squareup/javapoet/CodeBlockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import static com.google.common.truth.Truth.assertThat;
Expand All @@ -43,6 +44,15 @@ public final class CodeBlockTest {
assertThat(a.toString()).isEqualTo("delicious taco");
}

@Test public void ofStatement() {
CodeBlock a = CodeBlock.ofStatement("$L taco", "delicious");
assertThat(a.toString()).isEqualTo("delicious taco;\n");
}

@Test public void ofStatement2() {
assertThat(CodeBlock.ofStatement("println($L)", "\"hello world\"").toString()).isEqualTo("println(\"hello world\");\n");
}

@Test public void isEmpty() {
assertTrue(CodeBlock.builder().isEmpty());
assertTrue(CodeBlock.builder().add("").isEmpty());
Expand Down