Skip to content

Commit c14a2f2

Browse files
test(exceptions): add comprehensive unit tests for PolicySDKException
1 parent 64d366d commit c14a2f2

File tree

1 file changed

+264
-0
lines changed

1 file changed

+264
-0
lines changed
Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
import 'dart:async';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:flutter_policy_engine/src/exceptions/policy_sdk_exception.dart';
4+
import 'package:flutter_policy_engine/src/exceptions/i_policy_sdk_exceptions.dart';
5+
6+
// Helper classes for testing
7+
class _CustomException implements Exception {
8+
final String detail;
9+
_CustomException(this.detail);
10+
11+
@override
12+
String toString() => 'CustomException: $detail';
13+
}
14+
15+
class _NullToStringException implements Exception {
16+
@override
17+
String toString() => '';
18+
}
19+
20+
class _ComplexException implements Exception {
21+
final Map<String, dynamic> data;
22+
_ComplexException(this.data);
23+
24+
@override
25+
String toString() => 'ComplexException: ${data.toString()}';
26+
}
27+
28+
void main() {
29+
group('PolicySDKException', () {
30+
test('should create exception with required message and exception', () {
31+
const message = 'Failed to load policy configuration';
32+
final underlyingException = Exception('Network error');
33+
final exception = PolicySDKException(
34+
message,
35+
exception: underlyingException,
36+
);
37+
38+
expect(exception.message, equals(message));
39+
expect(exception.exception, equals(underlyingException));
40+
});
41+
42+
test('should create exception with null underlying exception', () {
43+
const message = 'Failed to load policy configuration';
44+
final exception = PolicySDKException(
45+
message,
46+
exception: null,
47+
);
48+
49+
expect(exception.message, equals(message));
50+
expect(exception.exception, isNull);
51+
});
52+
53+
test('should implement IPolicySDKException interface', () {
54+
const message = 'Failed to load policy configuration';
55+
final underlyingException = Exception('Network error');
56+
final exception = PolicySDKException(
57+
message,
58+
exception: underlyingException,
59+
);
60+
61+
expect(exception, isA<IPolicySDKException>());
62+
expect(exception, isA<Exception>());
63+
});
64+
65+
test(
66+
'should return correct string representation with underlying exception',
67+
() {
68+
const message = 'Failed to load policy configuration';
69+
final underlyingException = Exception('Network error');
70+
final exception = PolicySDKException(
71+
message,
72+
exception: underlyingException,
73+
);
74+
75+
expect(
76+
exception.toString(),
77+
equals('SDKException: $message\nExtra info: Exception: Network error'),
78+
);
79+
});
80+
81+
test(
82+
'should return correct string representation without underlying exception',
83+
() {
84+
const message = 'Failed to load policy configuration';
85+
final exception = PolicySDKException(
86+
message,
87+
exception: null,
88+
);
89+
90+
expect(exception.toString(), equals('SDKException: $message'));
91+
});
92+
93+
test('should handle different types of underlying exceptions', () {
94+
const message = 'Failed to load policy configuration';
95+
96+
// Test with FormatException
97+
final formatError = FormatException('Invalid format');
98+
final exceptionWithFormatError = PolicySDKException(
99+
message,
100+
exception: formatError,
101+
);
102+
103+
expect(exceptionWithFormatError.exception, equals(formatError));
104+
expect(
105+
exceptionWithFormatError.toString(),
106+
contains('FormatException: Invalid format'),
107+
);
108+
109+
// Test with another Exception type
110+
final timeoutError = TimeoutException('Operation timed out');
111+
final exceptionWithTimeoutError = PolicySDKException(
112+
message,
113+
exception: timeoutError,
114+
);
115+
116+
expect(exceptionWithTimeoutError.exception, equals(timeoutError));
117+
expect(
118+
exceptionWithTimeoutError.toString(),
119+
contains('TimeoutException: Operation timed out'),
120+
);
121+
});
122+
123+
test('should handle custom exception types', () {
124+
const message = 'Failed to load policy configuration';
125+
126+
final customError = _CustomException('Custom error detail');
127+
final exception = PolicySDKException(
128+
message,
129+
exception: customError,
130+
);
131+
132+
expect(exception.exception, equals(customError));
133+
expect(
134+
exception.toString(),
135+
equals(
136+
'SDKException: $message\nExtra info: CustomException: Custom error detail'),
137+
);
138+
});
139+
140+
test('should handle special characters in message', () {
141+
const message = 'Failed to load policy with special chars: !@#\$%^&*()';
142+
final underlyingException = Exception('Special error: !@#\$%^&*()');
143+
final exception = PolicySDKException(
144+
message,
145+
exception: underlyingException,
146+
);
147+
148+
expect(exception.message, equals(message));
149+
expect(exception.exception, equals(underlyingException));
150+
expect(
151+
exception.toString(),
152+
equals(
153+
'SDKException: $message\nExtra info: Exception: Special error: !@#\$%^&*()'),
154+
);
155+
});
156+
157+
test('should handle empty message', () {
158+
const message = '';
159+
final underlyingException = Exception('Network error');
160+
final exception = PolicySDKException(
161+
message,
162+
exception: underlyingException,
163+
);
164+
165+
expect(exception.message, equals(message));
166+
expect(exception.exception, equals(underlyingException));
167+
expect(
168+
exception.toString(),
169+
equals('SDKException: \nExtra info: Exception: Network error'),
170+
);
171+
});
172+
173+
test('should handle multiline message', () {
174+
const message =
175+
'Failed to load policy configuration\nThis is a multiline error message';
176+
final underlyingException = Exception('Network error');
177+
final exception = PolicySDKException(
178+
message,
179+
exception: underlyingException,
180+
);
181+
182+
expect(exception.message, equals(message));
183+
expect(exception.exception, equals(underlyingException));
184+
expect(
185+
exception.toString(),
186+
equals('SDKException: $message\nExtra info: Exception: Network error'),
187+
);
188+
});
189+
190+
test('should handle exception with null toString() result', () {
191+
const message = 'Failed to load policy configuration';
192+
193+
final nullToStringError = _NullToStringException();
194+
final exception = PolicySDKException(
195+
message,
196+
exception: nullToStringError,
197+
);
198+
199+
expect(exception.exception, equals(nullToStringError));
200+
expect(
201+
exception.toString(),
202+
equals('SDKException: $message\nExtra info: '),
203+
);
204+
});
205+
206+
test('should handle exception with complex toString() result', () {
207+
const message = 'Failed to load policy configuration';
208+
209+
final complexError = _ComplexException({
210+
'errorCode': 500,
211+
'details': ['detail1', 'detail2'],
212+
'timestamp': DateTime.now(),
213+
});
214+
215+
final exception = PolicySDKException(
216+
message,
217+
exception: complexError,
218+
);
219+
220+
expect(exception.exception, equals(complexError));
221+
expect(
222+
exception.toString(),
223+
contains('SDKException: $message\nExtra info: ComplexException:'),
224+
);
225+
});
226+
227+
test('should be immutable after creation', () {
228+
const message = 'Failed to load policy configuration';
229+
final underlyingException = Exception('Network error');
230+
final exception = PolicySDKException(
231+
message,
232+
exception: underlyingException,
233+
);
234+
235+
// Verify that the exception object is immutable
236+
expect(exception.message, equals(message));
237+
expect(exception.exception, equals(underlyingException));
238+
239+
// The fields should remain the same after multiple accesses
240+
expect(exception.message, equals(message));
241+
expect(exception.exception, equals(underlyingException));
242+
});
243+
244+
test('should handle multiple exceptions with same message', () {
245+
const message = 'Failed to load policy configuration';
246+
final exception1 = Exception('Network error 1');
247+
final exception2 = Exception('Network error 2');
248+
249+
final sdkException1 = PolicySDKException(
250+
message,
251+
exception: exception1,
252+
);
253+
254+
final sdkException2 = PolicySDKException(
255+
message,
256+
exception: exception2,
257+
);
258+
259+
expect(sdkException1.message, equals(sdkException2.message));
260+
expect(sdkException1.exception, isNot(equals(sdkException2.exception)));
261+
expect(sdkException1.toString(), isNot(equals(sdkException2.toString())));
262+
});
263+
});
264+
}

0 commit comments

Comments
 (0)