Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions dotnet/src/webdriver/BiDi/Script/RemoteValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ public abstract record RemoteValue
=> ConvertRemoteValuesToArray<TResult>(a.Value, t.GetElementType()!),
(ArrayRemoteValue a, Type t) when t.IsGenericType && t.IsAssignableFrom(typeof(List<>).MakeGenericType(t.GetGenericArguments()[0]))
=> ConvertRemoteValuesToGenericList<TResult>(a.Value, typeof(List<>).MakeGenericType(t.GetGenericArguments()[0])),
(MapRemoteValue m, Type t) when t.IsGenericType && t.GetGenericArguments().Length == 2 && t.IsAssignableFrom(typeof(Dictionary<,>).MakeGenericType(t.GetGenericArguments()))
=> ConvertRemoteValuesToDictionary<TResult>(m.Value, typeof(Dictionary<,>).MakeGenericType(t.GetGenericArguments())),
(ObjectRemoteValue o, Type t) when t.IsGenericType && t.GetGenericArguments().Length == 2 && t.IsAssignableFrom(typeof(Dictionary<,>).MakeGenericType(t.GetGenericArguments()))
=> ConvertRemoteValuesToDictionary<TResult>(o.Value, typeof(Dictionary<,>).MakeGenericType(t.GetGenericArguments())),

(_, Type t) when Nullable.GetUnderlyingType(t) is { } underlying
=> ConvertToNullable<TResult>(underlying),
Expand Down Expand Up @@ -150,6 +154,32 @@ private static TResult ConvertRemoteValuesToGenericList<TResult>(IEnumerable<Rem

return (TResult)list;
}

private static TResult ConvertRemoteValuesToDictionary<TResult>(IReadOnlyList<IReadOnlyList<RemoteValue>>? remoteValues, Type dictionaryType)
{
var typeArgs = dictionaryType.GetGenericArguments();
var dict = (System.Collections.IDictionary)Activator.CreateInstance(dictionaryType)!;

if (remoteValues is not null)
{
var convertKeyMethod = typeof(RemoteValue).GetMethod(nameof(ConvertTo))!.MakeGenericMethod(typeArgs[0]);
var convertValueMethod = typeof(RemoteValue).GetMethod(nameof(ConvertTo))!.MakeGenericMethod(typeArgs[1]);

foreach (var pair in remoteValues)
{
Comment thread
nvborisenko marked this conversation as resolved.
if (pair.Count != 2)
{
throw new FormatException($"Expected a pair of RemoteValues for dictionary entry, but got {pair.Count} values.");
}

var convertedKey = convertKeyMethod.Invoke(pair[0], null)!;
var convertedValue = convertValueMethod.Invoke(pair[1], null);
dict.Add(convertedKey, convertedValue);
}
Comment thread
qodo-code-review[bot] marked this conversation as resolved.
}

return (TResult)dict;
}
}

public abstract record PrimitiveProtocolRemoteValue : RemoteValue;
Expand Down
72 changes: 72 additions & 0 deletions dotnet/test/common/BiDi/Script/RemoteValueConversionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,76 @@ static void AssertValue(IEnumerable<int> value)
Assert.That(value, Is.Empty);
}
}

[Test]
public void CanConvertMapRemoteValueToDictionary()
{
MapRemoteValue arg = new()
{
Value =
[
[new StringRemoteValue("key1"), new NumberRemoteValue(1)],
[new StringRemoteValue("key2"), new NumberRemoteValue(2)],
]
};

AssertValue(arg.ConvertTo<Dictionary<string, int>>());
AssertValue(arg.ConvertTo<IDictionary<string, int>>());

static void AssertValue(IDictionary<string, int> value)
{
Assert.That(value, Has.Count.EqualTo(2));
Assert.That(value["key1"], Is.EqualTo(1));
Assert.That(value["key2"], Is.EqualTo(2));
}
}

[Test]
public void CanConvertEmptyMapRemoteValueToDictionary()
{
MapRemoteValue arg = new();

AssertValue(arg.ConvertTo<Dictionary<string, int>>());

static void AssertValue(IDictionary<string, int> value)
{
Assert.That(value, Is.Empty);
}
}

[Test]
public void CanConvertObjectRemoteValueToDictionary()
{
ObjectRemoteValue arg = new()
{
Value =
[
[new StringRemoteValue("a"), new BooleanRemoteValue(true)],
[new StringRemoteValue("b"), new BooleanRemoteValue(false)],
]
};

AssertValue(arg.ConvertTo<Dictionary<string, bool>>());
AssertValue(arg.ConvertTo<IDictionary<string, bool>>());

static void AssertValue(IDictionary<string, bool> value)
{
Assert.That(value, Has.Count.EqualTo(2));
Assert.That(value["a"], Is.True);
Assert.That(value["b"], Is.False);
}
}

[Test]
public void CanConvertEmptyObjectRemoteValueToDictionary()
{
ObjectRemoteValue arg = new();

AssertValue(arg.ConvertTo<Dictionary<string, string>>());

static void AssertValue(IDictionary<string, string> value)
{
Assert.That(value, Is.Empty);
}
}
}
Loading