1   /*
2    * Copyright 2007 Tim Wood
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package com.lexicalscope.jewel.cli;
15  
16  import static org.junit.Assert.*;
17  
18  import java.util.List;
19  
20  import org.junit.Test;
21  
22  public class TestObjectMethods {
23      public interface SingleOption {
24          @Option String getName();
25      }
26  
27      public interface ListOption {
28          @Option List<String> getName();
29      }
30  
31      public interface BooleanOption {
32          @Option boolean getName();
33      }
34  
35      public interface OptionalOption {
36          @Option String getName();
37  
38          boolean isName();
39      }
40  
41      public interface UnparsedOption {
42          @Unparsed List<String> getName();
43      }
44  
45      public interface SeveralOptions {
46          @Option String getName0();
47  
48          @Option List<String> getName1();
49  
50          @Option boolean getName2();
51  
52          @Option String getName3();
53  
54          boolean isName3();
55  
56          @Unparsed List<String> getName();
57      }
58  
59      @Test public void testHashCode() throws ArgumentValidationException {
60          final SingleOption parsedArguments = CliFactory.parseArguments(SingleOption.class, "--name", "value");
61          parsedArguments.hashCode();
62      }
63  
64      @Test public void testEquals() throws ArgumentValidationException {
65          final SingleOption parsedArguments = CliFactory.parseArguments(SingleOption.class, "--name", "value");
66          assertTrue(parsedArguments.equals(parsedArguments));
67      }
68  
69      @Test public void testNotEquals() throws ArgumentValidationException {
70          final SingleOption parsedArguments0 = CliFactory.parseArguments(SingleOption.class, "--name", "value");
71          final SingleOption parsedArguments1 = CliFactory.parseArguments(SingleOption.class, "--name", "value");
72          assertFalse(parsedArguments0.equals(parsedArguments1));
73      }
74  
75      @Test public void testToString() throws ArgumentValidationException {
76          final SingleOption parsedArguments = CliFactory.parseArguments(SingleOption.class, "--name", "value");
77          assertEquals(
78                  "com.lexicalscope.jewel.cli.TestObjectMethods$SingleOption {name=value}",
79                  parsedArguments.toString());
80      }
81  
82      @Test public void testToStringList() throws ArgumentValidationException {
83          final ListOption parsedArguments =
84                  CliFactory.parseArguments(ListOption.class, "--name", "value0", "value1", "value2");
85          assertEquals(
86                  "com.lexicalscope.jewel.cli.TestObjectMethods$ListOption {name=[value0, value1, value2]}",
87                  parsedArguments.toString());
88      }
89  
90      @Test public void testToStringBoolean() throws ArgumentValidationException {
91          final BooleanOption parsedArguments =
92                  CliFactory.parseArguments(BooleanOption.class, "--name");
93          assertEquals(
94                  "com.lexicalscope.jewel.cli.TestObjectMethods$BooleanOption {name=null}",
95                  parsedArguments.toString());
96      }
97  
98      @Test public void testToStringOptionalMissing() throws ArgumentValidationException {
99          final OptionalOption parsedArguments = CliFactory.parseArguments(OptionalOption.class);
100         assertEquals(
101                 "com.lexicalscope.jewel.cli.TestObjectMethods$OptionalOption {}",
102                 parsedArguments.toString());
103     }
104 
105     @Test public void testToStringOptionalPresent() throws ArgumentValidationException {
106         final OptionalOption parsedArguments = CliFactory.parseArguments(OptionalOption.class, "--name", "value");
107         assertEquals(
108                 "com.lexicalscope.jewel.cli.TestObjectMethods$OptionalOption {name=value}",
109                 parsedArguments.toString());
110     }
111 
112     @Test public void testToStringUnparsedOptions() throws ArgumentValidationException {
113         final UnparsedOption parsedArguments =
114                 CliFactory.parseArguments(UnparsedOption.class, "value0", "value1", "value2", "value3");
115         assertEquals(
116                 "com.lexicalscope.jewel.cli.TestObjectMethods$UnparsedOption {name=[value0, value1, value2, value3]}",
117                 parsedArguments.toString());
118     }
119 
120     @Test public void testToStringSeveralOptions() throws ArgumentValidationException {
121         final SeveralOptions parsedArguments =
122                 CliFactory.parseArguments(
123                         SeveralOptions.class,
124                         "--name0",
125                         "value0",
126                         "--name1",
127                         "value1",
128                         "value2",
129                         "--name2",
130                         "--name3",
131                         "value3",
132                         "value4",
133                         "value5");
134         assertEquals(
135                 "com.lexicalscope.jewel.cli.TestObjectMethods$SeveralOptions {name0=value0, name1=[value1, value2], name2=null, name3=value3, name=[value4, value5]}",
136                 parsedArguments.toString());
137     }
138 }