From 29f99e8710ee3b222075ea29d3c5ccebae2323ed Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Mon, 29 Sep 2014 12:35:07 +0200 Subject: [PATCH] Implemented equals for CommandContext and ParsedArgument --- .../commands/context/CommandContext.java | 22 ++++++++++++++++++ .../commands/context/ParsedArgument.java | 20 ++++++++++++++++ .../commands/context/CommandContextTest.java | 23 ++++++++++++++++--- .../commands/context/ParsedArgumentTest.java | 15 ++++++++++++ 4 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 src/test/java/net/minecraft/commands/context/ParsedArgumentTest.java diff --git a/src/main/java/net/minecraft/commands/context/CommandContext.java b/src/main/java/net/minecraft/commands/context/CommandContext.java index abc708d..828c618 100644 --- a/src/main/java/net/minecraft/commands/context/CommandContext.java +++ b/src/main/java/net/minecraft/commands/context/CommandContext.java @@ -38,4 +38,26 @@ public class CommandContext { 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; + } } diff --git a/src/main/java/net/minecraft/commands/context/ParsedArgument.java b/src/main/java/net/minecraft/commands/context/ParsedArgument.java index d77a19c..c47257f 100644 --- a/src/main/java/net/minecraft/commands/context/ParsedArgument.java +++ b/src/main/java/net/minecraft/commands/context/ParsedArgument.java @@ -16,4 +16,24 @@ public class ParsedArgument { public T getResult() { 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; + } } diff --git a/src/test/java/net/minecraft/commands/context/CommandContextTest.java b/src/test/java/net/minecraft/commands/context/CommandContextTest.java index 6f9731d..1c448c9 100644 --- a/src/test/java/net/minecraft/commands/context/CommandContextTest.java +++ b/src/test/java/net/minecraft/commands/context/CommandContextTest.java @@ -1,14 +1,17 @@ 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.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; +import static net.minecraft.commands.arguments.IntegerArgumentType.integer; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; @RunWith(MockitoJUnitRunner.class) public class CommandContextTest { @@ -27,13 +30,13 @@ public class CommandContextTest { @Test(expected = IllegalArgumentException.class) public void testGetArgument_wrongType() throws Exception { - CommandContext context = builder.withArgument("foo", IntegerArgumentType.integer().parse("123")).build(); + CommandContext context = builder.withArgument("foo", integer().parse("123")).build(); context.getArgument("foo", String.class); } @Test public void testGetArgument() throws Exception { - CommandContext context = builder.withArgument("foo", IntegerArgumentType.integer().parse("123")).build(); + CommandContext context = builder.withArgument("foo", integer().parse("123")).build(); assertThat(context.getArgument("foo", int.class).getResult(), is(123)); } @@ -41,4 +44,18 @@ public class CommandContextTest { public void testSource() throws Exception { 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(source).build(), new CommandContextBuilder(source).build()) + .addEqualityGroup(new CommandContextBuilder(otherSource).build(), new CommandContextBuilder(otherSource).build()) + .addEqualityGroup(new CommandContextBuilder(source).withCommand(command).build(), new CommandContextBuilder(source).withCommand(command).build()) + .addEqualityGroup(new CommandContextBuilder(source).withCommand(otherCommand).build(), new CommandContextBuilder(source).withCommand(otherCommand).build()) + .addEqualityGroup(new CommandContextBuilder(source).withArgument("foo", integer().parse("123")).build(), new CommandContextBuilder(source).withArgument("foo", integer().parse("123")).build()) + .testEquals(); + } } \ No newline at end of file diff --git a/src/test/java/net/minecraft/commands/context/ParsedArgumentTest.java b/src/test/java/net/minecraft/commands/context/ParsedArgumentTest.java new file mode 100644 index 0000000..c3b69c1 --- /dev/null +++ b/src/test/java/net/minecraft/commands/context/ParsedArgumentTest.java @@ -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("foo", "bar"), new ParsedArgument("foo", "bar")) + .addEqualityGroup(new ParsedArgument("bar", "baz"), new ParsedArgument("bar", "baz")) + .addEqualityGroup(new ParsedArgument("foo", "baz"), new ParsedArgument("foo", "baz")) + .testEquals(); + } +} \ No newline at end of file