-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40f1b5d
commit 56ac87f
Showing
12 changed files
with
191 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// This file is used by Code Analysis to maintain SuppressMessage | ||
// attributes that are applied to this project. | ||
// Project-level suppressions either have no target or are given | ||
// a specific target and scoped to a namespace, type, member, etc. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
|
||
[assembly: SuppressMessage("Style", "IDE0057:Use range operator", Justification = "Multiple target frameworks without support for range operator", Scope = "namespaceanddescendants", Target = "~N:Nemesis.TextParsers.CodeGen.Tests")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,89 @@ | ||
extern alias original; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Numerics; | ||
using System.Reflection; | ||
using System.Text.RegularExpressions; | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
using Nemesis.CodeAnalysis; | ||
using Nemesis.TextParsers.CodeGen.Deconstructable; | ||
using NUnit.Framework; | ||
|
||
namespace Nemesis.TextParsers.CodeGen.Tests | ||
namespace Nemesis.TextParsers.CodeGen.Tests; | ||
|
||
internal static class Utils | ||
{ | ||
internal static class Utils | ||
public static Compilation CreateCompilation(string source, OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary) | ||
{ | ||
public static Compilation CreateCompilation(string source, OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary) | ||
{ | ||
var (compilation, _, _) = CompilationUtils.CreateTestCompilation(source, new[] | ||
{ | ||
typeof(BigInteger).GetTypeInfo().Assembly, | ||
typeof(original::Nemesis.TextParsers.ITransformer).GetTypeInfo().Assembly | ||
}, outputKind); | ||
var (compilation, _, _) = CompilationUtils.CreateTestCompilation(source, new[] | ||
{ | ||
typeof(BigInteger).GetTypeInfo().Assembly, | ||
typeof(original::Nemesis.TextParsers.ITransformer).GetTypeInfo().Assembly | ||
}, outputKind); | ||
|
||
return compilation; | ||
} | ||
return compilation; | ||
} | ||
|
||
|
||
private static readonly Regex _headerPattern = new(@"/\*\s*<auto-generated> .+? </auto-generated>\s*\*/", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled); | ||
private static readonly Regex _generatorPattern = new(@""".*Generator""\s*,\s*""([0-9.]+)""", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled); | ||
private static readonly Regex _headerPattern = new(@"/\*\s*<auto-generated> .+? </auto-generated>\s*\*/", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled); | ||
private static readonly Regex _generatorPattern = new(@""".*Generator""\s*,\s*""([0-9.]+)""", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled); | ||
|
||
public static string ScrubGeneratorComments(string text) | ||
{ | ||
text = _generatorPattern.Replace(text, "string.Empty, string.Empty"); | ||
text = _headerPattern.Replace(text, "//HEAD"); | ||
public static string ScrubGeneratorComments(string text) | ||
{ | ||
text = _generatorPattern.Replace(text, "string.Empty, string.Empty"); | ||
text = _headerPattern.Replace(text, "//HEAD"); | ||
|
||
return text; | ||
} | ||
|
||
public static IReadOnlyList<string> GetGeneratedTreesOnly(Compilation compilation, int requiredCardinality = 1) | ||
{ | ||
var newComp = CompilationUtils.RunGenerators(compilation, out var diagnostics, new AutoDeconstructableGenerator()); | ||
Assert.That(diagnostics, Is.Empty); | ||
|
||
return text; | ||
SyntaxTree attributeTree = null; | ||
foreach (var tree in newComp.SyntaxTrees) | ||
{ | ||
var attributeDeclaration = tree.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>() | ||
.FirstOrDefault(cds => string.Equals(cds.Identifier.ValueText, AutoDeconstructableGenerator.ATTRIBUTE_NAME, StringComparison.Ordinal)); | ||
if (attributeDeclaration != null) | ||
{ | ||
attributeTree = tree; | ||
break; | ||
} | ||
} | ||
Assert.That(attributeTree, Is.Not.Null, "Auto attribute not found among generated trees"); | ||
|
||
var toRemove = compilation.SyntaxTrees.Append(attributeTree); | ||
|
||
var generatedTrees = newComp.RemoveSyntaxTrees(toRemove).SyntaxTrees.ToList(); | ||
Assert.That(generatedTrees, Has.Count.EqualTo(requiredCardinality)); | ||
|
||
return generatedTrees.Select(tree => | ||
((CompilationUnitSyntax)tree.GetRoot()) | ||
.ToFullString()).ToList(); | ||
} | ||
} | ||
|
||
|
||
internal class IgnoreNewLinesComparer : IComparer<string>, IEqualityComparer<string> | ||
{ | ||
public static readonly IComparer<string> Comparer = new IgnoreNewLinesComparer(); | ||
internal class IgnoreNewLinesComparer : IComparer<string>, IEqualityComparer<string> | ||
{ | ||
public static readonly IComparer<string> Comparer = new IgnoreNewLinesComparer(); | ||
|
||
public static readonly IEqualityComparer<string> EqualityComparer = new IgnoreNewLinesComparer(); | ||
public static readonly IEqualityComparer<string> EqualityComparer = new IgnoreNewLinesComparer(); | ||
|
||
public int Compare(string x, string y) => string.CompareOrdinal(NormalizeNewLines(x), NormalizeNewLines(y)); | ||
public int Compare(string x, string y) => string.CompareOrdinal(NormalizeNewLines(x), NormalizeNewLines(y)); | ||
|
||
public bool Equals(string x, string y) => NormalizeNewLines(x) == NormalizeNewLines(y); | ||
public bool Equals(string x, string y) => NormalizeNewLines(x) == NormalizeNewLines(y); | ||
|
||
public int GetHashCode(string s) => NormalizeNewLines(s)?.GetHashCode() ?? 0; | ||
public int GetHashCode(string s) => NormalizeNewLines(s)?.GetHashCode() ?? 0; | ||
|
||
public static string NormalizeNewLines(string s) => s? | ||
.Replace(Environment.NewLine, "") | ||
.Replace("\n", "") | ||
.Replace("\r", ""); | ||
} | ||
public static string NormalizeNewLines(string s) => s? | ||
.Replace(Environment.NewLine, "") | ||
.Replace("\n", "") | ||
.Replace("\r", ""); | ||
} |
Oops, something went wrong.