Skip to content

Commit

Permalink
Use primary constructos
Browse files Browse the repository at this point in the history
  • Loading branch information
Romfos committed Nov 18, 2023
1 parent 886f974 commit fd7fe16
Show file tree
Hide file tree
Showing 21 changed files with 30 additions and 169 deletions.
1 change: 1 addition & 0 deletions src/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ csharp_style_prefer_null_check_over_type_check=true:error
csharp_prefer_simple_default_expression=true:error
csharp_style_inlined_variable_declaration=true:error
dotnet_style_allow_multiple_blank_lines_experimental=false:error
csharp_style_prefer_primary_constructors=true:error

csharp_new_line_before_open_brace=all
csharp_new_line_before_else=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,8 @@

namespace AutoTests.Framework.Components.Application;

public sealed class ComponentReference
public sealed class ComponentReference(ComponentService componentService, string path)
{
private readonly ComponentService componentService;
private readonly string path;

public ComponentReference(ComponentService componentService, string path)
{
this.componentService = componentService;
this.path = path;
}

public T GetComponent<T>()
where T : class
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
namespace AutoTests.Framework.Components.Attributes;

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public sealed class RouteAttribute : Attribute
public sealed class RouteAttribute(string name) : Attribute
{
public string Name { get; }

public RouteAttribute(string name)
{
Name = name;
}
public string Name { get; } = name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
namespace AutoTests.Framework.Components.Attributes;

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public sealed class SelectorAttribute : Attribute
public sealed class SelectorAttribute(string name) : Attribute
{
public string Value { get; }

public SelectorAttribute(string name)
{
Value = name;
}
public string Value { get; } = name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,8 @@
namespace AutoTests.Framework.Components;

[Binding]
public sealed class ComponentsSpecflowHooks
public sealed class ComponentsSpecflowHooks(ComponentService componentService)
{
private readonly ComponentService componentService;

public ComponentsSpecflowHooks(ComponentService componentService)
{
this.componentService = componentService;
}

[StepArgumentTransformation]
public ComponentReference TransformComponentReference(string path)
{
Expand Down
9 changes: 1 addition & 8 deletions src/AutoTests.Framework.Components/ComponentsSteps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,8 @@
namespace AutoTests.Framework.Components;

[Binding]
public sealed class ComponentsSteps
public sealed class ComponentsSteps(ComponentService componentService)
{
private readonly ComponentService componentService;

public ComponentsSteps(ComponentService componentService)
{
this.componentService = componentService;
}

[When(@"click on '([^']*)'")]
public async Task WhenClickOn(ComponentReference componentReference)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@

namespace AutoTests.Framework.Components.Services;

public sealed class ComponentService
public sealed class ComponentService(IApplication application)
{
private readonly IApplication application;

public ComponentService(IApplication application)
{
this.application = application;
}
private readonly IApplication application = application;

public T GetComponent<T>(string path)
where T : class
Expand Down
10 changes: 1 addition & 9 deletions src/AutoTests.Framework.Playwright/Components/TrivialButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,17 @@

namespace AutoTests.Framework.Playwright.Components;

public sealed class TrivialButton : IClick, IVisible, IEnabled
public sealed class TrivialButton(IPage page) : IClick, IVisible, IEnabled
{
private readonly IPage page;

[FromSelector]
public string? Locator { get; set; }

public TrivialButton(IPage page)
{
this.page = page;
}

public async Task ClickAsync()
{
if (Locator == null)
{
throw new Exception("Locator is required");
}

await page.ClickAsync(Locator);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,11 @@

namespace AutoTests.Framework.Playwright.Components;

public sealed class TrivialInput : ISetValue, IGetValue, IVisible, IEnabled
public sealed class TrivialInput(IPage page) : ISetValue, IGetValue, IVisible, IEnabled
{
private readonly IPage page;

[FromSelector]
public string? Locator { get; set; }

public TrivialInput(IPage page)
{
this.page = page;
}

public async Task<object?> GetValueAsync()
{
if (Locator == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@

namespace AutoTests.Framework.Playwright.Components;

public sealed class TrivialLabel : IGetValue, IVisible
public sealed class TrivialLabel(IPage page) : IGetValue, IVisible
{
private readonly IPage page;

[FromSelector]
public string? Locator { get; set; }

public TrivialLabel(IPage page)
{
this.page = page;
}

public async Task<object?> GetValueAsync()
{
if (Locator == null)
Expand Down
9 changes: 1 addition & 8 deletions src/AutoTests.Framework.Playwright/NavigationSteps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,8 @@
namespace AutoTests.Framework.Playwright;

[Binding]
public sealed class NavigationSteps
public sealed class NavigationSteps(IPage page)
{
private readonly IPage page;

public NavigationSteps(IPage page)
{
this.page = page;
}

[Given(@"navigate to '([^']*)'")]
public async Task GivenNavigateTo(ArgumentExpression expression)
{
Expand Down
9 changes: 1 addition & 8 deletions src/AutoTests.Framework.Tests/Components/Steps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,8 @@
namespace AutoTests.Framework.Tests.Components;

[Binding]
internal sealed class Steps
internal sealed class Steps(Application application)
{
private readonly Application application;

public Steps(Application application)
{
this.application = application;
}

[Then(@"check component test 1")]
public void ThenCheckComponentTest1()
{
Expand Down
9 changes: 1 addition & 8 deletions src/AutoTests.Framework.Tests/Data/Steps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,8 @@
namespace AutoTests.Framework.Tests.Data;

[Binding]
public sealed class Steps
public sealed class Steps(DataService service)
{
private readonly DataService service;

public Steps(DataService service)
{
this.service = service;
}

[Then(@"test data step 1")]
public void ThenTestDataStep()
{
Expand Down
12 changes: 3 additions & 9 deletions src/AutoTests.Framework.Tests/Models/TestModel1.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
namespace AutoTests.Framework.Tests.Models;

internal sealed class TestModel1
internal sealed class TestModel1(int x, string y)
{
public int X { get; }
public string Y { get; }

public TestModel1(int x, string y)
{
X = x;
Y = y;
}
public int X { get; } = x;
public string Y { get; } = y;
}
9 changes: 2 additions & 7 deletions src/AutoTests.Framework/Data/DataService.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
namespace AutoTests.Framework.Data;

public sealed class DataService
public sealed class DataService(dynamic data)
{
public dynamic Data { get; }

public DataService(dynamic data)
{
Data = data;
}
public dynamic Data { get; } = data;
}
11 changes: 1 addition & 10 deletions src/AutoTests.Framework/Expressions/ArgumentExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,8 @@

namespace AutoTests.Framework.Expressions;

public sealed class ArgumentExpression
public sealed class ArgumentExpression(IExpressionService expressionService, string text)
{
private readonly IExpressionService expressionService;
private readonly string text;

public ArgumentExpression(IExpressionService expressionService, string text)
{
this.expressionService = expressionService;
this.text = text;
}

public async Task<T> ExecuteAsync<T>()
{
return await expressionService.ExecuteAsync<T>(text);
Expand Down
9 changes: 2 additions & 7 deletions src/AutoTests.Framework/Expressions/ExpressionEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

namespace AutoTests.Framework.Expressions;

public class ExpressionEnvironment
public class ExpressionEnvironment(DataService dataService)
{
public dynamic Data { get; }

public ExpressionEnvironment(DataService dataService)
{
Data = dataService.Data;
}
public dynamic Data { get; } = dataService.Data;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,8 @@
namespace AutoTests.Framework.Expressions;

[Binding]
public sealed class ExpressionSpecflowHooks
public sealed class ExpressionSpecflowHooks(IExpressionService expressionService)
{
private readonly IExpressionService expressionService;

public ExpressionSpecflowHooks(IExpressionService expressionService)
{
this.expressionService = expressionService;
}

[StepArgumentTransformation]
public ArgumentExpression TransformExpression(string text)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,9 @@

namespace AutoTests.Framework.Expressions;

internal sealed class RoslynCSharpExpressionService : IExpressionService
internal sealed class RoslynCSharpExpressionService(ExpressionEnvironment expressionEnvironment) : IExpressionService
{
private readonly ExpressionEnvironment expressionEnvironment;
private readonly ScriptOptions scriptOptions;

public RoslynCSharpExpressionService(ExpressionEnvironment expressionEnvironment)
{
this.expressionEnvironment = expressionEnvironment;
scriptOptions = ScriptOptions.Default.AddReferences("Microsoft.CSharp");
}
private readonly ScriptOptions scriptOptions = ScriptOptions.Default.AddReferences("Microsoft.CSharp");

public async Task<T> ExecuteAsync<T>(string text)
{
Expand Down
9 changes: 1 addition & 8 deletions src/AutoTests.Framework/Models/ExpressionSpecflowHooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,8 @@
namespace AutoTests.Framework.Models;

[Binding]
public sealed class ModelsSpecflowHooks
public sealed class ModelsSpecflowHooks(IExpressionService expressionService)
{
private readonly IExpressionService expressionService;

public ModelsSpecflowHooks(IExpressionService expressionService)
{
this.expressionService = expressionService;
}

[StepArgumentTransformation]
public IModel TransformModel(Table table)
{
Expand Down
17 changes: 2 additions & 15 deletions src/AutoTests.Framework/Models/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,13 @@

namespace AutoTests.Framework.Models;

internal sealed class Model : IModel
internal sealed class Model(IExpressionService expressionService, Table table) : IModel
{
private static readonly JsonSerializerOptions jsonSerializerOptions = new()
{
NumberHandling = JsonNumberHandling.AllowReadingFromString
};

private readonly IExpressionService expressionService;
private readonly Table table;

public Model(IExpressionService expressionService, Table table)
{
this.expressionService = expressionService;
this.table = table;
}

public async Task<T> GetValueAsync<T>()
{
var properties = table.Rows.ToDictionary(x => x["Name"], x => x["Value"]);
Expand Down Expand Up @@ -58,11 +49,7 @@ private async Task<T> ConvertToObject<T>(IEnumerable<KeyValuePair<string, string
private T ConvertToObject<T>(Dictionary<string, object?> properties)
{
var jsonModel = JsonSerializer.Serialize(properties);
var result = JsonSerializer.Deserialize<T>(jsonModel, jsonSerializerOptions);
if (result == null)
{
throw new Exception($"Unable to serialize {typeof(T).FullName}");
}
var result = JsonSerializer.Deserialize<T>(jsonModel, jsonSerializerOptions) ?? throw new Exception($"Unable to serialize {typeof(T).FullName}");
return result;
}
}

0 comments on commit fd7fe16

Please sign in to comment.