Skip to content

Commit 4599df9

Browse files
committed
Fix tests.
1 parent 3fcaed1 commit 4599df9

2 files changed

Lines changed: 27 additions & 15 deletions

File tree

AspNetCoreAnalyzers.Tests/Helpers/StringLiteralSpanTests.cs renamed to AspNetCoreAnalyzers.Tests/Helpers/SpanTests.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ namespace AspNetCoreAnalyzers.Tests.Helpers
55
using Microsoft.CodeAnalysis.CSharp;
66
using NUnit.Framework;
77

8-
public class StringLiteralSpanTests
8+
public class SpanTests
99
{
10-
[TestCase("\"abc\"", "abc", true)]
11-
[TestCase("\"abc\"", "ab", false)]
12-
[TestCase("\"abc\"", "bc", false)]
10+
[TestCase("\"abc\"", "abc", true)]
11+
[TestCase("\"abc\"", "ab", false)]
12+
[TestCase("\"abc\"", "bc", false)]
13+
[TestCase("\"abc\"", "a", false)]
14+
[TestCase("\"abc\"", "abcd", false)]
1315
public void Equals(string text, string value, bool expected)
1416
{
1517
var syntaxTree = CSharpSyntaxTree.ParseText(@"
@@ -28,10 +30,11 @@ class C
2830
Assert.AreEqual(expected, span.Equals(value, StringComparison.Ordinal));
2931
}
3032

31-
[TestCase("\"abc\"", "abc", true)]
32-
[TestCase("\"abc\"", "ab", true)]
33-
[TestCase("\"abc\"", "a", true)]
34-
[TestCase("\"abc\"", "bc", false)]
33+
[TestCase("\"abc\"", "abc", true)]
34+
[TestCase("\"abc\"", "ab", true)]
35+
[TestCase("\"abc\"", "a", true)]
36+
[TestCase("\"abc\"", "bc", false)]
37+
[TestCase("\"abc\"", "abcd", false)]
3538
public void StartsWith(string text, string value, bool expected)
3639
{
3740
var syntaxTree = CSharpSyntaxTree.ParseText(@"
@@ -50,10 +53,12 @@ class C
5053
Assert.AreEqual(expected, span.StartsWith(value, StringComparison.Ordinal));
5154
}
5255

53-
[TestCase("\"abc\"", "abc", true)]
54-
[TestCase("\"abc\"", "bc", true)]
55-
[TestCase("\"abc\"", "c", true)]
56-
[TestCase("\"abc\"", "ab", false)]
56+
[TestCase("\"abc\"", "abc", true)]
57+
[TestCase("\"abc\"", "bc", true)]
58+
[TestCase("\"abc\"", "c", true)]
59+
[TestCase("\"abc\"", "ab", false)]
60+
[TestCase("\"abc\"", "bc", false)]
61+
[TestCase("\"abc\"", "dabc", false)]
5762
public void EndsWith(string text, string value, bool expected)
5863
{
5964
var syntaxTree = CSharpSyntaxTree.ParseText(@"

AspNetCoreAnalyzers/Helpers/Span.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,20 @@ internal bool Equals(string value, StringComparison comparisonType)
115115

116116
internal bool StartsWith(string value, StringComparison comparisonType)
117117
{
118-
return this.Literal.ValueText.IndexOf(value, this.TextSpan.Start, value.Length, comparisonType) == this.TextSpan.Start;
118+
return this.Length >= value.Length &&
119+
this.Literal.ValueText.IndexOf(value, this.TextSpan.Start, value.Length, comparisonType) == this.TextSpan.Start;
119120
}
120121

121122
internal bool EndsWith(string value, StringComparison comparisonType)
122123
{
123-
var start = this.TextSpan.End - value.Length;
124-
return this.Literal.ValueText.IndexOf(value, start, value.Length, comparisonType) == start;
124+
if (this.Length >= value.Length)
125+
{
126+
var start = this.TextSpan.End - value.Length;
127+
return this.Literal.ValueText.IndexOf(value, start, value.Length, comparisonType) == start;
128+
}
129+
130+
131+
return false;
125132
}
126133
}
127134
}

0 commit comments

Comments
 (0)