Skip to content

Commit

Permalink
Fix type convertion for object primitive type (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
mialeska authored Jan 28, 2021
1 parent 46b775f commit 676c73f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ public T GetValue<T>(string path)
var envValue = GetEnvironmentValue(path);
if (envValue != null)
{
return ConvertEnvVar(() => (T) TypeDescriptor.GetConverter(typeof(T)).ConvertFrom(envValue),
return ConvertEnvVar(() =>
{
var type = typeof(T);
return type == typeof(object)
? (T) Convert.ChangeType(envValue, type)
: (T) TypeDescriptor.GetConverter(type).ConvertFrom(envValue);
},
envValue, path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ public void Should_BePossibleTo_GetEmptyValueDictionary()
[NonParallelizable]
public void Should_BePossibleTo_OverrideDictionaryOfValues_FromEnvVar()
{
CheckOverrideDictionaryFromEnvVar<string>();
}

[Test]
[NonParallelizable]
public void Should_BePossibleTo_OverrideDictionaryOfObjects_FromEnvVar()
{
CheckOverrideDictionaryFromEnvVar<object>();
}

private void CheckOverrideDictionaryFromEnvVar<T>()
{

var expectedDict = new Dictionary<string, object>
{
{"intl.accept_languages", "1"},
Expand All @@ -120,7 +133,7 @@ public void Should_BePossibleTo_OverrideDictionaryOfValues_FromEnvVar()
Environment.SetEnvironmentVariable("driverSettings.chrome.options.disable-popup-blocking", "bla");

Assert.AreEqual(expectedDict,
AddedParamsSettings.GetValueDictionary<string>(".driverSettings.chrome.options"),
AddedParamsSettings.GetValueDictionary<T>(".driverSettings.chrome.options"),
"Dictionary of keys and values was overriden successively");
}

Expand Down

0 comments on commit 676c73f

Please sign in to comment.