1 package com.lexicalscope.jewel.cli; 2 3 import static com.lexicalscope.jewel.cli.ValidationFailureMatcher.validationError; 4 import static java.util.Arrays.asList; 5 import static org.hamcrest.Matchers.contains; 6 import static org.junit.Assert.*; 7 8 import java.util.Collections; 9 import java.util.List; 10 11 import org.jmock.Expectations; 12 import org.jmock.auto.Mock; 13 import org.jmock.integration.junit4.JUnitRuleMockery; 14 import org.junit.Rule; 15 import org.junit.Test; 16 17 import com.lexicalscope.jewel.cli.validation.ArgumentValidator; 18 19 public class TestArgumentCollectionBuilder { 20 @Rule public final JUnitRuleMockery context = new JUnitRuleMockery(); 21 @Mock public ArgumentValidator argumentProcessor; 22 23 private final ArgumentCollectionBuilder argumentCollectionBuilder = new ArgumentCollectionBuilder(); 24 25 @Test public void testParseArguments() throws ArgumentValidationException { 26 context.checking(new Expectations() {{ 27 oneOf(argumentProcessor).processUnparsed(emptyStringList()); 28 oneOf(argumentProcessor).finishedProcessing(); 29 }}); 30 argumentCollectionBuilder.processArguments(argumentProcessor); 31 } 32 33 @Test public void testParseArgumentsNotUparsed() throws ArgumentValidationException { 34 argumentCollectionBuilder.addOption("a"); 35 argumentCollectionBuilder.addOption("b"); 36 argumentCollectionBuilder.addOption("c"); 37 38 context.checking(new Expectations() {{ 39 oneOf(argumentProcessor).processOption("a", emptyStringList()); 40 oneOf(argumentProcessor).processOption("b", emptyStringList()); 41 oneOf(argumentProcessor).processLastOption("c", emptyStringList()); 42 oneOf(argumentProcessor).processUnparsed(emptyStringList()); 43 oneOf(argumentProcessor).finishedProcessing(); 44 }}); 45 argumentCollectionBuilder.processArguments(argumentProcessor); 46 } 47 48 @Test public void noOptionsProducesUnparsed() throws ArgumentValidationException { 49 argumentCollectionBuilder.addValue("1"); 50 argumentCollectionBuilder.addValue("2"); 51 argumentCollectionBuilder.addValue("3"); 52 53 context.checking(new Expectations() {{ 54 oneOf(argumentProcessor).processUnparsed(asList("1", "2", "3")); 55 oneOf(argumentProcessor).finishedProcessing(); 56 }}); 57 argumentCollectionBuilder.processArguments(argumentProcessor); 58 } 59 60 @Test public void testParseArgumentsUnparsed() throws ArgumentValidationException { 61 argumentCollectionBuilder.unparsedOptionsFollow(); 62 argumentCollectionBuilder.addValue("3"); 63 argumentCollectionBuilder.addValue("4"); 64 65 context.checking(new Expectations() {{ 66 oneOf(argumentProcessor).processUnparsed(asList("3", "4")); 67 oneOf(argumentProcessor).finishedProcessing(); 68 }}); 69 argumentCollectionBuilder.processArguments(argumentProcessor); 70 } 71 72 @Test public void testParseArgumentsOnlyUnparsedSeperator() throws ArgumentValidationException { 73 argumentCollectionBuilder.unparsedOptionsFollow(); 74 75 context.checking(new Expectations() {{ 76 oneOf(argumentProcessor).processUnparsed(emptyStringList()); 77 oneOf(argumentProcessor).finishedProcessing(); 78 }}); 79 argumentCollectionBuilder.processArguments(argumentProcessor); 80 } 81 82 @Test public void testParseArgumentsMisplacedValue() { 83 try { 84 argumentCollectionBuilder.addValue("a"); 85 argumentCollectionBuilder.addOption("b"); 86 fail(); 87 } catch (final ArgumentValidationException e) { 88 assertThat(e.getValidationFailures(), contains(validationError(ValidationFailureType.MisplacedOption))); 89 } 90 } 91 92 public static List<String> emptyStringList() { 93 return Collections.emptyList(); 94 } 95 }