Skip to content

Commit 676c73f

Browse files
authored
Fix type convertion for object primitive type (#85)
1 parent 46b775f commit 676c73f

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

Aquality.Selenium.Core/src/Aquality.Selenium.Core/Utilities/JsonSettingsFile.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ public T GetValue<T>(string path)
6868
var envValue = GetEnvironmentValue(path);
6969
if (envValue != null)
7070
{
71-
return ConvertEnvVar(() => (T) TypeDescriptor.GetConverter(typeof(T)).ConvertFrom(envValue),
71+
return ConvertEnvVar(() =>
72+
{
73+
var type = typeof(T);
74+
return type == typeof(object)
75+
? (T) Convert.ChangeType(envValue, type)
76+
: (T) TypeDescriptor.GetConverter(type).ConvertFrom(envValue);
77+
},
7278
envValue, path);
7379
}
7480

Aquality.Selenium.Core/tests/Aquality.Selenium.Core.Tests/Utilities/JsonFileTests.cs

+14-1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,19 @@ public void Should_BePossibleTo_GetEmptyValueDictionary()
109109
[NonParallelizable]
110110
public void Should_BePossibleTo_OverrideDictionaryOfValues_FromEnvVar()
111111
{
112+
CheckOverrideDictionaryFromEnvVar<string>();
113+
}
114+
115+
[Test]
116+
[NonParallelizable]
117+
public void Should_BePossibleTo_OverrideDictionaryOfObjects_FromEnvVar()
118+
{
119+
CheckOverrideDictionaryFromEnvVar<object>();
120+
}
121+
122+
private void CheckOverrideDictionaryFromEnvVar<T>()
123+
{
124+
112125
var expectedDict = new Dictionary<string, object>
113126
{
114127
{"intl.accept_languages", "1"},
@@ -120,7 +133,7 @@ public void Should_BePossibleTo_OverrideDictionaryOfValues_FromEnvVar()
120133
Environment.SetEnvironmentVariable("driverSettings.chrome.options.disable-popup-blocking", "bla");
121134

122135
Assert.AreEqual(expectedDict,
123-
AddedParamsSettings.GetValueDictionary<string>(".driverSettings.chrome.options"),
136+
AddedParamsSettings.GetValueDictionary<T>(".driverSettings.chrome.options"),
124137
"Dictionary of keys and values was overriden successively");
125138
}
126139

0 commit comments

Comments
 (0)