11package dev .delivercraft .math .console ;
22
3- import static org .assertj .core .api .Assertions .assertThat ;
4- import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
5-
6- import java .io .ByteArrayInputStream ;
7- import java .io .ByteArrayOutputStream ;
8- import java .io .PrintStream ;
3+ import dev .delivercraft .io .CapturingLineWriter ;
4+ import dev .delivercraft .io .LineReader ;
5+ import dev .delivercraft .io .LineWriter ;
6+ import dev .delivercraft .io .StubLineReader ;
97import org .junit .jupiter .api .Test ;
108import org .junit .jupiter .params .ParameterizedTest ;
9+ import org .junit .jupiter .params .provider .CsvSource ;
10+ import org .junit .jupiter .params .provider .NullAndEmptySource ;
1111import org .junit .jupiter .params .provider .ValueSource ;
1212
13+ import static org .assertj .core .api .Assertions .assertThat ;
14+ import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
15+
1316class SimpleMathTest {
1417
1518 @ ParameterizedTest
16- @ ValueSource (strings = {"" , " " , "10\n " , "\n 5" })
19+ @ NullAndEmptySource
20+ @ ValueSource (strings = {" " })
1721 void inputIsRequired (String input ) {
18- SimpleMath simpleMath = new SimpleMath ( new ByteArrayInputStream ( input . getBytes ()),
19- new PrintStream ( new ByteArrayOutputStream () ));
22+ LineReader lineReader = () -> input ;
23+ SimpleMath simpleMath = new SimpleMath ( lineReader , new CapturingLineWriter ( ));
2024
2125 assertThatIllegalArgumentException ()
2226 .isThrownBy (simpleMath ::printOutput )
2327 .withMessage ("Input must not be empty!" );
2428 }
2529
26- @ Test
27- void firstInputMustBeANumber () {
28- SimpleMath simpleMath = new SimpleMath (new ByteArrayInputStream ("abc" .getBytes ()),
29- new PrintStream (new ByteArrayOutputStream ()));
30-
31- assertThatIllegalArgumentException ()
32- .isThrownBy (simpleMath ::printOutput )
33- .withMessage ("Please enter a valid number! Input: abc" );
34- }
35-
36- @ Test
37- void firstNumberMustBePositive () {
38- SimpleMath simpleMath = new SimpleMath (new ByteArrayInputStream ("-10\n 5" .getBytes ()),
39- new PrintStream (new ByteArrayOutputStream ()));
40-
41- assertThatIllegalArgumentException ()
42- .isThrownBy (simpleMath ::printOutput )
43- .withMessage ("Please enter a positive number! Input: -10" );
44- }
45-
46- @ Test
47- void secondInputMustBeANumber () {
48- SimpleMath simpleMath = new SimpleMath (new ByteArrayInputStream ("10\n asdf" .getBytes ()),
49- new PrintStream (new ByteArrayOutputStream ()));
50-
51- assertThatIllegalArgumentException ()
52- .isThrownBy (simpleMath ::printOutput )
53- .withMessage ("Please enter a valid number! Input: asdf" );
54- }
55-
56- @ Test
57- void secondNumberMustBePositive () {
58- SimpleMath simpleMath = new SimpleMath (new ByteArrayInputStream ("10\n -5" .getBytes ()),
59- new PrintStream (new ByteArrayOutputStream ()));
30+ @ ParameterizedTest
31+ @ CsvSource ({
32+ "abc, 5, Please enter a valid number! Input: abc" ,
33+ "-10, 5, Please enter a positive number! Input: -10" ,
34+ "10, asdf, Please enter a valid number! Input: asdf" ,
35+ "20, -5, Please enter a positive number! Input: -5"
36+ })
37+ void numbersMustBeValidPositiveNumbers (String firstInput , String secondInput , String expectedMessage ) {
38+ LineReader lineReader = new StubLineReader (firstInput , secondInput );
39+ SimpleMath simpleMath = new SimpleMath (lineReader , new CapturingLineWriter ());
6040
6141 assertThatIllegalArgumentException ()
6242 .isThrownBy (simpleMath ::printOutput )
63- .withMessage ("Please enter a positive number! Input: -5" );
43+ .withMessage (expectedMessage );
6444 }
6545
6646 @ Test
6747 void secondNumberMustNotBeZero () {
68- SimpleMath simpleMath = new SimpleMath ( new ByteArrayInputStream ( "10\n 0" . getBytes ()),
69- new PrintStream ( new ByteArrayOutputStream () ));
48+ LineReader lineReader = new StubLineReader ( "10" , "0" );
49+ SimpleMath simpleMath = new SimpleMath ( lineReader , new CapturingLineWriter ( ));
7050
7151 assertThatIllegalArgumentException ()
7252 .isThrownBy (simpleMath ::printOutput )
@@ -75,13 +55,13 @@ void secondNumberMustNotBeZero() {
7555
7656 @ Test
7757 void sumDifferenceProductAndQuotientIsPrinted () {
78- ByteArrayOutputStream outputStream = new ByteArrayOutputStream ( );
79- SimpleMath simpleMath = new SimpleMath ( new ByteArrayInputStream ( "10 \n 5" . getBytes ()),
80- new PrintStream ( outputStream ) );
58+ LineReader lineReader = new StubLineReader ( "10" , "5" );
59+ LineWriter lineWriter = new CapturingLineWriter ();
60+ SimpleMath simpleMath = new SimpleMath ( lineReader , lineWriter );
8161
8262 simpleMath .printOutput ();
8363
84- assertThat (outputStream ). hasToString ("""
64+ assertThat (lineWriter . toString ()). containsOnlyOnce ("""
8565 10 + 5 = 15
8666 10 - 5 = 5
8767 10 * 5 = 50
0 commit comments