Debian Codegraph @

maven-packager-utils/src/test/java/org/debian/maven/packager/interaction/ChoiceQuestionTest.java

raw
1 /*
2 * Copyright 2013 Emmanuel Bourg
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.debian.maven.packager.interaction;
18
19 import java.io.BufferedReader;
20 import java.io.PrintWriter;
21 import java.io.StringReader;
22 import java.io.StringWriter;
23 import java.util.Arrays;
24
25 import junit.framework.TestCase;
26
27 public class ChoiceQuestionTest extends TestCase {
28
29 private String EOL = System.getProperty("line.separator");
30
31 public void testQuestion() {
32 StringWriter output = new StringWriter();
33
34 ChoiceQuestion question = new ChoiceQuestion("What's the color of your poney?",2, Arrays.asList("Red", "Green", "Blue"));
35 question.setInput(new BufferedReader(new StringReader("1\n")));
36 question.setOutput(new PrintWriter(output, true));
37
38 int answer = question.ask();
39
40 assertEquals("Question", "What's the color of your poney?" + EOL +
41 " 0 - Red" + EOL +
42 " 1 - Green" + EOL +
43 "[2] - Blue" + EOL +
44 "> ", output.toString());
45 assertEquals("Answer", 1, answer);
46 }
47
48 public void testDefaultChoice() {
49 StringWriter output = new StringWriter();
50
51 ChoiceQuestion question = new ChoiceQuestion("What's the color of your poney?",2, Arrays.asList("Red", "Green", "Blue"));
52 question.setInput(new BufferedReader(new StringReader("\n")));
53 question.setOutput(new PrintWriter(output, true));
54
55 int answer = question.ask();
56
57 assertEquals("Question", "What's the color of your poney?" + EOL +
58 " 0 - Red" + EOL +
59 " 1 - Green" + EOL +
60 "[2] - Blue" + EOL +
61 "> ", output.toString());
62 assertEquals("Answer", 2, answer);
63 }
64
65 public void testOutOfRangeChoice() {
66 StringWriter output = new StringWriter();
67
68 ChoiceQuestion question = new ChoiceQuestion("What's the color of your poney?",1, Arrays.asList("Red", "Green", "Blue"));
69 question.setInput(new BufferedReader(new StringReader("3\n0\n")));
70 question.setOutput(new PrintWriter(output, true));
71
72 int answer = question.ask();
73
74 assertEquals("Question", "What's the color of your poney?" + EOL +
75 " 0 - Red" + EOL +
76 "[1] - Green" + EOL +
77 " 2 - Blue" + EOL +
78 "> " +
79 "What's the color of your poney?" + EOL +
80 " 0 - Red" + EOL +
81 "[1] - Green" + EOL +
82 " 2 - Blue" + EOL +
83 "> ", output.toString());
84
85 assertEquals("Answer", 0, answer);
86 }
87
88 public void testNegativeChoice() {
89 StringWriter output = new StringWriter();
90
91 ChoiceQuestion question = new ChoiceQuestion("What's the color of your poney?",1, Arrays.asList("Red", "Green", "Blue"));
92 question.setInput(new BufferedReader(new StringReader("-1\n0\n")));
93 question.setOutput(new PrintWriter(output, true));
94
95 int answer = question.ask();
96
97 assertEquals("Question", "What's the color of your poney?" + EOL +
98 " 0 - Red" + EOL +
99 "[1] - Green" + EOL +
100 " 2 - Blue" + EOL +
101 "> " +
102 "What's the color of your poney?" + EOL +
103 " 0 - Red" + EOL +
104 "[1] - Green" + EOL +
105 " 2 - Blue" + EOL +
106 "> ", output.toString());
107
108 assertEquals("Answer", 0, answer);
109 }
110
111 public void testNonNumericChoice() {
112 StringWriter output = new StringWriter();
113
114 ChoiceQuestion question = new ChoiceQuestion("What's the color of your poney?",1, Arrays.asList("Red", "Green", "Blue"));
115 question.setInput(new BufferedReader(new StringReader("X\n2\n")));
116 question.setOutput(new PrintWriter(output, true));
117
118 int answer = question.ask();
119
120 assertEquals("Question", "What's the color of your poney?" + EOL +
121 " 0 - Red" + EOL +
122 "[1] - Green" + EOL +
123 " 2 - Blue" + EOL +
124 "> " +
125 "What's the color of your poney?" + EOL +
126 " 0 - Red" + EOL +
127 "[1] - Green" + EOL +
128 " 2 - Blue" + EOL +
129 "> ", output.toString());
130
131 assertEquals("Answer", 2, answer);
132 }
133 }