1
1
namespace GWallet.Backend.Tests
2
2
3
3
open System
4
- open System.Runtime .Serialization
5
4
6
5
open NUnit.Framework
7
6
8
7
open GWallet.Backend
9
8
10
9
11
- type CustomExceptionWithoutSerializationCtor =
10
+ type CustomExceptionWithoutInnerExceptionCtor =
12
11
inherit Exception
13
12
14
13
new ( message) =
@@ -17,8 +16,6 @@ type CustomExceptionWithoutSerializationCtor =
17
16
type CustomException =
18
17
inherit Exception
19
18
20
- new ( info: SerializationInfo, context: StreamingContext) =
21
- { inherit Exception( info, context) }
22
19
new ( message: string, innerException: CustomException) =
23
20
{ inherit Exception( message, innerException) }
24
21
new ( message) =
@@ -68,17 +65,17 @@ type ExceptionMarshalling () =
68
65
cex
69
66
Marshalling.Serialize ex
70
67
71
- let msg = " Exceptions didn't match. Full binary form was "
68
+ let msg = " Expected Exception.ToString() differs from actual "
72
69
#if LEGACY_ FRAMEWORK
73
- let legacyMsg = " (Legacy)Exceptions didn't match. Full binary form was "
70
+ let legacyIgnoreMsg = " Mono or old .NETFramework might vary slightly; there's no need to really do any regression testing here "
74
71
#endif
75
72
76
73
[<Test>]
77
74
member __. ``can serialize basic exceptions`` () =
78
75
let json = SerializeBasicException ()
79
76
Assert.That( json, Is.Not.Null)
80
- Assert.That( json, Is.Not.Empty)
81
- Assert.That( MarshallingData.SerializedExceptionsAreSame json MarshallingData.BasicExceptionExampleInJson false msg)
77
+ Assert.That( json.Trim () , Is.Not.Empty)
78
+ Assert.That( MarshallingData.SerializedExceptionsAreSame json MarshallingData.BasicExceptionExampleInJson msg)
82
79
83
80
[<Test>]
84
81
member __. ``can deserialize basic exceptions`` () =
@@ -90,31 +87,24 @@ type ExceptionMarshalling () =
90
87
Assert.Inconclusive " Fix the serialization test first"
91
88
failwith " unreachable"
92
89
93
- let ex : Exception = Marshalling.Deserialize basicExSerialized
90
+ let ex : MarshalledException = Marshalling.Deserialize basicExSerialized
94
91
Assert.That( ex, Is.Not.Null)
95
- Assert.That( ex, Is.InstanceOf< Exception>())
96
- Assert.That( ex.Message, Is.EqualTo " msg" )
97
- Assert.That( ex.InnerException, Is.Null)
98
- Assert.That( ex.StackTrace, Is.Null)
92
+ Assert.That( ex, Is.InstanceOf< MarshalledException>())
93
+ Assert.That( ex.FullDescription.Trim() .Length, Is.GreaterThan 0 )
94
+ Assert.That(
95
+ MarshallingData.Sanitize ex.FullDescription,
96
+ Is.EqualTo ( MarshallingData.Sanitize " System.Exception: msg" )
97
+ )
99
98
100
99
[<Test>]
101
100
member __. ``can serialize real exceptions`` () =
102
101
let json = SerializeRealException ()
103
102
Assert.That( json, Is.Not.Null)
104
- Assert.That( json, Is.Not.Empty)
103
+ Assert.That( json.Trim () , Is.Not.Empty)
105
104
#if ! LEGACY_ FRAMEWORK
106
- Assert.That( MarshallingData.SerializedExceptionsAreSame json MarshallingData.RealExceptionExampleInJson false msg)
105
+ Assert.That( MarshallingData.SerializedExceptionsAreSame json MarshallingData.RealExceptionExampleInJson msg)
107
106
#else
108
- if Config.IsWindowsPlatform () then
109
- let serializedExceptionsAreSame =
110
- try
111
- MarshallingData.SerializedExceptionsAreSame json MarshallingData.RealExceptionExampleInJson false msg
112
- with
113
- | :? AssertionException ->
114
- MarshallingData.SerializedExceptionsAreSame json MarshallingData.RealExceptionWindowsLegacyExampleInJson false legacyMsg
115
- Assert.That serializedExceptionsAreSame
116
- else
117
- Assert.That( MarshallingData.SerializedExceptionsAreSame json MarshallingData.RealExceptionUnixLegacyExampleInJson false legacyMsg)
107
+ Assert.Ignore legacyIgnoreMsg
118
108
#endif
119
109
120
110
[<Test>]
@@ -127,20 +117,29 @@ type ExceptionMarshalling () =
127
117
Assert.Inconclusive " Fix the serialization test first"
128
118
failwith " unreachable"
129
119
130
- let ex : Exception = Marshalling.Deserialize realExceptionSerialized
120
+ let ex : MarshalledException = Marshalling.Deserialize realExceptionSerialized
131
121
Assert.That( ex, Is.Not.Null)
132
- Assert.That( ex, Is.InstanceOf< Exception>())
133
- Assert.That( ex.Message, Is.EqualTo " msg" )
134
- Assert.That( ex.InnerException, Is.Null)
135
- Assert.That( ex.StackTrace, Is.Not.Null)
136
- Assert.That( ex.StackTrace, Is.Not.Empty)
122
+ Assert.That( ex, Is.InstanceOf< MarshalledException>())
123
+ Assert.That( ex.FullDescription.Trim() .Length, Is.GreaterThan 0 )
124
+ #if ! LEGACY_ FRAMEWORK
125
+ let expected =
126
+ sprintf
127
+ " System.Exception: msg at GWallet.Backend.Tests.ExceptionMarshalling.SerializeRealException() in %s /ExceptionMarshalling.fs:line 38"
128
+ MarshallingData.ThisProjPath
129
+ Assert.That(
130
+ MarshallingData.Sanitize ex.FullDescription,
131
+ Is.EqualTo ( MarshallingData.Sanitize expected)
132
+ )
133
+ #else
134
+ Assert.Ignore legacyIgnoreMsg
135
+ #endif
137
136
138
137
[<Test>]
139
138
member __. ``can serialize inner exceptions`` () =
140
139
let json = SerializeInnerException ()
141
140
Assert.That( json, Is.Not.Null)
142
- Assert.That( json, Is.Not.Empty)
143
- Assert.That( MarshallingData.SerializedExceptionsAreSame json MarshallingData.InnerExceptionExampleInJson false msg)
141
+ Assert.That( json.Trim () , Is.Not.Empty)
142
+ Assert.That( MarshallingData.SerializedExceptionsAreSame json MarshallingData.InnerExceptionExampleInJson msg)
144
143
145
144
[<Test>]
146
145
member __. ``can deserialize inner exceptions`` () =
@@ -152,31 +151,28 @@ type ExceptionMarshalling () =
152
151
Assert.Inconclusive " Fix the serialization test first"
153
152
failwith " unreachable"
154
153
155
- let ex : Exception = Marshalling.Deserialize innerExceptionSerialized
154
+ let ex : MarshalledException = Marshalling.Deserialize innerExceptionSerialized
156
155
Assert.That ( ex, Is.Not.Null)
157
- Assert.That ( ex, Is.InstanceOf< Exception>())
158
- Assert.That ( ex.Message, Is.EqualTo " msg" )
159
- Assert.That ( ex.StackTrace, Is.Null)
160
- Assert.That ( ex.InnerException, Is.Not.Null)
161
-
162
- Assert.That ( ex.InnerException, Is.InstanceOf< Exception>())
163
- Assert.That ( ex.InnerException.Message, Is.EqualTo " innerMsg" )
164
- Assert.That ( ex.InnerException.StackTrace, Is.Null)
156
+ Assert.That ( ex, Is.InstanceOf< MarshalledException>())
157
+ Assert.That( ex.FullDescription.Trim() .Length, Is.GreaterThan 0 )
158
+ Assert.That (
159
+ MarshallingData.Sanitize ex.FullDescription,
160
+ Is.EqualTo ( MarshallingData.Sanitize " System.Exception: msg ---> System.Exception: innerMsg --- End of inner exception stack trace ---" )
161
+ )
165
162
166
163
[<Test>]
167
164
member __. ``can serialize custom exceptions`` () =
168
165
let json = SerializeCustomException ()
169
166
Assert.That( json, Is.Not.Null)
170
- Assert.That( json, Is.Not.Empty)
171
- Assert.That( MarshallingData.SerializedExceptionsAreSame json MarshallingData.CustomExceptionExampleInJson false msg)
167
+ Assert.That( json.Trim () , Is.Not.Empty)
168
+ Assert.That( MarshallingData.SerializedExceptionsAreSame json MarshallingData.CustomExceptionExampleInJson msg)
172
169
173
170
[<Test>]
174
- member __. ``serializing custom exception not prepared for binary serialization, throws`` () =
175
- let exToSerialize = CustomExceptionWithoutSerializationCtor " msg"
176
- let ex : MarshallingCompatibilityException =
177
- Assert.Throws( fun _ -> Marshalling.Serialize exToSerialize |> ignore< string>)
178
- Assert.That( ex, Is.TypeOf< MarshallingCompatibilityException>())
179
- Assert.That( ex.Message, IsString.WhichContains " GWallet.Backend.Tests.CustomExceptionWithoutSerializationCtor" )
171
+ member __. ``serializing custom exception without inner ex ctor does not crash`` () =
172
+ let exToSerialize = CustomExceptionWithoutInnerExceptionCtor " msg"
173
+ let serializedEx = ( Marshalling.Serialize exToSerialize) .Trim()
174
+ Assert.That( serializedEx, Is.Not.Null)
175
+ Assert.That( serializedEx.Trim() .Length, Is.GreaterThan 0 )
180
176
181
177
[<Test>]
182
178
member __. ``can deserialize custom exceptions`` () =
@@ -188,21 +184,24 @@ type ExceptionMarshalling () =
188
184
Assert.Inconclusive " Fix the serialization test first"
189
185
failwith " unreachable"
190
186
191
- let ex : Exception = Marshalling.Deserialize customExceptionSerialized
187
+ let ex : MarshalledException = Marshalling.Deserialize customExceptionSerialized
192
188
Assert.That( ex, Is.Not.Null)
193
- Assert.That( ex, Is.InstanceOf< CustomException>())
194
- Assert.That( ex.Message, Is.EqualTo " msg" )
195
- Assert.That( ex.InnerException, Is.Null)
196
- Assert.That( ex.StackTrace, Is.Null)
189
+ Assert.That( ex, Is.InstanceOf< MarshalledException>())
190
+ Assert.That(
191
+ MarshallingData.Sanitize ex.FullDescription,
192
+ Is.EqualTo ( MarshallingData.Sanitize " GWallet.Backend.Tests.CustomException: msg" )
193
+ )
197
194
198
195
[<Test>]
199
196
member __. ``can serialize F # custom exceptions`` () =
200
197
let json = SerializeCustomFSharpException ()
201
198
Assert.That( json, Is.Not.Null)
202
- Assert.That( json, Is.Not.Empty)
203
-
204
- // strangely enough, message would be different between linux_vanilla_dotnet6 and other dotnet6 configs (e.g. Windows, macOS, Linux-github)
205
- Assert.That( MarshallingData.SerializedExceptionsAreSame json MarshallingData.CustomFSharpExceptionExampleInJson true msg)
199
+ Assert.That( json.Trim(), Is.Not.Empty)
200
+ #if ! LEGACY_ FRAMEWORK
201
+ Assert.That( MarshallingData.SerializedExceptionsAreSame json MarshallingData.CustomFSharpExceptionExampleInJson msg)
202
+ #else
203
+ Assert.Ignore legacyIgnoreMsg
204
+ #endif
206
205
207
206
[<Test>]
208
207
member __. ``can deserialize F # custom exceptions`` () =
@@ -214,36 +213,34 @@ type ExceptionMarshalling () =
214
213
Assert.Inconclusive " Fix the serialization test first"
215
214
failwith " unreachable"
216
215
217
- let ex : Exception = Marshalling.Deserialize customExceptionSerialized
216
+ let ex : MarshalledException = Marshalling.Deserialize customExceptionSerialized
218
217
Assert.That( ex, Is.Not.Null)
219
- Assert.That( ex, Is.InstanceOf< CustomFSharpException>())
220
- Assert.That( ex.Message, Is.Not.Null)
221
- Assert.That( ex.Message, Is.Not.Empty)
222
- Assert.That( ex.InnerException, Is.Null)
223
- Assert.That( ex.StackTrace, Is.Null)
218
+ Assert.That( ex, Is.InstanceOf< MarshalledException>())
219
+ Assert.That( ex.FullDescription.Trim() .Length, Is.GreaterThan 0 )
220
+
221
+ if ex.FullDescription.Contains " of type" then
222
+ // old version of .NET6? (happens in stockdotnet6 CI lanes)
223
+ Assert.That(
224
+ MarshallingData.Sanitize ex.FullDescription,
225
+ Is.EqualTo ( MarshallingData.Sanitize " GWallet.Backend.Tests.CustomFSharpException: Exception of type 'GWallet.Backend.Tests.CustomFSharpException' was thrown." )
226
+ )
227
+ else
228
+ Assert.That(
229
+ MarshallingData.Sanitize ex.FullDescription,
230
+ Is.EqualTo ( MarshallingData.Sanitize " GWallet.Backend.Tests.CustomFSharpException: CustomFSharpException" )
231
+ )
224
232
225
- // TODO: test marshalling custom exceptions with custom properties/fields, and custom F# exception with subtypes
226
233
227
234
[<Test>]
228
235
member __. ``can serialize full exceptions ( all previous features combined ) ``() =
229
236
let json = SerializeFullException ()
230
237
231
238
Assert.That( json, Is.Not.Null)
232
- Assert.That( json, Is.Not.Empty)
233
-
239
+ Assert.That( json.Trim(), Is.Not.Empty)
234
240
#if ! LEGACY_ FRAMEWORK
235
- Assert.That( MarshallingData.SerializedExceptionsAreSame json MarshallingData.FullExceptionExampleInJson false msg)
241
+ Assert.That( MarshallingData.SerializedExceptionsAreSame json MarshallingData.FullExceptionExampleInJson msg)
236
242
#else
237
- if Config.IsWindowsPlatform () then
238
- let serializedExceptionsAreSame =
239
- try
240
- MarshallingData.SerializedExceptionsAreSame json MarshallingData.FullExceptionExampleInJson false msg
241
- with
242
- | :? AssertionException ->
243
- MarshallingData.SerializedExceptionsAreSame json MarshallingData.FullExceptionWindowsLegacyExampleInJson false legacyMsg
244
- Assert.That serializedExceptionsAreSame
245
- else
246
- Assert.That( MarshallingData.SerializedExceptionsAreSame json MarshallingData.FullExceptionUnixLegacyExampleInJson false legacyMsg)
243
+ Assert.Ignore legacyIgnoreMsg
247
244
#endif
248
245
249
246
[<Test>]
@@ -256,12 +253,23 @@ type ExceptionMarshalling () =
256
253
Assert.Inconclusive " Fix the serialization test first"
257
254
failwith " unreachable"
258
255
259
- let ex : Exception = Marshalling.Deserialize fullExceptionSerialized
256
+ let ex : MarshalledException = Marshalling.Deserialize fullExceptionSerialized
260
257
Assert.That( ex, Is.Not.Null)
261
- Assert.That( ex, Is.InstanceOf< CustomException> ())
262
- Assert.That( ex.Message, Is.Not.Null)
263
- Assert.That( ex.Message, Is.Not.Empty)
264
- Assert.That( ex.InnerException, Is.Not.Null)
265
- Assert.That( ex.StackTrace, Is.Not.Null)
266
- Assert.That( ex.StackTrace, Is.Not.Empty)
258
+ Assert.That( ex, Is.InstanceOf< MarshalledException> ())
259
+ Assert.That( ex.FullDescription.Trim() .Length, Is.GreaterThan 0 )
260
+
261
+ #if ! LEGACY_ FRAMEWORK
262
+ Assert.That(
263
+ MarshallingData.Sanitize ex.FullDescription,
264
+ Is.EqualTo (
265
+ MarshallingData.Sanitize
266
+ <| sprintf
267
+ " GWallet.Backend.Tests.CustomException: msg ---> GWallet.Backend.Tests.CustomException: innerMsg --- End of inner exception stack trace --- at GWallet.Backend.Tests.ExceptionMarshalling.SerializeFullException() in %s /ExceptionMarshalling.fs:line 61"
268
+ MarshallingData.ThisProjPath
269
+ )
270
+ )
271
+ #else
272
+ Assert.Ignore legacyIgnoreMsg
273
+ #endif
274
+
267
275
0 commit comments