Skip to content

Instantly share code, notes, and snippets.

@Vannevelj
Created May 4, 2015 17:02
Show Gist options
  • Save Vannevelj/5126a8259f6b0374c489 to your computer and use it in GitHub Desktop.
Save Vannevelj/5126a8259f6b0374c489 to your computer and use it in GitHub Desktop.
roslyn is also cool
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestHelper;
using VSDiagnostics.Diagnostics.Strings.ReplaceEmptyStringWithStringDotEmpty;
namespace VSDiagnostics.Test.Tests.Strings
{
[TestClass]
public class ReplaceEmptyStringWithStringDotEmptyTests : CodeFixVerifier
{
[TestMethod]
public void ReplaceEmptyStringsWithStringDotEmpty_WithLocalEmptyStringLiteral_InvokesWarning()
{
var original = @"
using System;
using System.Text;
namespace ConsoleApplication1
{
class MyClass
{
void Method()
{
string s = """";
}
}
}";
var result = @"
using System;
using System.Text;
namespace ConsoleApplication1
{
class MyClass
{
void Method()
{
string s = string.Empty;
}
}
}";
var expectedDiagnostic = new DiagnosticResult
{
Id = ReplaceEmptyStringWithStringDotEmptyAnalyzer.DiagnosticId,
Message = ReplaceEmptyStringWithStringDotEmptyAnalyzer.Message,
Severity = ReplaceEmptyStringWithStringDotEmptyAnalyzer.Severity,
Locations =
new[]
{
new DiagnosticResultLocation("Test0.cs", 9, 13)
}
};
VerifyCSharpDiagnostic(original, expectedDiagnostic);
VerifyCSharpFix(original, result);
}
[TestMethod]
public void ReplaceEmptyStringsWithStringDotEmpty_WithDefaultParameterEmptyStringLiteral_DoesNotInvokeWarning()
{
var original = @"
using System;
using System.Text;
namespace ConsoleApplication1
{
class MyClass
{
void Method(string s = """")
{
}
}
}";
VerifyCSharpDiagnostic(original);
}
[TestMethod]
public void ReplaceEmptyStringsWithStringDotEmpty_WithNonEmptyStringLiteral_DoesNotInvokeWarning()
{
var original = @"
using System;
using System.Text;
namespace ConsoleApplication1
{
class MyClass
{
void Method()
{
string s = ""hello world"";
}
}
}";
VerifyCSharpDiagnostic(original);
}
[TestMethod]
public void ReplaceEmptyStringsWithStringDotEmpty_WithStringLiteralAsArgument_InvokesWarning()
{
var original = @"
using System;
using System.Text;
namespace ConsoleApplication1
{
class MyClass
{
void Method()
{
Method2("""");
}
void Method2(string s)
{
}
}
}";
var result = @"
using System;
using System.Text;
namespace ConsoleApplication1
{
class MyClass
{
void Method()
{
Method2(string.Empty);
}
void Method2(string s)
{
}
}
}";
var expectedDiagnostic = new DiagnosticResult
{
Id = ReplaceEmptyStringWithStringDotEmptyAnalyzer.DiagnosticId,
Message = ReplaceEmptyStringWithStringDotEmptyAnalyzer.Message,
Severity = ReplaceEmptyStringWithStringDotEmptyAnalyzer.Severity,
Locations =
new[]
{
new DiagnosticResultLocation("Test0.cs", 9, 13)
}
};
VerifyCSharpDiagnostic(original, expectedDiagnostic);
VerifyCSharpFix(original, result);
}
protected override CodeFixProvider GetCSharpCodeFixProvider()
{
return new ReplaceEmptyStringWithStringDotEmptyCodeFix();
}
protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
{
return new ReplaceEmptyStringWithStringDotEmptyAnalyzer();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment