Example #1
0
        public void DateFormatAnalyzer_DateTimeddMMyyyy_Ignore()
        {
            var nameSpace = @"using System;";
            var method    = @"var date = DateTime.UtcNow.ToString(""dd/MM/yyyy"");";

            var test = CodeTestHelper.GetCodeInMainMethod(nameSpace, method);


            VerifyCSharpDiagnostic(test);
        }
Example #2
0
        public void TimeSpanFormatAnalyzer_FPattern_Ignore()
        {
            var nameSpace = @"using System;
using System.Globalization;";
            var method    = @"var date = new TimeSpan(20, 10, 5, 2).ToString(@""F\:ff"");";

            var test = CodeTestHelper.GetCodeInMainMethod(nameSpace, method);

            VerifyCSharpDiagnostic(test);
        }
Example #3
0
        public void DoubleFloatParseAnalyzer_TryParseFloatWithInvarientCulture_Ignore()
        {
            var nameSpace = @"using System;
using System.Globalization;";
            var method    = @"bool didParse = float.TryParse(""1.1"", NumberStyles.Any, CultureInfo.InvariantCulture, out float parsedDouble);";

            var test = CodeTestHelper.GetCodeInMainMethod(nameSpace, method);

            VerifyCSharpDiagnostic(test);
        }
Example #4
0
        public void DoubleFloatParseAnalyzer_TryParseDoubleInvariantCultureNumberInfo_Ignore()
        {
            var nameSpace = @"using System;
using System.Globalization;";
            var method    = @"double.TryParse(""1.1"", NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture.NumberFormat, out double longitude);";

            var test = CodeTestHelper.GetCodeInMainMethod(nameSpace, method);

            VerifyCSharpDiagnostic(test);
        }
Example #5
0
        public void TimeSpanFormatAnalyzer_MM_ProposeFix()
        {
            var nameSpace = @"using System;
using System.Globalization;";
            var method    = @"var time = new TimeSpan(20, 10, 5, 2).ToString(@""MM"", CultureInfo.InvariantCulture);";

            var test     = CodeTestHelper.GetCodeInMainMethod(nameSpace, method);
            var expected = CodeTestHelper.CreateDiagnosticResult("AN0009", "Month not valid in TimeSpan formatting. Did you mean 'm' (minutes)?", 10, 15);

            VerifyCSharpDiagnostic(test, expected);
        }
Example #6
0
        public void TimeSpanFormatAnalyzer_F_ProposeFix()
        {
            var nameSpace = @"using System;
using System.Globalization;";
            var method    = @"var time = new TimeSpan(20, 10, 5, 2).ToString(@""F"", CultureInfo.InvariantCulture);";

            var test     = CodeTestHelper.GetCodeInMainMethod(nameSpace, method);
            var expected = CodeTestHelper.CreateDiagnosticResult("AN0009", "'F' alone doesn't work in TimeSpan, use %f or in a pattern @\"F\\:ff\"", 10, 15);

            VerifyCSharpDiagnostic(test, expected);
        }
Example #7
0
        public void DateFormatAnalyzer_DateTimeyymmdd_ProposeFix()
        {
            var nameSpace = @"using System;";
            var method    = @"var date = DateTime.UtcNow.ToString(""yy/mm/dd"");";

            var test = CodeTestHelper.GetCodeInMainMethod(nameSpace, method);

            var expected = CodeTestHelper.CreateDiagnosticResult("AN0008", 9, 15);

            VerifyCSharpDiagnostic(test, expected);
        }
Example #8
0
        public void DateAndTimeStringAnalyzer_TimeSpanToStringWithoutInvariantCulture_ProposeFix()
        {
            var nameSpace = @"using System;";
            var method    = @"var timeSpan = new TimeSpan(0).ToString(""mm/dd/yyyy hh:mm:ss"");";

            var test = CodeTestHelper.GetCodeInMainMethod(nameSpace, method);

            var expected = CodeTestHelper.CreateDiagnosticResult("AN0007", "Use InvariantCulture for printing date/time", 9, 19);

            VerifyCSharpDiagnostic(test, expected);
        }
        public void DateTimeAnalyzer_InitializeDateTimeTicks_ProposeFix()
        {
            var nameSpace = @"using System;";
            var method    = @"DateTime dateTime = new DateTime(0);";

            var test = CodeTestHelper.GetCodeInMainMethod(nameSpace, method);

            var expected = CodeTestHelper.CreateDiagnosticResult("AN0002", "DateTime kind should be UTC", 9, 24);

            VerifyCSharpDiagnostic(test, expected);
        }
Example #10
0
        public void DoubleFloatParseAnalyzer_TryParseFloatWithoutInvarientCulture_ProposeFix()
        {
            var nameSpace = @"using System;
using System.Globalization;";
            var method    = @"bool didParse = float.TryParse(""1.1"", out float parsedFloat);";

            var test = CodeTestHelper.GetCodeInMainMethod(nameSpace, method);

            var expected = CodeTestHelper.CreateDiagnosticResult("AN0003", "Use InvariantCulture for double/float parsing", 10, 20);

            VerifyCSharpDiagnostic(test, expected);
        }
        public void CircuitBreakerAnalyzer_WhileBreakInIf_Ignore()
        {
            var methodBody = @"
			int i = 0;
			while(i > 1)
			{
				i++
                if(i > 100)
                    break;
			}"            ;
            var code       = CodeTestHelper.GetCodeInMainMethod("namespace ConsoleApplication1", methodBody);

            VerifyCSharpDiagnostic(code);
        }
        public void CollectionContainsAnalyzer_HashSetAnyAndExpression_Ignore()
        {
            var namespaces = @"
using System.Collections.Generic;
using System.Linq;
    ";

            var methodBody = @"
            var collection = new HashSet<string>();
            var result = collection.Any(e => e == ""test"" && e == ""othertest"");
            ";

            var code = CodeTestHelper.GetCodeInMainMethod(namespaces, methodBody);

            VerifyCSharpDiagnostic(code);
        }
        public void CollectionContainsAnalyzer_HashSetAnyEmpty_Ignore()
        {
            var namespaces = @"
using System.Collections.Generic;
using System.Linq;
    ";

            var methodBody = @"
            var collection = new HashSet<int>();
            var result = collection.Any();
            ";

            var code = CodeTestHelper.GetCodeInMainMethod(namespaces, methodBody);

            VerifyCSharpDiagnostic(code);
        }
        public void CollectionContainsAnalyzer_ListAnyCouldBeContainsLeft_ProposeFix()
        {
            var namespaces = @"
using System.Collections.Generic;
using System.Linq;
    ";

            var methodBody = @"
            var collection = new List<string>();
            var result = collection.Any(e => e == ""test"");
            ";

            var code = CodeTestHelper.GetCodeInMainMethod(namespaces, methodBody);

            var expected = CodeTestHelper.CreateDiagnosticResult("AN0010", 14, 41);

            VerifyCSharpDiagnostic(code, expected);
        }
        public void CollectionContainsAnalyzer_HashSetAnyIntCouldBeContainsRight_ProposeFix()
        {
            var namespaces = @"
using System.Collections.Generic;
using System.Linq;
    ";

            var methodBody = @"
            var collection = new HashSet<int>();
            var result = collection.Any(e => 5 == e);
            ";

            var code = CodeTestHelper.GetCodeInMainMethod(namespaces, methodBody);

            var expected = CodeTestHelper.CreateDiagnosticResult("AN0010", 14, 41);

            VerifyCSharpDiagnostic(code, expected);
        }