-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Return an empty list when parsing an empty string
- Loading branch information
Showing
2 changed files
with
32 additions
and
1 deletion.
There are no files selected for viewing
5 changes: 4 additions & 1 deletion
5
src/main/java/com/beust/jcommander/converters/CommaParameterSplitter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
package com.beust.jcommander.converters; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class CommaParameterSplitter implements IParameterSplitter { | ||
|
||
public List<String> split(String value) { | ||
if ("".equals(value)) { | ||
return Collections.emptyList(); | ||
} | ||
return Arrays.asList(value.split(",")); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/test/java/com/beust/jcommander/converters/CommaParameterSplitterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.beust.jcommander.converters; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
|
||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
@Test | ||
public class CommaParameterSplitterTest { | ||
|
||
private static final IParameterSplitter SPLITTER = new CommaParameterSplitter(); | ||
|
||
@Test | ||
public void testSplit() { | ||
// An empty string becomes an empty list. | ||
Assert.assertEquals(Collections.emptyList(), SPLITTER.split("")); | ||
|
||
// Whitespace is unaltered. | ||
Assert.assertEquals(Collections.singletonList(" "), SPLITTER.split(" ")); | ||
|
||
// Single value. | ||
Assert.assertEquals(Collections.singletonList("abc"), SPLITTER.split("abc")); | ||
|
||
// Multiple values. | ||
Assert.assertEquals(Arrays.asList("a", "b", "c"), SPLITTER.split("a,b,c")); | ||
} | ||
} |