tdd-java-demo
tdd java demo with micro-commits
git clone https://9o.is/git/tdd-java-demo.git
RunnerTest.java
(1406B)
1 package takehome;
2
3 import org.junit.jupiter.api.Test;
4
5 import java.io.ByteArrayInputStream;
6 import java.io.ByteArrayOutputStream;
7 import java.util.Arrays;
8 import java.util.List;
9
10 import static org.hamcrest.MatcherAssert.assertThat;
11 import static org.hamcrest.Matchers.contains;
12 import static org.hamcrest.Matchers.hasItem;
13
14 class RunnerTest {
15
16 @Test
17 public void example() {
18 assertThat(output("2", "1", "1", "2", "0"), contains(
19 "How much soup? ",
20 "How much bread? ",
21 "How much milk? ",
22 "How much apple? ",
23 "Purchase day (0 for today, 1 for tomorrow, etc): ",
24 "Total: $3.20"
25 ));
26 }
27
28 @Test
29 public void totals() {
30 assertThat(output("3", "2", "0", "0", "0"), hasItem("Total: $3.15"));
31 assertThat(output("0", "0", "1", "6", "0"), hasItem("Total: $1.90"));
32 assertThat(output("0", "0", "1", "6", "5"), hasItem("Total: $1.84"));
33 assertThat(output("2", "1", "0", "3", "5"), hasItem("Total: $1.97"));
34 }
35
36 private List<String> output(String ...inputs) {
37 String input = String.join("\r", inputs);
38 ByteArrayInputStream in = new ByteArrayInputStream(input.getBytes());
39 ByteArrayOutputStream out = new ByteArrayOutputStream();
40 new Runner(in, out).run();
41 return Arrays.asList(out.toString().split("\n"));
42 }
43 }