Skip to content

Commit

Permalink
Code Quality: Various refactorings (MudBlazor#8550)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielchalmers authored Apr 3, 2024
1 parent 11e3202 commit 0dcc84c
Show file tree
Hide file tree
Showing 122 changed files with 434 additions and 430 deletions.
2 changes: 1 addition & 1 deletion src/MudBlazor.Docs.Compiler/DocStrings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private static string convertSeeTags(string doc)
{
return SeeCrefRegularExpression().Replace(doc, match =>
{
string result = match.Groups[2].Value; // get the name of Type or type member (Field, Property, Method, or Event)
var result = match.Groups[2].Value; // get the name of Type or type member (Field, Property, Method, or Event)
result = BacktickRegularExpression().Replace(result, ""); // remove `1 from generic type name
return result;
});
Expand Down
2 changes: 1 addition & 1 deletion src/MudBlazor.Docs.Compiler/TestsForApiPages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static bool IsObsolete(Type type)
{
var attributes = (ObsoleteAttribute[])
type.GetCustomAttributes(typeof(ObsoleteAttribute), false);
return (attributes != null && attributes.Length > 0);
return attributes != null && attributes.Length > 0;
}

private static string SafeTypeName(Type type, bool removeT = false)
Expand Down
4 changes: 2 additions & 2 deletions src/MudBlazor.Docs.Compiler/XmlDocumentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ public static string GetDocumentation(this EventInfo eventInfo)
public static string XmlDocumentationKeyHelper(string typeFullNameString, string memberNameString)
{
var key = DocumentationKeyRegularExpression().Replace(typeFullNameString, string.Empty).Replace('+', '.');
if (!(memberNameString is null))
if (memberNameString is not null)
{
key += "." + memberNameString;
}
Expand Down Expand Up @@ -562,7 +562,7 @@ public static string GetDocumentation(this MemberInfo memberInfo)
public static string GetDocumentation(this ParameterInfo parameterInfo)
{
var memberDocumentation = parameterInfo.Member.GetDocumentation();
if (!(memberDocumentation is null))
if (memberDocumentation is not null)
{
var regexPattern =
Regex.Escape(@"<param name=" + "\"" + parameterInfo.Name + "\"" + @">") +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public async Task<IActionResult> SearchWithDelay(CancellationToken cancellationT
List<string> result = new();
foreach (var item in states)
{
if (string.IsNullOrEmpty(input) == true || item.Contains(input, StringComparison.InvariantCultureIgnoreCase))
if (string.IsNullOrEmpty(input) || item.Contains(input, StringComparison.InvariantCultureIgnoreCase))
{
result.Add(item);
}
Expand Down
6 changes: 3 additions & 3 deletions src/MudBlazor.Docs.WasmHost/Prerender/ICrawlerIdentifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public interface ICrawlerIdentifier

public class FileBasedCrawlerIdentifier : ICrawlerIdentifier
{
private record CrawlerEntry(string Pattern, string Url, IEnumerable<String> Instances);
private record CrawlerEntry(string Pattern, string Url, IEnumerable<string> Instances);

private readonly string _filename;
private LimitedConcurrentDictionary<string, bool> _cache = new(1_000);
Expand All @@ -49,11 +49,11 @@ public Task<bool> IsRequestByCrawler(HttpContext context)
if (userAgentHeader.Any() == false) { return Task.FromResult(false); }

var value = userAgentHeader.First();
if (_cache.ContainsKey(value) == true) { return Task.FromResult(_cache[value]); }
if (_cache.ContainsKey(value)) { return Task.FromResult(_cache[value]); }

foreach (var item in _patterns)
{
if (item.IsMatch(value) == true)
if (item.IsMatch(value))
{
_cache.TryAdd(value, true);
return Task.FromResult(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace MudBlazor.Docs.WasmHost.Prerender
public class LimitedConcurrentDictionary<TKey, TValue>
{
private ConcurrentDictionary<TKey, TValue> _dict = new();
private Int32 MaxCapacity { get; init; }
private int MaxCapacity { get; init; }

public LimitedConcurrentDictionary(Int32 maxCapacity)
public LimitedConcurrentDictionary(int maxCapacity)
{
MaxCapacity = maxCapacity;
}
Expand Down
4 changes: 2 additions & 2 deletions src/MudBlazor.Docs.WasmHost/Prerender/PrerenderMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public PrerenderMiddleware(RequestDelegate next, ICrawlerIdentifier crawlerIdent
public async Task Invoke(HttpContext context)
{
var path = context.Request.Path.ToString().ToLower();
if (path.Contains("webapi") == true)
if (path.Contains("webapi"))
{
await _next(context);
return;
Expand All @@ -38,7 +38,7 @@ public async Task Invoke(HttpContext context)
}
else
{
if (_responseCache.ContainsKey(path) == true)
if (_responseCache.ContainsKey(path))
{
context.Response.StatusCode = 200;
context.Response.ContentType = "text/html; charset=utf-8";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Expression(string expression)

public Expression(SimpleParser parser)
{
this._parser = parser;
_parser = parser;
}

public Expression(double nr)
Expand Down
2 changes: 1 addition & 1 deletion src/MudBlazor.Docs/Components/SectionContent.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ RenderFragment CodeComponent(string code) => builder =>

protected virtual async void RunOnTryMudBlazor()
{
string firstFile = "";
var firstFile = "";

if (Codes != null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/MudBlazor.Docs/Extensions/MethodInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static string GetSignature(this MethodInfo method, bool callable = false)
stringBuilder.Append("this ");
}
}
else if (secondParameter == true)
else if (secondParameter)
{
secondParameter = false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/MudBlazor.Docs/Models/XmlDocumentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ public static string GetDocumentation(this EventInfo eventInfo)
public static string XmlDocumentationKeyHelper(string typeFullNameString, string memberNameString)
{
var key = DocumentationKeyRegularExpression().Replace(typeFullNameString, string.Empty).Replace('+', '.');
if (!(memberNameString is null))
if (memberNameString is not null)
{
key += "." + memberNameString;
}
Expand Down Expand Up @@ -571,7 +571,7 @@ public static string GetDocumentation(this MemberInfo memberInfo)
public static string GetDocumentation(this ParameterInfo parameterInfo)
{
var memberDocumentation = parameterInfo.Member.GetDocumentation();
if (!(memberDocumentation is null))
if (memberDocumentation is not null)
{
var regexPattern =
Regex.Escape(@"<param name=" + "\"" + parameterInfo.Name + "\"" + @">") +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected override async Task OnParametersSetAsync()

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender == true)
if (firstRender)
{
await NotificationService.MarkNotificationsAsRead(Id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected override async Task OnInitializedAsync()

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender == true)
if (firstRender)
{
await NotificationService.MarkNotificationsAsRead();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public async Task MarkNotificationsAsRead(string id)
}

public Task<NotificationMessage> GetMessageById(string id) =>
Task.FromResult(_messages.FirstOrDefault((x => x.Id == id)));
Task.FromResult(_messages.FirstOrDefault(x => x.Id == id));

public async Task<IDictionary<NotificationMessage, bool>> GetNotifications()
{
Expand Down
2 changes: 1 addition & 1 deletion src/MudBlazor.Docs/Shared/AppbarButtons.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private async Task MarkNotificationAsRead()

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender == true)
if (firstRender)
{
_newNotificationsAvailable = await NotificationService.AreNewNotificationsAvailable();
_messages = await NotificationService.GetNotifications();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
{
var enums =
context.SyntaxProvider.CreateSyntaxProvider(
transform: GetEnumData,
predicate: (syntaxNode, _) => syntaxNode is EnumDeclarationSyntax)
predicate: (syntaxNode, _) => syntaxNode is EnumDeclarationSyntax,
transform: GetEnumData)
.Where(static enumData => enumData is not null);
context.RegisterSourceOutput(enums, Build);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected async Task<TableData<ComplexObject>> ServerData(TableState state, Canc
TableData<ComplexObject> data = new();
data.TotalItems = _simulatedServerData.Count;
// Serialize & deserialize to test a more real scenario where the references to the objects changes
string jsonData = JsonSerializer.Serialize(_simulatedServerData);
var jsonData = JsonSerializer.Serialize(_simulatedServerData);
data.Items = JsonSerializer.Deserialize<List<ComplexObject>>(jsonData).Skip(state.PageSize * state.Page).Take(state.PageSize);
return data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public async Task LoadItemsAsync()
var list = new List<Item>();
//delay to simulate web service call
await Task.Delay(50).ConfigureAwait(false);
for (int i = 0; i < 20; i++)
for (var i = 0; i < 20; i++)
{
list.Add(new Item { Text = i.ToString() });
}
Expand Down
5 changes: 4 additions & 1 deletion src/MudBlazor.UnitTests/Components/AutocompleteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,11 +475,14 @@ public async Task Autocomplete_Should_SelectValue_On_Tab_With_SelectValueOnTab()
}

/// <summary>
/// <para>
/// When selecting a value by clicking on it in the list the input will blur. However, this
/// must not cause the dropdown to close or else the click on the item will not be possible!
///
/// </para>
/// <para>
/// If this test fails it means the dropdown has closed before we can even click any value in the list.
/// Such a regression happened and caused PR #1807 to be reverted
/// </para>
/// </summary>
[Test]
public async Task Autocomplete_Should_NotCloseDropdownOnInputBlur()
Expand Down
10 changes: 7 additions & 3 deletions src/MudBlazor.UnitTests/Components/BunitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,24 @@ public void TearDown()
}

/// <summary>
/// <para>
/// Note: This is a last resort measure to wrap around the logic of flaky tests which fail often (and
/// especially on the integration server).
///
/// </para>
/// <para>
/// It reduces the chance of a perfectly working test to fail due to a race condition in bUnit by running it
/// multiple times. If it succeeds at least once, the test passes. In the best-case scenario the test will run
/// only once and pass. If it is particularly flaky it might run a few times until it passes.
///
/// </para>
/// <para>
/// If the test is really broken due to a bug
/// it will fail for all runs. To get the original test output we run it one last time outside of the catch block
/// so the test result gets reported.
/// </para>
/// </summary>
protected async Task ImproveChanceOfSuccess(Func<Task> testAction)
{
for (int i = 0; i < 10; i++)
for (var i = 0; i < 10; i++)
{
try
{
Expand Down
6 changes: 3 additions & 3 deletions src/MudBlazor.UnitTests/Components/Charts/BarChartTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void BarChartEmptyData()
[Test]
public void BarChartExampleData()
{
List<ChartSeries> chartSeries = new List<ChartSeries>()
var chartSeries = new List<ChartSeries>()
{
new () { Name = "United States", Data = new double[] { 40, 20, 25, 27, 46, 60, 48, 80, 15 } },
new () { Name = "Germany", Data = new double[] { 19, 24, 35, 13, 28, 15, -4, 16, 31 } },
Expand Down Expand Up @@ -112,7 +112,7 @@ public void BarChartExampleData()
[Test]
public void BarChartExampleSingleXAxis()
{
List<ChartSeries> chartSeries = new List<ChartSeries>()
var chartSeries = new List<ChartSeries>()
{
new () { Name = "United States", Data = new double[] { 40, 20, 25, 27, 46, 60, 48, 80, 15 } },
new () { Name = "Germany", Data = new double[] { 19, 24, 35, 13, 28, 15, -4, 16, 31 } },
Expand Down Expand Up @@ -166,7 +166,7 @@ public void BarChartExampleSingleXAxis()
[Test]
public void BarChartColoring()
{
List<ChartSeries> chartSeries = new List<ChartSeries>()
var chartSeries = new List<ChartSeries>()
{
new ChartSeries() { Name = "Deep Sea Blue", Data = new double[] { 40, 20, 25, 27, 46 } },
new ChartSeries() { Name = "Venetian Red", Data = new double[] { 19, 24, 35, 13, 28 } },
Expand Down
6 changes: 3 additions & 3 deletions src/MudBlazor.UnitTests/Components/Charts/DonutChartTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ public void DonutCirclePosition(double[] data)
.Add(p => p.ChartOptions, new ChartOptions { ChartPalette = _baseChartPalette })
.Add(p => p.InputLabels, labels));

var svgViewBox = comp.Find("svg").GetAttribute("viewBox")?.Split(" ")?.Select(s => Int32.Parse(s))?.ToArray();
var svgViewBox = comp.Find("svg").GetAttribute("viewBox")?.Split(" ")?.Select(s => int.Parse(s))?.ToArray();
var circles = comp.FindAll("circle");

svgViewBox.Should().NotBeNullOrEmpty("must have a valid viewbox", svgViewBox);

foreach (var c in circles)
{
var cx = Int32.Parse(c.GetAttribute("cx") ?? "0");
var cy = Int32.Parse(c.GetAttribute("cy") ?? "0");
var cx = int.Parse(c.GetAttribute("cx") ?? "0");
var cy = int.Parse(c.GetAttribute("cy") ?? "0");

cx.Should().Be(svgViewBox[2] / 2);

Expand Down
6 changes: 3 additions & 3 deletions src/MudBlazor.UnitTests/Components/Charts/LineChartTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void LineChartEmptyData()
[TestCaseSource("GetInterpolationOptions")]
public void LineChartExampleData(InterpolationOption opt)
{
List<ChartSeries> chartSeries = new List<ChartSeries>()
var chartSeries = new List<ChartSeries>()
{
new ChartSeries() { Name = "Series 1", Data = new double[] { 90, 79, -72, 69, 62, 62, -55, 65, 70 } },
new ChartSeries() { Name = "Series 2", Data = new double[] { 10, 41, 35, 51, 49, 62, -69, 91, -148 } },
Expand Down Expand Up @@ -152,7 +152,7 @@ public void LineChartExampleData(InterpolationOption opt)
[TestCaseSource("GetInterpolationOptions")]
public void LineChartExampleZeroValues(InterpolationOption opt)
{
List<ChartSeries> chartSeries = new List<ChartSeries>()
var chartSeries = new List<ChartSeries>()
{
new ChartSeries() { Name = "Series 1", Data = new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
};
Expand Down Expand Up @@ -201,7 +201,7 @@ public void LineChartExampleZeroValues(InterpolationOption opt)
[Test]
public void LineChartColoring()
{
List<ChartSeries> chartSeries = new List<ChartSeries>()
var chartSeries = new List<ChartSeries>()
{
new ChartSeries() { Name = "Deep Sea Blue", Data = new double[] { 40, 20, 25, 27, 46 } },
new ChartSeries() { Name = "Venetian Red", Data = new double[] { 19, 24, 35, 13, 28 } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void BarChartEmptyData()
[Test]
public void BarChartExampleData()
{
List<ChartSeries> chartSeries = new List<ChartSeries>()
var chartSeries = new List<ChartSeries>()
{
new () { Name = "United States", Data = new double[] { 40, 20, 25, 27, 46, 60, 48, 80, 15 } },
new () { Name = "Germany", Data = new double[] { 19, 24, 35, 13, 28, 15, 13, 16, 31 } },
Expand Down Expand Up @@ -112,7 +112,7 @@ public void BarChartExampleData()
[Test]
public void StackedBarChartColoring()
{
List<ChartSeries> chartSeries = new List<ChartSeries>()
var chartSeries = new List<ChartSeries>()
{
new ChartSeries() { Name = "Deep Sea Blue", Data = new double[] { 40, 20, 25, 27, 46 } },
new ChartSeries() { Name = "Venetian Red", Data = new double[] { 19, 24, 35, 13, 28 } },
Expand Down
Loading

0 comments on commit 0dcc84c

Please sign in to comment.