22
33import static org .assertj .core .api .Assertions .assertThat ;
44import static org .junit .jupiter .api .Assertions .fail ;
5+ import org .junit .jupiter .api .Disabled ;
56import org .junit .jupiter .api .Nested ;
67import org .junit .jupiter .api .Test ;
78
@@ -31,7 +32,7 @@ record SerializableReason(String name, int index, int min, int max) implements S
3132 @ Nested
3233 class TestConstructor {
3334 @ Test
34- void with_reason () {
35+ void with_Record_reason () {
3536 var exc = new Exc (new IndexOutOfRange ("data" , 4 , 0 , 3 ));
3637 var reason = IndexOutOfRange .class .cast (exc .getReason ());
3738 assertThat (reason .name ()).isEqualTo ("data" );
@@ -43,6 +44,28 @@ void with_reason() {
4344 // exc.printStackTrace();
4445 }
4546
47+ @ Test
48+ void with_enum_reason () {
49+ enum Reasons {
50+ FailToDoSomething ,
51+ }
52+
53+ var exc = new Exc (Reasons .FailToDoSomething );
54+ var reason = Reasons .class .cast (exc .getReason ());
55+ assertThat (reason .name ()).isEqualTo ("FailToDoSomething" );
56+ assertThat (exc .getCause ()).isNull ();
57+
58+ // exc.printStackTrace();
59+ }
60+
61+ @ Test
62+ void with_String_reason () {
63+ var exc = new Exc ("FailToDoSomething" );
64+ var reason = String .class .cast (exc .getReason ());
65+ assertThat (reason ).isEqualTo ("FailToDoSomething" );
66+ assertThat (exc .getCause ()).isNull ();
67+ }
68+
4669 @ Test
4770 void with_reason_but_reason_is_null () {
4871 try {
@@ -104,7 +127,7 @@ void identify_reason_with_instanceOf() {
104127 }
105128
106129 @ Test
107- void identify_reason_with_switch_expression () {
130+ void identify_Record_reason_with_switch_expression () {
108131 var exc = new Exc (new IndexOutOfRange ("data" , 4 , 0 , 3 ));
109132 switch (exc .getReason ()) {
110133 case IndexOutOfRange reason -> {
@@ -116,6 +139,24 @@ void identify_reason_with_switch_expression() {
116139 default -> fail ();
117140 }
118141 }
142+
143+ @ Test
144+ void identify_Enum_reason_with_switch_expression () {
145+ enum Reasons {
146+ FailToDoSomething , InvalidValue ,
147+ }
148+
149+ var exc = new Exc (Reasons .FailToDoSomething );
150+
151+ var s = switch (exc .getReason ()) {
152+ case Reasons enm -> switch (enm ) {
153+ case FailToDoSomething -> "fail to do something" ;
154+ case InvalidValue -> "invalid value" ;
155+ };
156+ default -> "unknown" ;
157+ };
158+ assertThat (s ).isEqualTo ("fail to do something" );
159+ }
119160 }
120161
121162 @ Nested
@@ -151,7 +192,7 @@ void getFile() {
151192 @ Test
152193 void getLine () {
153194 var exc = new Exc (new IndexOutOfRange ("data" , 4 , 0 , 3 ));
154- assertThat (exc .getLine ()).isEqualTo (153 );
195+ assertThat (exc .getLine ()).isEqualTo (194 );
155196 }
156197 }
157198
@@ -160,16 +201,16 @@ class TestGetMessage {
160201 @ Test
161202 void with_cause () {
162203 var exc = new Exc (new IndexOutOfRange ("data" , 4 , 0 , 3 ));
163- assertThat (exc .getMessage ())
164- . isEqualTo ( "com.github.sttk.errs.ExcTest$IndexOutOfRange { name=data, index=4, min=0, max=3 } " );
204+ assertThat (exc .getMessage ()). isEqualTo (
205+ "com.github.sttk.errs.ExcTest$IndexOutOfRange IndexOutOfRange[ name=data, index=4, min=0, max=3] " );
165206 }
166207
167208 @ Test
168209 void with_no_cause () {
169210 var cause = new IndexOutOfBoundsException (4 );
170211 var exc = new Exc (new IndexOutOfRange ("data" , 4 , 0 , 3 ), cause );
171- assertThat (exc .getMessage ())
172- . isEqualTo ( "com.github.sttk.errs.ExcTest$IndexOutOfRange { name=data, index=4, min=0, max=3 } " );
212+ assertThat (exc .getMessage ()). isEqualTo (
213+ "com.github.sttk.errs.ExcTest$IndexOutOfRange IndexOutOfRange[ name=data, index=4, min=0, max=3] " );
173214 }
174215 }
175216
@@ -179,15 +220,15 @@ class TestToString {
179220 void with_reason () {
180221 var exc = new Exc (new IndexOutOfRange ("data" , 4 , 0 , 3 ));
181222 assertThat (exc .toString ()).isEqualTo (
182- "com.github.sttk.errs.Exc { reason = com.github.sttk.errs.ExcTest$IndexOutOfRange { name=data, index=4, min=0, max=3 } , file = ExcTest.java, line = 180 }" );
223+ "com.github.sttk.errs.Exc { reason = com.github.sttk.errs.ExcTest$IndexOutOfRange IndexOutOfRange[ name=data, index=4, min=0, max=3] , file = ExcTest.java, line = 221 }" );
183224 }
184225
185226 @ Test
186227 void with_reason_and_cause () {
187228 var cause = new IndexOutOfBoundsException (4 );
188229 var exc = new Exc (new IndexOutOfRange ("data" , 4 , 0 , 3 ), cause );
189230 assertThat (exc .toString ()).isEqualTo (
190- "com.github.sttk.errs.Exc { reason = com.github.sttk.errs.ExcTest$IndexOutOfRange { name=data, index=4, min=0, max=3 } , file = ExcTest.java, line = 188 , cause = java.lang.IndexOutOfBoundsException: Index out of range: 4 }" );
231+ "com.github.sttk.errs.Exc { reason = com.github.sttk.errs.ExcTest$IndexOutOfRange IndexOutOfRange[ name=data, index=4, min=0, max=3] , file = ExcTest.java, line = 229 , cause = java.lang.IndexOutOfBoundsException: Index out of range: 4 }" );
191232 }
192233 }
193234
@@ -197,8 +238,8 @@ class TestToRuntimeException {
197238 void getMessage () {
198239 var exc = new Exc (new IndexOutOfRange ("data" , 4 , 0 , 3 ));
199240 var rtExc = exc .toRuntimeException ();
200- assertThat (rtExc .getMessage ())
201- . isEqualTo ( "com.github.sttk.errs.ExcTest$IndexOutOfRange { name=data, index=4, min=0, max=3 } " );
241+ assertThat (rtExc .getMessage ()). isEqualTo (
242+ "com.github.sttk.errs.ExcTest$IndexOutOfRange IndexOutOfRange[ name=data, index=4, min=0, max=3] " );
202243 }
203244
204245 @ Test
0 commit comments