Skip to content

Commit 81c7fc5

Browse files
committed
[dotnet] [bidi] Make LocalValue types not nested
1 parent 0661b06 commit 81c7fc5

File tree

7 files changed

+70
-70
lines changed

7 files changed

+70
-70
lines changed

dotnet/src/webdriver/BiDi/Communication/Json/BiDiJsonSerializerContext.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ namespace OpenQA.Selenium.BiDi.Communication.Json;
140140
[JsonSerializable(typeof(Modules.Network.FetchErrorEventArgs))]
141141
[JsonSerializable(typeof(Modules.Network.AuthRequiredEventArgs))]
142142

143-
[JsonSerializable(typeof(Modules.Script.Channel), TypeInfoPropertyName = "Script_Channel")]
144-
[JsonSerializable(typeof(Modules.Script.LocalValue.String), TypeInfoPropertyName = "Script_LocalValue_String")]
143+
[JsonSerializable(typeof(Modules.Script.ChannelLocalValue), TypeInfoPropertyName = "Script_ChannelLocalValue")]
145144
[JsonSerializable(typeof(Modules.Script.Target.Realm), TypeInfoPropertyName = "Script_Target_Realm")]
146145
[JsonSerializable(typeof(Modules.Script.Target.Context), TypeInfoPropertyName = "Script_Target_Context")]
147146

dotnet/src/webdriver/BiDi/Modules/Script/AddPreloadScriptCommand.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace OpenQA.Selenium.BiDi.Modules.Script;
2525
internal class AddPreloadScriptCommand(AddPreloadScriptCommandParameters @params)
2626
: Command<AddPreloadScriptCommandParameters>(@params, "script.addPreloadScript");
2727

28-
internal record AddPreloadScriptCommandParameters(string FunctionDeclaration, IEnumerable<LocalValue.Channel>? Arguments, IEnumerable<BrowsingContext.BrowsingContext>? Contexts, string? Sandbox) : CommandParameters;
28+
internal record AddPreloadScriptCommandParameters(string FunctionDeclaration, IEnumerable<ChannelLocalValue>? Arguments, IEnumerable<BrowsingContext.BrowsingContext>? Contexts, string? Sandbox) : CommandParameters;
2929

3030
public record AddPreloadScriptOptions : CommandOptions
3131
{
@@ -37,7 +37,7 @@ internal AddPreloadScriptOptions(BrowsingContextAddPreloadScriptOptions? options
3737
Sandbox = options?.Sandbox;
3838
}
3939

40-
public IEnumerable<LocalValue.Channel>? Arguments { get; set; }
40+
public IEnumerable<ChannelLocalValue>? Arguments { get; set; }
4141

4242
public IEnumerable<BrowsingContext.BrowsingContext>? Contexts { get; set; }
4343

@@ -46,7 +46,7 @@ internal AddPreloadScriptOptions(BrowsingContextAddPreloadScriptOptions? options
4646

4747
public record BrowsingContextAddPreloadScriptOptions
4848
{
49-
public IEnumerable<LocalValue.Channel>? Arguments { get; set; }
49+
public IEnumerable<ChannelLocalValue>? Arguments { get; set; }
5050

5151
public string? Sandbox { get; set; }
5252
}

dotnet/src/webdriver/BiDi/Modules/Script/LocalValue.cs

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@
2323
namespace OpenQA.Selenium.BiDi.Modules.Script;
2424

2525
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
26-
[JsonDerivedType(typeof(Number), "number")]
27-
[JsonDerivedType(typeof(String), "string")]
28-
[JsonDerivedType(typeof(Null), "null")]
29-
[JsonDerivedType(typeof(Undefined), "undefined")]
30-
[JsonDerivedType(typeof(Channel), "channel")]
31-
[JsonDerivedType(typeof(Array), "array")]
32-
[JsonDerivedType(typeof(Date), "date")]
33-
[JsonDerivedType(typeof(Map), "map")]
34-
[JsonDerivedType(typeof(Object), "object")]
35-
[JsonDerivedType(typeof(RegExp), "regexp")]
36-
[JsonDerivedType(typeof(Set), "set")]
26+
[JsonDerivedType(typeof(NumberLocalValue), "number")]
27+
[JsonDerivedType(typeof(StringLocalValue), "string")]
28+
[JsonDerivedType(typeof(NullLocalValue), "null")]
29+
[JsonDerivedType(typeof(UndefinedLocalValue), "undefined")]
30+
[JsonDerivedType(typeof(ChannelLocalValue), "channel")]
31+
[JsonDerivedType(typeof(ArrayLocalValue), "array")]
32+
[JsonDerivedType(typeof(DateLocalValue), "date")]
33+
[JsonDerivedType(typeof(MapLocalValue), "map")]
34+
[JsonDerivedType(typeof(ObjectLocalValue), "object")]
35+
[JsonDerivedType(typeof(RegExpLocalValue), "regexp")]
36+
[JsonDerivedType(typeof(SetLocalValue), "set")]
3737
public abstract record LocalValue
3838
{
39-
public static implicit operator LocalValue(int value) { return new Number(value); }
40-
public static implicit operator LocalValue(string value) { return new String(value); }
39+
public static implicit operator LocalValue(int value) { return new NumberLocalValue(value); }
40+
public static implicit operator LocalValue(string value) { return new StringLocalValue(value); }
4141

4242
// TODO: Extend converting from types
4343
public static LocalValue ConvertFrom(object? value)
@@ -47,7 +47,7 @@ public static LocalValue ConvertFrom(object? value)
4747
case LocalValue:
4848
return (LocalValue)value;
4949
case null:
50-
return new Null();
50+
return new NullLocalValue();
5151
case int:
5252
return (int)value;
5353
case string:
@@ -65,52 +65,53 @@ public static LocalValue ConvertFrom(object? value)
6565
values.Add([property.Name, ConvertFrom(property.GetValue(value))]);
6666
}
6767

68-
return new Object(values);
68+
return new ObjectLocalValue(values);
6969
}
7070
}
7171
}
72+
}
7273

73-
public abstract record PrimitiveProtocolLocalValue : LocalValue;
74+
public abstract record PrimitiveProtocolLocalValue : LocalValue;
7475

75-
public record Number(double Value) : PrimitiveProtocolLocalValue
76-
{
77-
public static explicit operator Number(double n) => new Number(n);
78-
}
76+
public record NumberLocalValue(double Value) : PrimitiveProtocolLocalValue
77+
{
78+
public static explicit operator NumberLocalValue(double n) => new NumberLocalValue(n);
79+
}
7980

80-
public record String(string Value) : PrimitiveProtocolLocalValue;
81+
public record StringLocalValue(string Value) : PrimitiveProtocolLocalValue;
8182

82-
public record Null : PrimitiveProtocolLocalValue;
83+
public record NullLocalValue : PrimitiveProtocolLocalValue;
8384

84-
public record Undefined : PrimitiveProtocolLocalValue;
85+
public record UndefinedLocalValue : PrimitiveProtocolLocalValue;
8586

86-
public record Channel(Channel.ChannelProperties Value) : LocalValue
87-
{
88-
[JsonInclude]
89-
internal string type = "channel";
87+
public record ChannelLocalValue(ChannelLocalValue.ChannelProperties Value) : LocalValue
88+
{
89+
// TODO: Revise why we need it
90+
[JsonInclude]
91+
internal string type = "channel";
9092

91-
public record ChannelProperties(Script.Channel Channel)
92-
{
93-
public SerializationOptions? SerializationOptions { get; set; }
93+
public record ChannelProperties(Channel Channel)
94+
{
95+
public SerializationOptions? SerializationOptions { get; set; }
9496

95-
public ResultOwnership? Ownership { get; set; }
96-
}
97+
public ResultOwnership? Ownership { get; set; }
9798
}
99+
}
98100

99-
public record Array(IEnumerable<LocalValue> Value) : LocalValue;
101+
public record ArrayLocalValue(IEnumerable<LocalValue> Value) : LocalValue;
100102

101-
public record Date(string Value) : LocalValue;
103+
public record DateLocalValue(string Value) : LocalValue;
102104

103-
public record Map(IEnumerable<IEnumerable<LocalValue>> Value) : LocalValue;
105+
public record MapLocalValue(IEnumerable<IEnumerable<LocalValue>> Value) : LocalValue;
104106

105-
public record Object(IEnumerable<IEnumerable<LocalValue>> Value) : LocalValue;
107+
public record ObjectLocalValue(IEnumerable<IEnumerable<LocalValue>> Value) : LocalValue;
106108

107-
public record RegExp(RegExp.RegExpValue Value) : LocalValue
109+
public record RegExpLocalValue(RegExpLocalValue.RegExpValue Value) : LocalValue
110+
{
111+
public record RegExpValue(string Pattern)
108112
{
109-
public record RegExpValue(string Pattern)
110-
{
111-
public string? Flags { get; set; }
112-
}
113+
public string? Flags { get; set; }
113114
}
114-
115-
public record Set(IEnumerable<LocalValue> Value) : LocalValue;
116115
}
116+
117+
public record SetLocalValue(IEnumerable<LocalValue> Value) : LocalValue;

dotnet/test/common/BiDi/Script/CallFunctionLocalValueTest.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class CallFunctionLocalValueTest : BiDiTestFixture
2727
[Test]
2828
public void CanCallFunctionWithArgumentUndefined()
2929
{
30-
var arg = new LocalValue.Undefined();
30+
var arg = new UndefinedLocalValue();
3131
Assert.That(async () =>
3232
{
3333
await context.Script.CallFunctionAsync($$"""
@@ -43,7 +43,7 @@ await context.Script.CallFunctionAsync($$"""
4343
[Test]
4444
public void CanCallFunctionWithArgumentNull()
4545
{
46-
var arg = new LocalValue.Null();
46+
var arg = new NullLocalValue();
4747
Assert.That(async () =>
4848
{
4949
await context.Script.CallFunctionAsync($$"""
@@ -59,7 +59,7 @@ await context.Script.CallFunctionAsync($$"""
5959
[Test]
6060
public void CanCallFunctionWithArgumentEmptyString()
6161
{
62-
var arg = new LocalValue.String(string.Empty);
62+
var arg = new StringLocalValue(string.Empty);
6363
Assert.That(async () =>
6464
{
6565
await context.Script.CallFunctionAsync($$"""
@@ -75,7 +75,7 @@ await context.Script.CallFunctionAsync($$"""
7575
[Test]
7676
public void CanCallFunctionWithArgumentNonEmptyString()
7777
{
78-
var arg = new LocalValue.String("whoa");
78+
var arg = new StringLocalValue("whoa");
7979
Assert.That(async () =>
8080
{
8181
await context.Script.CallFunctionAsync($$"""
@@ -93,7 +93,7 @@ public void CanCallFunctionWithArgumentRecentDate()
9393
{
9494
const string PinnedDateTimeString = "2025-03-09T00:30:33.083Z";
9595

96-
var arg = new LocalValue.Date(PinnedDateTimeString);
96+
var arg = new DateLocalValue(PinnedDateTimeString);
9797

9898
Assert.That(async () =>
9999
{
@@ -112,7 +112,7 @@ public void CanCallFunctionWithArgumentEpochDate()
112112
{
113113
const string EpochString = "1970-01-01T00:00:00.000Z";
114114

115-
var arg = new LocalValue.Date(EpochString);
115+
var arg = new DateLocalValue(EpochString);
116116

117117
Assert.That(async () =>
118118
{
@@ -129,7 +129,7 @@ await context.Script.CallFunctionAsync($$"""
129129
[Test]
130130
public void CanCallFunctionWithArgumentNumberFive()
131131
{
132-
var arg = new LocalValue.Number(5);
132+
var arg = new NumberLocalValue(5);
133133

134134
Assert.That(async () =>
135135
{
@@ -146,7 +146,7 @@ await context.Script.CallFunctionAsync($$"""
146146
[Test]
147147
public void CanCallFunctionWithArgumentNumberNegativeFive()
148148
{
149-
var arg = new LocalValue.Number(-5);
149+
var arg = new NumberLocalValue(-5);
150150

151151
Assert.That(async () =>
152152
{
@@ -163,7 +163,7 @@ await context.Script.CallFunctionAsync($$"""
163163
[Test]
164164
public void CanCallFunctionWithArgumentNumberZero()
165165
{
166-
var arg = new LocalValue.Number(0);
166+
var arg = new NumberLocalValue(0);
167167

168168
Assert.That(async () =>
169169
{
@@ -182,7 +182,7 @@ await context.Script.CallFunctionAsync($$"""
182182
[IgnoreBrowser(Selenium.Browser.Chrome, "Chromium can't handle -0 argument as a number: https://github.com/w3c/webdriver-bidi/issues/887")]
183183
public void CanCallFunctionWithArgumentNumberNegativeZero()
184184
{
185-
var arg = new LocalValue.Number(double.NegativeZero);
185+
var arg = new NumberLocalValue(double.NegativeZero);
186186

187187
Assert.That(async () =>
188188
{
@@ -199,7 +199,7 @@ await context.Script.CallFunctionAsync($$"""
199199
[Test]
200200
public void CanCallFunctionWithArgumentNumberPositiveInfinity()
201201
{
202-
var arg = new LocalValue.Number(double.PositiveInfinity);
202+
var arg = new NumberLocalValue(double.PositiveInfinity);
203203

204204
Assert.That(async () =>
205205
{
@@ -216,7 +216,7 @@ await context.Script.CallFunctionAsync($$"""
216216
[Test]
217217
public void CanCallFunctionWithArgumentNumberNegativeInfinity()
218218
{
219-
var arg = new LocalValue.Number(double.NegativeInfinity);
219+
var arg = new NumberLocalValue(double.NegativeInfinity);
220220

221221
Assert.That(async () =>
222222
{
@@ -233,7 +233,7 @@ await context.Script.CallFunctionAsync($$"""
233233
[Test]
234234
public void CanCallFunctionWithArgumentNumberNaN()
235235
{
236-
var arg = new LocalValue.Number(double.NaN);
236+
var arg = new NumberLocalValue(double.NaN);
237237
Assert.That(async () =>
238238
{
239239
await context.Script.CallFunctionAsync($$"""
@@ -249,7 +249,7 @@ await context.Script.CallFunctionAsync($$"""
249249
[Test]
250250
public void CanCallFunctionWithArgumentRegExp()
251251
{
252-
var arg = new LocalValue.RegExp(new LocalValue.RegExp.RegExpValue("foo*") { Flags = "g" });
252+
var arg = new RegExpLocalValue(new RegExpLocalValue.RegExpValue("foo*") { Flags = "g" });
253253

254254
Assert.That(async () =>
255255
{
@@ -266,7 +266,7 @@ await context.Script.CallFunctionAsync($$"""
266266
[Test]
267267
public void CanCallFunctionWithArgumentArray()
268268
{
269-
var arg = new LocalValue.Array([new LocalValue.String("hi")]);
269+
var arg = new ArrayLocalValue([new StringLocalValue("hi")]);
270270

271271
Assert.That(async () =>
272272
{
@@ -283,7 +283,7 @@ await context.Script.CallFunctionAsync($$"""
283283
[Test]
284284
public void CanCallFunctionWithArgumentObject()
285285
{
286-
var arg = new LocalValue.Object([[new LocalValue.String("objKey"), new LocalValue.String("objValue")]]);
286+
var arg = new ObjectLocalValue([[new StringLocalValue("objKey"), new StringLocalValue("objValue")]]);
287287

288288
Assert.That(async () =>
289289
{
@@ -300,7 +300,7 @@ await context.Script.CallFunctionAsync($$"""
300300
[Test]
301301
public void CanCallFunctionWithArgumentMap()
302302
{
303-
var arg = new LocalValue.Map([[new LocalValue.String("mapKey"), new LocalValue.String("mapValue")]]);
303+
var arg = new MapLocalValue([[new StringLocalValue("mapKey"), new StringLocalValue("mapValue")]]);
304304

305305
Assert.That(async () =>
306306
{
@@ -317,7 +317,7 @@ await context.Script.CallFunctionAsync($$"""
317317
[Test]
318318
public void CanCallFunctionWithArgumentSet()
319319
{
320-
var arg = new LocalValue.Set([new LocalValue.String("setKey")]);
320+
var arg = new SetLocalValue([new StringLocalValue("setKey")]);
321321

322322
Assert.That(async () =>
323323
{

dotnet/test/common/BiDi/Script/CallFunctionParameterTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ async function() {
138138
[Test]
139139
public async Task CanCallFunctionWithThisParameter()
140140
{
141-
var thisParameter = new LocalValue.Object([["some_property", 42]]);
141+
var thisParameter = new ObjectLocalValue([["some_property", 42]]);
142142

143143
var res = await context.Script.CallFunctionAsync<int>("""
144144
function(){return this.some_property}

dotnet/test/common/BiDi/Script/ScriptCommandsTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public async Task CanAddPreloadScriptWithArguments()
110110
{
111111
var preloadScript = await bidi.Script.AddPreloadScriptAsync("(channel) => channel('will_be_send', 'will_be_ignored')", new()
112112
{
113-
Arguments = [new LocalValue.Channel(new(new("channel_name")))]
113+
Arguments = [new ChannelLocalValue(new(new("channel_name")))]
114114
});
115115

116116
Assert.That(preloadScript, Is.Not.Null);
@@ -122,7 +122,7 @@ public async Task CanAddPreloadScriptWithChannelOptions()
122122
{
123123
var preloadScript = await bidi.Script.AddPreloadScriptAsync("(channel) => channel('will_be_send', 'will_be_ignored')", new()
124124
{
125-
Arguments = [new LocalValue.Channel(new(new("channel_name"))
125+
Arguments = [new ChannelLocalValue(new(new("channel_name"))
126126
{
127127
SerializationOptions = new()
128128
{

dotnet/test/common/BiDi/Script/ScriptEventsTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public async Task CanListenToChannelMessage()
3535

3636
await context.Script.CallFunctionAsync("(channel) => channel('foo')", false, new()
3737
{
38-
Arguments = [new LocalValue.Channel(new(new("channel_name")))]
38+
Arguments = [new ChannelLocalValue(new(new("channel_name")))]
3939
});
4040

4141
var message = await tcs.Task.WaitAsync(TimeSpan.FromSeconds(5));

0 commit comments

Comments
 (0)