Permalink
Browse files

Paginate text.

  • Loading branch information...
Stuart Childs
Stuart Childs committed Apr 27, 2012
1 parent c935886 commit 6bdb1dc3681cf369de5db6313de42ab808337ede
Showing with 35 additions and 4 deletions.
  1. +15 −4 src/com/example/classproject/HelloWorld.java
  2. +20 −0 src/com/example/classproject/HelloWorldTest.java
@@ -1,5 +1,7 @@
package com.example.classproject;
import sun.net.idn.StringPrep;
import java.io.*;
public class HelloWorld {
@@ -13,7 +15,7 @@ public static void main(String[] args) throws IOException {
public static void runProgram(String filename, InputStream userInputStream) throws IOException {
File storyFile = new File(filename);
readFile(storyFile);
printText(_tale);
printText(_tale, userInputStream);
System.out.print("Please type your selection: ");
String userInput = readInput(userInputStream);
@@ -23,12 +25,21 @@ public static void runProgram(String filename, InputStream userInputStream) thro
}
File file = new File("story/" + userInput + ".txt");
readFile(file);
printText(_tale);
printText(_tale, userInputStream);
}
private static void printText(String text) throws IOException {
public static void printText(String text, InputStream userInputStream) throws IOException {
String[] lines = text.split("\n");
int i = 0;
for (String line : lines) {
if ((i) % 10 == 0 && i > 0) {
System.out.println("Please press enter to view more.");
readInput(userInputStream);
}
System.out.println(line);
System.out.print(text);
i++;
}
System.out.println();
}
@@ -113,6 +113,26 @@ public void runProgramPrintsAnErrorIfUserInputDoesNotValidate() {
}
}
@Test
public void testShouldPaginateText() {
ByteArrayInputStream input = getInputStream("\n");
String firstChunk = "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n";
String prompt = "Please press enter to view more.";
String secondChunk = "11\n12\n13\n14\n\n";
ByteArrayOutputStream output = new ByteArrayOutputStream();
System.setOut(new PrintStream(output));
try {
HelloWorld.printText(firstChunk + secondChunk, input);
} catch (IOException e) {
fail(e.getMessage());
}
assertTrue(output.toString().startsWith(firstChunk));
assertTrue(output.toString().contains(prompt));
assertTrue(output.toString().endsWith(secondChunk));
}
private ByteArrayInputStream getInputStream(String inputStream) {
return new ByteArrayInputStream(inputStream.getBytes());
}

0 comments on commit 6bdb1dc

Please sign in to comment.