Skip to content

Commit 5768145

Browse files
committed
Preserve interpolation alignment and add coverage
1 parent 4409a24 commit 5768145

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

tests/Lucene.Net.CodeAnalysis.Dev.CodeFixes.Tests/LuceneDev1xxx/TestLuceneDev1006_FloatingPointFormattingInterpolationCSCodeFixProvider.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,68 @@ public string Message()
8787
await test.RunAsync();
8888
}
8989

90+
91+
[Test]
92+
public async Task TestFix_WithAlignmentAndFormatClause()
93+
{
94+
var testCode = @"namespace J2N.Numerics
95+
{
96+
public static class Single
97+
{
98+
public static string ToString(float value) => value.ToString();
99+
public static string ToString(float value, string format) => value.ToString(format);
100+
}
101+
}
102+
103+
public class C
104+
{
105+
private float levelBottom = 1f;
106+
107+
public string Message()
108+
{
109+
return $"" level {levelBottom,5:F2}"";
110+
}
111+
}
112+
";
113+
114+
var fixedCode = @"namespace J2N.Numerics
115+
{
116+
public static class Single
117+
{
118+
public static string ToString(float value) => value.ToString();
119+
public static string ToString(float value, string format) => value.ToString(format);
120+
}
121+
}
122+
123+
public class C
124+
{
125+
private float levelBottom = 1f;
126+
127+
public string Message()
128+
{
129+
return $"" level {J2N.Numerics.Single.ToString(levelBottom, ""F2""),5}"";
130+
}
131+
}
132+
";
133+
134+
var expected = new DiagnosticResult(Descriptors.LuceneDev1006_FloatingPointFormatting)
135+
.WithSeverity(DiagnosticSeverity.Warning)
136+
.WithMessageFormat(Descriptors.LuceneDev1006_FloatingPointFormatting.MessageFormat)
137+
.WithArguments("levelBottom")
138+
.WithLocation("/0/Test0.cs", line: 16, column: 27);
139+
140+
var test = new InjectableCodeFixTest(
141+
() => new LuceneDev1006_FloatingPointFormattingConcatenationCSCodeAnalyzer(),
142+
() => new LuceneDev1001_FloatingPointFormattingCSCodeFixProvider())
143+
{
144+
TestCode = testCode,
145+
FixedCode = fixedCode,
146+
ExpectedDiagnostics = { expected }
147+
};
148+
149+
await test.RunAsync();
150+
}
151+
90152
[Test]
91153
public async Task TestFix_WithFormatClause()
92154
{

tests/Lucene.Net.CodeAnalysis.Dev.Tests/LuceneDev1xxx/TestLuceneDev1006_FloatingPointFormattingInterpolationCSCodeAnalyzer.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,37 @@ public void M()
9393
await test.RunAsync();
9494
}
9595

96+
[Test]
97+
public async Task TestDiagnostic_WithAlignmentAndFormatClause()
98+
{
99+
var testCode = @"
100+
public class C
101+
{
102+
private float levelBottom = 1f;
103+
104+
private string Message(string value) => value;
105+
106+
public void M()
107+
{
108+
Message($"" level {levelBottom,5:F2}"");
109+
}
110+
}
111+
";
112+
113+
var expected = DiagnosticResult.CompilerWarning(Descriptors.LuceneDev1006_FloatingPointFormatting.Id)
114+
.WithMessageFormat(Descriptors.LuceneDev1006_FloatingPointFormatting.MessageFormat)
115+
.WithArguments("levelBottom")
116+
.WithLocation("/0/Test0.cs", line: 10, column: 28);
117+
118+
var test = new InjectableCSharpAnalyzerTest(() => new LuceneDev1006_FloatingPointFormattingConcatenationCSCodeAnalyzer())
119+
{
120+
TestCode = testCode,
121+
ExpectedDiagnostics = { expected }
122+
};
123+
124+
await test.RunAsync();
125+
}
126+
96127
[Test]
97128
public async Task TestNoDiagnostic_WhenValuesAlreadyFormatted()
98129
{

0 commit comments

Comments
 (0)