1 package com.lexicalscope.jewel.cli; 2 3 import static com.lexicalscope.jewel.cli.ArgumentValidationExceptionMatcher.*; 4 import static com.lexicalscope.jewel.cli.CliFactory.parseArguments; 5 6 import org.junit.Rule; 7 import org.junit.Test; 8 import org.junit.rules.ExpectedException; 9 10 public class TestArgumentValidationException { 11 @Rule public ExpectedException exception = ExpectedException.none(); 12 13 public interface TwoOptions { 14 @Option int getCount0(); 15 16 @Option int getCount1(); 17 } 18 19 public interface DescribedOption { 20 @Option(description = "the count") int getCount(); 21 } 22 23 @Test public void testUnrecognisedOptionDoesNotCauseMissingOption() { 24 exception.expect(validationExceptionWithMessageLines( 25 "Unexpected Option: coutn")); 26 27 parseArguments(TwoOptions.class, new String[] { "--count0", "3", "--coutn", "5" }); 28 } 29 30 @Test public void testMissingOption() { 31 exception.expect(validationExceptionWithMessage("Option is mandatory: --count1 value")); 32 33 parseArguments(TwoOptions.class, new String[] { "--count0", "3" }); 34 } 35 36 @Test public void testMissingDashes() { 37 exception.expect(validationExceptionWithMessage("Option not expected in this position (count1)")); 38 39 parseArguments(TwoOptions.class, new String[] { "count0", "3", "--count1", "4" }); 40 } 41 42 @Test public void testMultipleMissingOption() { 43 exception.expect(validationExceptionWithMessageLines( 44 "Option is mandatory: --count0 value", 45 "Option is mandatory: --count1 value")); 46 47 parseArguments(TwoOptions.class, new String[] {}); 48 } 49 50 @Test public void testMissingOptionWithDescription() { 51 exception.expect(validationExceptionWithMessage("Option is mandatory: --count value : the count")); 52 53 parseArguments(DescribedOption.class, new String[] {}); 54 } 55 }