Compare commits

...

3 commits

Author SHA1 Message Date
Searge
9779c6dfc0 Version 0.1.25 2018-05-02 14:45:30 +02:00
Searge
48267e9c9c Added tests for suggesting numerical values instead of string values in suggestion builder 2018-05-02 14:45:30 +02:00
Searge
a49e92840b Support lists of integers as suggestions 2018-05-02 12:43:10 +02:00
6 changed files with 105 additions and 3 deletions

View file

@ -3,7 +3,7 @@ import groovy.io.FileType
apply plugin: 'java-library'
apply plugin: 'maven'
version = '0.1.24'
version = '0.1.25'
group = 'com.mojang'
task wrapper(type: Wrapper) {

View file

@ -0,0 +1,63 @@
package com.mojang.brigadier.suggestion;
import com.mojang.brigadier.Message;
import com.mojang.brigadier.context.StringRange;
import java.util.Objects;
public class IntegerSuggestion extends Suggestion {
private int value;
public IntegerSuggestion(final StringRange range, final int value) {
this(range, value, null);
}
public IntegerSuggestion(final StringRange range, final int value, final Message tooltip) {
super(range, Integer.toString(value), tooltip);
this.value = value;
}
public int getValue() {
return value;
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (!(o instanceof IntegerSuggestion)) {
return false;
}
final IntegerSuggestion that = (IntegerSuggestion) o;
return value == that.value && super.equals(o);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), value);
}
@Override
public String toString() {
return "IntegerSuggestion{" +
"value=" + value +
", range=" + getRange() +
", text='" + getText() + '\'' +
", tooltip='" + getTooltip() + '\'' +
'}';
}
@Override
public int compareTo(final Suggestion o) {
if (o instanceof IntegerSuggestion) {
return Integer.compare(value, ((IntegerSuggestion) o).value);
}
return super.compareTo(o);
}
@Override
public int compareToIgnoreCase(final Suggestion b) {
return compareTo(b);
}
}

View file

@ -78,6 +78,10 @@ public class Suggestion implements Comparable<Suggestion> {
return text.compareTo(o.text);
}
public int compareToIgnoreCase(final Suggestion b) {
return text.compareToIgnoreCase(b.text);
}
public Suggestion expand(final String command, final StringRange range) {
if (range.equals(this.range)) {
return this;

View file

@ -94,7 +94,7 @@ public class Suggestions {
texts.add(suggestion.expand(command, range));
}
final List<Suggestion> sorted = Lists.newArrayList(texts);
sorted.sort((a, b) -> a.getText().compareToIgnoreCase(b.getText()));
sorted.sort((a, b) -> a.compareToIgnoreCase(b));
return new Suggestions(range, sorted);
}
}

View file

@ -55,6 +55,16 @@ public class SuggestionsBuilder {
return this;
}
public SuggestionsBuilder suggest(final int value) {
result.add(new IntegerSuggestion(StringRange.between(start, input.length()), value));
return this;
}
public SuggestionsBuilder suggest(final int value, final Message tooltip) {
result.add(new IntegerSuggestion(StringRange.between(start, input.length()), value, tooltip));
return this;
}
public SuggestionsBuilder add(final SuggestionsBuilder other) {
result.addAll(other.result);
return this;

View file

@ -5,6 +5,10 @@ import com.mojang.brigadier.context.StringRange;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
@ -58,4 +62,25 @@ public class SuggestionsBuilderTest {
assertThat(other.getStart(), is(builder.getStart()));
assertThat(other.getRemaining(), equalTo(builder.getRemaining()));
}
@Test
public void sort_alphabetical() {
Suggestions result = builder.suggest("2").suggest("4").suggest("6").suggest("8").suggest("30").suggest("32").build();
List<String> actual = result.getList().stream().map(Suggestion::getText).collect(Collectors.toList());
assertThat(actual, equalTo(Lists.newArrayList( "2", "30", "32", "4", "6", "8")));
}
@Test
public void sort_numerical() {
Suggestions result = builder.suggest(2).suggest(4).suggest(6).suggest(8).suggest(30).suggest(32).build();
List<String> actual = result.getList().stream().map(Suggestion::getText).collect(Collectors.toList());
assertThat(actual, equalTo(Lists.newArrayList( "2", "4", "6", "8", "30", "32")));
}
@Test
public void sort_mixed() {
Suggestions result = builder.suggest("11").suggest("22").suggest("33").suggest("a").suggest("b").suggest("c").suggest(2).suggest(4).suggest(6).suggest(8).suggest(30).suggest(32).suggest("3a").suggest("a3").build();
List<String> actual = result.getList().stream().map(Suggestion::getText).collect(Collectors.toList());
assertThat(actual, equalTo(Lists.newArrayList( "11", "2", "22", "33", "3a", "4", "6", "8", "30", "32", "a", "a3", "b", "c")));
}
}