Beispiel #1
0
            public void BinarySolution()
            {
                var binaryReferencedCode = @"
namespace BinaryReferencedAssembly
{
    public class Base
    {
        private int _fieldName;
    }
}";
                var code     = @"
namespace RoslynSandbox
{
    using System.Reflection;

    public class C : BinaryReferencedAssembly.Base
    {
        private int f;
    }
}";
                var analyzer = new FieldNameMustNotBeginWithUnderscore();
                var solution = CodeFactory.CreateSolution(
                    code,
                    CodeFactory.DefaultCompilationOptions(new[] { analyzer }),
                    AnalyzerAssert.MetadataReferences.Append(Asserts.MetadataReferences.CreateBinary(binaryReferencedCode)));

                AnalyzerAssert.Valid(analyzer, solution);
            }
Beispiel #2
0
        public static void SingleDocumentTwoErrors()
        {
            var code = @"
namespace N
{
    class C
    {
        private readonly int _value1;
        private readonly int _value2;
    }
}";

            var after              = @"
namespace N
{
    class C
    {
        private readonly int value1;
        private readonly int value2;
    }
}";
            var analyzer           = new FieldNameMustNotBeginWithUnderscore();
            var compilationOptions = CodeFactory.DefaultCompilationOptions(analyzer);
            var metadataReferences = new[] { MetadataReference.CreateFromFile(typeof(int).Assembly.Location) };
            var sln         = CodeFactory.CreateSolution(code, compilationOptions, metadataReferences);
            var diagnostics = Analyze.GetDiagnostics(sln, analyzer);
            var fixedSln    = Fix.Apply(sln, new DoNotUseUnderscoreFix(), diagnostics);

            CodeAssert.AreEqual(after, fixedSln.Projects.Single().Documents.Single());
        }
Beispiel #3
0
        public void SingleClassOneErrorCorrectFixAll()
        {
            var code = @"
namespace RoslynSandbox
{
    class Foo
    {
        private readonly int _value;
    }
}";

            var fixedCode = @"
namespace RoslynSandbox
{
    class Foo
    {
        private readonly int value;
    }
}";
            var analyzer  = new FieldNameMustNotBeginWithUnderscore();
            var cSharpCompilationOptions = CodeFactory.DefaultCompilationOptions(analyzer);
            var metadataReferences       = new[] { MetadataReference.CreateFromFile(typeof(int).Assembly.Location) };
            var sln         = CodeFactory.CreateSolution(code, cSharpCompilationOptions, metadataReferences);
            var diagnostics = Analyze.GetDiagnostics(sln, analyzer);
            var fixedSln    = Fix.Apply(sln, new DontUseUnderscoreCodeFixProvider(), diagnostics);

            CodeAssert.AreEqual(fixedCode, fixedSln.Projects.Single().Documents.Single());
        }
Beispiel #4
0
        public static async Task SingleDocumentOneError()
        {
            var code = @"
namespace N
{
    class C
    {
        private readonly int _value;
    }
}";

            var after    = @"
namespace N
{
    class C
    {
        private readonly int value;
    }
}";
            var analyzer = new FieldNameMustNotBeginWithUnderscore();
            var cSharpCompilationOptions = CodeFactory.DefaultCompilationOptions(analyzer);
            var metadataReferences       = new[] { MetadataReference.CreateFromFile(typeof(int).Assembly.Location) };
            var sln        = CodeFactory.CreateSolution(code, cSharpCompilationOptions, metadataReferences);
            var diagnostic = Analyze.GetDiagnostics(sln, analyzer).SelectMany(x => x).Single();
            var fixedSln   = Fix.Apply(sln, new DoNotUseUnderscoreFix(), diagnostic);

            CodeAssert.AreEqual(after, fixedSln.Projects.Single().Documents.Single());

            fixedSln = await Fix.ApplyAsync(sln, new DoNotUseUnderscoreFix(), diagnostic).ConfigureAwait(false);

            CodeAssert.AreEqual(after, fixedSln.Projects.Single().Documents.Single());
        }
Beispiel #5
0
        public static async Task Project()
        {
            var analyzer  = new FieldNameMustNotBeginWithUnderscore();
            var sln       = CodeFactory.CreateSolution(ProjectFile.Find("Gu.Roslyn.Asserts.csproj"));
            var benchmark = await Benchmark.CreateAsync(sln.Projects.Single(), analyzer).ConfigureAwait(false);

            CollectionAssert.IsNotEmpty(benchmark.SyntaxNodeActions);
            Assert.AreSame(analyzer, benchmark.Analyzer);
            benchmark.Run();
            benchmark.Run();
        }
        public async Task Project()
        {
            var analyzer = new FieldNameMustNotBeginWithUnderscore();
            var sln      = CodeFactory.CreateSolution(
                CodeFactory.FindProjectFile("Gu.Roslyn.Asserts.csproj"),
                MetadataReferences.Transitive(typeof(Benchmark).Assembly).ToArray());
            var benchmark = await Benchmark.CreateAsync(sln.Projects.Single(), analyzer).ConfigureAwait(false);

            CollectionAssert.IsNotEmpty(benchmark.SyntaxNodeActions);
            Assert.AreSame(analyzer, benchmark.Analyzer);
            benchmark.Run();
            benchmark.Run();
        }
Beispiel #7
0
        public static async Task Solution()
        {
            var analyzer  = new FieldNameMustNotBeginWithUnderscore();
            var sln       = CodeFactory.CreateSolution(SolutionFile.Find("Gu.Roslyn.Asserts.sln"));
            var benchmark = await Benchmark.CreateAsync(sln, analyzer).ConfigureAwait(false);

            CollectionAssert.IsNotEmpty(benchmark.SyntaxNodeActions);
            CollectionAssert.AllItemsAreInstancesOfType(benchmark.SyntaxNodeActions.Select(x => x.Context.Node), typeof(FieldDeclarationSyntax));
            CollectionAssert.AllItemsAreInstancesOfType(benchmark.SyntaxNodeActions.Select(x => x.Context.ContainingSymbol), typeof(IFieldSymbol));
            Assert.AreSame(analyzer, benchmark.Analyzer);
            benchmark.Run();
            benchmark.Run();
        }
        public async Task ClassLibrary1FieldNameMustNotBeginWithUnderscore()
        {
            var analyzer  = new FieldNameMustNotBeginWithUnderscore();
            var benchmark = await Benchmark.CreateAsync(SolutionWithClassLibrary1, analyzer).ConfigureAwait(false);

            var expected = new[] { "private int _value;" };

            CollectionAssert.AreEqual(expected, benchmark.SyntaxNodeActions.Select(x => x.Context.Node.ToString()));

            expected = new[] { "ClassLibrary1.ClassLibrary1Class1._value" };
            CollectionAssert.AreEqual(expected, benchmark.SyntaxNodeActions.Select(x => x.Context.ContainingSymbol.ToString()));
            Assert.AreSame(analyzer, benchmark.Analyzer);
            benchmark.Run();
            benchmark.Run();
        }
Beispiel #9
0
            public void OneErrorWithExpectedDiagnosticsWithMessageAndErrorIndicatedInCode()
            {
                var code = @"
namespace RoslynSandbox
{
    class Foo
    {
        private readonly int ↓_value1;
    }
}";

                var expectedDiagnostic = ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated("SA1309", "Field '_value1' must not begin with an underscore", code, out code);
                var analyzer           = new FieldNameMustNotBeginWithUnderscore();

                AnalyzerAssert.Diagnostics <FieldNameMustNotBeginWithUnderscore>(new[] { expectedDiagnostic }, code);
                AnalyzerAssert.Diagnostics(analyzer.GetType(), new[] { expectedDiagnostic }, code);
                AnalyzerAssert.Diagnostics(analyzer, new[] { expectedDiagnostic }, code);
            }
Beispiel #10
0
        public static async Task GetDiagnosticsAsyncSolution()
        {
            var solutionFile = SolutionFile.Find("Gu.Roslyn.Asserts.sln");
            var expected     = new[]
            {
                "ClassLibrary1Class1.cs(6,21): warning SA1309: Field '_value' must not begin with an underscore",
                "ClassLibrary2Class1.cs(6,21): warning SA1309: Field '_value' must not begin with an underscore",
            };

            var sln         = CodeFactory.CreateSolution(solutionFile, MetadataReferences);
            var analyzer    = new FieldNameMustNotBeginWithUnderscore();
            var diagnostics = await Analyze.GetDiagnosticsAsync(sln, analyzer).ConfigureAwait(false);

            CollectionAssert.AreEquivalent(expected, diagnostics.SelectMany(x => x).Select(SkipDirectory));

            diagnostics = Analyze.GetDiagnostics(analyzer, sln);
            CollectionAssert.AreEquivalent(expected, diagnostics.SelectMany(x => x).Select(SkipDirectory));
        }
Beispiel #11
0
            public void WithExpectedDiagnostic()
            {
                var code       = @"
namespace RoslynSandbox
{
    class Foo
    {
        private readonly int value1;
    }
}";
                var analyzer   = new FieldNameMustNotBeginWithUnderscore();
                var descriptor = FieldNameMustNotBeginWithUnderscore.Descriptor;

                AnalyzerAssert.Valid <FieldNameMustNotBeginWithUnderscore>(descriptor, code);
                AnalyzerAssert.Valid(typeof(FieldNameMustNotBeginWithUnderscore), descriptor, code);
                AnalyzerAssert.Valid(analyzer, descriptor, code);
                AnalyzerAssert.Valid <FieldNameMustNotBeginWithUnderscore>(new[] { descriptor }, code);
                AnalyzerAssert.Valid(typeof(FieldNameMustNotBeginWithUnderscore), new[] { descriptor }, code);
                AnalyzerAssert.Valid(analyzer, new[] { descriptor }, code);
                AnalyzerAssert.Valid(analyzer, descriptor, new List <string> {
                    code
                });
            }
Beispiel #12
0
            public void OneErrorIndicatedPosition()
            {
                var code               = @"
namespace RoslynSandbox
{
    class Foo
    {
        private readonly int ↓_value;
    }
}";
                var analyzer           = new FieldNameMustNotBeginWithUnderscore();
                var expectedDiagnostic = ExpectedDiagnostic.Create(FieldNameMustNotBeginWithUnderscore.DiagnosticId);

                AnalyzerAssert.Diagnostics <FieldNameMustNotBeginWithUnderscore>(code);
                AnalyzerAssert.Diagnostics(typeof(FieldNameMustNotBeginWithUnderscore), code);
                AnalyzerAssert.Diagnostics(analyzer, code);
                AnalyzerAssert.Diagnostics(analyzer, code, CodeFactory.DefaultCompilationOptions(analyzer, expectedDiagnostic, AnalyzerAssert.SuppressedDiagnostics), AnalyzerAssert.MetadataReferences);
                AnalyzerAssert.Diagnostics(analyzer, new[] { code }, CodeFactory.DefaultCompilationOptions(analyzer, expectedDiagnostic, AnalyzerAssert.SuppressedDiagnostics), AnalyzerAssert.MetadataReferences);

                AnalyzerAssert.Diagnostics <FieldNameMustNotBeginWithUnderscore>(expectedDiagnostic, code);
                AnalyzerAssert.Diagnostics(typeof(FieldNameMustNotBeginWithUnderscore), expectedDiagnostic, code);
                AnalyzerAssert.Diagnostics(analyzer, expectedDiagnostic, code);
            }
Beispiel #13
0
            public void SingleClassOneErrorNoFix()
            {
                var code               = @"
namespace RoslynSandbox
{
    class Foo
    {
        private readonly int ↓_value;
    }
}";
                var analyzer           = new FieldNameMustNotBeginWithUnderscore();
                var expectedDiagnostic = ExpectedDiagnostic.Create(FieldNameMustNotBeginWithUnderscore.DiagnosticId);

                AnalyzerAssert.NoFix <FieldNameMustNotBeginWithUnderscore, NoCodeFixProvider>(code);
                AnalyzerAssert.NoFix <FieldNameMustNotBeginWithUnderscore, NoCodeFixProvider>(expectedDiagnostic, code);
                AnalyzerAssert.NoFix <FieldNameMustNotBeginWithUnderscore, NoCodeFixProvider>((IReadOnlyList <string>) new[] { code });
                AnalyzerAssert.NoFix(analyzer, new NoCodeFixProvider(), code);
                AnalyzerAssert.NoFix(analyzer, new NoCodeFixProvider(), expectedDiagnostic, code);
                AnalyzerAssert.NoFix(analyzer, new NoCodeFixProvider(), expectedDiagnostic, new List <string> {
                    code
                });
                AnalyzerAssert.NoFix(analyzer, new NoCodeFixProvider(), new[] { code }, CodeFactory.DefaultCompilationOptions(analyzer, expectedDiagnostic, AnalyzerAssert.SuppressedDiagnostics), AnalyzerAssert.MetadataReferences);
            }
            public void ProjectFromDisk()
            {
                var fixedCode          = @"// ReSharper disable InconsistentNaming
// ReSharper disable ArrangeThisQualifier
// ReSharper disable NotAccessedField.Local
#pragma warning disable IDE0009 // Member access should be qualified.
#pragma warning disable IDE0044 // Add readonly modifier
namespace ClassLibrary1
{
    public class ClassLibrary1Class1
    {
        private int value;

        public ClassLibrary1Class1(int value)
        {
            this.value = value;
        }
    }
}
";
                var csproj             = ProjectFile.Find("ClassLibrary1.csproj");
                var analyzer           = new FieldNameMustNotBeginWithUnderscore();
                var expectedDiagnostic = ExpectedDiagnostic.Create(
                    FieldNameMustNotBeginWithUnderscore.DiagnosticId,
                    "Field '_value' must not begin with an underscore",
                    Path.Combine(csproj.DirectoryName, "ClassLibrary1Class1.cs"),
                    9,
                    20);
                var sln = CodeFactory.CreateSolution(
                    csproj,
                    analyzer,
                    expectedDiagnostic,
                    metadataReferences: new[] { MetadataReference.CreateFromFile(typeof(int).Assembly.Location) });

                AnalyzerAssert.CodeFix(analyzer, new DontUseUnderscoreCodeFixProvider(), expectedDiagnostic, sln, fixedCode);
            }