001 package ps6.test;
002
003 import java.io.ByteArrayInputStream;
004 import ps6.*;
005 import junit.framework.*;
006
007 /**
008 * Public (Specification) TextUI Test Suite
009 * @author tws
010 */
011 public class PublicTextUITest extends TestSuite{
012
013 public static final String INPUT_PROMPTS =
014 "starting number? starting street? starting zipcode? " +
015 "destination number? destination street? destination zipcode? walking or driving [w/d]? ";
016
017 public static final String END_PROMPT = "starting number? ";
018
019 public PublicTextUITest(){
020 this("Problem Set 6 TextUI Tests");
021 }
022
023 public static String EOL = System.getProperty("line.separator");
024
025 public PublicTextUITest(String name){
026 super(name);
027 addTests();
028 }
029
030 /**
031 * Constructs and adds the tests for the textui.
032 */
033 private void addTests() {
034 // You could test groups of queries separately
035 // e.g. loop over ValidateQueries.badQueries
036
037 for (TestRecord record : ValidateQueries.allQueries) {
038 addTest(makeUiTest(record));
039 }
040 }
041
042 public PS6TextUiTestCase makeUiTest(TestRecord record){
043 return new PS6TextUiTestCase(record.getTestName(),
044 new String[] {record.getDb().dbPath()},
045 TextUI.class,
046 new ByteArrayInputStream((genTextUiStdIn(record) + "-1" + EOL).getBytes()),
047 INPUT_PROMPTS + genTextUiStdOut(record));
048 }
049
050 public static Test suite(){
051 return new PublicTextUITest();
052 }
053
054 /**
055 * Generate the expected TextUI output for a test record
056 * @param record a PS6 test
057 * @return the expected TestUI output for a test
058 */
059 public static String genTextUiStdOut(TestRecord record){
060 StringBuilder b = new StringBuilder();
061
062 switch (record.getType()){
063 case INVALID_DIR_TYPE:
064 case INVALID_ADDRESS:
065 case NO_PATH:
066 b.append(record.getErrorMessage());
067 b.append(EOL);
068 break;
069 case WALKING:
070 case DRIVING:
071 b.append("Start at "
072 + record.getStart().getNum() + " "
073 + record.getStart().getName()
074 + " " + record.getStart().getZipcode());
075
076 b.append(EOL);
077 for (String line : record.getDirections()){
078 b.append(line);
079 b.append(EOL);
080 }
081 b.append(record.getTripLength());
082 b.append(EOL);
083 b.append(END_PROMPT);
084 break;
085 }
086
087 return b.toString();
088 }
089
090 /**
091 * Generate the TextUI input for a test record
092 * @param record a PS6 test
093 * @return the TextUI input for the test
094 */
095 public static String genTextUiStdIn(TestRecord record){
096 StringBuilder b = new StringBuilder();
097 b.append(record.getStart().getNum());
098 b.append(EOL);
099 b.append(record.getStart().getName());
100 b.append(EOL);
101 b.append(record.getStart().getZipcode());
102 b.append(EOL);
103 b.append(record.getEnd().getNum());
104 b.append(EOL);
105 b.append(record.getEnd().getName());
106 b.append(EOL);
107 b.append(record.getEnd().getZipcode());
108 b.append(EOL);
109 b.append(record.getDirectionType());
110 b.append(EOL);
111 return b.toString();
112 }
113 }