|
| 1 | +package beginner; |
| 2 | + |
| 3 | +import static com.sandwich.koan.constant.KoanConstants.__; |
| 4 | +import static com.sandwich.util.Assert.assertEquals; |
| 5 | + |
| 6 | +import com.sandwich.koan.Koan; |
| 7 | + |
| 8 | +public class AboutVarArgs { |
| 9 | + |
| 10 | + class ExampleClass { |
| 11 | + public boolean canBeTreatedAsArray(Integer... arguments) { return arguments instanceof Integer[]; } |
| 12 | + public int getLength(Integer... arguments) { return arguments.length; } |
| 13 | + public String verboseLength(String prefix, Object... arguments) { return prefix + arguments.length; } |
| 14 | + |
| 15 | + // ******* |
| 16 | + // The following methods won't compile because Java doesn't allow this type of method declaration |
| 17 | + // ******* |
| 18 | + // public String invalidMethodDeclaration(String... arguments, String... otherArguments) { return ""; } |
| 19 | + // public String otherInvalidMethodDeclaration(String... arguments, String otherArgument) { return ""; } |
| 20 | + } |
| 21 | + |
| 22 | + @Koan |
| 23 | + public void varArgsCanBeTreatedAsArrays() { |
| 24 | + assertEquals(new ExampleClass().canBeTreatedAsArray(1, 2, 3), __); |
| 25 | + } |
| 26 | + |
| 27 | + @Koan |
| 28 | + public void youCanPassInAsManyArgumentsAsYouLike() { |
| 29 | + assertEquals(new ExampleClass().getLength(1, 2, 3), __); |
| 30 | + assertEquals(new ExampleClass().getLength(1, 2, 3, 4, 5, 6, 7, 8), __); |
| 31 | + } |
| 32 | + |
| 33 | + @Koan |
| 34 | + public void youCanPassInZeroArgumentsIfYouLike() { |
| 35 | + assertEquals(new ExampleClass().getLength(), __); |
| 36 | + } |
| 37 | + |
| 38 | + @Koan |
| 39 | + public void youCanHaveOtherTypesInTheMethodSignature() { |
| 40 | + assertEquals(new ExampleClass().verboseLength("This is how many items were passed in: ", 1, 2, 3, 4), |
| 41 | + __); |
| 42 | + } |
| 43 | +} |
0 commit comments