/*
* Copyright (c) 2009-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.planet57.gshell.util.cli2;
import java.util.List;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Tests options.
*/
public class OptionsTest
extends CliProcessorTestSupport
{
private static class Simple
{
@Option(name = "h", longName = "help")
boolean help;
@Option(name = "v", optionalArg = true)
Boolean verbose;
@Option(name = "s")
String string;
@Option(name = "S", args = 2)
List<String> strings;
private boolean debug;
@Option(name = "d", longName = "debug", optionalArg = true)
private void setDebug(final boolean flag) {
debug = flag;
}
}
private Simple bean;
@Override
protected Object createBean() {
return bean = new Simple();
}
@Test
public void testHelp() throws Exception {
underTest.process("-h");
assertTrue(bean.help);
}
@Test
public void testHelp2() throws Exception {
underTest.process("--help");
assertTrue(bean.help);
}
@Test
public void testVerbose() throws Exception {
underTest.process("-v");
assertTrue(bean.verbose);
}
@Test
public void testVerbose2() throws Exception {
underTest.process("-v", "false");
assertFalse(bean.verbose);
}
@Test
public void testDebug() throws Exception {
underTest.process("--debug");
assertTrue(bean.debug);
}
@Test
public void testDebugFalse() throws Exception {
underTest.process("--debug=false");
assertFalse(bean.debug);
}
@Test
public void testDebugTrue() throws Exception {
underTest.process("--debug=true");
assertTrue(bean.debug);
}
@Test
public void testString() throws Exception {
try {
underTest.process("-s");
fail();
}
catch (Exception e) {
// ignore
}
underTest.process("-s", "foo");
assertEquals("foo", bean.string);
}
@Test
public void testStrings() throws Exception {
try {
underTest.process("-S");
fail();
}
catch (Exception e) {
// ignore
}
underTest.process("-S", "foo", "bar");
assertEquals("foo", bean.strings.get(0));
assertEquals("bar", bean.strings.get(1));
}
@Test
public void testStrings2() throws Exception {
try {
underTest.process("-S", "foo", "bar", "baz");
fail();
}
catch (Exception e) {
// ignore
}
}
}