示例#1
0
            public static void WhenNoAnalyzerDiagnosticsButCompilerError()
            {
                var code = @"
namespace N
{
    class C
    {
        private readonly int ↓value = SYNTAX_ERROR;
    }
}";

                var analyzer           = new FieldNameMustNotBeginWithUnderscore();
                var fix                = new CodeFixes.NoFix();
                var expectedDiagnostic = ExpectedDiagnostic.Create(FieldNameMustNotBeginWithUnderscore.DiagnosticId);
                var expected           = "Expected and actual diagnostics do not match.\r\n" +
                                         "Expected:\r\n" +
                                         "  SA1309 \r\n" +
                                         "    at line 5 and character 29 in file C.cs | private readonly int ↓value = SYNTAX_ERROR;\r\n" +
                                         "Actual:\r\n" +
                                         "  CS0103 The name 'SYNTAX_ERROR' does not exist in the current context\r\n" +
                                         "    at line 5 and character 37 in file C.cs | private readonly int value = ↓SYNTAX_ERROR;\r\n";

                var exception = Assert.Throws <AssertException>(() => RoslynAssert.NoFix(analyzer, fix, code));

                Assert.AreEqual(expected, exception.Message);

                exception = Assert.Throws <AssertException>(() => RoslynAssert.NoFix(analyzer, fix, code, string.Empty));
                Assert.AreEqual(expected, exception.Message);

                exception = Assert.Throws <AssertException>(() => RoslynAssert.NoFix(analyzer, fix, expectedDiagnostic, code));
                Assert.AreEqual(expected, exception.Message);

                exception = Assert.Throws <AssertException>(() => RoslynAssert.NoFix(analyzer, fix, expectedDiagnostic, code, string.Empty));
                Assert.AreEqual(expected, exception.Message);
            }
示例#2
0
            public static void TwoDocumentsTwoErrors()
            {
                var c1        = @"
namespace N
{
    class C1
    {
        private readonly int _value1 = 1;
    }
}";
                var c2        = @"
namespace N
{
    class C2
    {
        private readonly int _value2 = 2;
    }
}";
                var analyzer  = new FieldNameMustNotBeginWithUnderscore();
                var exception = Assert.Throws <AssertException>(() => RoslynAssert.Valid(analyzer, c1, c2));

                StringAssert.Contains("SA1309 Field '_value1' must not begin with an underscore", exception.Message);
                StringAssert.Contains("SA1309 Field '_value2' must not begin with an underscore", exception.Message);

                exception = Assert.Throws <AssertException>(() => RoslynAssert.Valid(typeof(FieldNameMustNotBeginWithUnderscore), c1, c2));
                StringAssert.Contains("SA1309 Field '_value1' must not begin with an underscore", exception.Message);
                StringAssert.Contains("SA1309 Field '_value2' must not begin with an underscore", exception.Message);
            }
示例#3
0
            public static void TwoDocumentsExpectedDiagnosticWithoutPath()
            {
                var code1    = @"
namespace N
{
    class Code1
    {
    }
}";
                var code2    = @"
namespace N
{
    class Code2
    {
    }
}";
                var expected = "Expected diagnostic must specify path when more than one document is tested.\r\n" +
                               "Either specify path or indicate expected error position with ↓";

                var expectedDiagnostic = ExpectedDiagnostic.Create("SA1309", "ANY", 1, 2);
                var analyzer           = new FieldNameMustNotBeginWithUnderscore();
                var exception          = Assert.Throws <InvalidOperationException>(() => RoslynAssert.Diagnostics(analyzer, expectedDiagnostic, code1, code2));

                Assert.AreEqual(expected, exception.Message);

                exception = Assert.Throws <InvalidOperationException>(() => RoslynAssert.Diagnostics(analyzer, new[] { expectedDiagnostic }, code1, code2));
                Assert.AreEqual(expected, exception.Message);
            }
示例#4
0
            public static void SingleDocumentTwoErrors()
            {
                var before    = @"
namespace N
{
    class C
    {
        private readonly int ↓_value1;
        private readonly int _value2;
    }
}";
                var analyzer  = new FieldNameMustNotBeginWithUnderscore();
                var fix       = new DoNotUseUnderscoreFix();
                var exception = Assert.Throws <AssertException>(() => RoslynAssert.CodeFix(analyzer, fix, before, string.Empty));
                var expected  = @"Expected and actual diagnostics do not match.
Expected:
  SA1309 
    at line 5 and character 29 in file C.cs | private readonly int ↓_value1;
Actual:
  SA1309 Field '_value1' must not begin with an underscore
    at line 5 and character 29 in file C.cs | private readonly int ↓_value1;
  SA1309 Field '_value2' must not begin with an underscore
    at line 6 and character 29 in file C.cs | private readonly int ↓_value2;
";

                CodeAssert.AreEqual(expected, exception.Message);
            }
示例#5
0
            public static void TwoDocumentsNoErrorInCode()
            {
                var code1     = @"
namespace N
{
    ↓class Code1
    {
    }
}";
                var code2     = @"
namespace N
{
    class Code2
    {
    }
}";
                var analyzer  = new FieldNameMustNotBeginWithUnderscore();
                var exception = Assert.Throws <AssertException>(() => RoslynAssert.Diagnostics(analyzer, code1, code2));
                var expected  = "Expected and actual diagnostics do not match.\r\n" +
                                "Expected:\r\n" +
                                "  SA1309 \r\n" +
                                "    at line 3 and character 4 in file Code1.cs | ↓class Code1\r\n" +
                                "Actual: <no diagnostics>\r\n";

                Assert.AreEqual(expected, exception.Message);
            }
示例#6
0
            public static void IndicatedAndActualPositionDoNotMatch()
            {
                var before   = @"
namespace N
{
    class C
    {
        private ↓readonly int _value1;
    }
}";
                var expected = "Expected and actual diagnostics do not match.\r\n" +
                               "Expected:\r\n" +
                               "  SA1309 \r\n" +
                               "    at line 5 and character 16 in file C.cs | private ↓readonly int _value1;\r\n" +
                               "Actual:\r\n" +
                               "  SA1309 Field '_value1' must not begin with an underscore\r\n" +
                               "    at line 5 and character 29 in file C.cs | private readonly int ↓_value1;\r\n";
                var analyzer  = new FieldNameMustNotBeginWithUnderscore();
                var fix       = new DoNotUseUnderscoreFix();
                var exception = Assert.Throws <AssertException>(() => RoslynAssert.CodeFix(analyzer, fix, new[] { before }, string.Empty));

                Assert.AreEqual(expected, exception.Message);
                exception = Assert.Throws <AssertException>(() => RoslynAssert.CodeFix(analyzer, fix, new[] { before }, new[] { string.Empty }));
                Assert.AreEqual(expected, exception.Message);
                exception = Assert.Throws <AssertException>(() => RoslynAssert.CodeFix(analyzer, fix, new[] { before }, string.Empty, fixTitle: "WRONG"));
                Assert.AreEqual(expected, exception.Message);
                exception = Assert.Throws <AssertException>(() => RoslynAssert.CodeFix(analyzer, fix, new[] { before }, new[] { string.Empty }, fixTitle: "WRONG"));
                Assert.AreEqual(expected, exception.Message);
            }
示例#7
0
            public static void PartialTwoDocumentsOneFix()
            {
                var before = @"
namespace N
{
    public partial class C
    {
        private readonly int ↓_value;
    }
}";

                var part2 = @"
namespace N
{
    public partial class C
    {
    }
}";

                var after = @"
namespace N
{
    public partial class C
    {
        private readonly int wrong;
    }
}";
                var expectedDiagnostic = ExpectedDiagnostic.Create(FieldNameMustNotBeginWithUnderscore.Descriptor);
                var expected           = "Mismatch on line 6.\r\n" +
                                         "Expected:         private readonly int wrong;\r\n" +
                                         "Actual:           private readonly int value;\r\n" +
                                         "                                       ^\r\n" +
                                         "Expected:\r\n" +
                                         "\r\n" +
                                         "namespace N\r\n" +
                                         "{\r\n" +
                                         "    public partial class C\r\n" +
                                         "    {\r\n" +
                                         "        private readonly int wrong;\r\n" +
                                         "    }\r\n" +
                                         "}\r\n" +
                                         "Actual:\r\n" +
                                         "\r\n" +
                                         "namespace N\r\n" +
                                         "{\r\n" +
                                         "    public partial class C\r\n" +
                                         "    {\r\n" +
                                         "        private readonly int value;\r\n" +
                                         "    }\r\n" +
                                         "}\r\n";
                var analyzer  = new FieldNameMustNotBeginWithUnderscore();
                var fix       = new DoNotUseUnderscoreFix();
                var exception = Assert.Throws <AssertException>(() => RoslynAssert.CodeFix(analyzer, fix, new[] { before, part2 }, after));

                CodeAssert.AreEqual(expected, exception.Message);
                exception = Assert.Throws <AssertException>(() => RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, new[] { before, part2 }, after));
                CodeAssert.AreEqual(expected, exception.Message);
                exception = Assert.Throws <AssertException>(() => RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, new[] { before, part2 }, new[] { after, part2 }));
                CodeAssert.AreEqual(expected, exception.Message);
            }
示例#8
0
            public static void SingleDocumentExplicitTitle()
            {
                var before = @"
namespace N
{
    class C
    {
        private readonly int ↓_value;
    }
}";

                var after    = @"
namespace N
{
    class C
    {
        private readonly int value;
    }
}";
                var expected = "Did not find a code fix with title WRONG.\r\n" +
                               "Found:\r\n" +
                               "Rename to: 'value'\r\n";

                var analyzer  = new FieldNameMustNotBeginWithUnderscore();
                var fix       = new DoNotUseUnderscoreFix();
                var exception = Assert.Throws <AssertException>(() => RoslynAssert.CodeFix(analyzer, fix, before, after, fixTitle: "WRONG"));

                Assert.AreEqual(expected, exception.Message);

                exception = Assert.Throws <AssertException>(() => RoslynAssert.CodeFix(analyzer, fix, new[] { before }, after, fixTitle: "WRONG"));
                Assert.AreEqual(expected, exception.Message);

                exception = Assert.Throws <AssertException>(() => RoslynAssert.CodeFix(analyzer, fix, new[] { before }, new[] { after }, fixTitle: "WRONG"));
                Assert.AreEqual(expected, exception.Message);
            }
示例#9
0
            public static void IndicatedAndActualPositionDoNotMatchWithWrongMessage()
            {
                var code     = @"
namespace N
{
    class C
    {
        ↓private readonly int _value1;
    }
}";
                var expected = "Expected and actual diagnostics do not match.\r\n" +
                               "Expected:\r\n" +
                               "  SA1309 Wrong message\r\n" +
                               "    at line 5 and character 8 in file C.cs | ↓private readonly int _value1;\r\n" +
                               "Actual:\r\n" +
                               "  SA1309 Field '_value1' must not begin with an underscore\r\n" +
                               "    at line 5 and character 29 in file C.cs | private readonly int ↓_value1;\r\n";

                var expectedDiagnostic = ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated("SA1309", "Wrong message", code, out code);

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

                Assert.AreEqual(expected, exception.Message);

                exception = Assert.Throws <AssertException>(() => RoslynAssert.Diagnostics(analyzer, new[] { expectedDiagnostic }, code));
                Assert.AreEqual(expected, exception.Message);
            }
示例#10
0
            public static void TwoDocumentsOneErrorCodeFixFixedTheCode()
            {
                var barCode = @"
namespace N
{
    class Bar
    {
        private readonly int value;
    }
}";

                var code      = @"
namespace N
{
    class C
    {
        private readonly int ↓_value;
    }
}";
                var analyzer  = new FieldNameMustNotBeginWithUnderscore();
                var fix       = new DoNotUseUnderscoreFix();
                var exception = Assert.Throws <AssertException>(() => RoslynAssert.NoFix(analyzer, fix, barCode, code));
                var expected  = "Expected code to have no fixable diagnostics.\r\n" +
                                "The following actions were registered:\r\n" +
                                "  'Rename to: 'value''\r\n";

                CodeAssert.AreEqual(expected, exception.Message);
            }
示例#11
0
            public static void BinarySolution()
            {
                var binaryReferencedCode = @"
namespace BinaryReferencedAssembly
{
    public class Base
    {
        private int _fieldName;
    }
}";
                var code     = @"
namespace N
{
    using System.Reflection;

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

                RoslynAssert.Valid(analyzer, solution);
            }
示例#12
0
            public static void TwoDocumentsOneFixCorrectFixPassOnlyFixedCode(string fixTitle, string expected)
            {
                var before1 = @"
namespace N
{
    class C1
    {
        private readonly int ↓_value;
    }
}";
                var before2 = @"
namespace N
{
    class C2
    {
        private readonly int value;
    }
}";

                var after    = @"
namespace N
{
    class C1
    {
        private readonly int value;
    }
}".AssertReplace("value", expected);
                var analyzer = new FieldNameMustNotBeginWithUnderscore();
                var fix      = new DontUseUnderscoreManyFix();

                RoslynAssert.FixAll(analyzer, fix, new[] { before1, before2 }, after, fixTitle);
            }
示例#13
0
            public static void SingleDocumentOneErrorTwoFixes(string fixTitle, string expected)
            {
                var before = @"
namespace N
{
    class C
    {
        private readonly int ↓_value;
    }
}";

                var after = @"
namespace N
{
    class C
    {
        private readonly int value;
    }
}".AssertReplace("value", expected);

                var analyzer = new FieldNameMustNotBeginWithUnderscore();
                var fix      = new DontUseUnderscoreManyFix();

                RoslynAssert.FixAll(analyzer, fix, before, after, fixTitle);
            }
示例#14
0
            public static void TwoErrorsOnlyOneIndicated()
            {
                var code      = @"
namespace N
{
    class C
    {
        private readonly int ↓_value1;
        private readonly int _value2;
    }
}";
                var analyzer  = new FieldNameMustNotBeginWithUnderscore();
                var exception = Assert.Throws <AssertException>(() => RoslynAssert.Diagnostics(analyzer, code));
                var expected  = @"Expected and actual diagnostics do not match.
Expected:
  SA1309 
    at line 5 and character 29 in file C.cs | private readonly int ↓_value1;
Actual:
  SA1309 Field '_value1' must not begin with an underscore
    at line 5 and character 29 in file C.cs | private readonly int ↓_value1;
  SA1309 Field '_value2' must not begin with an underscore
    at line 6 and character 29 in file C.cs | private readonly int ↓_value2;
";

                CodeAssert.AreEqual(expected, exception.Message);
            }
示例#15
0
            public static void ProjectFromDisk()
            {
                var after              = @"// ReSharper disable All
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"),
                    5,
                    20);
                var solution = CodeFactory.CreateSolution(
                    csproj,
                    analyzer,
                    expectedDiagnostic,
                    metadataReferences: new[] { MetadataReference.CreateFromFile(typeof(int).Assembly.Location) });

                RoslynAssert.CodeFix(analyzer, new DoNotUseUnderscoreFix(), expectedDiagnostic, solution, after);
            }
示例#16
0
            public static void SingleDocumentOneErrorCorrectFixExplicitTitleExpectedDiagnosticWithPosition()
            {
                var before = @"
namespace N
{
    class C
    {
        private readonly int ↓_value;
    }
}";

                var after = @"
namespace N
{
    class C
    {
        private readonly int value;
    }
}";
                var expectedDiagnostic = ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated(FieldNameMustNotBeginWithUnderscore.DiagnosticId, before, out before);
                var analyzer           = new FieldNameMustNotBeginWithUnderscore();
                var fix = new DoNotUseUnderscoreFix();

                RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, before, after, "Rename to: 'value'");
                RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, new[] { before }, after, "Rename to: 'value'");
                RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, new[] { before }, after, fixTitle: "Rename to: 'value'");
                RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, new[] { before }, new[] { after }, fixTitle: "Rename to: 'value'");
            }
示例#17
0
            public static void SingleDocumentOneErrorTwoFixes(string fixTitle, string expected)
            {
                var before = @"
namespace N
{
    class C
    {
        private readonly int ↓_value;
    }
}";

                var after    = @"
namespace N
{
    class C
    {
        private readonly int value;
    }
}".AssertReplace("value", expected);
                var analyzer = new FieldNameMustNotBeginWithUnderscore();
                var fix      = new DontUseUnderscoreManyFix();

                RoslynAssert.CodeFix(analyzer, fix, before, after, fixTitle);
                RoslynAssert.CodeFix(analyzer, fix, new[] { before }, after, fixTitle: fixTitle);
                RoslynAssert.CodeFix(analyzer, fix, new[] { before }, new[] { after }, fixTitle: fixTitle);
                var expectedDiagnostic = ExpectedDiagnostic.Create(FieldNameMustNotBeginWithUnderscore.Descriptor);

                RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, before, after, fixTitle);
                RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, new[] { before }, after, fixTitle: fixTitle);
                RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, new[] { before }, new[] { after }, fixTitle: fixTitle);
            }
示例#18
0
            public static void TwoDocumentsOneErrorNoFix()
            {
                var barCode = @"
namespace N
{
    class Bar
    {
        private readonly int value;
    }
}";

                var code = @"
namespace N
{
    class C
    {
        private readonly int ↓_value;
    }
}";

                var analyzer = new FieldNameMustNotBeginWithUnderscore();
                var fix      = new CodeFixes.NoFix();

                RoslynAssert.NoFix(analyzer, fix, barCode, code);
                var expectedDiagnostic = ExpectedDiagnostic.Create(FieldNameMustNotBeginWithUnderscore.DiagnosticId);

                RoslynAssert.NoFix(analyzer, fix, expectedDiagnostic, barCode, code);
            }
示例#19
0
            public static void SingleDocumentOneError()
            {
                var before = @"
namespace N
{
    class C
    {
        private readonly int ↓_value;
    }
}";

                var after    = @"
namespace N
{
    class C
    {
        private readonly int value;
    }
}";
                var analyzer = new FieldNameMustNotBeginWithUnderscore();
                var fix      = new DoNotUseUnderscoreFix();

                RoslynAssert.CodeFix(analyzer, fix, new[] { before }, after);
                RoslynAssert.CodeFix(analyzer, fix, new[] { before }, new[] { after });
                var expectedDiagnostic = ExpectedDiagnostic.Create(FieldNameMustNotBeginWithUnderscore.Descriptor);

                RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, new[] { before }, after);
                RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, new[] { before }, new[] { after });
            }
示例#20
0
            public static void TwoDocumentsOneError()
            {
                var c1       = @"
namespace N
{
    class C1
    {
        private readonly int _value = 1;
    }
}";
                var c2       = @"
namespace N
{
    class C2
    {
    }
}";
                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 C1.cs | private readonly int ↓_value = 1;\r\n";

                var analyzer  = new FieldNameMustNotBeginWithUnderscore();
                var exception = Assert.Throws <AssertException>(() => RoslynAssert.Valid(analyzer, c1, c2));

                Assert.AreEqual(expected, exception.Message);

                exception = Assert.Throws <AssertException>(() => RoslynAssert.Valid(typeof(FieldNameMustNotBeginWithUnderscore), c1, c2));
                Assert.AreEqual(expected, exception.Message);
            }
示例#21
0
            public static void NoDiagnosticOrError()
            {
                var code     = @"
namespace N
{
    class C
    {
        private int ↓i;
        
        C(int i)
        {
            this.i = i;
        }

        public override string ToString() => this.i.ToString();
    }
}";
                var expected = "Expected and actual diagnostics do not match.\r\n" +
                               "Expected:\r\n" +
                               "  SA1309 \r\n" +
                               "    at line 5 and character 20 in file C.cs | private int ↓i;\r\n" +
                               "Actual: <no diagnostics>\r\n";
                var analyzer  = new FieldNameMustNotBeginWithUnderscore();
                var exception = Assert.Throws <AssertException>(() => RoslynAssert.Diagnostics(analyzer, code));

                Assert.AreEqual(expected, exception.Message);
            }
示例#22
0
            public static void TwoDocumentsIndicatedAndActualPositionDoNotMatch()
            {
                var code1 = @"
namespace N
{
    class C1
    {
        private readonly int _value1;
    }
}";

                var code2 = @"
namespace N
{
    class C2
    {
        private readonly int ↓value2;
    }
}";

                var analyzer  = new FieldNameMustNotBeginWithUnderscore();
                var exception = Assert.Throws <AssertException>(() => RoslynAssert.Diagnostics(analyzer, code1, code2));
                var expected  = @"Expected and actual diagnostics do not match.
Expected:
  SA1309 
    at line 5 and character 29 in file C2.cs | private readonly int ↓value2;
Actual:
  SA1309 Field '_value1' must not begin with an underscore
    at line 5 and character 29 in file C1.cs | private readonly int ↓_value1;
";

                CodeAssert.AreEqual(expected, exception.Message);
            }
示例#23
0
            public static void FixAllInDocumentTwoErrors()
            {
                var before = @"
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 fix      = new DoNotUseUnderscoreFix();

                RoslynAssert.FixAllInDocument(analyzer, fix, before, after);
            }
示例#24
0
            public static void TwoDocumentsOneError()
            {
                var barCode = @"
namespace N
{
    class Bar
    {
        private readonly int value;
    }
}";

                var before = @"
namespace N
{
    class C
    {
        private readonly int ↓_value;
    }
}";

                var after    = @"
namespace N
{
    class C
    {
        private readonly int value;
    }
}";
                var analyzer = new FieldNameMustNotBeginWithUnderscore();
                var fix      = new DoNotUseUnderscoreFix();

                RoslynAssert.CodeFix(analyzer, fix, new[] { barCode, before }, after);
                RoslynAssert.CodeFix(analyzer, fix, new[] { barCode, before }, new[] { barCode, after });
            }
示例#25
0
            public static void TwoDocumentsOneErrorErrorInFix()
            {
                var barCode = @"
namespace N
{
    class Bar
    {
        private readonly int value;
    }
}";

                var before = @"
namespace N
{
    class C
    {
        private readonly int ↓_value;
    }
}";

                var after     = @"
namespace N
{
    class C
    {
        private readonly int bar;
    }
}";
                var analyzer  = new FieldNameMustNotBeginWithUnderscore();
                var fix       = new DoNotUseUnderscoreFix();
                var exception = Assert.Throws <AssertException>(() => RoslynAssert.FixAll(analyzer, fix, new[] { barCode, before }, new[] { barCode, after }));
                var expected  = "Applying fixes one by one failed.\r\n" +
                                "Mismatch on line 6 of file C.cs.\r\n" +
                                "Expected:         private readonly int bar;\r\n" +
                                "Actual:           private readonly int value;\r\n" +
                                "                                       ^\r\n" +
                                "Expected:\r\n\r\n" +
                                "namespace N\r\n" +
                                "{\r\n" +
                                "    class C\r\n" +
                                "    {\r\n" +
                                "        private readonly int bar;\r\n" +
                                "    }\r\n" +
                                "}\r\n" +
                                "Actual:\r\n\r\n" +
                                "namespace N\r\n" +
                                "{\r\n" +
                                "    class C\r\n" +
                                "    {\r\n" +
                                "        private readonly int value;\r\n" +
                                "    }\r\n" +
                                "}\r\n";

                CodeAssert.AreEqual(expected, exception.Message);
            }
示例#26
0
        public static void ValidOrdersController()
        {
            var order      = @"
namespace ValidCode
{
    public class Order
    {
        public int Id { get; set; }
    }
}";
            var db         = @"
namespace ValidCode
{
    using Microsoft.EntityFrameworkCore;

    public class Db : DbContext
    {
        public DbSet<Order> Orders { get; set; }
    }
}";
            var controller = @"
namespace ValidCode
{
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.EntityFrameworkCore;

    [ApiController]
    public class OrdersController : Controller
    {
        private readonly Db db;

        public OrdersController(Db db)
        {
            this.db = db;
        }

        [HttpGet(""api/orders/{id}"")]
        public async Task<IActionResult> GetOrder(int id)
        {
            var match = await this.db.Orders.FirstOrDefaultAsync(x => x.Id == id);
            if (match is null)
            {
                return this.NotFound();
            }

            return this.Ok(match);
        }
    }
}";
            var analyzer   = new FieldNameMustNotBeginWithUnderscore();

            RoslynAssert.Valid(analyzer, order, db, controller);
        }
示例#27
0
            public static void SingleDocumentOneErrorPassingAnalyzer()
            {
                var code     = @"
namespace N
{
    class C
    {
        private readonly int ↓_value = 1;
    }
}";
                var analyzer = new FieldNameMustNotBeginWithUnderscore();

                RoslynAssert.Diagnostics(analyzer, code);
            }
示例#28
0
            public static void NoErrorIndicated()
            {
                var code      = @"
namespace N
{
    class C
    {
    }
}";
                var expected  = "Expected code to have at least one error position indicated with '↓'";
                var analyzer  = new FieldNameMustNotBeginWithUnderscore();
                var exception = Assert.Throws <InvalidOperationException>(() => RoslynAssert.Diagnostics(analyzer, code));

                Assert.AreEqual(expected, exception.Message);
            }
示例#29
0
            public static void WhenCompilationError()
            {
                var code     = @"
namespace N
{
    class C
    {
        private readonly int ↓_value = 1;
        INCOMPLETE
    }
}";
                var analyzer = new FieldNameMustNotBeginWithUnderscore();

                RoslynAssert.Diagnostics(analyzer, code, AllowCompilationErrors.Yes);
            }
示例#30
0
            public static void OneErrorWithExpectedDiagnosticIdAndPosition()
            {
                var code = @"
namespace N
{
    class C
    {
        private readonly int _value1;
    }
}";

                var expectedDiagnostic = ExpectedDiagnostic.Create("SA1309", 5, 29);
                var analyzer           = new FieldNameMustNotBeginWithUnderscore();

                RoslynAssert.Diagnostics(analyzer, expectedDiagnostic, code);
            }