コード例 #1
0
        public void WithSyntaxErrors(DiagnosticAnalyzer analyzer)
        {
            var testCode = @"
    using System;
    using System.IO;

    public class Foo : SyntaxError
    {
        private readonly Stream stream = File.SyntaxError(string.Empty);
        private bool disposed;

        protected override void Dispose(bool disposing)
        {
            if (this.syntaxError)
            {
                return;
            }

            this.disposed = true;
            if (disposing)
            {
                this.stream.Dispose();
            }

            base.Dispose(disposing);
        }
    }";

            RoslynAssert.NoAnalyzerDiagnostics(analyzer, testCode);
        }
コード例 #2
0
        public static void IgnoreNewFormShow()
        {
            var winForm = @"
namespace N
{
    using System.Windows.Forms;

    public class Winform : Form
    {
    }
}";

            var code = @"
namespace N
{
    public class C
    {
        void M()
        {
            var form = new Winform();
            form.Show();
        }
    }
}";

            RoslynAssert.NoAnalyzerDiagnostics(Analyzer, winForm, code);
        }
コード例 #3
0
        private static void Assert(string code, string descriptorName)
        {
            var analyzer   = new CommutativityAnalyzer();
            var references = new[]
            {
                MetadataReference.CreateFromFile(typeof(string).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(Console).Assembly.Location),
            };
            var allCode = CommutativeAttribute + code;

            if (descriptorName != null)
            {
                var descriptor = Descriptors.ByName(descriptorName);

                RoslynAssert.Diagnostics(
                    analyzer,
                    ExpectedDiagnostic.Create(descriptor),
                    allCode,
                    metadataReferences: references);
            }
            else
            {
                RoslynAssert.NoAnalyzerDiagnostics(analyzer, allCode);
            }
        }
コード例 #4
0
        public void NoDiagnosticWhenExpectedIsErrorTypeArray()
        {
            var testCode = TestUtility.WrapInTestMethod(@"
                var actual = new[] { 3 };
                var expected = HaveNoIdea();
                Assert.That(actual, Is.EqualTo(new[] { expected }));");

            RoslynAssert.NoAnalyzerDiagnostics(analyzer, testCode);
        }
コード例 #5
0
        public void NoDiagnosticWhenActualIsErrorType()
        {
            var testCode = TestUtility.WrapInTestMethod(@"
                var actual = HaveNoIdea();
                var expected = 3;
                Assert.That(actual, Is.EqualTo(expected));");

            RoslynAssert.NoAnalyzerDiagnostics(analyzer, testCode);
        }
コード例 #6
0
ファイル: ValidCode.cs プロジェクト: nevaran/Gu.Localization
        public void WhenValid()
        {
            this.projectFile.Directory.FindFile("Properties\\Resources.resx").ReplaceText("\"Key\"", "\"Value\"");
            this.projectFile.Directory.FindFile("Properties\\Resources.sv.resx").ReplaceText("\"Key\"", "\"Value\"");
            this.projectFile.Directory.FindFile("Properties\\Resources.sv-SE.resx").ReplaceText("\"Key\"", "\"Value\"");
            this.projectFile.Directory.FindFile("Properties\\Resources.Designer.cs").ReplaceText("public static string Key", "public static string Value");
            var sln = CodeFactory.CreateSolution(this.projectFile, MetadataReferences.FromAttributes());

            RoslynAssert.NoAnalyzerDiagnostics(Analyzer, sln);
        }
コード例 #7
0
        public static void WhenValid(string key, string value)
        {
            projectFile.Directory.FindFile("Properties\\Resources.resx").ReplaceText("\"Key\"", $"\"{key}\"");
            projectFile.Directory.FindFile("Properties\\Resources.resx").ReplaceText("<value>Value</value>", $"<value>{value}</value>");
            projectFile.Directory.FindFile("Properties\\Resources.sv.resx").ReplaceText("\"Key\"", $"\"{key}\"");
            projectFile.Directory.FindFile("Properties\\Resources.sv-SE.resx").ReplaceText("\"Key\"", $"\"{key}\"");
            projectFile.Directory.FindFile("Properties\\Resources.Designer.cs").ReplaceText("public static string Key", $"public static string {key}");
            var solution = CodeFactory.CreateSolution(projectFile, MetadataReferences.FromAttributes());

            RoslynAssert.NoAnalyzerDiagnostics(Analyzer, solution);
        }
コード例 #8
0
        public void DemoProject(DiagnosticAnalyzer analyzer)
        {
            switch (analyzer)
            {
            case LiteralAnalyzer _:
            case MemberAccessAnalyzer _:
            case ResourceAnalyzer _:
                // Just checking so that the analyzer does not throw here.
                _ = Analyze.GetDiagnostics(analyzer, DemoProjectSolution);
                break;

            default:
                RoslynAssert.NoAnalyzerDiagnostics(analyzer, DemoProjectSolution);
                break;
            }
        }
コード例 #9
0
ファイル: Valid.cs プロジェクト: hackf5/IDisposableAnalyzers
        public static void LocalAddedToFormComponents(string expression)
        {
            var code = @"
namespace N
{
    using System.IO;
    using System.Windows.Forms;

    public class Winform : Form
    {
        Winform()
        {
            var stream = File.OpenRead(string.Empty);
            // Since this is added to components, it is automatically disposed of with the form.
            this.components.Add(stream);
        }
    }
}".AssertReplace("this.components.Add(stream)", expression);

            RoslynAssert.NoAnalyzerDiagnostics(Analyzer, code);
        }
コード例 #10
0
            public static void WhenAnalyzerThrows()
            {
                var code     = @"
namespace N
{
    class C
    {
    }
}";
                var expected = "Expected no diagnostics, found:\r\n" +
                               "AD0001 Analyzer 'Gu.Roslyn.Asserts.Tests.ThrowingAnalyzer' threw an exception of type 'System.InvalidOperationException' with message 'Analyzer threw this.'.\r\n" +
                               "  at line 0 and character 0 in file  | Code did not have position 0,0\r\n";

                var exception = Assert.Throws <AssertException>(() =>
                                                                RoslynAssert.NoAnalyzerDiagnostics(new ThrowingAnalyzer(), code));

                Assert.AreEqual(expected, exception.Message);

                exception = Assert.Throws <AssertException>(() =>
                                                            RoslynAssert.NoAnalyzerDiagnostics(typeof(ThrowingAnalyzer), code));
                Assert.AreEqual(expected, exception.Message);
            }
コード例 #11
0
            public static void SingleDocumentFieldNameMustNotBeginWithUnderscore()
            {
                var code     = @"
namespace N
{
    class C
    {
        private readonly int _value = 1;
    }
}";
                var expected = "Expected no diagnostics, found:\r\n" +
                               "SA1309 Field '_value' must not begin with an underscore\r\n" +
                               "  at line 5 and character 29 in file C.cs | private readonly int ↓_value = 1;\r\n";

                var exception = Assert.Throws <AssertException>(() =>
                                                                RoslynAssert.NoAnalyzerDiagnostics(new FieldNameMustNotBeginWithUnderscore(), code));

                Assert.AreEqual(expected, exception.Message);

                exception = Assert.Throws <AssertException>(() =>
                                                            RoslynAssert.NoAnalyzerDiagnostics(typeof(FieldNameMustNotBeginWithUnderscore), code));
                Assert.AreEqual(expected, exception.Message);
            }
コード例 #12
0
        public void Analyzer_AsNoTrackingAndIdEqualsNumber_DoesNotReport()
        {
            var code = TestCode("↓context.Things.AsNoTracking().First(x => x.Id == 42);");

            RoslynAssert.NoAnalyzerDiagnostics(Analyzer, code);
        }
コード例 #13
0
        public void Analyzer_IdEqualsLambdaParameter_DoesNotReport()
        {
            var code = TestCode("↓context.Things.First(x => x.Id == x.Name.Length);");

            RoslynAssert.NoAnalyzerDiagnostics(Analyzer, code);
        }
コード例 #14
0
 public static void AnalyzersSolution(DiagnosticAnalyzer analyzer)
 {
     RoslynAssert.NoAnalyzerDiagnostics(analyzer, AnalyzersProjectSolution);
 }
コード例 #15
0
 public static void Run(DiagnosticAnalyzer analyzer)
 {
     RoslynAssert.NoAnalyzerDiagnostics(analyzer, Solution);
 }