Skip to content

Commit

Permalink
Return an empty list when parsing an empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
wfarner authored and mkarg committed Oct 1, 2023
1 parent 024f648 commit 8cca22c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
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(","));
}

}
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"));
}
}

0 comments on commit 8cca22c

Please sign in to comment.