Skip to content

Commit d4c5427

Browse files
committed
Merge pull request matyb#40 from dcmoore/master
Varargs
2 parents fbb992a + cba4405 commit d4c5427

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ koans/data*
1010
.project
1111
koans/app/data
1212
lib/file-compiler/bin
13+
koans/app/config/file_hashes.dat

‎koans/app/config/PathToEnlightenment.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<suite class="AboutMethodPreference"/>
2222
<suite class="AboutOperators"/>
2323
<suite class="AboutArrays"/>
24+
<suite class="AboutVarArgs"/>
2425
</package>
2526
<package pkg="intermediate" name="Intermediate">
2627
<suite class="AboutAutoboxing"/>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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

Comments
 (0)