@@ -23,14 +23,26 @@ public class Activity
23
23
public string Comment { get ; set ; }
24
24
}
25
25
26
+ public class NullableActivity
27
+ {
28
+ [ AutoIncrement ]
29
+ public int Id { get ; set ; }
30
+ public ActivityType ? ActivityType { get ; set ; }
31
+ public string Comment { get ; set ; }
32
+ }
33
+
26
34
public class ExpressionUsingCustomSerializedEnumTests : ExpressionsTestBase
27
35
{
28
- [ Test ]
29
- public void Can_select_on_custom_serialized_enum ( )
36
+ [ TestCase ( 0 ) ]
37
+ [ TestCase ( 1 ) ]
38
+ [ TestCase ( 2 ) ]
39
+ public void Can_select_on_custom_default_null_serialized_enum ( int index )
30
40
{
41
+ EnumSerializerWithNullDefaults . Configure ( ) ;
42
+
31
43
using ( var db = OpenDbConnection ( ) )
32
44
{
33
- var expected = Init ( db ) ;
45
+ var expected = Init ( db , index ) ;
34
46
35
47
var unknownActivities = db . Select < Activity > (
36
48
s => s . ActivityType == expected . ActivityType
@@ -40,39 +52,149 @@ public void Can_select_on_custom_serialized_enum()
40
52
}
41
53
}
42
54
43
- private static Activity Init ( IDbConnection db )
55
+ private static Activity Init ( IDbConnection db , int index )
44
56
{
45
- EnumSerializer . Configure ( ) ;
46
-
47
57
db . DropAndCreateTable < Activity > ( ) ;
48
58
49
59
var activities = new [ ]
50
60
{
51
61
new Activity { ActivityType = ActivityType . Unknown , Comment = "know nothing about this" } ,
52
62
new Activity { ActivityType = ActivityType . Unspecified , Comment = "know we don't know about this" } ,
53
- new Activity { ActivityType = ActivityType . HavingFun , Comment = "want to be doing this" } ,
63
+ new Activity { ActivityType = ActivityType . HavingFun , Comment = "want to be doing this" }
64
+ } ;
65
+ db . InsertAll ( activities ) ;
66
+ return activities [ index ] ;
67
+ }
68
+
69
+ [ TestCase ( 0 ) ]
70
+ [ TestCase ( 1 ) ]
71
+ [ TestCase ( 2 ) ]
72
+ public void Can_select_on_custom_default_empty_serialized_enum ( int index )
73
+ {
74
+ EnumSerializerWithEmptyDefaults . Configure ( ) ;
75
+
76
+ using ( var db = OpenDbConnection ( ) )
77
+ {
78
+ var expected = Init ( db , index ) ;
79
+
80
+ var unknownActivities = db . Select < Activity > (
81
+ s => s . ActivityType == expected . ActivityType
82
+ && s . Comment == expected . Comment ) ;
83
+
84
+ Assert . That ( unknownActivities . Count , Is . EqualTo ( 1 ) ) ;
85
+ }
86
+ }
87
+
88
+ [ TestCase ( 0 ) ]
89
+ [ TestCase ( 1 ) ]
90
+ [ TestCase ( 2 ) ]
91
+ public void Can_select_on_custom_prefix_serialized_enum ( int index )
92
+ {
93
+ EnumSerializerWithPrefixing . Configure ( ) ;
94
+
95
+ using ( var db = OpenDbConnection ( ) )
96
+ {
97
+ var expected = Init ( db , index ) ;
98
+
99
+ var unknownActivities = db . Select < Activity > (
100
+ s => s . ActivityType == expected . ActivityType
101
+ && s . Comment == expected . Comment ) ;
102
+
103
+ Assert . That ( unknownActivities . Count , Is . EqualTo ( 1 ) ) ;
104
+ }
105
+ }
106
+
107
+ [ TestCase ( 0 ) ]
108
+ [ TestCase ( 1 ) ]
109
+ [ TestCase ( 2 ) ]
110
+ public void Can_select_on_custom_default_null_serialized_nullable_enum ( int index )
111
+ {
112
+ EnumSerializerWithNullDefaults . Configure ( ) ;
113
+
114
+ using ( var db = OpenDbConnection ( ) )
115
+ {
116
+ var expected = InitNullable ( db , index ) ;
117
+
118
+ var unknownActivities = db . Select < Activity > (
119
+ s => s . ActivityType == expected . ActivityType
120
+ && s . Comment == expected . Comment ) ;
121
+
122
+ Assert . That ( unknownActivities . Count , Is . EqualTo ( 1 ) ) ;
123
+ }
124
+ }
125
+
126
+ [ TestCase ( 0 ) ]
127
+ [ TestCase ( 1 ) ]
128
+ [ TestCase ( 2 ) ]
129
+ public void Can_select_on_custom_default_empty_serialized_nullable_enum ( int index )
130
+ {
131
+ EnumSerializerWithEmptyDefaults . Configure ( ) ;
132
+
133
+ using ( var db = OpenDbConnection ( ) )
134
+ {
135
+ var expected = InitNullable ( db , index ) ;
136
+
137
+ var unknownActivities = db . Select < Activity > (
138
+ s => s . ActivityType == expected . ActivityType
139
+ && s . Comment == expected . Comment ) ;
140
+
141
+ Assert . That ( unknownActivities . Count , Is . EqualTo ( 1 ) ) ;
142
+ }
143
+ }
144
+
145
+ private static NullableActivity InitNullable ( IDbConnection db , int index )
146
+ {
147
+ db . DropAndCreateTable < NullableActivity > ( ) ;
148
+
149
+ var activities = new [ ]
150
+ {
151
+ new NullableActivity { ActivityType = ActivityType . Unknown , Comment = "know nothing about this" } ,
152
+ new NullableActivity { ActivityType = ActivityType . Unspecified , Comment = "know we don't know about this" } ,
153
+ new NullableActivity { ActivityType = ActivityType . HavingFun , Comment = "want to be doing this" }
54
154
} ;
55
155
db . InsertAll ( activities ) ;
56
- return activities [ 0 ] ;
156
+ return activities [ index ] ;
157
+ }
158
+
159
+ [ TestCase ( 0 ) ]
160
+ [ TestCase ( 1 ) ]
161
+ [ TestCase ( 2 ) ]
162
+ public void Can_select_on_custom_prefix_serialized_nullable_enum ( int index )
163
+ {
164
+ EnumSerializerWithPrefixing . Configure ( ) ;
165
+
166
+ using ( var db = OpenDbConnection ( ) )
167
+ {
168
+ var expected = InitNullable ( db , index ) ;
169
+
170
+ var unknownActivities = db . Select < Activity > (
171
+ s => s . ActivityType == expected . ActivityType
172
+ && s . Comment == expected . Comment ) ;
173
+
174
+ Assert . That ( unknownActivities . Count , Is . EqualTo ( 1 ) ) ;
175
+ }
57
176
}
58
177
}
59
178
60
- public class EnumSerializer
179
+ public class EnumSerializerWithNullDefaults
61
180
{
62
181
public static void Configure ( )
63
182
{
183
+ if ( DefaultEnumValues . Count > 0 )
184
+ return ;
185
+
64
186
var type = typeof ( ActivityType ) ;
65
187
InvokeStaticGenericMethod ( type , "ConfigureEnumSerialization" ) ;
66
188
}
67
189
68
- private static object InvokeStaticGenericMethod ( Type genericType , string methodName )
190
+ private static void InvokeStaticGenericMethod ( Type genericType , string methodName )
69
191
{
70
- return InvokeGenericMethod ( genericType , methodName , null ) ;
192
+ InvokeGenericMethod ( genericType , methodName , null ) ;
71
193
}
72
194
73
- private static object InvokeGenericMethod ( Type genericType , string methodName , object obj )
195
+ private static void InvokeGenericMethod ( Type genericType , string methodName , object obj )
74
196
{
75
- return typeof ( EnumSerializer ) . GetMethod ( methodName ) . MakeGenericMethod ( genericType ) . Invoke ( obj , null ) ;
197
+ typeof ( EnumSerializerWithNullDefaults ) . GetMethod ( methodName ) . MakeGenericMethod ( genericType ) . Invoke ( obj , null ) ;
76
198
}
77
199
78
200
public static void ConfigureEnumSerialization < TEnum > ( )
@@ -99,4 +221,87 @@ private static T GetDefault<T>()
99
221
return default ( T ) ;
100
222
}
101
223
}
224
+
225
+ public class EnumSerializerWithEmptyDefaults
226
+ {
227
+ public static void Configure ( )
228
+ {
229
+ if ( DefaultEnumValues . Count > 0 )
230
+ return ;
231
+
232
+ var type = typeof ( ActivityType ) ;
233
+ InvokeStaticGenericMethod ( type , "ConfigureEnumSerialization" ) ;
234
+ }
235
+
236
+ private static void InvokeStaticGenericMethod ( Type genericType , string methodName )
237
+ {
238
+ InvokeGenericMethod ( genericType , methodName , null ) ;
239
+ }
240
+
241
+ private static void InvokeGenericMethod ( Type genericType , string methodName , object obj )
242
+ {
243
+ typeof ( EnumSerializerWithEmptyDefaults ) . GetMethod ( methodName ) . MakeGenericMethod ( genericType ) . Invoke ( obj , null ) ;
244
+ }
245
+
246
+ public static void ConfigureEnumSerialization < TEnum > ( )
247
+ {
248
+ DefaultEnumValues . Add ( typeof ( TEnum ) , GetDefault < TEnum > ( ) ) ;
249
+ JsConfig < TEnum > . SerializeFn = NonDefaultSerializer ;
250
+ JsConfig < TEnum > . DeSerializeFn = NonDefaultDeSerializer < TEnum > ;
251
+ }
252
+
253
+ private static readonly Dictionary < Type , object > DefaultEnumValues = new Dictionary < Type , object > ( ) ;
254
+
255
+ private static string NonDefaultSerializer < TEnum > ( TEnum value )
256
+ {
257
+ return value . Equals ( DefaultEnumValues [ typeof ( TEnum ) ] ) ? "" : value . ToString ( ) ;
258
+ }
259
+
260
+ private static TEnum NonDefaultDeSerializer < TEnum > ( string value )
261
+ {
262
+ return ( String . IsNullOrEmpty ( value ) ? ( TEnum ) DefaultEnumValues [ typeof ( TEnum ) ] : ( TEnum ) Enum . Parse ( typeof ( TEnum ) , value , true ) ) ;
263
+ }
264
+
265
+ private static T GetDefault < T > ( )
266
+ {
267
+ return default ( T ) ;
268
+ }
269
+ }
270
+
271
+ public class EnumSerializerWithPrefixing
272
+ {
273
+ public static void Configure ( )
274
+ {
275
+ var type = typeof ( ActivityType ) ;
276
+ InvokeStaticGenericMethod ( type , "ConfigureEnumSerialization" ) ;
277
+ }
278
+
279
+ private static void InvokeStaticGenericMethod ( Type genericType , string methodName )
280
+ {
281
+ InvokeGenericMethod ( genericType , methodName , null ) ;
282
+ }
283
+
284
+ private static void InvokeGenericMethod ( Type genericType , string methodName , object obj )
285
+ {
286
+ typeof ( EnumSerializerWithPrefixing ) . GetMethod ( methodName ) . MakeGenericMethod ( genericType ) . Invoke ( obj , null ) ;
287
+ }
288
+
289
+ public static void ConfigureEnumSerialization < TEnum > ( )
290
+ {
291
+ JsConfig < TEnum > . SerializeFn = Serializer ;
292
+ JsConfig < TEnum > . DeSerializeFn = Deserializer < TEnum > ;
293
+ }
294
+
295
+ private const string Prefix = "_prefix_" ;
296
+
297
+ private static string Serializer < TEnum > ( TEnum value )
298
+ {
299
+ return Prefix + value ;
300
+ }
301
+
302
+ private static TEnum Deserializer < TEnum > ( string value )
303
+ {
304
+ return ( TEnum ) Enum . Parse ( typeof ( TEnum ) , value . Replace ( Prefix , "" ) , true ) ;
305
+ }
306
+ }
102
307
}
0 commit comments