Implemented equals for CommandContext and ParsedArgument

This commit is contained in:
Nathan Adams 2014-09-29 12:35:07 +02:00
parent 2114f086cb
commit 29f99e8710
4 changed files with 77 additions and 3 deletions

View file

@ -38,4 +38,26 @@ public class CommandContext<T> {
throw new IllegalArgumentException("Argument '" + name + "' is defined as " + argument.getResult().getClass().getSimpleName() + ", not " + clazz); throw new IllegalArgumentException("Argument '" + name + "' is defined as " + argument.getResult().getClass().getSimpleName() + ", not " + clazz);
} }
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof CommandContext)) return false;
CommandContext that = (CommandContext) o;
if (!arguments.equals(that.arguments)) return false;
if (command != null ? !command.equals(that.command) : that.command != null) return false;
if (!source.equals(that.source)) return false;
return true;
}
@Override
public int hashCode() {
int result = source.hashCode();
result = 31 * result + arguments.hashCode();
result = 31 * result + (command != null ? command.hashCode() : 0);
return result;
}
} }

View file

@ -16,4 +16,24 @@ public class ParsedArgument<T> {
public T getResult() { public T getResult() {
return result; return result;
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ParsedArgument)) return false;
ParsedArgument that = (ParsedArgument) o;
if (!raw.equals(that.raw)) return false;
if (!result.equals(that.result)) return false;
return true;
}
@Override
public int hashCode() {
int result1 = raw.hashCode();
result1 = 31 * result1 + result.hashCode();
return result1;
}
} }

View file

@ -1,14 +1,17 @@
package net.minecraft.commands.context; package net.minecraft.commands.context;
import net.minecraft.commands.arguments.IntegerArgumentType; import com.google.common.testing.EqualsTester;
import net.minecraft.commands.Command;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import static net.minecraft.commands.arguments.IntegerArgumentType.integer;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class CommandContextTest { public class CommandContextTest {
@ -27,13 +30,13 @@ public class CommandContextTest {
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testGetArgument_wrongType() throws Exception { public void testGetArgument_wrongType() throws Exception {
CommandContext<Object> context = builder.withArgument("foo", IntegerArgumentType.integer().parse("123")).build(); CommandContext<Object> context = builder.withArgument("foo", integer().parse("123")).build();
context.getArgument("foo", String.class); context.getArgument("foo", String.class);
} }
@Test @Test
public void testGetArgument() throws Exception { public void testGetArgument() throws Exception {
CommandContext<Object> context = builder.withArgument("foo", IntegerArgumentType.integer().parse("123")).build(); CommandContext<Object> context = builder.withArgument("foo", integer().parse("123")).build();
assertThat(context.getArgument("foo", int.class).getResult(), is(123)); assertThat(context.getArgument("foo", int.class).getResult(), is(123));
} }
@ -41,4 +44,18 @@ public class CommandContextTest {
public void testSource() throws Exception { public void testSource() throws Exception {
assertThat(builder.build().getSource(), is(source)); assertThat(builder.build().getSource(), is(source));
} }
@Test
public void testEquals() throws Exception {
Object otherSource = new Object();
Command command = mock(Command.class);
Command otherCommand = mock(Command.class);
new EqualsTester()
.addEqualityGroup(new CommandContextBuilder<Object>(source).build(), new CommandContextBuilder<Object>(source).build())
.addEqualityGroup(new CommandContextBuilder<Object>(otherSource).build(), new CommandContextBuilder<Object>(otherSource).build())
.addEqualityGroup(new CommandContextBuilder<Object>(source).withCommand(command).build(), new CommandContextBuilder<Object>(source).withCommand(command).build())
.addEqualityGroup(new CommandContextBuilder<Object>(source).withCommand(otherCommand).build(), new CommandContextBuilder<Object>(source).withCommand(otherCommand).build())
.addEqualityGroup(new CommandContextBuilder<Object>(source).withArgument("foo", integer().parse("123")).build(), new CommandContextBuilder<Object>(source).withArgument("foo", integer().parse("123")).build())
.testEquals();
}
} }

View file

@ -0,0 +1,15 @@
package net.minecraft.commands.context;
import com.google.common.testing.EqualsTester;
import org.junit.Test;
public class ParsedArgumentTest {
@Test
public void testEquals() throws Exception {
new EqualsTester()
.addEqualityGroup(new ParsedArgument<String>("foo", "bar"), new ParsedArgument<String>("foo", "bar"))
.addEqualityGroup(new ParsedArgument<String>("bar", "baz"), new ParsedArgument<String>("bar", "baz"))
.addEqualityGroup(new ParsedArgument<String>("foo", "baz"), new ParsedArgument<String>("foo", "baz"))
.testEquals();
}
}