Skip to content

Commit 1553f62

Browse files
committed
Simplify
1 parent 083ab75 commit 1553f62

File tree

2 files changed

+138
-62
lines changed

2 files changed

+138
-62
lines changed

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

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,6 @@ public static LocalValue ConvertFrom(object? value)
101101
}
102102
}
103103

104-
public static LocalValue ConvertFrom(bool value)
105-
{
106-
return new BooleanLocalValue(value);
107-
}
108-
109104
public static LocalValue ConvertFrom(bool? value)
110105
{
111106
if (value is bool b)
@@ -116,11 +111,6 @@ public static LocalValue ConvertFrom(bool? value)
116111
return new NullLocalValue();
117112
}
118113

119-
public static LocalValue ConvertFrom(int value)
120-
{
121-
return new NumberLocalValue(value);
122-
}
123-
124114
public static LocalValue ConvertFrom(int? value)
125115
{
126116
if (value is int b)
@@ -131,11 +121,6 @@ public static LocalValue ConvertFrom(int? value)
131121
return new NullLocalValue();
132122
}
133123

134-
public static LocalValue ConvertFrom(double value)
135-
{
136-
return new NumberLocalValue(value);
137-
}
138-
139124
public static LocalValue ConvertFrom(double? value)
140125
{
141126
if (value is double b)
@@ -146,11 +131,6 @@ public static LocalValue ConvertFrom(double? value)
146131
return new NullLocalValue();
147132
}
148133

149-
public static LocalValue ConvertFrom(long value)
150-
{
151-
return new NumberLocalValue(value);
152-
}
153-
154134
public static LocalValue ConvertFrom(long? value)
155135
{
156136
if (value is long b)
@@ -171,52 +151,63 @@ public static LocalValue ConvertFrom(string? value)
171151
return new NullLocalValue();
172152
}
173153

174-
public static LocalValue ConvertFrom(DateTime dateTime)
154+
public static LocalValue ConvertFrom(DateTime? value)
175155
{
176-
return new DateLocalValue(dateTime.ToString("o"));
156+
if (value is null)
157+
{
158+
return new NullLocalValue();
159+
}
160+
161+
return new DateLocalValue(value.Value.ToString("o"));
177162
}
178163

179-
public static LocalValue ConvertFrom(BigInteger bigInt)
164+
public static LocalValue ConvertFrom(BigInteger? value)
180165
{
181-
return new BigIntLocalValue(bigInt.ToString());
166+
if (value is not null)
167+
{
168+
return new BigIntLocalValue(value.Value.ToString());
169+
}
170+
171+
return new NullLocalValue();
182172
}
183173

184-
public static LocalValue ConvertFrom(IEnumerable<object?>? values)
174+
public static LocalValue ConvertFrom(IEnumerable<object?>? value)
185175
{
186-
if (values is null)
176+
if (value is null)
187177
{
188178
return new NullLocalValue();
189179
}
190180

191-
LocalValue[] convertedList = values.Select(ConvertFrom).ToArray();
181+
LocalValue[] convertedList = [.. value.Select(ConvertFrom)];
192182
return new ArrayLocalValue(convertedList);
193183
}
194184

195-
public static LocalValue ConvertFrom<T>(IList<T?>? values)
185+
public static LocalValue ConvertFrom<T>(IList<T?>? value)
196186
{
197-
if (values is null)
187+
if (value is null)
198188
{
199189
return new NullLocalValue();
200190
}
201191

202-
List<LocalValue> convertedList = [.. values.Select(element => ConvertFrom(element))];
192+
List<LocalValue> convertedList = [.. value.Select(element => ConvertFrom(element))];
203193
return new ArrayLocalValue(convertedList);
204194
}
205195

206-
public static LocalValue ConvertFrom<TValue>(IDictionary<string, TValue?>? dictionary)
196+
public static LocalValue ConvertFrom<TValue>(IDictionary<string, TValue?>? value)
207197
{
208-
return ConvertFrom<string, TValue>(dictionary);
198+
return ConvertFrom<string, TValue>(value);
209199
}
210200

211-
public static LocalValue ConvertFrom<TKey, TValue>(IDictionary<TKey, TValue?>? dictionary)
201+
public static LocalValue ConvertFrom<TKey, TValue>(IDictionary<TKey, TValue?>? value)
212202
{
213-
if (dictionary is null)
203+
if (value is null)
214204
{
215205
return new NullLocalValue();
216206
}
217207

218-
var bidiObject = new List<List<LocalValue>>(dictionary.Count);
219-
foreach (KeyValuePair<TKey, TValue?> item in dictionary)
208+
var bidiObject = new List<List<LocalValue>>(value.Count);
209+
210+
foreach (KeyValuePair<TKey, TValue?> item in value)
220211
{
221212
bidiObject.Add([ConvertFrom(item.Key), ConvertFrom(item.Value)]);
222213
}
@@ -229,14 +220,15 @@ public static LocalValue ConvertFrom<TKey, TValue>(IDictionary<TKey, TValue?>? d
229220
return new MapLocalValue(bidiObject);
230221
}
231222

232-
public static LocalValue ConvertFrom<T>(ISet<T?>? set)
223+
public static LocalValue ConvertFrom<T>(ISet<T?>? value)
233224
{
234-
if (set is null)
225+
if (value is null)
235226
{
236227
return new NullLocalValue();
237228
}
238229

239-
LocalValue[] convertedValues = [.. set.Select(x => ConvertFrom(x))];
230+
LocalValue[] convertedValues = [.. value.Select(x => ConvertFrom(x))];
231+
240232
return new SetLocalValue(convertedValues);
241233
}
242234

@@ -252,9 +244,11 @@ private static LocalValue ReflectionBasedConvertFrom(object? value)
252244
System.Reflection.PropertyInfo[] properties = value.GetType().GetProperties(Flags);
253245

254246
var values = new List<List<LocalValue>>(properties.Length);
247+
255248
foreach (System.Reflection.PropertyInfo? property in properties)
256249
{
257250
object? propertyValue;
251+
258252
try
259253
{
260254
propertyValue = property.GetValue(value);
@@ -263,6 +257,7 @@ private static LocalValue ReflectionBasedConvertFrom(object? value)
263257
{
264258
throw new BiDiException($"Could not retrieve property {property.Name} from {property.DeclaringType}", ex);
265259
}
260+
266261
values.Add([property.Name, ConvertFrom(propertyValue)]);
267262
}
268263

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

Lines changed: 104 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
using NUnit.Framework;
2121
using OpenQA.Selenium.BiDi.Modules.Script;
22+
using System;
23+
using System.Collections.Generic;
2224

2325
namespace OpenQA.Selenium.BiDi.Script;
2426

@@ -28,72 +30,151 @@ class LocalValueConversionTests
2830
public void CanConvertNullBoolToLocalValue()
2931
{
3032
bool? arg = null;
31-
LocalValue result = arg;
32-
Assert.That(result, Is.TypeOf<NullLocalValue>());
33+
34+
AssertValue(arg);
35+
AssertValue(LocalValue.ConvertFrom(arg));
36+
37+
static void AssertValue(LocalValue value)
38+
{
39+
Assert.That(value, Is.TypeOf<NullLocalValue>());
40+
}
3341
}
3442

3543
[Test]
3644
public void CanConvertTrueToLocalValue()
3745
{
38-
LocalValue result = true;
39-
Assert.That(result, Is.TypeOf<BooleanLocalValue>());
40-
Assert.That((result as BooleanLocalValue).Value, Is.True);
46+
AssertValue(true);
47+
48+
AssertValue(LocalValue.ConvertFrom(true));
49+
50+
static void AssertValue(LocalValue value)
51+
{
52+
Assert.That(value, Is.TypeOf<BooleanLocalValue>());
53+
Assert.That((value as BooleanLocalValue).Value, Is.True);
54+
}
4155
}
4256

4357
[Test]
4458
public void CanConvertFalseToLocalValue()
4559
{
46-
LocalValue result = false;
47-
Assert.That(result, Is.TypeOf<BooleanLocalValue>());
48-
Assert.That((result as BooleanLocalValue).Value, Is.False);
60+
AssertValue(false);
61+
62+
AssertValue(LocalValue.ConvertFrom(false));
63+
64+
static void AssertValue(LocalValue value)
65+
{
66+
Assert.That(value, Is.TypeOf<BooleanLocalValue>());
67+
Assert.That((value as BooleanLocalValue).Value, Is.False);
68+
}
4969
}
5070

5171
[Test]
5272
public void CanConvertNullIntToLocalValue()
5373
{
5474
int? arg = null;
55-
LocalValue result = arg;
56-
Assert.That(result, Is.TypeOf<NullLocalValue>());
75+
76+
AssertValue(arg);
77+
78+
AssertValue(LocalValue.ConvertFrom(arg));
79+
80+
static void AssertValue(LocalValue value)
81+
{
82+
Assert.That(value, Is.TypeOf<NullLocalValue>());
83+
}
5784
}
5885

5986
[Test]
6087
public void CanConvertZeroIntToLocalValue()
6188
{
62-
LocalValue result = 0;
63-
Assert.That(result, Is.TypeOf<NumberLocalValue>());
64-
Assert.That((result as NumberLocalValue).Value, Is.Zero);
89+
LocalValue arg = 0;
90+
91+
AssertValue(arg);
92+
93+
AssertValue(LocalValue.ConvertFrom(0));
94+
95+
static void AssertValue(LocalValue value)
96+
{
97+
Assert.That(value, Is.TypeOf<NumberLocalValue>());
98+
Assert.That((value as NumberLocalValue).Value, Is.Zero);
99+
}
65100
}
66101

67102
[Test]
68103
public void CanConvertNullDoubleToLocalValue()
69104
{
70105
double? arg = null;
71-
LocalValue result = arg;
72-
Assert.That(result, Is.TypeOf<NullLocalValue>());
106+
107+
AssertValue(arg);
108+
109+
AssertValue(LocalValue.ConvertFrom(arg));
110+
111+
static void AssertValue(LocalValue value)
112+
{
113+
Assert.That(value, Is.TypeOf<NullLocalValue>());
114+
}
73115
}
74116

75117
[Test]
76118
public void CanConvertZeroDoubleToLocalValue()
77119
{
78120
double arg = 0;
79-
LocalValue result = arg;
80-
Assert.That(result, Is.TypeOf<NumberLocalValue>());
81-
Assert.That((result as NumberLocalValue).Value, Is.Zero);
121+
122+
AssertValue(arg);
123+
124+
AssertValue(LocalValue.ConvertFrom(0));
125+
126+
static void AssertValue(LocalValue value)
127+
{
128+
Assert.That(value, Is.TypeOf<NumberLocalValue>());
129+
Assert.That((value as NumberLocalValue).Value, Is.Zero);
130+
}
82131
}
83132

84133
[Test]
85134
public void CanConvertNullStringToLocalValue()
86135
{
87136
string arg = null;
88-
LocalValue result = arg;
89-
Assert.That(result, Is.TypeOf<NullLocalValue>());
137+
138+
AssertValue(arg);
139+
140+
AssertValue(LocalValue.ConvertFrom(arg));
141+
142+
static void AssertValue(LocalValue value)
143+
{
144+
Assert.That(value, Is.TypeOf<NullLocalValue>());
145+
}
90146
}
91147

92148
[Test]
93149
public void CanConvertStringToLocalValue()
94150
{
95-
LocalValue result = "value";
96-
Assert.That(result, Is.TypeOf<StringLocalValue>());
97-
Assert.That((result as StringLocalValue).Value, Is.EqualTo("value"));
151+
AssertValue("value");
152+
153+
AssertValue(LocalValue.ConvertFrom("value"));
154+
155+
static void AssertValue(LocalValue value)
156+
{
157+
Assert.That(value, Is.TypeOf<StringLocalValue>());
158+
Assert.That((value as StringLocalValue).Value, Is.EqualTo("value"));
159+
}
160+
}
161+
162+
[Test]
163+
public void CanConvertObjectValue()
164+
{
165+
var my = new MyClass() { IntNumber = 5 };
166+
167+
var value = LocalValue.ConvertFrom(my);
168+
169+
Console.WriteLine(value);
170+
171+
Assert.That(value, Is.TypeOf<ObjectLocalValue>());
172+
}
173+
174+
class MyClass
175+
{
176+
public int IntNumber { get; set; }
177+
178+
public List<int> ListOfInt { get; set; }
98179
}
99180
}

0 commit comments

Comments
 (0)