Skip to content

Commit 6240be0

Browse files
authored
Merge pull request #46 from csf-dev/44-dupe-defensive-code
Resolve #44 - Logic corrected
2 parents 6da6f32 + 67a55b0 commit 6240be0

4 files changed

Lines changed: 41 additions & 14 deletions

File tree

CSF.Extensions.WebDriver.Tests/Factories/WebDriverCreationConfigureOptionsTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public async Task ConfigureShouldBeAbleToGetSelectedConfigWhenASelectedConfigIsN
138138
}
139139

140140
[Test,AutoMoqData]
141-
public async Task ConfigureShouldProvideNullSelectedConfigWhenThereAreTwoConfigsAndNoExplicitSelection([StandardTypes] IGetsWebDriverAndOptionsTypes typeProvider)
141+
public async Task ConfigureShouldProvideThrowWhenThereAreTwoConfigsAndNoExplicitSelection([StandardTypes] IGetsWebDriverAndOptionsTypes typeProvider)
142142
{
143143
var options = await GetOptionsAsync(typeProvider,
144144
@"{
@@ -148,7 +148,7 @@ public async Task ConfigureShouldProvideNullSelectedConfigWhenThereAreTwoConfigs
148148
}
149149
}");
150150

151-
Assert.That(options.GetSelectedConfiguration(), Is.Null);
151+
Assert.That(() => options.GetSelectedConfiguration(), Throws.InvalidOperationException);
152152
}
153153

154154
[Test,AutoMoqData]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace CSF.Extensions.WebDriver.Factories;
2+
3+
[TestFixture,Parallelizable]
4+
public class WebDriverFactoryExtensionsTests
5+
{
6+
[Test,AutoMoqData]
7+
public void GetWebDriverShouldNotThrowIfSelectedConfigIsEmptyButOnlyOneConfigPresent(ICreatesWebDriverFromOptions factory,
8+
WebDriverCreationOptions options,
9+
WebDriverAndOptions expectedResult)
10+
{
11+
var configCollection = new WebDriverCreationOptionsCollection();
12+
configCollection.DriverConfigurations.Add("config", options);
13+
WebDriverAndOptions? result = null;
14+
Mock.Get(factory).Setup(x => x.GetWebDriver(options, null)).Returns(expectedResult);
15+
16+
Assert.Multiple(() =>
17+
{
18+
Assert.That(() => result = factory.GetWebDriver(configCollection), Throws.Nothing, "Does not throw exception");
19+
Assert.That(result, Is.SameAs(expectedResult), "Correct expected result");
20+
});
21+
}
22+
}

CSF.Extensions.WebDriver/Factories/WebDriverCreationOptionsCollection.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,19 @@ public IDictionary<string,WebDriverCreationOptions> DriverConfigurations
5757
/// <see cref="DriverConfigurations"/>; that single configuration is considered to be currently selected.</description></item>
5858
/// </list>
5959
/// </remarks>
60-
/// <returns>A <see cref="WebDriverCreationOptions"/>, or a <see langword="null" /> reference if both <see cref="SelectedConfiguration"/>
61-
/// is <see langword="null" /> or an empty string and there is not precisely one configuration within <see cref="DriverConfigurations"/>.</returns>
60+
/// <returns>A <see cref="WebDriverCreationOptions"/></returns>
6261
/// <exception cref="InvalidOperationException">If <see cref="SelectedConfiguration"/> is not <see langword="null" /> or empty, but
63-
/// there is no item within <see cref="DriverConfigurations"/> with a key matching the selected configuration.</exception>
62+
/// there is no item within <see cref="DriverConfigurations"/> with a key matching the selected configuration
63+
/// or if <see cref="SelectedConfiguration"/> is either <see langword="null" /> or empty and there is not precisely one configuration
64+
/// within <see cref="DriverConfigurations"/>.</exception>
6465
public WebDriverCreationOptions GetSelectedConfiguration()
6566
{
6667
if (string.IsNullOrEmpty(SelectedConfiguration))
67-
return DriverConfigurations.Count == 1 ? DriverConfigurations.Single().Value : null;
68+
{
69+
return DriverConfigurations.Count == 1
70+
? DriverConfigurations.Single().Value
71+
: throw new InvalidOperationException($"If the {nameof(SelectedConfiguration)} is not set then there must be precisely one configuration present in {nameof(DriverConfigurations)}.");
72+
}
6873

6974
if(driverConfigurations.TryGetValue(SelectedConfiguration, out var options)) return options;
7075
throw new InvalidOperationException($"The {nameof(SelectedConfiguration)}: '{SelectedConfiguration}' must exist as a key within the {nameof(DriverConfigurations)}.");

CSF.Extensions.WebDriver/Factories/WebDriverFactoryExtensions.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ public static class WebDriverFactoryExtensions
3232
/// <exception cref="ArgumentException">
3333
/// If any of:
3434
/// <list type="bullet">
35-
/// <item><description>The <paramref name="configuration"/> has a <see langword="null" /> or empty <see cref="WebDriverCreationOptionsCollection.SelectedConfiguration"/></description></item>
36-
/// <item><description>The <paramref name="configuration"/> has no entry within <see cref="WebDriverCreationOptionsCollection.DriverConfigurations"/> with a key matching the
37-
/// <see cref="WebDriverCreationOptionsCollection.SelectedConfiguration"/></description></item>
3835
/// <item><description>The selected <see cref="WebDriverCreationOptionsCollection.DriverConfigurations"/> entry is <see langword="null" /></description></item>
3936
/// <item><description>The <see cref="WebDriverCreationOptions.DriverType"/> of the selected <see cref="WebDriverCreationOptionsCollection.DriverConfigurations"/> is <see langword="null" /> or empty</description></item>
4037
/// <item><description>The <see cref="WebDriverCreationOptions.OptionsFactory"/> of the selected <see cref="WebDriverCreationOptionsCollection.DriverConfigurations"/> is <see langword="null" /></description></item>
@@ -46,18 +43,21 @@ public static class WebDriverFactoryExtensions
4643
/// Either <see cref="WebDriverCreationOptions.DriverType"/> or <see cref="WebDriverCreationOptions.OptionsType"/> of the
4744
/// selected <see cref="WebDriverCreationOptionsCollection.DriverConfigurations"/> are non-null/non-empty but no type can be found matching the specifed values.
4845
/// </exception>
46+
/// <exception cref="InvalidOperationException">
47+
/// If <see cref="WebDriverCreationOptionsCollection.SelectedConfiguration"/> is <see langword="null" /> or empty and there is not precisely one
48+
/// configuration within <see cref="WebDriverCreationOptionsCollection.DriverConfigurations"/>.
49+
/// Or if the <paramref name="configuration"/> does not contain a configuration item with a key matching the
50+
/// <see cref="WebDriverCreationOptionsCollection.SelectedConfiguration"/>.
51+
/// </exception>
4952
public static WebDriverAndOptions GetWebDriver(this ICreatesWebDriverFromOptions factory,
5053
WebDriverCreationOptionsCollection configuration,
5154
Action<DriverOptions> supplementaryConfiguration = null)
5255
{
5356
if (factory is null) throw new ArgumentNullException(nameof(factory));
5457
if (configuration is null) throw new ArgumentNullException(nameof(configuration));
55-
if (string.IsNullOrEmpty(configuration.SelectedConfiguration))
56-
throw new ArgumentException($"The {nameof(WebDriverCreationOptionsCollection)}.{nameof(WebDriverCreationOptionsCollection.SelectedConfiguration)} must not be null or empty.", nameof(configuration));
57-
if (!configuration.DriverConfigurations.TryGetValue(configuration.SelectedConfiguration, out WebDriverCreationOptions value) || value is null)
58-
throw new ArgumentException($"The {nameof(WebDriverCreationOptionsCollection)}.{nameof(WebDriverCreationOptionsCollection.DriverConfigurations)} must contain a non-null entry matching the {nameof(WebDriverCreationOptionsCollection.SelectedConfiguration)}: '{configuration.SelectedConfiguration}'. No such entry was found.", nameof(configuration));
5958

60-
return factory.GetWebDriver(value, supplementaryConfiguration);
59+
var configItem = configuration.GetSelectedConfiguration() ?? throw new ArgumentException($"The selected web driver configuration item must not be null.", nameof(configuration));
60+
return factory.GetWebDriver(configItem, supplementaryConfiguration);
6161
}
6262

6363
/// <summary>

0 commit comments

Comments
 (0)