Skip to content

Commit

Permalink
Added double argument type
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinnerbone committed Nov 16, 2017
1 parent 647f859 commit 557352f
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.mojang.brigadier.arguments;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType;

public class DoubleArgumentType implements ArgumentType<Double> {
public static final ParameterizedCommandExceptionType ERROR_TOO_SMALL = new ParameterizedCommandExceptionType("argument.double.low", "Double must not be less than ${minimum}, found ${found}", "found", "minimum");
public static final ParameterizedCommandExceptionType ERROR_TOO_BIG = new ParameterizedCommandExceptionType("argument.double.big", "Double must not be more than ${maximum}, found ${found}", "found", "maximum");

private final double minimum;
private final double maximum;

private DoubleArgumentType(final double minimum, final double maximum) {
this.minimum = minimum;
this.maximum = maximum;
}

public static DoubleArgumentType doubleArg() {
return doubleArg(-Double.MAX_VALUE);
}

public static DoubleArgumentType doubleArg(final double min) {
return doubleArg(min, Double.MAX_VALUE);
}

public static DoubleArgumentType doubleArg(final double min, final double max) {
return new DoubleArgumentType(min, max);
}

public static double getDouble(final CommandContext<?> context, final String name) {
return context.getArgument(name, Double.class);
}

public double getMinimum() {
return minimum;
}

public double getMaximum() {
return maximum;
}

@Override
public <S> Double parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandSyntaxException {
final int start = reader.getCursor();
final double result = reader.readDouble();
if (result < minimum) {
reader.setCursor(start);
throw ERROR_TOO_SMALL.createWithContext(reader, result, minimum);
}
if (result > maximum) {
reader.setCursor(start);
throw ERROR_TOO_BIG.createWithContext(reader, result, maximum);
}
return result;
}

@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof DoubleArgumentType)) return false;

final DoubleArgumentType that = (DoubleArgumentType) o;
return maximum == that.maximum && minimum == that.minimum;
}

@Override
public int hashCode() {
return (int) (31 * minimum + maximum);
}

@Override
public String toString() {
if (minimum == -Double.MAX_VALUE && maximum == Double.MAX_VALUE) {
return "double()";
} else if (maximum == Double.MAX_VALUE) {
return "double(" + minimum + ")";
} else {
return "double(" + minimum + ", " + maximum + ")";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.mojang.brigadier.arguments;

import com.google.common.collect.ImmutableMap;
import com.google.common.testing.EqualsTester;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static com.mojang.brigadier.arguments.DoubleArgumentType.doubleArg;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasToString;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

@RunWith(MockitoJUnitRunner.class)
public class DoubleArgumentTypeTest {
private DoubleArgumentType type;
@Mock
private CommandContextBuilder<Object> context;

@Before
public void setUp() throws Exception {
type = doubleArg(-100, 100);
}

@Test
public void parse() throws Exception {
final StringReader reader = new StringReader("15");
assertThat(doubleArg().parse(reader, context), is(15.0));
assertThat(reader.canRead(), is(false));
}

@Test
public void parse_tooSmall() throws Exception {
final StringReader reader = new StringReader("-5");
try {
doubleArg(0, 100).parse(reader, context);
fail();
} catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(DoubleArgumentType.ERROR_TOO_SMALL));
assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("found", "-5.0", "minimum", "0.0")));
assertThat(ex.getCursor(), is(0));
}
}

@Test
public void parse_tooBig() throws Exception {
final StringReader reader = new StringReader("5");
try {
doubleArg(-100, 0).parse(reader, context);
fail();
} catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(DoubleArgumentType.ERROR_TOO_BIG));
assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("found", "5.0", "maximum", "0.0")));
assertThat(ex.getCursor(), is(0));
}
}

@Test
public void testEquals() throws Exception {
new EqualsTester()
.addEqualityGroup(doubleArg(), doubleArg())
.addEqualityGroup(doubleArg(-100, 100), doubleArg(-100, 100))
.addEqualityGroup(doubleArg(-100, 50), doubleArg(-100, 50))
.addEqualityGroup(doubleArg(-50, 100), doubleArg(-50, 100))
.testEquals();
}

@Test
public void testToString() throws Exception {
assertThat(doubleArg(), hasToString("double()"));
assertThat(doubleArg(-100), hasToString("double(-100.0)"));
assertThat(doubleArg(-100, 100), hasToString("double(-100.0, 100.0)"));
assertThat(doubleArg(Integer.MIN_VALUE, 100), hasToString("double(-2.147483648E9, 100.0)"));
}
}

0 comments on commit 557352f

Please sign in to comment.