File tree 2 files changed +38
-4
lines changed
2 files changed +38
-4
lines changed Original file line number Diff line number Diff line change @@ -33,25 +33,25 @@ public FlagMetadata() : this([])
33
33
return this . GetValue < double > ( key ) ;
34
34
}
35
35
36
- private T ? GetValue < T > ( string key ) where T : struct
36
+ public string ? GetString ( string key )
37
37
{
38
38
var hasValue = this . _metadata . TryGetValue ( key , out var value ) ;
39
39
if ( ! hasValue )
40
40
{
41
41
return null ;
42
42
}
43
43
44
- return value is T tValue ? tValue : throw new InvalidCastException ( $ "Cannot cast { value ? . GetType ( ) . ToString ( ) ?? "Nullable" } to { typeof ( T ) } ") ;
44
+ return value as string ?? throw new InvalidCastException ( $ "Cannot cast { value ? . GetType ( ) . ToString ( ) ?? "Nullable" } to { typeof ( string ) } ") ;
45
45
}
46
46
47
- public string ? GetString ( string key )
47
+ private T ? GetValue < T > ( string key ) where T : struct
48
48
{
49
49
var hasValue = this . _metadata . TryGetValue ( key , out var value ) ;
50
50
if ( ! hasValue )
51
51
{
52
52
return null ;
53
53
}
54
54
55
- return value is string tValue ? tValue : throw new InvalidCastException ( $ "Cannot cast { value ? . GetType ( ) . ToString ( ) ?? "Nullable" } to { typeof ( string ) } ") ;
55
+ return value is T tValue ? tValue : throw new InvalidCastException ( $ "Cannot cast { value ? . GetType ( ) . ToString ( ) ?? "Nullable" } to { typeof ( T ) } ") ;
56
56
}
57
57
}
Original file line number Diff line number Diff line change @@ -104,5 +104,39 @@ public void GetDouble_Should_Return_Value_If_Key_Found()
104
104
Assert . NotNull ( result ) ;
105
105
Assert . Equal ( 1.2 , result ) ;
106
106
}
107
+
108
+ [ Fact ]
109
+ public void GetString_Should_Return_Null_If_Key_Not_Found ( )
110
+ {
111
+ // Arrange
112
+ var metadata = new Dictionary < string , object > ( ) ;
113
+ var flagMetadata = new FlagMetadata ( metadata ) ;
114
+
115
+ // Act
116
+ var result = flagMetadata . GetString ( "nonexistentKey" ) ;
117
+
118
+ // Assert
119
+ Assert . Null ( result ) ;
120
+ }
121
+
122
+ [ Fact ]
123
+ public void GetString_Should_Return_Value_If_Key_Found ( )
124
+ {
125
+ // Arrange
126
+ var metadata = new Dictionary < string , object >
127
+ {
128
+ {
129
+ "stringKey" , "11"
130
+ }
131
+ } ;
132
+ var flagMetadata = new FlagMetadata ( metadata ) ;
133
+
134
+ // Act
135
+ var result = flagMetadata . GetString ( "stringKey" ) ;
136
+
137
+ // Assert
138
+ Assert . NotNull ( result ) ;
139
+ Assert . Equal ( "11" , result ) ;
140
+ }
107
141
}
108
142
}
You can’t perform that action at this time.
0 commit comments