diff options
author | rmichela <deltahat@gmail.com> | 2012-01-22 02:35:42 -0500 |
---|---|---|
committer | EvilSeph <evilseph@gmail.com> | 2012-03-01 03:52:18 -0500 |
commit | 463856b8a01c869154627865866d6125683e259b (patch) | |
tree | 7497c577e377dc6944cbbc0e07878b2b385585ea /src/test | |
parent | 24f43ae5a64a8b0ae0d3e3211d6bee7f3d6b7f0e (diff) | |
download | bukkit-463856b8a01c869154627865866d6125683e259b.tar bukkit-463856b8a01c869154627865866d6125683e259b.tar.gz bukkit-463856b8a01c869154627865866d6125683e259b.tar.lz bukkit-463856b8a01c869154627865866d6125683e259b.tar.xz bukkit-463856b8a01c869154627865866d6125683e259b.zip |
[Bleeding] Added Conversations API. Addresses BUKKIT-864
Diffstat (limited to 'src/test')
5 files changed, 381 insertions, 0 deletions
diff --git a/src/test/java/org/bukkit/conversations/ConversationContextTest.java b/src/test/java/org/bukkit/conversations/ConversationContextTest.java new file mode 100644 index 00000000..dfc462b5 --- /dev/null +++ b/src/test/java/org/bukkit/conversations/ConversationContextTest.java @@ -0,0 +1,34 @@ +package org.bukkit.conversations; + +import org.junit.Test; +import static org.junit.Assert.*; + +import java.util.HashMap; +import java.util.Map; + +/** + */ +public class ConversationContextTest { + @Test + public void TestFromWhom() { + Conversable conversable = new FakeConversable(); + ConversationContext context = new ConversationContext(null, conversable, new HashMap<Object, Object>()); + assertEquals(conversable, context.getForWhom()); + } + + @Test + public void TestPlugin() { + Conversable conversable = new FakeConversable(); + ConversationContext context = new ConversationContext(null, conversable, new HashMap<Object, Object>()); + assertEquals(null, context.getPlugin()); + } + + @Test + public void TestSessionData() { + Conversable conversable = new FakeConversable(); + Map session = new HashMap(); + session.put("key", "value"); + ConversationContext context = new ConversationContext(null, conversable, session); + assertEquals("value", context.getSessionData("key")); + } +} diff --git a/src/test/java/org/bukkit/conversations/ConversationTest.java b/src/test/java/org/bukkit/conversations/ConversationTest.java new file mode 100644 index 00000000..732caab2 --- /dev/null +++ b/src/test/java/org/bukkit/conversations/ConversationTest.java @@ -0,0 +1,116 @@ +package org.bukkit.conversations; + +import org.junit.Test; +import static org.junit.Assert.*; + +/** + */ +public class ConversationTest { + + @Test + public void testBaseConversationFlow() { + FakeConversable forWhom = new FakeConversable(); + Conversation conversation = new Conversation(null, forWhom, new FirstPrompt()); + + // Conversation not yet begun + assertNull(forWhom.lastSentMessage); + assertEquals(conversation.getForWhom(), forWhom); + assertTrue(conversation.isModal()); + + // Begin the conversation + conversation.begin(); + assertEquals("FirstPrompt", forWhom.lastSentMessage); + assertEquals(conversation, forWhom.begunConversation); + + // Send the first input + conversation.acceptInput("FirstInput"); + assertEquals("SecondPrompt", forWhom.lastSentMessage); + assertEquals(conversation, forWhom.abandonedConverstion); + } + + @Test + public void testConversationFactory() { + FakeConversable forWhom = new FakeConversable(); + NullConversationPrefix prefix = new NullConversationPrefix(); + ConversationFactory factory = new ConversationFactory(null) + .withFirstPrompt(new FirstPrompt()) + .withModality(false) + .withPrefix(prefix); + Conversation conversation = factory.buildConversation(forWhom); + + // Conversation not yet begun + assertNull(forWhom.lastSentMessage); + assertEquals(conversation.getForWhom(), forWhom); + assertFalse(conversation.isModal()); + assertEquals(conversation.getPrefix(), prefix); + + // Begin the conversation + conversation.begin(); + assertEquals("FirstPrompt", forWhom.lastSentMessage); + assertEquals(conversation, forWhom.begunConversation); + + // Send the first input + conversation.acceptInput("FirstInput"); + assertEquals("SecondPrompt", forWhom.lastSentMessage); + assertEquals(conversation, forWhom.abandonedConverstion); + } + + @Test + public void testEscapeSequence() { + FakeConversable forWhom = new FakeConversable(); + Conversation conversation = new Conversation(null, forWhom, new FirstPrompt()); + conversation.addConversationCanceller(new ExactMatchConversationCanceller("bananas")); + + // Begin the conversation + conversation.begin(); + assertEquals("FirstPrompt", forWhom.lastSentMessage); + assertEquals(conversation, forWhom.begunConversation); + + // Send the first input + conversation.acceptInput("bananas"); + assertEquals("bananas", forWhom.lastSentMessage); + assertEquals(conversation, forWhom.abandonedConverstion); + } + + @Test + public void testNotPlayer() { + FakeConversable forWhom = new FakeConversable(); + NullConversationPrefix prefix = new NullConversationPrefix(); + ConversationFactory factory = new ConversationFactory(null) + .thatExcludesNonPlayersWithMessage("bye"); + Conversation conversation = factory.buildConversation(forWhom); + + // Begin the conversation + conversation.begin(); + assertEquals("bye", forWhom.lastSentMessage); + assertEquals(conversation, forWhom.begunConversation); + assertEquals(conversation, forWhom.abandonedConverstion); + } + + private class FirstPrompt extends StringPrompt { + + public String getPromptText(ConversationContext context) { + return "FirstPrompt"; + } + + public Prompt acceptInput(ConversationContext context, String input) { + assertEquals("FirstInput", input); + context.setSessionData("data", 10); + return new SecondPrompt(); + } + } + + private class SecondPrompt extends MessagePrompt { + + @Override + protected Prompt getNextPrompt(ConversationContext context) { + return Prompt.END_OF_CONVERSATION; + } + + public String getPromptText(ConversationContext context) { + // Assert that session data passes from one prompt to the next + assertEquals(context.getSessionData("data"), 10); + return "SecondPrompt"; + } + } +} diff --git a/src/test/java/org/bukkit/conversations/FakeConversable.java b/src/test/java/org/bukkit/conversations/FakeConversable.java new file mode 100644 index 00000000..a04bfc8c --- /dev/null +++ b/src/test/java/org/bukkit/conversations/FakeConversable.java @@ -0,0 +1,99 @@ +package org.bukkit.conversations; + +import org.bukkit.Server; +import org.bukkit.permissions.Permission; +import org.bukkit.permissions.PermissionAttachment; +import org.bukkit.permissions.PermissionAttachmentInfo; +import org.bukkit.plugin.Plugin; + +import java.util.Set; + +/** + */ +public class FakeConversable implements Conversable { + public String lastSentMessage; + public Conversation begunConversation; + public Conversation abandonedConverstion; + + public boolean isConversing() { + return false; + } + + public void acceptConversationInput(String input) { + + } + + public boolean beginConversation(Conversation conversation) { + begunConversation = conversation; + conversation.outputNextPrompt(); + return true; + } + + public void abandonConversation(Conversation conversation) { + abandonedConverstion = conversation; + } + + public void sendRawMessage(String message) { + lastSentMessage = message; + } + + public Server getServer() { + return null; + } + + public String getName() { + return null; + } + + public boolean isPermissionSet(String name) { + return false; + } + + public boolean isPermissionSet(Permission perm) { + return false; + } + + public boolean hasPermission(String name) { + return false; + } + + public boolean hasPermission(Permission perm) { + return false; + } + + public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value) { + return null; + } + + public PermissionAttachment addAttachment(Plugin plugin) { + return null; + } + + public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value, int ticks) { + return null; + } + + public PermissionAttachment addAttachment(Plugin plugin, int ticks) { + return null; + } + + public void removeAttachment(PermissionAttachment attachment) { + + } + + public void recalculatePermissions() { + + } + + public Set<PermissionAttachmentInfo> getEffectivePermissions() { + return null; + } + + public boolean isOp() { + return false; + } + + public void setOp(boolean value) { + + } +} diff --git a/src/test/java/org/bukkit/conversations/ValidatingPromptTest.java b/src/test/java/org/bukkit/conversations/ValidatingPromptTest.java new file mode 100644 index 00000000..d1c0f42c --- /dev/null +++ b/src/test/java/org/bukkit/conversations/ValidatingPromptTest.java @@ -0,0 +1,115 @@ +package org.bukkit.conversations; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + */ +public class ValidatingPromptTest { + + @Test + public void TestBooleanPrompt() { + TestBooleanPrompt prompt = new TestBooleanPrompt(); + assertTrue(prompt.isInputValid(null, "true")); + assertFalse(prompt.isInputValid(null, "bananas")); + prompt.acceptInput(null, "true"); + assertTrue(prompt.result); + prompt.acceptInput(null, "no"); + assertFalse(prompt.result); + } + + @Test + public void TestFixedSetPrompt() { + TestFixedSetPrompt prompt = new TestFixedSetPrompt("foo", "bar"); + assertTrue(prompt.isInputValid(null, "foo")); + assertFalse(prompt.isInputValid(null, "cheese")); + prompt.acceptInput(null, "foo"); + assertEquals("foo", prompt.result); + } + + @Test + public void TestNumericPrompt() { + TestNumericPrompt prompt = new TestNumericPrompt(); + assertTrue(prompt.isInputValid(null, "1010220")); + assertFalse(prompt.isInputValid(null, "tomato")); + prompt.acceptInput(null, "1010220"); + assertEquals(1010220, prompt.result); + } + + @Test + public void TestRegexPrompt() { + TestRegexPrompt prompt = new TestRegexPrompt("a.c"); + assertTrue(prompt.isInputValid(null, "abc")); + assertTrue(prompt.isInputValid(null, "axc")); + assertFalse(prompt.isInputValid(null, "xyz")); + prompt.acceptInput(null, "abc"); + assertEquals("abc", prompt.result); + } + + //TODO: TestPlayerNamePrompt() + + private class TestBooleanPrompt extends BooleanPrompt { + public boolean result; + + @Override + protected Prompt acceptValidatedInput(ConversationContext context, boolean input) { + result = input; + return null; + } + + public String getPromptText(ConversationContext context) { + return null; + } + } + + private class TestFixedSetPrompt extends FixedSetPrompt { + public String result; + + public TestFixedSetPrompt(String... fixedSet) { + super(fixedSet); + } + + @Override + protected Prompt acceptValidatedInput(ConversationContext context, String input) { + result = input; + return null; + } + + public String getPromptText(ConversationContext context) { + return null; + } + } + + private class TestNumericPrompt extends NumericPrompt { + public Number result; + + @Override + protected Prompt acceptValidatedInput(ConversationContext context, Number input) { + result = input; + return null; + } + + public String getPromptText(ConversationContext context) { + return null; + } + } + + private class TestRegexPrompt extends RegexPrompt { + public String result; + + public TestRegexPrompt(String pattern) { + super(pattern); + } + + @Override + protected Prompt acceptValidatedInput(ConversationContext context, String input) { + result = input; + return null; + } + + public String getPromptText(ConversationContext context) { + return null; + } + } +} diff --git a/src/test/java/org/bukkit/plugin/messaging/TestPlayer.java b/src/test/java/org/bukkit/plugin/messaging/TestPlayer.java index 7cb648a1..2ad65732 100644 --- a/src/test/java/org/bukkit/plugin/messaging/TestPlayer.java +++ b/src/test/java/org/bukkit/plugin/messaging/TestPlayer.java @@ -9,6 +9,7 @@ import java.util.Set; import java.util.UUID; import org.bukkit.*; import org.bukkit.block.Block; +import org.bukkit.conversations.Conversation; import org.bukkit.entity.Arrow; import org.bukkit.entity.Egg; import org.bukkit.entity.Entity; @@ -718,4 +719,20 @@ public class TestPlayer implements Player { public boolean setWindowProperty(Property prop, int value) { throw new UnsupportedOperationException("Not supported yet."); } + + public boolean isConversing() { + throw new UnsupportedOperationException("Not supported yet."); + } + + public void acceptConversationInput(String input) { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean beginConversation(Conversation conversation) { + throw new UnsupportedOperationException("Not supported yet."); + } + + public void abandonConversation(Conversation conversation) { + throw new UnsupportedOperationException("Not supported yet."); + } } |