1
+ using System ;
1
2
using System . Collections . Generic ;
2
3
using OpenFeature . Model ;
3
4
using Xunit ;
@@ -10,8 +11,7 @@ public class FlagMetadataTest
10
11
public void GetBool_Should_Return_Null_If_Key_Not_Found ( )
11
12
{
12
13
// Arrange
13
- var metadata = new Dictionary < string , object > ( ) ;
14
- var flagMetadata = new FlagMetadata ( metadata ) ;
14
+ var flagMetadata = new FlagMetadata ( ) ;
15
15
16
16
// Act
17
17
var result = flagMetadata . GetBool ( "nonexistentKey" ) ;
@@ -26,7 +26,9 @@ public void GetBool_Should_Return_Value_If_Key_Found()
26
26
// Arrange
27
27
var metadata = new Dictionary < string , object >
28
28
{
29
- { "boolKey" , true }
29
+ {
30
+ "boolKey" , true
31
+ }
30
32
} ;
31
33
var flagMetadata = new FlagMetadata ( metadata ) ;
32
34
@@ -38,12 +40,31 @@ public void GetBool_Should_Return_Value_If_Key_Found()
38
40
}
39
41
40
42
[ Fact ]
41
- public void GetInt_Should_Return_Null_If_Key_Not_Found ( )
43
+ public void GetBool_Should_Throw_Value_Is_Invalid ( )
42
44
{
43
45
// Arrange
44
- var metadata = new Dictionary < string , object > ( ) ;
46
+ var metadata = new Dictionary < string , object >
47
+ {
48
+ {
49
+ "wrongKey" , "11a"
50
+ }
51
+ } ;
45
52
var flagMetadata = new FlagMetadata ( metadata ) ;
46
53
54
+ // Act
55
+ var exception = Assert . Throws < InvalidCastException > ( ( ) => flagMetadata . GetBool ( "wrongKey" ) ) ;
56
+
57
+ // Assert
58
+ Assert . NotNull ( exception ) ;
59
+ Assert . Equal ( "Cannot cast System.String to System.Boolean" , exception . Message ) ;
60
+ }
61
+
62
+ [ Fact ]
63
+ public void GetInt_Should_Return_Null_If_Key_Not_Found ( )
64
+ {
65
+ // Arrange
66
+ var flagMetadata = new FlagMetadata ( ) ;
67
+
47
68
// Act
48
69
var result = flagMetadata . GetInt ( "nonexistentKey" ) ;
49
70
@@ -72,12 +93,31 @@ public void GetInt_Should_Return_Value_If_Key_Found()
72
93
}
73
94
74
95
[ Fact ]
75
- public void GetDouble_Should_Return_Null_If_Key_Not_Found ( )
96
+ public void GetInt_Should_Throw_Value_Is_Invalid ( )
76
97
{
77
98
// Arrange
78
- var metadata = new Dictionary < string , object > ( ) ;
99
+ var metadata = new Dictionary < string , object >
100
+ {
101
+ {
102
+ "wrongKey" , "11a"
103
+ }
104
+ } ;
79
105
var flagMetadata = new FlagMetadata ( metadata ) ;
80
106
107
+ // Act
108
+ var exception = Assert . Throws < InvalidCastException > ( ( ) => flagMetadata . GetInt ( "wrongKey" ) ) ;
109
+
110
+ // Assert
111
+ Assert . NotNull ( exception ) ;
112
+ Assert . Equal ( "Cannot cast System.String to System.Int32" , exception . Message ) ;
113
+ }
114
+
115
+ [ Fact ]
116
+ public void GetDouble_Should_Return_Null_If_Key_Not_Found ( )
117
+ {
118
+ // Arrange
119
+ var flagMetadata = new FlagMetadata ( ) ;
120
+
81
121
// Act
82
122
var result = flagMetadata . GetDouble ( "nonexistentKey" ) ;
83
123
@@ -106,12 +146,31 @@ public void GetDouble_Should_Return_Value_If_Key_Found()
106
146
}
107
147
108
148
[ Fact ]
109
- public void GetString_Should_Return_Null_If_Key_Not_Found ( )
149
+ public void GetDouble_Should_Throw_Value_Is_Invalid ( )
110
150
{
111
151
// Arrange
112
- var metadata = new Dictionary < string , object > ( ) ;
152
+ var metadata = new Dictionary < string , object >
153
+ {
154
+ {
155
+ "wrongKey" , "11a"
156
+ }
157
+ } ;
113
158
var flagMetadata = new FlagMetadata ( metadata ) ;
114
159
160
+ // Act
161
+ var exception = Assert . Throws < InvalidCastException > ( ( ) => flagMetadata . GetDouble ( "wrongKey" ) ) ;
162
+
163
+ // Assert
164
+ Assert . NotNull ( exception ) ;
165
+ Assert . Equal ( "Cannot cast System.String to System.Double" , exception . Message ) ;
166
+ }
167
+
168
+ [ Fact ]
169
+ public void GetString_Should_Return_Null_If_Key_Not_Found ( )
170
+ {
171
+ // Arrange
172
+ var flagMetadata = new FlagMetadata ( ) ;
173
+
115
174
// Act
116
175
var result = flagMetadata . GetString ( "nonexistentKey" ) ;
117
176
@@ -138,5 +197,25 @@ public void GetString_Should_Return_Value_If_Key_Found()
138
197
Assert . NotNull ( result ) ;
139
198
Assert . Equal ( "11" , result ) ;
140
199
}
200
+
201
+ [ Fact ]
202
+ public void GetString_Should_Throw_Value_Is_Invalid ( )
203
+ {
204
+ // Arrange
205
+ var metadata = new Dictionary < string , object >
206
+ {
207
+ {
208
+ "wrongKey" , new object ( )
209
+ }
210
+ } ;
211
+ var flagMetadata = new FlagMetadata ( metadata ) ;
212
+
213
+ // Act
214
+ var exception = Assert . Throws < InvalidCastException > ( ( ) => flagMetadata . GetString ( "wrongKey" ) ) ;
215
+
216
+ // Assert
217
+ Assert . NotNull ( exception ) ;
218
+ Assert . Equal ( "Cannot cast System.Object to System.String" , exception . Message ) ;
219
+ }
141
220
}
142
221
}
0 commit comments