Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code improvements #1608

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.UpgradeAssistant.Mappings.Tests;

public partial class ValidationTests
{
private static readonly string[] Kinds = new string[] { "property", "method", "namespace", "type", "xmlnamespace" };
private static readonly string[] States = new string[] { "NotImplemented", "Removed", "Replaced" };
private static readonly string[] Kinds = ["property", "method", "namespace", "type", "xmlnamespace"];
private static readonly string[] States = ["NotImplemented", "Removed", "Replaced"];

[TestMethod]
public void ValidateApiMaps()
Expand All @@ -25,7 +24,7 @@ public void ValidateApiMaps()
foreach (var path in jsonFiles)
{
var fileName = Path.GetFileName(path);

if (fileName.Equals("apimap.json", StringComparison.OrdinalIgnoreCase) || fileName.EndsWith(".apimap.json", StringComparison.OrdinalIgnoreCase))
{
AssertApiMap(options, path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.UpgradeAssistant.Mappings.Tests;
Expand Down Expand Up @@ -70,7 +69,7 @@ private static void AssertMetadataFile(JsonSerializerOptions options, string ful
else if (property.NameEquals("order"))
{
AssertPropertyType(relativePath, string.Empty, property, JsonValueKind.Number);
Assert.IsTrue(property.Value.TryGetInt32(out int order), $"Failed to parse \"{property.Name}\" property in `{relativePath}': {property}");
Assert.IsTrue(property.Value.TryGetInt32(out var order), $"Failed to parse \"{property.Name}\" property in `{relativePath}': {property}");
Assert.IsTrue(order >= 0, $"`{relativePath}' - [\"{property.Name}\"] must be greater than or equal to 0.");
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.UpgradeAssistant.Mappings.Tests;
Expand Down Expand Up @@ -37,7 +36,7 @@ private static void AssertPackageMapEntry(string relativePath, string packagePat
foreach (var framework in package.Frameworks)
{
var count = framework.Value.Count;
int index = 0;
var index = 0;

foreach (var frameworkEntry in framework.Value)
{
Expand Down Expand Up @@ -85,7 +84,7 @@ private static void AssertPackageMap(JsonSerializerOptions options, string fullP

if (config.Packages != null)
{
int index = 0;
var index = 0;

foreach (var package in config.Packages)
{
Expand Down
15 changes: 7 additions & 8 deletions tests/UpgradeAssistant.Mappings.Tests/SchemaValidationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.UpgradeAssistant.Mappings.Tests;
Expand Down Expand Up @@ -36,10 +35,10 @@ public void ValidateSchemas()
{
AssertConfigSchema(options, path);
}
else
/*else
{
//Assert.Fail($"Unknown file type: {fileName}");
}
Assert.Fail($"Unknown file type: {fileName}");
}*/
}
}

Expand All @@ -64,7 +63,7 @@ private static void AssertPropertyTypeIsBoolean(string relativePath, string elem
{
var propertyPath = GetPropertyPath(elementPath, property);

Assert.IsTrue(property.Value.ValueKind == JsonValueKind.True || property.Value.ValueKind == JsonValueKind.False, $"The {propertyPath} property in `{relativePath}' is expected to be a boolean.");
Assert.IsTrue(property.Value.ValueKind is JsonValueKind.True or JsonValueKind.False, $"The {propertyPath} property in `{relativePath}' is expected to be a boolean.");
}

private static void AssertUnknownProperty(string relativePath, string elementPath, JsonProperty property)
Expand All @@ -74,7 +73,7 @@ private static void AssertUnknownProperty(string relativePath, string elementPat

private static void AssertPackageMapEntryFramework(string relativePath, string frameworkPath, JsonElement framework)
{
int index = 0;
var index = 0;

foreach (var element in framework.EnumerateArray())
{
Expand Down Expand Up @@ -135,7 +134,7 @@ private static void AssertPackageMapEntry(string relativePath, string elementPat

private static void AssertPackageMapPackages(string relativePath, string packagesPath, JsonElement packages)
{
int index = 0;
var index = 0;

foreach (var element in packages.EnumerateArray())
{
Expand Down Expand Up @@ -224,7 +223,7 @@ private static void AssertApiMapEntry(string relativePath, string elementPath, J
AssertPropertyType(relativePath, elementPath, property, JsonValueKind.Array);

var propertyPath = GetPropertyPath(elementPath, property);
int index = 0;
var index = 0;

foreach (var paramElement in property.Value.EnumerateArray())
{
Expand Down
2 changes: 1 addition & 1 deletion tests/UpgradeAssistant.Mappings.Tests/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Microsoft.UpgradeAssistant.Mappings.Tests;

static class TestHelper
internal static class TestHelper
{
public static readonly string MappingsDir;

Expand Down
19 changes: 8 additions & 11 deletions tests/UpgradeAssistant.Mappings.Tests/TraitToken.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;

namespace Microsoft.UpgradeAssistant.Mappings.Tests;

internal class TraitToken
{
private static readonly char[] DotSeparator = new[] { '.' };
private static readonly char[] DotSeparator = ['.'];

public static string? GetPropertyName(TraitToken token, string id)
{
Expand Down Expand Up @@ -48,8 +45,8 @@ public static bool TryParse(string input, out TraitToken? token)
return false;
}

int i = 0;
int begin = i;
var i = 0;
var begin = i;

while (i < input.Length && !IsOperatorCharacter(input[i])) { i++; }

Expand All @@ -58,7 +55,7 @@ public static bool TryParse(string input, out TraitToken? token)
return false;
}

string key = input.Substring(begin, i).Trim();
var key = input.Substring(begin, i).Trim();
if (string.IsNullOrEmpty(key))
{
return false;
Expand All @@ -68,13 +65,13 @@ public static bool TryParse(string input, out TraitToken? token)

while (i < input.Length && IsOperatorCharacter(input[i])) { i++; }

string @operator = input.Substring(begin, i - begin);
var @operator = input.Substring(begin, i - begin);
if (!Operators.Contains(@operator))
{
return false;
}

string value = i < input.Length ? input.Substring(i) : string.Empty;
var value = i < input.Length ? input.Substring(i) : string.Empty;

token = new TraitToken(key, value.Trim(), @operator);

Expand Down Expand Up @@ -112,7 +109,7 @@ public string? TraitName
if (_traitName is null)
{
var parts = Key.Trim().Split(DotSeparator, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 1 || parts.Length > 2)
if (parts.Length is < 1 or > 2)
{
return null;
}
Expand All @@ -138,7 +135,7 @@ public string? PropertyName
if (_propertyName is null)
{
var parts = Key.Trim().Split(DotSeparator, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 1 || parts.Length > 2)
if (parts.Length is < 1 or > 2)
{
return null;
}
Expand Down
42 changes: 22 additions & 20 deletions tests/UpgradeAssistant.Mappings.Tests/TraitsExpressionParser.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

namespace Microsoft.UpgradeAssistant.Mappings.Tests;

internal struct TraitsExpressionParser
Expand Down Expand Up @@ -85,7 +83,7 @@ private void ValidateAndTerm()
/// </summary>
private void ValidateTerm()
{
int notCount = 0;
var notCount = 0;
while (_tokenizer.Peek() == "!")
{
_tokenizer.Next();
Expand All @@ -107,7 +105,7 @@ private void ValidateTerm()
}
else if (_tokenizer.Peek() != null && IsSymbolCharacter(_tokenizer.Peek()![0]))
{
string ident = _tokenizer.Next()!;
var ident = _tokenizer.Next()!;
}
else if (_tokenizer.Peek() != null && _tokenizer.Peek()![0] == '{')
{
Expand All @@ -122,7 +120,7 @@ private void ValidateTerm()
/// <summary>
/// Process special tokens like {key:value} where ':' should be <c>=</c>, <c>&lt;</c>, <c>&gt;</c>, <c>&lt;=</c>, <c>&gt;=</c>, <c>!=</c>.
/// </summary>
private void ProcessToken(string input)
private static void ProcessToken(string input)
{
input = input.TrimStart('{').TrimEnd('}');

Expand Down Expand Up @@ -193,7 +191,7 @@ internal Tokenizer(string input)
// the Peek() doesn't impact the token stream.
if (_peeked != null)
{
string token = _peeked;
var token = _peeked;
_peeked = null;
return token;
}
Expand All @@ -211,59 +209,63 @@ internal Tokenizer(string input)

if (IsSymbolCharacter(Input[Position]))
{
int begin = Position;
var begin = Position;
while (Position < Input.Length && IsSymbolCharacter(Input[Position]))
{
Position++;
}

int end = Position;
var end = Position;
return Input.Substring(begin, end - begin);
}
else if (Input[Position] == '&' || Input[Position] == '+') // we prefer & but also accept + so that XML manifest files don't have to write the &amp; escape sequence.

if (Input[Position] == '&' || Input[Position] == '+') // we prefer & but also accept + so that XML manifest files don't have to write the &amp; escape sequence.
{
Position++;
return "&"; // always return '&' to simplify the parser logic by consolidating on only one of the two possible operators.
}
else if (Input[Position] == '|')

if (Input[Position] == '|')
{
Position++;
return "|";
}
else if (Input[Position] == '(')

if (Input[Position] == '(')
{
Position++;
return "(";
}
else if (Input[Position] == ')')

if (Input[Position] == ')')
{
Position++;
return ")";
}
else if (Input[Position] == '!')

if (Input[Position] == '!')
{
Position++;
return "!";
}
else if (Input[Position] == '{')

if (Input[Position] == '{')
{
// read special tokens like {xxx:vvv}
int begin = Position;
var begin = Position;
while (Position < Input.Length && Input[Position] != '}')
{
Position++;
}

int end = Position;
var end = Position;

Position++;

return Input.Substring(begin, end - begin);
}
else
{
throw new TraitsExpressionSyntaxException(string.Format(InvalidTraitExpression, Position, Input));
}

throw new TraitsExpressionSyntaxException(string.Format(InvalidTraitExpression, Position, Input));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

namespace Microsoft.UpgradeAssistant.Mappings.Tests;

internal class TraitsExpressionSyntaxException : FormatException
Expand Down