Skip to content

Commit

Permalink
Merge pull request #1512 from spyrkob/WFCORE-1487
Browse files Browse the repository at this point in the history
WFCORE-1487 Include indexed properties in the suggestion list if no c…
  • Loading branch information
bstansberry committed May 2, 2016
2 parents 64d5b63 + 5ff703b commit 2496e9a
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 15 deletions.
Expand Up @@ -163,6 +163,9 @@ protected int complete(CommandContext ctx, ParsedCommandLine parsedCmd, Operatio
final CommandLineCompleter valCompl = arg.getValueCompleter();
if (valCompl != null) {
valCompl.complete(ctx, "", 0, candidates);
} else if (arg.getFullName() != null) {
String argFullName = arg.getFullName();
candidates.add(argFullName);
}
} else {
String argName = arg.getFullName();
Expand Down Expand Up @@ -294,6 +297,11 @@ protected int complete(CommandContext ctx, ParsedCommandLine parsedCmd, Operatio
if (valCompl != null) {
final String value = chunk == null ? "" : chunk;
valCompl.complete(ctx, value, value.length(), candidates);
} else if (arg.getFullName() != null) {
String argFullName = arg.getFullName();
if (chunk == null || argFullName.startsWith(chunk)) {
candidates.add(argFullName);
}
}
} else {
String argFullName = arg.getFullName();
Expand Down
Expand Up @@ -23,15 +23,35 @@

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
*
* @author Alexey Loubyansky
*/
public class MockOperation {

public static class Property {
private final String name;
private final int index;

public Property(String name, int index) {
this.name = name;
this.index = index;
}

public String getName() {
return name;
}

public int getIndex() {
return index;
}
}

private final String name;
private List<String> parameterNames = Collections.emptyList();

private List<Property> properties = Collections.emptyList();

public MockOperation(String name) {
this.name = name;
Expand All @@ -41,11 +61,15 @@ public String getName() {
return name;
}

public List<String> getPropertyNames() {
return parameterNames;
public void setPropertyNames(List<String> parameterNames) {
properties = parameterNames.stream().map(n->new Property(n, -1)).collect(Collectors.toList());
}

public void setPropertyNames(List<String> parameterNames) {
this.parameterNames = parameterNames;
public void setProperties(List<Property> properties) {
this.properties = properties;
}

public List<Property> getProperties() {
return properties;
}
}
Expand Up @@ -114,13 +114,13 @@ public List<CommandArgument> getProperties(CommandContext ctx, String operationN
return Collections.emptyList();
}

final List<String> names = operation.getPropertyNames();
final List<CommandArgument> result = new ArrayList<CommandArgument>(names.size());
for(final String name : names) {
final List<MockOperation.Property> properties = operation.getProperties();
final List<CommandArgument> result = new ArrayList<CommandArgument>(properties.size());
for(final MockOperation.Property p : properties) {
result.add(new CommandArgument(){
@Override
public String getFullName() {
return name;
return p.getName();
}

@Override
Expand All @@ -130,13 +130,13 @@ public String getShortName() {

@Override
public int getIndex() {
return -1;
return p.getIndex();
}

@Override
public boolean isPresent(ParsedCommandLine args)
throws CommandFormatException {
return args.hasProperty(name);
return args.hasProperty(p.getName());
}

@Override
Expand All @@ -150,23 +150,23 @@ public boolean canAppearNext(CommandContext ctx) throws CommandFormatException {

@Override
public String getValue(ParsedCommandLine args) throws CommandFormatException {
return args.getPropertyValue(name);
return args.getPropertyValue(p.getName());
}

@Override
public String getValue(ParsedCommandLine args, boolean required) throws CommandFormatException {
if(!isPresent(args)) {
throw new CommandFormatException("Property '" + name + "' is missing required value.");
throw new CommandFormatException("Property '" + p.getName() + "' is missing required value.");
}
return args.getPropertyValue(name);
return args.getPropertyValue(p.getName());
}

@Override
public boolean isValueComplete(ParsedCommandLine args) throws CommandFormatException {
if(!isPresent(args)) {
return false;
}
if(name.equals(args.getLastParsedPropertyName())) {
if(p.getName().equals(args.getLastParsedPropertyName())) {
return false;
}
return true;
Expand Down
@@ -0,0 +1,75 @@
/*
Copyright 2016 Red Hat, Inc.
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 org.jboss.as.cli.completion.operation.test;

import org.jboss.as.cli.CommandFormatException;
import org.jboss.as.cli.completion.mock.MockCommandContext;
import org.jboss.as.cli.completion.mock.MockNode;
import org.jboss.as.cli.completion.mock.MockOperation;
import org.jboss.as.cli.completion.mock.MockOperationCandidatesProvider;
import org.jboss.as.cli.operation.OperationRequestCompleter;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class PropertiesWithIndexCompletionTestCase {

private MockCommandContext ctx;
private OperationRequestCompleter completer;

public PropertiesWithIndexCompletionTestCase() {
MockNode root = new MockNode("root");

MockOperation op = new MockOperation("operation-no-properties");
root.addOperation(op);

op = new MockOperation("operation-properties-one-two-three");
op.setProperties(Arrays.asList(new MockOperation.Property("one", 1)));
root.addOperation(op);

ctx = new MockCommandContext();
ctx.setOperationCandidatesProvider(new MockOperationCandidatesProvider(root));
completer = new OperationRequestCompleter();
}

@Test
public void testAllCandidates() {
List<String> candidates = fetchCandidates(":operation-properties-one-two-three(");
assertEquals(Arrays.asList("one"), candidates);
}

@Test
public void testOneCandidate() {
List<String> candidates = fetchCandidates(":operation-properties-one-two-three(o");
assertEquals(Arrays.asList("one"), candidates);
}

protected List<String> fetchCandidates(String buffer) {
ArrayList<String> candidates = new ArrayList<String>();
try {
ctx.parseCommandLine(buffer);
} catch (CommandFormatException e) {
return Collections.emptyList();
}
completer.complete(ctx, buffer, 0, candidates);
return candidates;
}
}

0 comments on commit 2496e9a

Please sign in to comment.