|
| 1 | +import 'package:flutter_test/flutter_test.dart'; |
| 2 | +import 'package:flutter_policy_engine/src/exceptions/i_policy_sdk_exceptions.dart'; |
| 3 | +import 'package:flutter_policy_engine/src/exceptions/json_parse_exception.dart'; |
| 4 | +import 'package:flutter_policy_engine/src/exceptions/json_serialize_exception.dart'; |
| 5 | +import 'package:flutter_policy_engine/src/exceptions/policy_not_initialized_exception.dart'; |
| 6 | + |
| 7 | +void main() { |
| 8 | + group('Exceptions Integration Tests', () { |
| 9 | + test('all exceptions should implement IPolicySDKException', () { |
| 10 | + final exceptions = [ |
| 11 | + const PolicyNotInitializedException('Test message'), |
| 12 | + JsonParseException('Test message'), |
| 13 | + JsonSerializeException('Test message'), |
| 14 | + ]; |
| 15 | + |
| 16 | + for (final exception in exceptions) { |
| 17 | + expect(exception, isA<IPolicySDKException>()); |
| 18 | + expect(exception, isA<Exception>()); |
| 19 | + } |
| 20 | + }); |
| 21 | + |
| 22 | + test('detail exceptions should implement IDetailPolicySDKException', () { |
| 23 | + final detailExceptions = [ |
| 24 | + JsonParseException('Test message'), |
| 25 | + JsonSerializeException('Test message'), |
| 26 | + ]; |
| 27 | + |
| 28 | + for (final exception in detailExceptions) { |
| 29 | + expect(exception, isA<IDetailPolicySDKException>()); |
| 30 | + expect(exception, isA<IPolicySDKException>()); |
| 31 | + } |
| 32 | + }); |
| 33 | + |
| 34 | + test('basic exceptions should not implement IDetailPolicySDKException', () { |
| 35 | + const basicException = PolicyNotInitializedException('Test message'); |
| 36 | + |
| 37 | + expect(basicException, isA<IPolicySDKException>()); |
| 38 | + expect(basicException, isNot(isA<IDetailPolicySDKException>())); |
| 39 | + }); |
| 40 | + |
| 41 | + test('all exceptions should have consistent message handling', () { |
| 42 | + const testMessage = 'Test error message'; |
| 43 | + |
| 44 | + final exceptions = [ |
| 45 | + const PolicyNotInitializedException(testMessage), |
| 46 | + JsonParseException(testMessage), |
| 47 | + JsonSerializeException(testMessage), |
| 48 | + ]; |
| 49 | + |
| 50 | + for (final exception in exceptions) { |
| 51 | + expect(exception.message, equals(testMessage)); |
| 52 | + expect(exception.toString(), contains(testMessage)); |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + test('detail exceptions should handle optional parameters consistently', |
| 57 | + () { |
| 58 | + const message = 'Test error message'; |
| 59 | + const key = 'test_key'; |
| 60 | + const originalError = 'test_original_error'; |
| 61 | + final errors = {'field1': 'error1', 'field2': 'error2'}; |
| 62 | + |
| 63 | + final detailExceptions = [ |
| 64 | + JsonParseException( |
| 65 | + message, |
| 66 | + key: key, |
| 67 | + originalError: originalError, |
| 68 | + errors: errors, |
| 69 | + ), |
| 70 | + JsonSerializeException( |
| 71 | + message, |
| 72 | + key: key, |
| 73 | + originalError: originalError, |
| 74 | + errors: errors, |
| 75 | + ), |
| 76 | + ]; |
| 77 | + |
| 78 | + for (final exception in detailExceptions) { |
| 79 | + expect(exception.message, equals(message)); |
| 80 | + expect(exception.key, equals(key)); |
| 81 | + expect(exception.originalError, equals(originalError)); |
| 82 | + expect(exception.errors, equals(errors)); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + test('exceptions should have distinct type names in toString', () { |
| 87 | + const message = 'Test error message'; |
| 88 | + |
| 89 | + final exceptions = [ |
| 90 | + const PolicyNotInitializedException(message), |
| 91 | + JsonParseException(message), |
| 92 | + JsonSerializeException(message), |
| 93 | + ]; |
| 94 | + |
| 95 | + final typeNames = |
| 96 | + exceptions.map((e) => e.toString().split(':')[0]).toSet(); |
| 97 | + expect(typeNames.length, equals(3)); |
| 98 | + expect(typeNames, contains('PolicyNotInitializedException')); |
| 99 | + expect(typeNames, contains('JsonParseException')); |
| 100 | + expect(typeNames, contains('JsonSerializeException')); |
| 101 | + }); |
| 102 | + |
| 103 | + test('exceptions should handle null optional parameters gracefully', () { |
| 104 | + const message = 'Test error message'; |
| 105 | + |
| 106 | + final detailExceptions = [ |
| 107 | + JsonParseException( |
| 108 | + message, |
| 109 | + key: null, |
| 110 | + originalError: null, |
| 111 | + errors: null, |
| 112 | + ), |
| 113 | + JsonSerializeException( |
| 114 | + message, |
| 115 | + key: null, |
| 116 | + originalError: null, |
| 117 | + errors: null, |
| 118 | + ), |
| 119 | + ]; |
| 120 | + |
| 121 | + for (final exception in detailExceptions) { |
| 122 | + expect(exception.message, equals(message)); |
| 123 | + expect(exception.key, isNull); |
| 124 | + expect(exception.originalError, isNull); |
| 125 | + expect(exception.errors, isNull); |
| 126 | + expect(exception.toString(), contains(message)); |
| 127 | + expect(exception.toString(), isNot(contains('key:'))); |
| 128 | + expect(exception.toString(), isNot(contains('original:'))); |
| 129 | + expect(exception.toString(), isNot(contains('total errors'))); |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + test('exceptions should handle empty errors map correctly', () { |
| 134 | + const message = 'Test error message'; |
| 135 | + final emptyErrors = <String, String>{}; |
| 136 | + |
| 137 | + final detailExceptions = [ |
| 138 | + JsonParseException(message, errors: emptyErrors), |
| 139 | + JsonSerializeException(message, errors: emptyErrors), |
| 140 | + ]; |
| 141 | + |
| 142 | + for (final exception in detailExceptions) { |
| 143 | + expect(exception.errors, equals(emptyErrors)); |
| 144 | + expect(exception.toString(), isNot(contains('total errors'))); |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | + test('exceptions should be throwable and catchable', () { |
| 149 | + const message = 'Test error message'; |
| 150 | + |
| 151 | + final exceptions = [ |
| 152 | + const PolicyNotInitializedException(message), |
| 153 | + JsonParseException(message), |
| 154 | + JsonSerializeException(message), |
| 155 | + ]; |
| 156 | + |
| 157 | + for (final exception in exceptions) { |
| 158 | + expect(() => throw exception, throwsA(isA<Exception>())); |
| 159 | + expect(() => throw exception, throwsA(isA<IPolicySDKException>())); |
| 160 | + } |
| 161 | + }); |
| 162 | + |
| 163 | + test('detail exceptions should provide rich error information', () { |
| 164 | + const message = 'Test error message'; |
| 165 | + const key = 'test_key'; |
| 166 | + const originalError = 'test_original_error'; |
| 167 | + final errors = {'field1': 'error1', 'field2': 'error2'}; |
| 168 | + |
| 169 | + final detailExceptions = [ |
| 170 | + JsonParseException( |
| 171 | + message, |
| 172 | + key: key, |
| 173 | + originalError: originalError, |
| 174 | + errors: errors, |
| 175 | + ), |
| 176 | + JsonSerializeException( |
| 177 | + message, |
| 178 | + key: key, |
| 179 | + originalError: originalError, |
| 180 | + errors: errors, |
| 181 | + ), |
| 182 | + ]; |
| 183 | + |
| 184 | + for (final exception in detailExceptions) { |
| 185 | + final stringRep = exception.toString(); |
| 186 | + expect(stringRep, contains(message)); |
| 187 | + expect(stringRep, contains(key)); |
| 188 | + expect(stringRep, contains(originalError)); |
| 189 | + expect(stringRep, contains('2 total errors')); |
| 190 | + } |
| 191 | + }); |
| 192 | + |
| 193 | + test('exceptions should maintain immutability', () { |
| 194 | + const message = 'Test error message'; |
| 195 | + |
| 196 | + final exceptions = [ |
| 197 | + const PolicyNotInitializedException(message), |
| 198 | + JsonParseException(message), |
| 199 | + JsonSerializeException(message), |
| 200 | + ]; |
| 201 | + |
| 202 | + for (final exception in exceptions) { |
| 203 | + expect(exception.message, equals(message)); |
| 204 | + // Verify that the message field is final and immutable |
| 205 | + expect(exception.message, isA<String>()); |
| 206 | + expect(exception.message, isNot(equals('Modified message'))); |
| 207 | + } |
| 208 | + }); |
| 209 | + }); |
| 210 | +} |
0 commit comments