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

Commit

Permalink
Merge pull request #225 from dhleong/master
Browse files Browse the repository at this point in the history
Fix nextControlFlow not accepting arguments
  • Loading branch information
swankjesse committed Feb 11, 2015
2 parents a8b85d1 + cbe76bb commit 33bb07e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/main/java/com/squareup/javapoet/CodeBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ public Builder beginControlFlow(String controlFlow, Object... args) {
*/
public Builder nextControlFlow(String controlFlow, Object... args) {
unindent();
add("} ", args);
add(controlFlow, args);
add(" {\n", args);
add("} " + controlFlow + " {\n", args);
indent();
return this;
}
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/com/squareup/javapoet/TypeSpecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1546,4 +1546,31 @@ private String toString(TypeSpec typeSpec) {
+ "}\n"
);
}

@Test public void tryCatch() {
TypeSpec taco = TypeSpec.classBuilder("Taco")
.addMethod(
MethodSpec.methodBuilder("addTopping")
.addParameter(ClassName.get("com.squareup.tacos", "Topping"), "topping")
.beginControlFlow("try")
.addCode("/* do something tricky with the topping */\n")
.nextControlFlow("catch ($T e)",
ClassName.get("com.squareup.tacos", "IllegalToppingException"))
.endControlFlow()
.build()
)
.build();
assertThat(toString(taco)).isEqualTo(""
+ "package com.squareup.tacos;\n"
+ "\n"
+ "class Taco {\n"
+ " void addTopping(Topping topping) {\n"
+ " try {\n"
+ " /* do something tricky with the topping */\n"
+ " } catch (IllegalToppingException e) {\n"
+ " }\n"
+ " }\n"
+ "}\n"
);
}
}

0 comments on commit 33bb07e

Please sign in to comment.