Skip to content

Commit dfb7a7c

Browse files
committedApr 29, 2022
tests added
1 parent 4b4c1d2 commit dfb7a7c

File tree

5 files changed

+227
-38
lines changed

5 files changed

+227
-38
lines changed
 

‎.classpath

-6
This file was deleted.

‎.project

-17
This file was deleted.

‎CLI.java

+141-11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
import Users.Client;
1414
import Users.MallAdmin;
1515

16+
import java.io.File;
17+
import java.io.FileNotFoundException;
18+
1619
public class CLI {
1720
Database db = new Database();
1821
private int path = 0;
@@ -21,6 +24,10 @@ public class CLI {
2124
public static final String RED = "\u001B[31m";
2225
public static final String GREEN = "\u001B[32m";
2326

27+
// For testing
28+
boolean test = false;
29+
boolean errorHappened = false;
30+
2431
// Either one should have a value if logged in
2532
BasicUser user;
2633

@@ -36,26 +43,32 @@ public class CLI {
3643
"--- ADMIN PAGE ---\n 1. Get restaurant sales and statistics\n 2. Log out\n-----------------------------"
3744
};
3845

39-
public static void printError(String err) {
46+
public void printError(String err) {
4047
System.out.println(RED + err + WHITE);
48+
49+
if(test) errorHappened = true;
4150
}
4251

43-
public static void printSuccess(String text) {
52+
public void printSuccess(String text) {
4453
System.out.println(GREEN + text + WHITE);
4554
}
4655

47-
public static void printBold(String text) {
56+
public void printBold(String text) {
4857
System.out.println("\033[1m" + text + "\033[0m");
4958
}
5059

60+
public void isTest(boolean it) {
61+
test = it;
62+
}
63+
5164
public void logout() {
5265
path = 0;
5366
user = null;
5467
cart = null;
5568
order = null;
5669
}
5770

58-
public boolean mainCommands(int cmd, Scanner scanner) {
71+
public boolean mainCommands(int cmd, CustomScanner scanner) {
5972
boolean shouldExit = false;
6073

6174
switch (cmd) {
@@ -159,7 +172,7 @@ public boolean mainCommands(int cmd, Scanner scanner) {
159172
return shouldExit;
160173
}
161174

162-
public void userCommands(int cmd, Scanner scanner) {
175+
public void userCommands(int cmd, CustomScanner scanner) {
163176
/*
164177
* 1. Show restaurants
165178
* 2. Start a new order
@@ -275,7 +288,7 @@ else if (checkStatusStatus == OrderStatus.PREPARING)
275288

276289
}
277290

278-
public void restaurantOwnerCommands(int cmd, Scanner scanner) {
291+
public void restaurantOwnerCommands(int cmd, CustomScanner scanner) {
279292
/*
280293
* 1. Show restaurant menu
281294
* 2. Add item to menu
@@ -360,7 +373,7 @@ public void restaurantOwnerCommands(int cmd, Scanner scanner) {
360373
}
361374
}
362375

363-
public void mallAdminCommands(int cmd, Scanner scanner) {
376+
public void mallAdminCommands(int cmd, CustomScanner scanner) {
364377
/*
365378
* 1. Get sales statistics
366379
* 2. Log out
@@ -436,9 +449,7 @@ public static void prepopulate(CLI cli) {
436449
db.addAdmin("admin", newAdmin);
437450
}
438451

439-
public static void main(String[] args) {
440-
Scanner scanner = new Scanner(System.in);
441-
CLI cli = new CLI();
452+
public static void run(CLI cli, CustomScanner scanner) {
442453
boolean shouldExit = false;
443454
prepopulate(cli);
444455

@@ -447,6 +458,7 @@ public static void main(String[] args) {
447458
System.out.print("Select an option (number): ");
448459
int cmd = scanner.nextInt();
449460
System.out.println();
461+
450462
switch (cli.path) {
451463
case 0: // run it back
452464
shouldExit = cli.mainCommands(cmd, scanner);
@@ -467,7 +479,125 @@ public static void main(String[] args) {
467479
System.out.println();
468480

469481
}
482+
}
483+
public static void main(String[] args) {
484+
// Run program on test or normal mode
485+
Scanner scanner = new Scanner(System.in);
486+
System.out.print("Is this a test [y/n]: ");
487+
String answer = scanner.next();
488+
489+
CLI cli = new CLI();
490+
491+
// Custom scanner
492+
CustomScanner customScanner;
493+
boolean isTest = answer.equals("y");
494+
495+
// For tests
496+
ArrayList<String> inputLines = new ArrayList<String>();
497+
ArrayList<String> outputLines = new ArrayList<String>();
498+
499+
500+
if(!isTest) {
501+
customScanner = new CustomScanner(scanner);
502+
} else {
503+
// Display possible tests
504+
File folder = new File("../testcases/");
505+
File[] listOfFiles = folder.listFiles();
506+
System.out.println();
507+
System.out.println("Tests available:");
508+
for (File f : listOfFiles) {
509+
System.out.println(" - " + f.getName());
510+
}
511+
System.out.println();
512+
513+
514+
// Get test name
515+
System.out.print("What is the test name? ");
516+
String testName = scanner.next();
517+
scanner.close();
518+
519+
// Read test files
520+
String inputFileName = "../testcases/" + testName + "/input.txt";
521+
String outputFileName = "../testcases/" + testName + "/output.txt";
522+
523+
try {
524+
// Read input test file
525+
Scanner inputScanner = new Scanner(new File(inputFileName));
526+
while (inputScanner.hasNextLine()) inputLines.add(inputScanner.nextLine());
527+
inputScanner.close();
528+
529+
// Read output test file
530+
Scanner outputScanner = new Scanner(new File(outputFileName));
531+
while (outputScanner.hasNextLine()) outputLines.add(outputScanner.nextLine());
532+
outputScanner.close();
533+
} catch (FileNotFoundException e) {
534+
e.printStackTrace();
535+
}
536+
537+
customScanner = new CustomScanner(inputLines);
538+
cli.isTest(true);
539+
}
470540

471-
scanner.close();
541+
try {
542+
run(cli, customScanner);
543+
} catch(IndexOutOfBoundsException e) {
544+
if(isTest) {
545+
// Capture expected db output
546+
ArrayList<String> testOutputLines = new ArrayList<String>();
547+
testOutputLines.add(cli.errorHappened ? "true" : "false");
548+
testOutputLines.add(cli.db.ownerDatabaseToString());
549+
testOutputLines.add(cli.db.clientDatabaseToString());
550+
testOutputLines.add(cli.db.restaurantDatabaseToString());
551+
testOutputLines.add(cli.db.adminDatabaseToString());
552+
553+
System.out.println();
554+
555+
// Get if we are expecting an error and if it happened
556+
boolean expectedError = outputLines.get(0).equals("true");
557+
boolean errorOcurred = testOutputLines.get(0).equals("true");
558+
559+
// Check if output is the same
560+
boolean testPassed = true;
561+
for (int i = 1; i < testOutputLines.size(); i++) {
562+
if(!testOutputLines.get(i).equals(outputLines.get(i))) {
563+
testPassed = false;
564+
break;
565+
}
566+
}
567+
568+
// Make space
569+
for(int i = 0; i < 40; i++) System.out.println();
570+
571+
// Display results
572+
if(testPassed && errorOcurred == expectedError) cli.printSuccess("Test passed");
573+
else if(!testPassed && errorOcurred && expectedError) cli.printSuccess("Test passed");
574+
else if (errorOcurred != expectedError) {
575+
cli.printError("Test failed");
576+
cli.printError("A runtime error ocurred.");
577+
}
578+
else {
579+
cli.printError("Test failed");
580+
System.out.println();
581+
cli.printError("** OUTPUT **");
582+
for (String s : testOutputLines) {
583+
cli.printError(s);
584+
}
585+
System.out.println();
586+
cli.printError("** EXPECTED OUTPUT **");
587+
for (String s : outputLines) {
588+
cli.printError(s);
589+
}
590+
}
591+
}
592+
} catch (Exception e) {
593+
// Unexpected runtime error ocurred
594+
595+
// Make space
596+
for(int i = 0; i < 40; i++) System.out.println();
597+
598+
cli.printError("Test failed");
599+
cli.printError("A runtime error ocurred.");
600+
cli.printError(e.toString());
601+
}
472602
}
473603
}

‎CustomScanner.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.util.ArrayList;
2+
import java.util.Scanner;
3+
4+
public class CustomScanner {
5+
// Custom scanner class to run tests or cli input
6+
7+
Scanner scanner;
8+
ArrayList<String> answers;
9+
10+
public CustomScanner(Scanner _scanner) {
11+
scanner = _scanner;
12+
}
13+
14+
public CustomScanner(ArrayList<String> _answers) {
15+
answers = _answers;
16+
}
17+
18+
public int nextInt() {
19+
if(answers != null) {
20+
return Integer.parseInt(answers.remove(0));
21+
} else {
22+
return scanner.nextInt();
23+
}
24+
}
25+
26+
public double nextDouble() {
27+
if(answers != null) {
28+
return Double.parseDouble(answers.remove(0));
29+
} else {
30+
return scanner.nextDouble();
31+
}
32+
}
33+
34+
public String next() {
35+
if(answers != null) {
36+
return answers.remove(0);
37+
} else {
38+
return scanner.next();
39+
}
40+
}
41+
}

‎Database/Database.java

+45-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import Restaurants.Restaurant;
99

1010
public class Database {
11-
private HashMap<String, RestaurantOwner> ownerDatabase = new HashMap<>();
12-
private HashMap<String, Client> clientDatabase = new HashMap<>();
13-
private HashMap<String, Restaurant> restaurantDatabase = new HashMap<>();
14-
private HashMap<String, MallAdmin> adminDatabase = new HashMap<>();
11+
public HashMap<String, RestaurantOwner> ownerDatabase = new HashMap<>();
12+
public HashMap<String, Client> clientDatabase = new HashMap<>();
13+
public HashMap<String, Restaurant> restaurantDatabase = new HashMap<>();
14+
public HashMap<String, MallAdmin> adminDatabase = new HashMap<>();
1515

1616
// GET METHODS
1717
public Client getClientByUserName(String userName) {
@@ -119,4 +119,45 @@ public MallAdmin authenticateAdmin(String userName, String password) {
119119
return null;
120120

121121
}
122+
123+
// TESTING METHODS
124+
public String adminDatabaseToString() {
125+
String res = "{";
126+
for (String k : adminDatabase.keySet()) {
127+
MallAdmin a = adminDatabase.get(k);
128+
res += k + "=" + a.getUserName() + "-" + a.getPassword() + ",";
129+
}
130+
res += "}";
131+
return res;
132+
}
133+
134+
public String clientDatabaseToString() {
135+
String res = "{";
136+
for (String k : clientDatabase.keySet()) {
137+
Client c = clientDatabase.get(k);
138+
res += k + "=" + c.getUserName() + "-" + c.getPassword() + ",";
139+
}
140+
res += "}";
141+
return res;
142+
}
143+
144+
public String restaurantDatabaseToString() {
145+
String res = "{";
146+
for (String k : restaurantDatabase.keySet()) {
147+
Restaurant r = restaurantDatabase.get(k);
148+
res += k + "=" + r.getRestaurantName() + "-" + r.getIsClosed() + "-" + r.getNumOfOrders() + ",";
149+
}
150+
res += "}";
151+
return res;
152+
}
153+
154+
public String ownerDatabaseToString() {
155+
String res = "{";
156+
for (String k : ownerDatabase.keySet()) {
157+
RestaurantOwner r = ownerDatabase.get(k);
158+
res += k + "=" + r.getUserName() + "-" + r.getPassword() + "-" + r.getRestaurant() + ",";
159+
}
160+
res += "}";
161+
return res;
162+
}
122163
}

0 commit comments

Comments
 (0)
Please sign in to comment.