Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
ConversationTest.java
1 package org.bukkit.conversations;
2 
3 import org.junit.Test;
4 import static org.junit.Assert.*;
5 
6 /**
7  */
8 public class ConversationTest {
9 
10  @Test
11  public void testBaseConversationFlow() {
12  FakeConversable forWhom = new FakeConversable();
13  Conversation conversation = new Conversation(null, forWhom, new FirstPrompt());
14 
15  // Conversation not yet begun
16  assertNull(forWhom.lastSentMessage);
17  assertEquals(conversation.getForWhom(), forWhom);
18  assertTrue(conversation.isModal());
19 
20  // Begin the conversation
21  conversation.begin();
22  assertEquals("FirstPrompt", forWhom.lastSentMessage);
23  assertEquals(conversation, forWhom.begunConversation);
24 
25  // Send the first input
26  conversation.acceptInput("FirstInput");
27  assertEquals("SecondPrompt", forWhom.lastSentMessage);
28  assertEquals(conversation, forWhom.abandonedConverstion);
29  }
30 
31  @Test
32  public void testConversationFactory() {
33  FakeConversable forWhom = new FakeConversable();
35  ConversationFactory factory = new ConversationFactory(null)
36  .withFirstPrompt(new FirstPrompt())
37  .withModality(false)
38  .withPrefix(prefix);
39  Conversation conversation = factory.buildConversation(forWhom);
40 
41  // Conversation not yet begun
42  assertNull(forWhom.lastSentMessage);
43  assertEquals(conversation.getForWhom(), forWhom);
44  assertFalse(conversation.isModal());
45  assertEquals(conversation.getPrefix(), prefix);
46 
47  // Begin the conversation
48  conversation.begin();
49  assertEquals("FirstPrompt", forWhom.lastSentMessage);
50  assertEquals(conversation, forWhom.begunConversation);
51 
52  // Send the first input
53  conversation.acceptInput("FirstInput");
54  assertEquals("SecondPrompt", forWhom.lastSentMessage);
55  assertEquals(conversation, forWhom.abandonedConverstion);
56  }
57 
58  @Test
59  public void testEscapeSequence() {
60  FakeConversable forWhom = new FakeConversable();
61  Conversation conversation = new Conversation(null, forWhom, new FirstPrompt());
62  conversation.addConversationCanceller(new ExactMatchConversationCanceller("bananas"));
63 
64  // Begin the conversation
65  conversation.begin();
66  assertEquals("FirstPrompt", forWhom.lastSentMessage);
67  assertEquals(conversation, forWhom.begunConversation);
68 
69  // Send the first input
70  conversation.acceptInput("bananas");
71  assertEquals("bananas", forWhom.lastSentMessage);
72  assertEquals(conversation, forWhom.abandonedConverstion);
73  }
74 
75  @Test
76  public void testNotPlayer() {
77  FakeConversable forWhom = new FakeConversable();
79  ConversationFactory factory = new ConversationFactory(null)
81  Conversation conversation = factory.buildConversation(forWhom);
82 
83  // Begin the conversation
84  conversation.begin();
85  assertEquals("bye", forWhom.lastSentMessage);
86  assertEquals(conversation, forWhom.begunConversation);
87  assertEquals(conversation, forWhom.abandonedConverstion);
88  }
89 
90  private class FirstPrompt extends StringPrompt {
91 
92  public String getPromptText(ConversationContext context) {
93  return "FirstPrompt";
94  }
95 
96  public Prompt acceptInput(ConversationContext context, String input) {
97  assertEquals("FirstInput", input);
98  context.setSessionData("data", 10);
99  return new SecondPrompt();
100  }
101  }
102 
103  private class SecondPrompt extends MessagePrompt {
104 
105  @Override
106  protected Prompt getNextPrompt(ConversationContext context) {
108  }
109 
110  public String getPromptText(ConversationContext context) {
111  // Assert that session data passes from one prompt to the next
112  assertEquals(context.getSessionData("data"), 10);
113  return "SecondPrompt";
114  }
115  }
116 }
Conversation buildConversation(Conversable forWhom)
static final Prompt END_OF_CONVERSATION
Definition: Prompt.java:15
ConversationFactory withPrefix(ConversationPrefix prefix)
ConversationFactory thatExcludesNonPlayersWithMessage(String playerOnlyMessage)
ConversationFactory withModality(boolean modal)
ConversationFactory withFirstPrompt(Prompt firstPrompt)