Skip to content

Commit

Permalink
More unit test coverage for positional args (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
rvesse committed Apr 16, 2019
1 parent d85581a commit d711152
Show file tree
Hide file tree
Showing 11 changed files with 370 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ public PositionalArgumentMetadata(int position, String title,
throw new IllegalArgumentException("Position must be >= 0");
if (title == null)
throw new NullPointerException("title cannot be null");
if (path == null)
throw new NullPointerException("path cannot be null");
if (!path.iterator().hasNext())
throw new IllegalArgumentException("path cannot be empty");

this.position = position;
this.title = title;
Expand All @@ -82,7 +78,12 @@ public PositionalArgumentMetadata(int position, String title,
this.restrictions = restrictions != null ? AirlineUtils.unmodifiableListCopy(restrictions)
: Collections.<ArgumentsRestriction> emptyList();
this.provider = typeConverterProvider != null ? typeConverterProvider : new DefaultTypeConverterProvider();
this.accessors = SetUtils.unmodifiableSet(Collections.singleton(new Accessor(path)));

if (path != null) {
if (!path.iterator().hasNext())
throw new IllegalArgumentException("path cannot be empty");
this.accessors = SetUtils.unmodifiableSet(Collections.singleton(new Accessor(path)));
}
}

public PositionalArgumentMetadata(Iterable<PositionalArgumentMetadata> arguments) {
Expand Down Expand Up @@ -232,7 +233,8 @@ public String toString() {
* Child
* @return Merged metadata
*/
public static PositionalArgumentMetadata override(PositionalArgumentMetadata parent, PositionalArgumentMetadata child) {
public static PositionalArgumentMetadata override(PositionalArgumentMetadata parent,
PositionalArgumentMetadata child) {
// Cannot change position
if (parent.position != child.position)
throw new IllegalArgumentException(
Expand Down Expand Up @@ -267,15 +269,17 @@ public static PositionalArgumentMetadata override(PositionalArgumentMetadata par
// Parent must not state it is sealed UNLESS it is a duplicate which can
// happen when using @Inject to inject options via delegates
if (parent.sealed && !isDuplicate)
throw new IllegalArgumentException(
String.format("Cannot override positional argument %d (%s) as parent argument declares it to be sealed", parent.position, parent.title));
throw new IllegalArgumentException(String.format(
"Cannot override positional argument %d (%s) as parent argument declares it to be sealed",
parent.position, parent.title));

// Child must explicitly state that it overrides otherwise we cannot
// override UNLESS it is the case that this is a duplicate which
// can happen when using @Inject to inject options via delegates
if (!child.overrides && !isDuplicate)
throw new IllegalArgumentException(
String.format("Cannot override positional argument %d (%s) unless child argument sets overrides to true", parent.position, parent.title));
throw new IllegalArgumentException(String.format(
"Cannot override positional argument %d (%s) unless child argument sets overrides to true",
parent.position, parent.title));

PositionalArgumentMetadata merged;
//@formatter:off
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.rvesse.airline.args;
package com.github.rvesse.airline.args.positional;

import com.github.rvesse.airline.annotations.Arguments;
import com.github.rvesse.airline.annotations.Command;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (C) 2010-16 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.github.rvesse.airline.args.positional;

import com.github.rvesse.airline.annotations.Arguments;
import com.github.rvesse.airline.annotations.Command;
import com.github.rvesse.airline.annotations.PositionalArgument;
import com.github.rvesse.airline.annotations.restrictions.Required;

import java.util.ArrayList;
import java.util.List;

@Command(name = "ArgsPositional", description = "ArgsPositional description")
public class ArgsPositionalConflict
{
@PositionalArgument(position = PositionalArgument.FIRST, title = "File")
@Required
public String file;

@PositionalArgument(position = PositionalArgument.FIRST, title = "Mode")
public Integer mode;

@Arguments
public List<String> parameters = new ArrayList<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (C) 2010-16 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.github.rvesse.airline.args.positional;

import com.github.rvesse.airline.annotations.Arguments;
import com.github.rvesse.airline.annotations.Command;
import com.github.rvesse.airline.annotations.PositionalArgument;
import com.github.rvesse.airline.annotations.restrictions.Required;

import java.util.ArrayList;
import java.util.List;

@Command(name = "ArgsPositional", description = "ArgsPositional description")
public class ArgsPositionalDuplicate
{
@PositionalArgument(position = PositionalArgument.FIRST, title = "File")
@Required
public String file;

@PositionalArgument(position = PositionalArgument.FIRST, title = "File")
@Required
public String otherFile;

@PositionalArgument(position = PositionalArgument.SECOND, title = "Mode")
public Integer mode;

@Arguments
public List<String> parameters = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.rvesse.airline.args;
package com.github.rvesse.airline.args.positional;

import com.github.rvesse.airline.annotations.Arguments;
import com.github.rvesse.airline.annotations.Command;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (C) 2010-16 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.github.rvesse.airline.args.positional;

import com.github.rvesse.airline.annotations.Command;
import com.github.rvesse.airline.annotations.PositionalArgument;
import com.github.rvesse.airline.annotations.restrictions.Unrestricted;

@Command(name = "ArgsPositional", description = "ArgsPositional description")
public class ArgsPositionalOverride extends ArgsPositional {

// Override the title and the restriction
@PositionalArgument(position = PositionalArgument.FIRST, title = "YourFile", override = true, sealed = true)
@Unrestricted
public String file;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (C) 2010-16 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.github.rvesse.airline.args.positional;

import com.github.rvesse.airline.annotations.Command;
import com.github.rvesse.airline.annotations.PositionalArgument;

@Command(name = "ArgsPositional", description = "ArgsPositional description")
public class ArgsPositionalOverrideChild extends ArgsPositionalOverride {

// Override the title and the restriction
@PositionalArgument(position = PositionalArgument.FIRST, title = "AwesomeFile", override = true)
public String file;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (C) 2010-16 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.github.rvesse.airline.args.positional;

import com.github.rvesse.airline.annotations.Arguments;
import com.github.rvesse.airline.annotations.Command;
import com.github.rvesse.airline.annotations.PositionalArgument;
import com.github.rvesse.airline.annotations.restrictions.Required;

import java.util.ArrayList;
import java.util.List;

@Command(name = "ArgsPositional", description = "ArgsPositional description")
public class ArgsPositionalRequiredAfterOptional
{
@PositionalArgument(position = PositionalArgument.FIRST, title = "File")
public String file;

@PositionalArgument(position = PositionalArgument.SECOND, title = "Mode")
@Required
public Integer mode;

@Arguments
public List<String> parameters = new ArrayList<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (C) 2010-16 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.github.rvesse.airline.args.positional;

import com.github.rvesse.airline.annotations.Arguments;
import com.github.rvesse.airline.annotations.Command;
import com.github.rvesse.airline.annotations.PositionalArgument;
import com.github.rvesse.airline.annotations.restrictions.Required;

import java.util.ArrayList;
import java.util.List;

@Command(name = "ArgsPositional", description = "ArgsPositional description")
public class ArgsPositionalRequiredAfterOptional2
{
@PositionalArgument(position = PositionalArgument.FIRST, title = "File")
public String file;

@PositionalArgument(position = PositionalArgument.SECOND, title = "Mode")
public Integer mode;

@Arguments
@Required
public List<String> parameters = new ArrayList<>();
}
Loading

0 comments on commit d711152

Please sign in to comment.