Skip to content

Commit 0a22d9f

Browse files
committed
Fix code. Added unit tests.
1 parent 6b71ef5 commit 0a22d9f

File tree

2 files changed

+90
-11
lines changed

2 files changed

+90
-11
lines changed

src/OpenFeature/Model/FlagMetadata.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public FlagMetadata() : this([])
4141
return null;
4242
}
4343

44-
return value as string ?? throw new InvalidCastException($"Cannot cast {value?.GetType().ToString() ?? "Nullable"} to {typeof(string)}");
44+
return value as string ?? throw new InvalidCastException($"Cannot cast {value?.GetType()} to {typeof(string)}");
4545
}
4646

4747
private T? GetValue<T>(string key) where T : struct
@@ -52,6 +52,6 @@ public FlagMetadata() : this([])
5252
return null;
5353
}
5454

55-
return value is T tValue ? tValue : throw new InvalidCastException($"Cannot cast {value?.GetType().ToString() ?? "Nullable"} to {typeof(T)}");
55+
return value is T tValue ? tValue : throw new InvalidCastException($"Cannot cast {value?.GetType()} to {typeof(T)}");
5656
}
5757
}

test/OpenFeature.Tests/FlagMetadataTest.cs

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using OpenFeature.Model;
34
using Xunit;
@@ -10,8 +11,7 @@ public class FlagMetadataTest
1011
public void GetBool_Should_Return_Null_If_Key_Not_Found()
1112
{
1213
// Arrange
13-
var metadata = new Dictionary<string, object>();
14-
var flagMetadata = new FlagMetadata(metadata);
14+
var flagMetadata = new FlagMetadata();
1515

1616
// Act
1717
var result = flagMetadata.GetBool("nonexistentKey");
@@ -26,7 +26,9 @@ public void GetBool_Should_Return_Value_If_Key_Found()
2626
// Arrange
2727
var metadata = new Dictionary<string, object>
2828
{
29-
{ "boolKey", true }
29+
{
30+
"boolKey", true
31+
}
3032
};
3133
var flagMetadata = new FlagMetadata(metadata);
3234

@@ -38,12 +40,31 @@ public void GetBool_Should_Return_Value_If_Key_Found()
3840
}
3941

4042
[Fact]
41-
public void GetInt_Should_Return_Null_If_Key_Not_Found()
43+
public void GetBool_Should_Throw_Value_Is_Invalid()
4244
{
4345
// Arrange
44-
var metadata = new Dictionary<string, object>();
46+
var metadata = new Dictionary<string, object>
47+
{
48+
{
49+
"wrongKey", "11a"
50+
}
51+
};
4552
var flagMetadata = new FlagMetadata(metadata);
4653

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+
4768
// Act
4869
var result = flagMetadata.GetInt("nonexistentKey");
4970

@@ -72,12 +93,31 @@ public void GetInt_Should_Return_Value_If_Key_Found()
7293
}
7394

7495
[Fact]
75-
public void GetDouble_Should_Return_Null_If_Key_Not_Found()
96+
public void GetInt_Should_Throw_Value_Is_Invalid()
7697
{
7798
// Arrange
78-
var metadata = new Dictionary<string, object>();
99+
var metadata = new Dictionary<string, object>
100+
{
101+
{
102+
"wrongKey", "11a"
103+
}
104+
};
79105
var flagMetadata = new FlagMetadata(metadata);
80106

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+
81121
// Act
82122
var result = flagMetadata.GetDouble("nonexistentKey");
83123

@@ -106,12 +146,31 @@ public void GetDouble_Should_Return_Value_If_Key_Found()
106146
}
107147

108148
[Fact]
109-
public void GetString_Should_Return_Null_If_Key_Not_Found()
149+
public void GetDouble_Should_Throw_Value_Is_Invalid()
110150
{
111151
// Arrange
112-
var metadata = new Dictionary<string, object>();
152+
var metadata = new Dictionary<string, object>
153+
{
154+
{
155+
"wrongKey", "11a"
156+
}
157+
};
113158
var flagMetadata = new FlagMetadata(metadata);
114159

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+
115174
// Act
116175
var result = flagMetadata.GetString("nonexistentKey");
117176

@@ -138,5 +197,25 @@ public void GetString_Should_Return_Value_If_Key_Found()
138197
Assert.NotNull(result);
139198
Assert.Equal("11", result);
140199
}
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+
}
141220
}
142221
}

0 commit comments

Comments
 (0)