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,14 @@ 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 ("IndexOutOfRange[name=data, index=4, min=0, max=3]" );
165205 }
166206
167207 @ Test
168208 void with_no_cause () {
169209 var cause = new IndexOutOfBoundsException (4 );
170210 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 }" );
211+ assertThat (exc .getMessage ()).isEqualTo ("IndexOutOfRange[name=data, index=4, min=0, max=3]" );
173212 }
174213 }
175214
@@ -179,15 +218,15 @@ class TestToString {
179218 void with_reason () {
180219 var exc = new Exc (new IndexOutOfRange ("data" , 4 , 0 , 3 ));
181220 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 }" );
221+ "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 = 219 }" );
183222 }
184223
185224 @ Test
186225 void with_reason_and_cause () {
187226 var cause = new IndexOutOfBoundsException (4 );
188227 var exc = new Exc (new IndexOutOfRange ("data" , 4 , 0 , 3 ), cause );
189228 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 }" );
229+ "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 = 227 , cause = java.lang.IndexOutOfBoundsException: Index out of range: 4 }" );
191230 }
192231 }
193232
@@ -197,8 +236,7 @@ class TestToRuntimeException {
197236 void getMessage () {
198237 var exc = new Exc (new IndexOutOfRange ("data" , 4 , 0 , 3 ));
199238 var rtExc = exc .toRuntimeException ();
200- assertThat (rtExc .getMessage ())
201- .isEqualTo ("com.github.sttk.errs.ExcTest$IndexOutOfRange { name=data, index=4, min=0, max=3 }" );
239+ assertThat (rtExc .getMessage ()).isEqualTo ("IndexOutOfRange[name=data, index=4, min=0, max=3]" );
202240 }
203241
204242 @ Test
0 commit comments