コード例 #1
0
        public static void Run(DiagnosticAnalyzer analyzer)
        {
            Assert.Inconclusive("VS does not understand [Explicit]");
            var diagnostics = Analyze.GetDiagnostics(Solution, analyzer);

            RoslynAssert.NoDiagnostics(diagnostics);
        }
コード例 #2
0
        public static void ConstructorCycle(DiagnosticAnalyzer analyzer)
        {
            var code     = @"
namespace N
{
    using System;

    public sealed class C : IDisposable
    {
        private IDisposable disposable;

        public C(int i, IDisposable disposable)
            : this(disposable, i)
        {
            this.disposable = disposable;
        }

        public C(IDisposable disposable, int i)
            : this(i, disposable)
        {
            this.disposable = disposable;
        }

        public void Dispose()
        {
        }
    }
}";
            var solution = CodeFactory.CreateSolution(code, CodeFactory.DefaultCompilationOptions(analyzer), MetadataReferences.FromAttributes());

            RoslynAssert.NoDiagnostics(Analyze.GetDiagnostics(analyzer, solution));
        }
コード例 #3
0
        public static void WithSyntaxErrors(DiagnosticAnalyzer analyzer)
        {
            var code     = @"
namespace N
{
    using System;
    using System.IO;

    public class C : 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);
        }
    }
}";
            var solution = CodeFactory.CreateSolution(code, CodeFactory.DefaultCompilationOptions(analyzer), MetadataReferences.FromAttributes());

            RoslynAssert.NoDiagnostics(Analyze.GetDiagnostics(analyzer, solution));
        }
コード例 #4
0
ファイル: Valid.cs プロジェクト: GeertvanHorrik/Gu.Analyzers
        public static void MultipleIEnumerableInterfaces()
        {
            var testCode    = @"
namespace N
{
    using System.Collections;
    using System.Collections.Generic;

    class C : IEnumerable<IEnumerable<char>>, IEnumerable<int>
    {
        IEnumerator<int> IEnumerable<int>.GetEnumerator()
        {
            yield return 42;
        }

        void GetEnumerator(int x)
        {
        }

        IEnumerator<IEnumerable<char>> IEnumerable<IEnumerable<char>>.GetEnumerator()
        {
            yield return string.Empty;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable<IEnumerable<char>>)this).GetEnumerator();
        }
    }

    public class A
    {
        public void F()
        {
            foreach(int a in new C())
            {
            }
        }
    }
}";
            var sln         = CodeFactory.CreateSolution(testCode, CodeFactory.DefaultCompilationOptions(Analyzer), RoslynAssert.MetadataReferences);
            var diagnostics = Analyze.GetDiagnostics(Analyzer, sln);

            RoslynAssert.NoDiagnostics(diagnostics);
        }
コード例 #5
0
        public void CtorCallingSelf(DiagnosticAnalyzer analyzer)
        {
            var testCode    = @"
namespace N
{
    internal abstract class C
    {
        internal C()
            : this()
        {
        }
    }
}";
            var sln         = CodeFactory.CreateSolution(testCode, CodeFactory.DefaultCompilationOptions(analyzer), RoslynAssert.MetadataReferences);
            var diagnostics = Analyze.GetDiagnostics(analyzer, sln);

            RoslynAssert.NoDiagnostics(diagnostics);
        }
コード例 #6
0
            public static void Recursion()
            {
                var code     = @"
namespace RoslynSandbox
{
    using System;

    public class C<T1, T2>
        where T1 : T2
        where T2 : T1
    {
        public static void Bar()
        {
            var type = typeof(C<,>).MakeGenericType(typeof(int), typeof(int));
        }
    }
}";
                var solution = CodeFactory.CreateSolution(code, CodeFactory.DefaultCompilationOptions(Analyzer), RoslynAssert.MetadataReferences);

                RoslynAssert.NoDiagnostics(Analyze.GetDiagnostics(Analyzer, solution));
            }
コード例 #7
0
ファイル: Repro.cs プロジェクト: pgmstan/AspNetCoreAnalyzers
        public static void Run(DiagnosticAnalyzer analyzer)
        {
            var diagnostics = Analyze.GetDiagnostics(Solution, analyzer);

            RoslynAssert.NoDiagnostics(diagnostics);
        }
コード例 #8
0
            public static void Issue63()
            {
                var viewModelBaseCode  = @"
namespace MVVM
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Reflection;

    using System.Runtime.CompilerServices;

    /// <summary>
    /// Base class for all ViewModel classes in the application.
    /// It provides support for property change notifications
    /// and has a DisplayName property. This class is abstract.
    /// </summary>
    public abstract class ViewModelBase : INotifyPropertyChanged, IDisposable
    {
        private readonly List<IDisposable> disposables = new List<IDisposable>();
        private readonly object disposeLock = new object();
        private bool isDisposed;

        protected ViewModelBase()
        {
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void Dispose()
        {
            lock (disposeLock)
            {
                this.OnDispose();

                if (isDisposed)
                    return;

                foreach (var disposable in disposables)
                    disposable.Dispose();

                isDisposed = true;
            }
        }

        protected virtual void OnDispose()
        {
        }
    }
}";
                var popupViewModelCode = @"
namespace ProjectX.ViewModel
{
    using System;
    using MVVM;

    public class PopupViewModel : ViewModelBase
    {
        public PopupViewModel()
        {
            ClosePopupCommand = new ClosePopupCommand(this);
        }

        // Gives an IDISP006 warning (need to implement IDispose)
        public ClosePopupCommand ClosePopupCommand { get; }

        protected override void OnDispose()
        {
            ClosePopupCommand.Dispose();
            CloseProgramCommand.Dispose();
        }
    }
}";

                var closePopupCommandCode = @"
namespace ProjectX.Commands
{
    using System;

    public sealed class ClosePopupCommand : IDisposable
    {
        private readonly object disposeLock = new object();
        private bool isDisposed;
        private bool isBusy = false;

        internal ClosePopupCommand()
        {
        }

        public event EventHandler CanExecuteChanged;

        public void Dispose()
        {
            lock (disposeLock)
            {
                if (isDisposed)
                    return;

                // Here we have code that actually needs to be disposed off...

                isDisposed = true;
            }
        }
    }
}";

                var solution = CodeFactory.CreateSolution(
                    new[] { viewModelBaseCode, popupViewModelCode, closePopupCommandCode },
                    CodeFactory.DefaultCompilationOptions(Analyzer),
                    MetadataReferences.FromAttributes());

                RoslynAssert.NoDiagnostics(Analyze.GetDiagnostics(Analyzer, solution));
            }