forked from airlift/airline
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More unit test coverage for positional args (#91)
Showing
11 changed files
with
370 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 0 additions & 47 deletions
47
airline-core/src/test/java/com/github/rvesse/airline/TestPositionalArgs.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...-core/src/test/java/com/github/rvesse/airline/args/positional/ArgsPositionalConflict.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<>(); | ||
} |
42 changes: 42 additions & 0 deletions
42
...core/src/test/java/com/github/rvesse/airline/args/positional/ArgsPositionalDuplicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...-core/src/test/java/com/github/rvesse/airline/args/positional/ArgsPositionalOverride.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
28 changes: 28 additions & 0 deletions
28
.../src/test/java/com/github/rvesse/airline/args/positional/ArgsPositionalOverrideChild.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
38 changes: 38 additions & 0 deletions
38
...t/java/com/github/rvesse/airline/args/positional/ArgsPositionalRequiredAfterOptional.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<>(); | ||
} |
38 changes: 38 additions & 0 deletions
38
.../java/com/github/rvesse/airline/args/positional/ArgsPositionalRequiredAfterOptional2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<>(); | ||
} |
140 changes: 140 additions & 0 deletions
140
airline-core/src/test/java/com/github/rvesse/airline/args/positional/TestPositionalArgs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/** | ||
* 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.SingleCommand; | ||
import com.github.rvesse.airline.model.PositionalArgumentMetadata; | ||
import static com.github.rvesse.airline.TestingUtil.singleCommandParser; | ||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertFalse; | ||
import static org.testng.Assert.assertTrue; | ||
|
||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
@SuppressWarnings("unused") | ||
public class TestPositionalArgs { | ||
|
||
@Test | ||
public void positional_args_01() { | ||
SingleCommand<ArgsPositional> parser = singleCommandParser(ArgsPositional.class); | ||
assertFalse(parser.getCommandMetadata().getPositionalArguments().isEmpty()); | ||
assertEquals(parser.getCommandMetadata().getPositionalArguments().size(), 2); | ||
|
||
PositionalArgumentMetadata posArg = parser.getCommandMetadata().getPositionalArguments().get(0); | ||
assertEquals(posArg.getZeroBasedPosition(), 0); | ||
assertEquals(posArg.getOneBasedPosition(), 1); | ||
assertEquals(posArg.getJavaType(), String.class); | ||
assertTrue(posArg.isRequired()); | ||
assertEquals(posArg.getTitle(), "File"); | ||
|
||
posArg = parser.getCommandMetadata().getPositionalArguments().get(1); | ||
assertEquals(posArg.getZeroBasedPosition(), 1); | ||
assertEquals(posArg.getOneBasedPosition(), 2); | ||
assertEquals(posArg.getJavaType(), Integer.class); | ||
assertEquals(posArg.getTitle(), "Mode"); | ||
|
||
ArgsPositional cmd = parser.parse("example.txt", "600", "extra"); | ||
assertEquals(cmd.file, "example.txt"); | ||
assertEquals(cmd.mode, new Integer(600)); | ||
assertEquals(cmd.parameters.size(), 1); | ||
assertEquals(cmd.parameters.get(0), "extra"); | ||
|
||
} | ||
|
||
@Test | ||
public void positional_args_02() { | ||
SingleCommand<ArgsPositional> parser = singleCommandParser(ArgsPositional.class); | ||
ArgsPositional cmd = parser.parse("example.txt", "600", "extra", "other", "another"); | ||
|
||
assertEquals(cmd.file, "example.txt"); | ||
assertEquals(cmd.mode, new Integer(600)); | ||
assertEquals(cmd.parameters.size(), 3); | ||
assertEquals(cmd.parameters.get(0), "extra"); | ||
assertEquals(cmd.parameters.get(1), "other"); | ||
assertEquals(cmd.parameters.get(2), "another"); | ||
|
||
} | ||
|
||
@Test | ||
public void positional_args_03() { | ||
SingleCommand<ArgsPositionalDuplicate> parser = singleCommandParser(ArgsPositionalDuplicate.class); | ||
assertFalse(parser.getCommandMetadata().getPositionalArguments().isEmpty()); | ||
assertEquals(parser.getCommandMetadata().getPositionalArguments().size(), 2); | ||
|
||
ArgsPositionalDuplicate cmd = parser.parse("example.txt", "600", "extra"); | ||
assertEquals(cmd.file, "example.txt"); | ||
assertEquals(cmd.otherFile, "example.txt"); | ||
assertEquals(cmd.file, cmd.otherFile); | ||
assertEquals(cmd.mode, new Integer(600)); | ||
assertEquals(cmd.parameters.size(), 1); | ||
assertEquals(cmd.parameters.get(0), "extra"); | ||
|
||
} | ||
|
||
@Test | ||
public void positional_args_04() { | ||
SingleCommand<ArgsPositionalOverride> parser = singleCommandParser(ArgsPositionalOverride.class); | ||
assertFalse(parser.getCommandMetadata().getPositionalArguments().isEmpty()); | ||
assertEquals(parser.getCommandMetadata().getPositionalArguments().size(), 2); | ||
|
||
PositionalArgumentMetadata posArg = parser.getCommandMetadata().getPositionalArguments().get(0); | ||
assertEquals(posArg.getZeroBasedPosition(), 0); | ||
assertEquals(posArg.getOneBasedPosition(), 1); | ||
assertEquals(posArg.getJavaType(), String.class); | ||
assertEquals(posArg.getTitle(), "YourFile"); | ||
assertFalse(posArg.isRequired()); | ||
|
||
posArg = parser.getCommandMetadata().getPositionalArguments().get(1); | ||
assertEquals(posArg.getZeroBasedPosition(), 1); | ||
assertEquals(posArg.getOneBasedPosition(), 2); | ||
assertEquals(posArg.getJavaType(), Integer.class); | ||
assertEquals(posArg.getTitle(), "Mode"); | ||
|
||
ArgsPositional cmd = parser.parse("example.txt", "600", "extra"); | ||
assertEquals(cmd.file, "example.txt"); | ||
assertEquals(cmd.mode, new Integer(600)); | ||
assertEquals(cmd.parameters.size(), 1); | ||
assertEquals(cmd.parameters.get(0), "extra"); | ||
|
||
} | ||
|
||
@Test(expectedExceptions = IllegalStateException.class) | ||
public void positional_args_gap_01() { | ||
singleCommandParser(ArgsPositionalGap.class); | ||
} | ||
|
||
@Test(expectedExceptions = IllegalArgumentException.class) | ||
public void positional_args_conflict_01() { | ||
singleCommandParser(ArgsPositionalConflict.class); | ||
} | ||
|
||
@Test(expectedExceptions = IllegalArgumentException.class) | ||
public void positional_args_conflcit_02() { | ||
singleCommandParser(ArgsPositionalOverrideChild.class); | ||
} | ||
|
||
@Test(expectedExceptions = IllegalArgumentException.class) | ||
public void positional_args_required_01() { | ||
singleCommandParser(ArgsPositionalRequiredAfterOptional.class); | ||
} | ||
|
||
@Test(expectedExceptions = IllegalArgumentException.class) | ||
public void positional_args_required_02() { | ||
singleCommandParser(ArgsPositionalRequiredAfterOptional2.class); | ||
} | ||
} |