Skip to content

Commit

Permalink
Use .NET 8 for unit tests so GitHub builds work
Browse files Browse the repository at this point in the history
Update code to use C# 12 collection expressions. Fix warnings from other .NET 8 code analyzers.
  • Loading branch information
menees committed Jan 14, 2024
1 parent a572a6b commit a401dd6
Show file tree
Hide file tree
Showing 27 changed files with 244 additions and 257 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ jobs:

steps:
- uses: actions/checkout@v2

- name: Add MSBuild to PATH
uses: microsoft/setup-msbuild@v1

- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: '3.1.x'

- name: Build everything
run: |
Expand Down
6 changes: 3 additions & 3 deletions src/Menees.Analyzers/Men003MethodTooLong.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public sealed class Men003MethodTooLong : Analyzer
private static readonly DiagnosticDescriptor Rule =
new(DiagnosticId, Title, MessageFormat, Rules.Layout, DiagnosticSeverity.Warning, Rules.EnabledByDefault, Description);

private static readonly HashSet<SyntaxKind> SupportedSyntaxKinds = new()
{
private static readonly HashSet<SyntaxKind> SupportedSyntaxKinds =
[
SyntaxKind.MethodDeclaration,
SyntaxKind.ConstructorDeclaration,
SyntaxKind.DestructorDeclaration,
SyntaxKind.ConversionOperatorDeclaration,
SyntaxKind.OperatorDeclaration,
};
];

#endregion

Expand Down
6 changes: 3 additions & 3 deletions src/Menees.Analyzers/Men004PropertyAccessorTooLong.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public sealed class Men004PropertyAccessorTooLong : Analyzer
private static readonly DiagnosticDescriptor Rule =
new(DiagnosticId, Title, MessageFormat, Rules.Layout, DiagnosticSeverity.Warning, Rules.EnabledByDefault, Description);

private static readonly HashSet<SyntaxKind> SupportedSyntaxKinds = new()
{
private static readonly HashSet<SyntaxKind> SupportedSyntaxKinds =
[
SyntaxKind.GetAccessorDeclaration,
SyntaxKind.SetAccessorDeclaration,
SyntaxKind.AddAccessorDeclaration,
SyntaxKind.RemoveAccessorDeclaration,
};
];

#endregion

Expand Down
12 changes: 6 additions & 6 deletions src/Menees.Analyzers/Men006RegionsShouldBeUsed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,24 @@ public sealed class Men006RegionsShouldBeUsed : Analyzer
private static readonly DiagnosticDescriptor Rule =
new(DiagnosticId, Title, MessageFormat, Rules.Layout, Rules.InfoSeverity, Rules.DisabledByDefault, Description);

private static readonly HashSet<SyntaxKind> SupportedTypeDeclarationKinds = new()
{
private static readonly HashSet<SyntaxKind> SupportedTypeDeclarationKinds =
[
SyntaxKind.ClassDeclaration,
SyntaxKind.StructDeclaration,
SyntaxKind.InterfaceDeclaration,
SyntaxKind.EnumDeclaration,
SyntaxKind.RecordDeclaration,

// Note: We don't care about SyntaxKind.DelegateDeclaration because those will usually be one-liners.
};
];

private static readonly HashSet<string> DesignerGeneratedRegions = new()
{
private static readonly HashSet<string> DesignerGeneratedRegions =
[
// VS 2002/3 put the designer-generated code in the main file (since partial classes didn't exist until VS 2005).
// We'll ignore those designer-generated regions.
"Windows Form Designer generated code",
"Component Designer generated code",
};
];

#endregion

Expand Down
6 changes: 3 additions & 3 deletions src/Menees.Analyzers/Men008FileNameShouldMatchType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public sealed class Men008FileNameShouldMatchType : Analyzer
private static readonly DiagnosticDescriptor Rule =
new(DiagnosticId, Title, MessageFormat, Rules.Naming, DiagnosticSeverity.Warning, Rules.EnabledByDefault, Description);

private static readonly HashSet<SyntaxKind> SupportedTypeDeclarationKinds = new()
{
private static readonly HashSet<SyntaxKind> SupportedTypeDeclarationKinds =
[
SyntaxKind.ClassDeclaration,
SyntaxKind.StructDeclaration,
SyntaxKind.InterfaceDeclaration,
SyntaxKind.EnumDeclaration,
SyntaxKind.DelegateDeclaration,
SyntaxKind.RecordDeclaration,
};
];

#endregion

Expand Down
2 changes: 1 addition & 1 deletion src/Menees.Analyzers/Men012FlagsShouldBePowersOfTwo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public sealed class Men012FlagsShouldBePowersOfTwo : Analyzer
private static readonly DiagnosticDescriptor RuleNoValue =
new(DiagnosticId, Title, MessageFormatNoValue, Rules.Layout, DiagnosticSeverity.Warning, Rules.EnabledByDefault, Description);

private static readonly HashSet<string> FlagsAttributeNames = new() { "Flags", "FlagsAttribute" };
private static readonly HashSet<string> FlagsAttributeNames = ["Flags", "FlagsAttribute"];

#endregion

Expand Down
12 changes: 6 additions & 6 deletions src/Menees.Analyzers/Men015UsePreferredTerms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public sealed class Men015UsePreferredTerms : Analyzer
private static readonly DiagnosticDescriptor Rule =
new(DiagnosticId, Title, MessageFormat, Rules.Naming, Rules.InfoSeverity, Rules.EnabledByDefault, Description);

private static readonly HashSet<SyntaxKind> SimpleIdentifierDeclarationKinds = new()
{
private static readonly HashSet<SyntaxKind> SimpleIdentifierDeclarationKinds =
[
SyntaxKind.ClassDeclaration,
SyntaxKind.StructDeclaration,
SyntaxKind.InterfaceDeclaration,
Expand All @@ -43,13 +43,13 @@ public sealed class Men015UsePreferredTerms : Analyzer
SyntaxKind.TypeParameter,
SyntaxKind.ForEachStatement,
SyntaxKind.EnumMemberDeclaration,
};
];

private static readonly HashSet<SyntaxKind> OverrideIdentifierDeclarationKinds = new()
{
private static readonly HashSet<SyntaxKind> OverrideIdentifierDeclarationKinds =
[
SyntaxKind.MethodDeclaration,
SyntaxKind.PropertyDeclaration,
};
];

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private static Document[] GetDocuments(string[] sources, string language)
/// <returns>A Document created from the source string</returns>
protected static Document CreateDocument(string source, string language = LanguageNames.CSharp)
{
return CreateProject(new[] { source }, language).Documents.First();
return CreateProject([source], language).Documents.First();
}

/// <summary>
Expand Down
16 changes: 8 additions & 8 deletions tests/Menees.Analyzers.Test/Men001UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ class TypeName
}
}";
var analyzer = this.CSharpDiagnosticAnalyzer;
DiagnosticResult[] expected = new[]
{
new DiagnosticResult(analyzer) { Locations = new[] { new DiagnosticResultLocation("Test0.cs", 1, 1) } },
new DiagnosticResult(analyzer) { Locations = new[] { new DiagnosticResultLocation("Test0.cs", 5, 1) } },
new DiagnosticResult(analyzer) { Locations = new[] { new DiagnosticResultLocation("Test0.cs", 10, 1) } },
new DiagnosticResult(analyzer) { Locations = new[] { new DiagnosticResultLocation("Test0.cs", 12, 1) } },
new DiagnosticResult(analyzer) { Locations = new[] { new DiagnosticResultLocation("Test0.cs", 13, 1) } },
};
DiagnosticResult[] expected =
[
new DiagnosticResult(analyzer) { Locations = [new DiagnosticResultLocation("Test0.cs", 1, 1)] },
new DiagnosticResult(analyzer) { Locations = [new DiagnosticResultLocation("Test0.cs", 5, 1)] },
new DiagnosticResult(analyzer) { Locations = [new DiagnosticResultLocation("Test0.cs", 10, 1)] },
new DiagnosticResult(analyzer) { Locations = [new DiagnosticResultLocation("Test0.cs", 12, 1)] },
new DiagnosticResult(analyzer) { Locations = [new DiagnosticResultLocation("Test0.cs", 13, 1)] },
];

this.VerifyCSharpDiagnostic(test, expected);

Expand Down
16 changes: 8 additions & 8 deletions tests/Menees.Analyzers.Test/Men002UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,38 +77,38 @@ public TypeName() // This line is also much too long.
}
}";
var analyzer = this.CSharpDiagnosticAnalyzer;
DiagnosticResult[] expected = new[]
{
DiagnosticResult[] expected =
[
new DiagnosticResult(analyzer)
{
Message = "Line must be no longer than 40 characters (now 83).",
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 5, 38) }
Locations = [new DiagnosticResultLocation("Test0.cs", 5, 38)]
},
new DiagnosticResult(analyzer)
{
Message = "Line must be no longer than 40 characters (now 73).",
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 12, 35) }
Locations = [new DiagnosticResultLocation("Test0.cs", 12, 35)]
},
new DiagnosticResult(analyzer)
{
Message = "Line must be no longer than 40 characters (now 61).",
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 13, 35) }
Locations = [new DiagnosticResultLocation("Test0.cs", 13, 35)]
},
#if DEBUG // MEN002A is disabled by default, so it won't run in release build unit tests.
new DiagnosticResult(analyzer)
{
Id = Men002LineTooLong.DiagnosticIdNotify,
Severity = DiagnosticSeverity.Info,
Message = "Line is over 35 characters (now 37).",
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 15, 27) },
Locations = [new DiagnosticResultLocation("Test0.cs", 15, 27)],
},
#endif
new DiagnosticResult(analyzer)
{
Message = "Line must be no longer than 40 characters (now 149).",
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 16, 32) }
Locations = [new DiagnosticResultLocation("Test0.cs", 16, 32)]
},
};
];

this.VerifyCSharpDiagnostic(test, expected);
}
Expand Down
22 changes: 11 additions & 11 deletions tests/Menees.Analyzers.Test/Men003UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,49 +143,49 @@ public Testing Create()
public void InvalidCodeTest()
{
var analyzer = this.CSharpDiagnosticAnalyzer;
DiagnosticResult[] expected = new[]
{
DiagnosticResult[] expected =
[
new DiagnosticResult(analyzer)
{
Message = "Constructor Testing must be no longer than 5 lines (now 7).",
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 4, 2) }
Locations = [new DiagnosticResultLocation("Test0.cs", 4, 2)]
},
new DiagnosticResult(analyzer)
{
Message = "Method Create must be no longer than 5 lines (now 7).",
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 12, 2) }
Locations = [new DiagnosticResultLocation("Test0.cs", 12, 2)]
},
new DiagnosticResult(analyzer)
{
Message = "Static constructor Testing must be no longer than 5 lines (now 7).",
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 25, 2) }
Locations = [new DiagnosticResultLocation("Test0.cs", 25, 2)]
},
new DiagnosticResult(analyzer)
{
Message = "Destructor ~Testing must be no longer than 5 lines (now 7).",
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 33, 2) }
Locations = [new DiagnosticResultLocation("Test0.cs", 33, 2)]
},
new DiagnosticResult(analyzer)
{
Message = "Operator Explicit must be no longer than 5 lines (now 8).",
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 54, 2) }
Locations = [new DiagnosticResultLocation("Test0.cs", 54, 2)]
},
new DiagnosticResult(analyzer)
{
Message = "Operator Implicit must be no longer than 5 lines (now 8).",
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 63, 2) }
Locations = [new DiagnosticResultLocation("Test0.cs", 63, 2)]
},
new DiagnosticResult(analyzer)
{
Message = "Operator Equality must be no longer than 5 lines (now 8).",
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 72, 2) }
Locations = [new DiagnosticResultLocation("Test0.cs", 72, 2)]
},
new DiagnosticResult(analyzer)
{
Message = "Operator Inequality must be no longer than 5 lines (now 8).",
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 81, 2) }
Locations = [new DiagnosticResultLocation("Test0.cs", 81, 2)]
},
};
];

this.VerifyCSharpDiagnostic(InvalidCode, expected);
}
Expand Down
16 changes: 8 additions & 8 deletions tests/Menees.Analyzers.Test/Men004UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,34 +138,34 @@ public Testing Create()
}
}";
var analyzer = this.CSharpDiagnosticAnalyzer;
DiagnosticResult[] expected = new[]
{
DiagnosticResult[] expected =
[
new DiagnosticResult(analyzer)
{
Message = "Property Now get accessor must be no longer than 5 lines (now 8).",
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 6, 3) }
Locations = [new DiagnosticResultLocation("Test0.cs", 6, 3)]
},
new DiagnosticResult(analyzer)
{
Message = "Property Now set accessor must be no longer than 5 lines (now 7).",
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 15, 3) }
Locations = [new DiagnosticResultLocation("Test0.cs", 15, 3)]
},
new DiagnosticResult(analyzer)
{
Message = "Event Changed add accessor must be no longer than 5 lines (now 7).",
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 26, 3) }
Locations = [new DiagnosticResultLocation("Test0.cs", 26, 3)]
},
new DiagnosticResult(analyzer)
{
Message = "Event Changed remove accessor must be no longer than 5 lines (now 7).",
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 34, 3) }
Locations = [new DiagnosticResultLocation("Test0.cs", 34, 3)]
},
new DiagnosticResult(analyzer)
{
Message = "Property Item get accessor must be no longer than 5 lines (now 8).",
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 45, 3) }
Locations = [new DiagnosticResultLocation("Test0.cs", 45, 3)]
},
};
];

this.VerifyCSharpDiagnostic(test, expected);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Menees.Analyzers.Test/Men005UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ public TypeName()
}
}";
var analyzer = this.CSharpDiagnosticAnalyzer;
DiagnosticResult[] expected = new[]
{
DiagnosticResult[] expected =
[
new DiagnosticResult(analyzer)
{
Message = "File Test0.cs must be no longer than 20 lines (now 21).",
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 21, 1) }
Locations = [new DiagnosticResultLocation("Test0.cs", 21, 1)]
},
};
];

this.VerifyCSharpDiagnostic(test, expected);
}
Expand Down
Loading

0 comments on commit a401dd6

Please sign in to comment.