13
13
import Users .Client ;
14
14
import Users .MallAdmin ;
15
15
16
+ import java .io .File ;
17
+ import java .io .FileNotFoundException ;
18
+
16
19
public class CLI {
17
20
Database db = new Database ();
18
21
private int path = 0 ;
@@ -21,6 +24,10 @@ public class CLI {
21
24
public static final String RED = "\u001B [31m" ;
22
25
public static final String GREEN = "\u001B [32m" ;
23
26
27
+ // For testing
28
+ boolean test = false ;
29
+ boolean errorHappened = false ;
30
+
24
31
// Either one should have a value if logged in
25
32
BasicUser user ;
26
33
@@ -36,26 +43,32 @@ public class CLI {
36
43
"--- ADMIN PAGE ---\n 1. Get restaurant sales and statistics\n 2. Log out\n -----------------------------"
37
44
};
38
45
39
- public static void printError (String err ) {
46
+ public void printError (String err ) {
40
47
System .out .println (RED + err + WHITE );
48
+
49
+ if (test ) errorHappened = true ;
41
50
}
42
51
43
- public static void printSuccess (String text ) {
52
+ public void printSuccess (String text ) {
44
53
System .out .println (GREEN + text + WHITE );
45
54
}
46
55
47
- public static void printBold (String text ) {
56
+ public void printBold (String text ) {
48
57
System .out .println ("\033 [1m" + text + "\033 [0m" );
49
58
}
50
59
60
+ public void isTest (boolean it ) {
61
+ test = it ;
62
+ }
63
+
51
64
public void logout () {
52
65
path = 0 ;
53
66
user = null ;
54
67
cart = null ;
55
68
order = null ;
56
69
}
57
70
58
- public boolean mainCommands (int cmd , Scanner scanner ) {
71
+ public boolean mainCommands (int cmd , CustomScanner scanner ) {
59
72
boolean shouldExit = false ;
60
73
61
74
switch (cmd ) {
@@ -159,7 +172,7 @@ public boolean mainCommands(int cmd, Scanner scanner) {
159
172
return shouldExit ;
160
173
}
161
174
162
- public void userCommands (int cmd , Scanner scanner ) {
175
+ public void userCommands (int cmd , CustomScanner scanner ) {
163
176
/*
164
177
* 1. Show restaurants
165
178
* 2. Start a new order
@@ -275,7 +288,7 @@ else if (checkStatusStatus == OrderStatus.PREPARING)
275
288
276
289
}
277
290
278
- public void restaurantOwnerCommands (int cmd , Scanner scanner ) {
291
+ public void restaurantOwnerCommands (int cmd , CustomScanner scanner ) {
279
292
/*
280
293
* 1. Show restaurant menu
281
294
* 2. Add item to menu
@@ -360,7 +373,7 @@ public void restaurantOwnerCommands(int cmd, Scanner scanner) {
360
373
}
361
374
}
362
375
363
- public void mallAdminCommands (int cmd , Scanner scanner ) {
376
+ public void mallAdminCommands (int cmd , CustomScanner scanner ) {
364
377
/*
365
378
* 1. Get sales statistics
366
379
* 2. Log out
@@ -436,9 +449,7 @@ public static void prepopulate(CLI cli) {
436
449
db .addAdmin ("admin" , newAdmin );
437
450
}
438
451
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 ) {
442
453
boolean shouldExit = false ;
443
454
prepopulate (cli );
444
455
@@ -447,6 +458,7 @@ public static void main(String[] args) {
447
458
System .out .print ("Select an option (number): " );
448
459
int cmd = scanner .nextInt ();
449
460
System .out .println ();
461
+
450
462
switch (cli .path ) {
451
463
case 0 : // run it back
452
464
shouldExit = cli .mainCommands (cmd , scanner );
@@ -467,7 +479,125 @@ public static void main(String[] args) {
467
479
System .out .println ();
468
480
469
481
}
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
+ }
470
540
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
+ }
472
602
}
473
603
}
0 commit comments