示例#1
0
        public static TestSource Read(string rawSource)
        {
            var testInput = new TestSource();
            var lines     = rawSource.Split(new[] { "\n", "\r\n" }, StringSplitOptions.None);

            for (var i = 0; i < lines.Length; i++)
            {
                var line             = lines[i];
                var markerStartIndex = line.IndexOf(MarkerStart, StringComparison.Ordinal);
                if (markerStartIndex != -1)
                {
                    var markerEndIndex = line.IndexOf(MarkerEnd, markerStartIndex, StringComparison.Ordinal);
                    var markerName     = line.Substring(markerStartIndex + 2, markerEndIndex - markerStartIndex - 2);
                    var markerLocation = new DiagnosticLocation(i + 1, markerStartIndex + 1);
                    if (testInput.DefaultMarkerLocation == null)
                    {
                        testInput.DefaultMarkerLocation = markerLocation;
                    }

                    testInput.MarkerLocations.Add(markerName, markerLocation);
                    line = line.Substring(0, markerStartIndex) + line.Substring(markerEndIndex + MarkerEnd.Length);
                }

                lines[i] = line;
            }

            testInput.Source = string.Join(Environment.NewLine, lines);
            return(testInput);
        }
示例#2
0
        public async Task AZC0010ProducedForClientOptionsCtorWithNonMaxVersionImplicit()
        {
            var testSource  = TestSource.Read(@"
namespace RandomNamespace
{
    public class SomeClientOptions { 

        public enum ServiceVersion
        {
            V2018_11_09,
            V2019_03_20
        }

        public SomeClientOptions(ServiceVersion /*MM*/version = ServiceVersion.V2018_11_09)
        {
        }
    }
}
");
            var diagnostics = await _runner.GetDiagnosticsAsync(testSource.Source);

            var diagnostic = Assert.Single(diagnostics);

            Assert.Equal("AZC0010", diagnostic.Id);
            AnalyzerAssert.DiagnosticLocation(testSource.DefaultMarkerLocation, diagnostics[0].Location);
        }
示例#3
0
        public async Task AZC0002SkippedForPrivateMethods()
        {
            var testSource  = TestSource.Read(@"
using System.Threading;
using System.Threading.Tasks;

namespace RandomNamespace
{
    public class SomeClient
    {
        private Task GetAsync(CancellationToken cancellationToken = default)
        {
            return null;
        }

        private void Get(CancellationToken cancellationToken = default)
        {
        }
    }
}
");
            var diagnostics = await _runner.GetDiagnosticsAsync(testSource.Source);

            Assert.Empty(diagnostics);
        }
示例#4
0
        public async Task AZC0003ProducedForMethodsWithWrongNameParameter()
        {
            var testSource  = TestSource.Read(@"
using System.Threading;
using System.Threading.Tasks;

namespace RandomNamespace
{
    public class SomeClient
    {
        public virtual Task /*MM0*/GetAsync(CancellationToken cancellation = default)
        {
            return null;
        }

        public virtual void /*MM1*/Get(CancellationToken cancellation = default)
        {
        }
    }
}
");
            var diagnostics = await _runner.GetDiagnosticsAsync(testSource.Source);

            Assert.Equal(2, diagnostics.Length);

            Assert.Equal("AZC0002", diagnostics[0].Id);
            Assert.Equal("AZC0002", diagnostics[1].Id);
            AnalyzerAssert.DiagnosticLocation(testSource.MarkerLocations["MM0"], diagnostics[0].Location);
            AnalyzerAssert.DiagnosticLocation(testSource.MarkerLocations["MM1"], diagnostics[1].Location);
        }
示例#5
0
        public async Task AZC0009ProducedForClientOptionsCtorWhereServiceVersionNotFirstParam()
        {
            var testSource  = TestSource.Read(@"
namespace RandomNamespace
{
    public class SomeClientOptions { 

        public enum ServiceVersion
        {
            V2018_11_09 = 0
        }

        public SomeClientOptions(string /*MM*/anOption, ServiceVersion version = ServiceVersion.V2018_11_09)
        {
        }
    }
}
");
            var diagnostics = await _runner.GetDiagnosticsAsync(testSource.Source);

            var diagnostic = Assert.Single(diagnostics);

            Assert.Equal("AZC0009", diagnostic.Id);
            AnalyzerAssert.DiagnosticLocation(testSource.DefaultMarkerLocation, diagnostics[0].Location);
        }
示例#6
0
        public async Task AZC0009ProducedForClientOptionsWithoutServiceVersionInCtor()
        {
            var testSource  = TestSource.Read(@"
namespace RandomNamespace
{
    public class SomeClientOptions {

        public enum ServiceVersion
        {
            V2018_11_09 = 0
        }

        public /*MM*/SomeClientOptions()
        {
        }
    }
}
");
            var diagnostics = await _runner.GetDiagnosticsAsync(testSource.Source);

            var diagnostic = Assert.Single(diagnostics);

            Assert.Equal("AZC0009", diagnostic.Id);
            AnalyzerAssert.DiagnosticLocation(testSource.DefaultMarkerLocation, diagnostics[0].Location);
        }
示例#7
0
        public async Task AZC1002ProducedForWhitespaceInTheEndOfTheLine(string testCase)
        {
            var testSource  = TestSource.Read(testCase);
            var diagnostics = await _runner.GetDiagnosticsAsync(testSource.Source);

            var diagnostic = Assert.Single(diagnostics);

            Assert.Equal("AZC1002", diagnostic.Id);
            AnalyzerAssert.DiagnosticLocation(testSource.DefaultMarkerLocation, diagnostics[0].Location);
        }
示例#8
0
        public async Task AZC0001NotProducedForNamespacesWithPrivateMembersOnly()
        {
            var testSource  = TestSource.Read(@"
namespace RandomNamespace
{
    internal class Program { }
}
");
            var diagnostics = await _runner.GetDiagnosticsAsync(testSource.Source);

            Assert.Empty(diagnostics);
        }
示例#9
0
        public async Task AZC0001NotProducedForAllowedNamespaces()
        {
            var testSource  = TestSource.Read(@"
namespace Azure.Storage.Hello
{
    public class Program { }
}
");
            var diagnostics = await _runner.GetDiagnosticsAsync(testSource.Source);

            Assert.Empty(diagnostics);
        }
示例#10
0
        public async Task AZC1001NotProducedForSingleEmptyLine()
        {
            var testSource  = TestSource.Read(@"
namespace RandomNamespace
{

}
");
            var diagnostics = await _runner.GetDiagnosticsAsync(testSource.Source);

            Assert.Empty(diagnostics);
        }
示例#11
0
        public async Task AZC1001NotProducedForIfDefs()
        {
            var testSource  = TestSource.Read(@"
namespace RandomNamespace
{

#if DEBUG

#endif

}
");
            var diagnostics = await _runner.GetDiagnosticsAsync(testSource.Source);

            Assert.Empty(diagnostics);
        }
示例#12
0
        public async Task AZC1001ProducedForDoubleNewline()
        {
            var testSource  = TestSource.Read(@"
namespace RandomNamespace
{

/*MM*/
}
");
            var diagnostics = await _runner.GetDiagnosticsAsync(testSource.Source);

            var diagnostic = Assert.Single(diagnostics);

            Assert.Equal("AZC1001", diagnostic.Id);
            AnalyzerAssert.DiagnosticLocation(testSource.DefaultMarkerLocation, diagnostics[0].Location);
        }
示例#13
0
        public async Task AZC0007ProducedForClientsWithoutOptionsCtor()
        {
            var testSource  = TestSource.Read(@"
namespace RandomNamespace
{
    public class SomeClient
    {
        public /*MM*/SomeClient() {}
    }
}
");
            var diagnostics = await _runner.GetDiagnosticsAsync(testSource.Source);

            var diagnostic = Assert.Single(diagnostics);

            Assert.Equal("AZC0007", diagnostic.Id);
            AnalyzerAssert.DiagnosticLocation(testSource.DefaultMarkerLocation, diagnostics[0].Location);
        }
示例#14
0
        public async Task AZC0008NotProducedForClientOptionsWithServiceVersionEnum()
        {
            var testSource  = TestSource.Read(@"
namespace RandomNamespace
{
    public class SomeClientOptions { 

        public enum ServiceVersion
        {
            V2018_11_09 = 0
        }
    }
}
");
            var diagnostics = await _runner.GetDiagnosticsAsync(testSource.Source);

            Assert.Empty(diagnostics.Where(d => d.Id == "AZC0008"));
        }
示例#15
0
        public async Task AZC0001ProducedOneErrorPerNamspaceDefinition()
        {
            var testSource  = TestSource.Read(@"
namespace RandomNamespace
{
    public class Program { }
}

namespace RandomNamespace
{
    public class Program2 { }
}
");
            var diagnostics = await _runner.GetDiagnosticsAsync(testSource.Source);

            Assert.Equal(2, diagnostics.Length);
            Assert.All(diagnostics, d => Assert.Equal("AZC0001", d.Id));
        }
示例#16
0
        public async Task AZC0001ProducedForInvalidNamespaces()
        {
            var testSource  = TestSource.Read(@"
namespace /*MM*/RandomNamespace
{
    public class Program { }
}
");
            var diagnostics = await _runner.GetDiagnosticsAsync(testSource.Source);

            var diagnostic = Assert.Single(diagnostics);

            Assert.Equal("AZC0001", diagnostic.Id);
            Assert.Equal("Namespace 'RandomNamespace' shouldn't contain public types. Use one of the following pre-approved namespace groups: " +
                         "Azure.ApplicationModel, Azure.Analytics, Azure.Data, Azure.Iot, Azure.Media, Azure.Messaging, Azure.ML, Azure.Security, Azure.Storage", diagnostic.GetMessage());

            AnalyzerAssert.DiagnosticLocation(testSource.DefaultMarkerLocation, diagnostic.Location);
        }
示例#17
0
        public async Task AZC1001ProducedForTripleNewline()
        {
            var testSource  = TestSource.Read(@"
namespace RandomNamespace
{

/*MM*/
/*MM1*/
}
");
            var diagnostics = await _runner.GetDiagnosticsAsync(testSource.Source);

            Assert.Equal(2, diagnostics.Length);

            Assert.Equal("AZC1001", diagnostics[0].Id);
            Assert.Equal("AZC1001", diagnostics[1].Id);
            AnalyzerAssert.DiagnosticLocation(testSource.DefaultMarkerLocation, diagnostics[0].Location);
            AnalyzerAssert.DiagnosticLocation(testSource.MarkerLocations["MM1"], diagnostics[1].Location);
        }
示例#18
0
        public async Task AZC0005ProducedForClientTypesWithoutProtectedCtor()
        {
            var testSource  = TestSource.Read(@"
namespace RandomNamespace
{
    public class SomeClientOptions { }

    public class /*MM*/SomeClient
    {
        public SomeClient(string connectionString) {}
        public SomeClient(string connectionString, SomeClientOptions options) {}
    }
}
");
            var diagnostics = await _runner.GetDiagnosticsAsync(testSource.Source);

            var diagnostic = Assert.Single(diagnostics);

            Assert.Equal("AZC0005", diagnostic.Id);
            AnalyzerAssert.DiagnosticLocation(testSource.DefaultMarkerLocation, diagnostics[0].Location);
        }
示例#19
0
        public async Task AZC0009NotProducedForClientOptionsCtorWhereServiceVersionFirstParam()
        {
            var testSource  = TestSource.Read(@"
namespace RandomNamespace
{
    public class SomeClientOptions { 

        public enum ServiceVersion
        {
            V2018_11_09 = 0
        }

        public SomeClientOptions(ServiceVersion version = ServiceVersion.V2018_11_09)
        {
        }
    }
}
");
            var diagnostics = await _runner.GetDiagnosticsAsync(testSource.Source);

            Assert.Empty(diagnostics);
        }
示例#20
0
        public async Task AZC0010NotProducedForClientOptionsCtorWithMaxServiceVersionDefault()
        {
            var testSource  = TestSource.Read(@"
namespace RandomNamespace
{
    public class SomeClientOptions { 

        public enum ServiceVersion
        {
            V2018_11_09 = 0,
            V2019_03_20 = 1
        }

        public SomeClientOptions(ServiceVersion version = ServiceVersion.V2019_03_20)
        {
        }
    }
}
");
            var diagnostics = await _runner.GetDiagnosticsAsync(testSource.Source);

            Assert.Empty(diagnostics);
        }
示例#21
0
        public async Task AZC0004ProducedForMethodsWithoutSyncAlternative()
        {
            var testSource  = TestSource.Read(@"
using System.Threading;
using System.Threading.Tasks;

namespace RandomNamespace
{
    public class SomeClient
    {
        public virtual Task /*MM0*/GetAsync(CancellationToken cancellationToken = default)
        {
            return null;
        }
    }
}
");
            var diagnostics = await _runner.GetDiagnosticsAsync(testSource.Source);

            var diagnostic = Assert.Single(diagnostics);

            Assert.Equal("AZC0004", diagnostic.Id);
            AnalyzerAssert.DiagnosticLocation(testSource.MarkerLocations["MM0"], diagnostics[0].Location);
        }