Skip to content

Commit 47c9883

Browse files
committed
More tests.
1 parent 4bbf2e6 commit 47c9883

7 files changed

Lines changed: 75 additions & 31 deletions

File tree

AspNetCoreAnalyzers.Tests/Helpers/StringLiteralSpanTests.cs

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ namespace AspNetCoreAnalyzers.Tests.Helpers
77

88
public class StringLiteralSpanTests
99
{
10-
[TestCase("\"abc\"", "abc")]
11-
public void WhenEqual(string text, string expected)
10+
[TestCase("\"abc\"", "abc", true)]
11+
[TestCase("\"abc\"", "ab", false)]
12+
[TestCase("\"abc\"", "bc", false)]
13+
public void Equals(string text, string value, bool expected)
1214
{
1315
var syntaxTree = CSharpSyntaxTree.ParseText(@"
1416
namespace AspBox.Controllers
@@ -22,10 +24,52 @@ class C
2224
}
2325
}".AssertReplace("\"abc\"", text));
2426
var literal = syntaxTree.FindLiteralExpression(text);
25-
var span = new StringLiteralSpan(new StringLiteral(literal), 0, 3);
26-
Assert.AreEqual(true, span.StartsWith(expected, StringComparison.Ordinal));
27-
Assert.AreEqual(true, span.EndsWith(expected, StringComparison.Ordinal));
28-
Assert.AreEqual(true, span.Equals(expected, StringComparison.Ordinal));
27+
var span = new Span(new StringLiteral(literal), 0, 3);
28+
Assert.AreEqual(expected, span.Equals(value, StringComparison.Ordinal));
29+
}
30+
31+
[TestCase("\"abc\"", "abc", true)]
32+
[TestCase("\"abc\"", "ab", true)]
33+
[TestCase("\"abc\"", "a", true)]
34+
[TestCase("\"abc\"", "bc", false)]
35+
public void StartsWith(string text, string value, bool expected)
36+
{
37+
var syntaxTree = CSharpSyntaxTree.ParseText(@"
38+
namespace AspBox.Controllers
39+
{
40+
using Microsoft.AspNetCore.Mvc;
41+
42+
[Route(""abc"")]
43+
class C
44+
{
45+
46+
}
47+
}".AssertReplace("\"abc\"", text));
48+
var literal = syntaxTree.FindLiteralExpression(text);
49+
var span = new Span(new StringLiteral(literal), 0, 3);
50+
Assert.AreEqual(expected, span.StartsWith(value, StringComparison.Ordinal));
51+
}
52+
53+
[TestCase("\"abc\"", "abc", true)]
54+
[TestCase("\"abc\"", "bc", true)]
55+
[TestCase("\"abc\"", "c", true)]
56+
[TestCase("\"abc\"", "ab", false)]
57+
public void EndsWith(string text, string value, bool expected)
58+
{
59+
var syntaxTree = CSharpSyntaxTree.ParseText(@"
60+
namespace AspBox.Controllers
61+
{
62+
using Microsoft.AspNetCore.Mvc;
63+
64+
[Route(""abc"")]
65+
class C
66+
{
67+
68+
}
69+
}".AssertReplace("\"abc\"", text));
70+
var literal = syntaxTree.FindLiteralExpression(text);
71+
var span = new Span(new StringLiteral(literal), 0, 3);
72+
Assert.AreEqual(expected, span.EndsWith(value, StringComparison.Ordinal));
2973
}
3074
}
3175
}

AspNetCoreAnalyzers/Analyzers/AttributeAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private static bool HasWrongType(ParameterPair pair, out string correctType)
201201
correctType = null;
202202
return false;
203203

204-
bool TryGetType(StringLiteralSpan constraint, out QualifiedType type)
204+
bool TryGetType(Span constraint, out QualifiedType type)
205205
{
206206
if (constraint.Equals("bool", StringComparison.Ordinal))
207207
{

AspNetCoreAnalyzers/Helpers/PathSegment.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ public struct PathSegment
77
{
88
public PathSegment(StringLiteral literal, int start, int end)
99
{
10-
this.Span = new StringLiteralSpan(literal, start, end);
10+
this.Span = new Span(literal, start, end);
1111
this.Parameter = TemplateParameter.TryParse(this.Span, out var parameter)
1212
? parameter
1313
: (TemplateParameter?)null;
1414
}
1515

16-
public StringLiteralSpan Span { get; }
16+
public Span Span { get; }
1717

1818
public TemplateParameter? Parameter { get; }
1919

AspNetCoreAnalyzers/Helpers/RouteConstraint.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ namespace AspNetCoreAnalyzers
66
[DebuggerDisplay("{this.Span.ToString()}")]
77
public struct RouteConstraint : IEquatable<RouteConstraint>
88
{
9-
public RouteConstraint(StringLiteralSpan span)
9+
public RouteConstraint(Span span)
1010
{
1111
this.Span = span;
1212
}
1313

14-
public StringLiteralSpan Span { get; }
14+
public Span Span { get; }
1515

1616
public static bool operator ==(RouteConstraint left, RouteConstraint right)
1717
{
@@ -23,7 +23,7 @@ public RouteConstraint(StringLiteralSpan span)
2323
return !left.Equals(right);
2424
}
2525

26-
public static bool TryRead(StringLiteralSpan span, int pos, out RouteConstraint constraint)
26+
public static bool TryRead(Span span, int pos, out RouteConstraint constraint)
2727
{
2828
if (pos >= span.TextSpan.End ||
2929
span[pos] != ':')

AspNetCoreAnalyzers/Helpers/StringLiteralSpan.cs renamed to AspNetCoreAnalyzers/Helpers/Span.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace AspNetCoreAnalyzers
44
using Microsoft.CodeAnalysis;
55
using Microsoft.CodeAnalysis.Text;
66

7-
public struct StringLiteralSpan : IEquatable<StringLiteralSpan>
7+
public struct Span : IEquatable<Span>
88
{
9-
public StringLiteralSpan(StringLiteral literal, int start, int end)
9+
public Span(StringLiteral literal, int start, int end)
1010
{
1111
this.Literal = literal;
1212
this.TextSpan = new TextSpan(start, end - start);
@@ -20,24 +20,24 @@ public StringLiteralSpan(StringLiteral literal, int start, int end)
2020

2121
public char this[int index] => this.Literal.ValueText[this.TextSpan.Start + index];
2222

23-
public static bool operator ==(StringLiteralSpan left, StringLiteralSpan right)
23+
public static bool operator ==(Span left, Span right)
2424
{
2525
return left.Equals(right);
2626
}
2727

28-
public static bool operator !=(StringLiteralSpan left, StringLiteralSpan right)
28+
public static bool operator !=(Span left, Span right)
2929
{
3030
return !left.Equals(right);
3131
}
3232

33-
public bool Equals(StringLiteralSpan other)
33+
public bool Equals(Span other)
3434
{
3535
return this.Literal.Equals(other.Literal) && this.TextSpan == other.TextSpan;
3636
}
3737

3838
public override bool Equals(object obj)
3939
{
40-
return obj is StringLiteralSpan other &&
40+
return obj is Span other &&
4141
this.Equals(other);
4242
}
4343

@@ -59,7 +59,7 @@ public override string ToString() => this.TextSpan.Length == 0
5959

6060
public Location GetLocation(int start, int length) => this.Literal.GetLocation(new TextSpan(this.TextSpan.Start + start, length));
6161

62-
internal StringLiteralSpan Slice(int start, int end)
62+
internal Span Slice(int start, int end)
6363
{
6464
if (start > end)
6565
{
@@ -71,12 +71,12 @@ internal StringLiteralSpan Slice(int start, int end)
7171
throw new InvalidOperationException("Expected end to be less than TextSpan.End.");
7272
}
7373

74-
return new StringLiteralSpan(this.Literal, this.TextSpan.Start + start, this.TextSpan.Start + end);
74+
return new Span(this.Literal, this.TextSpan.Start + start, this.TextSpan.Start + end);
7575
}
7676

77-
internal StringLiteralSpan Substring(int index, int length)
77+
internal Span Substring(int index, int length)
7878
{
79-
return new StringLiteralSpan(this.Literal, this.TextSpan.Start + index, this.TextSpan.Start + index + length);
79+
return new Span(this.Literal, this.TextSpan.Start + index, this.TextSpan.Start + index + length);
8080
}
8181

8282
internal int IndexOf(char value, int startIndex = 0)
@@ -115,13 +115,13 @@ 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, comparisonType) == this.TextSpan.Start;
118+
return this.Literal.ValueText.IndexOf(value, this.TextSpan.Start, value.Length, comparisonType) == this.TextSpan.Start;
119119
}
120120

121121
internal bool EndsWith(string value, StringComparison comparisonType)
122122
{
123123
var start = this.TextSpan.End - value.Length;
124-
return this.Literal.ValueText.IndexOf(value, start, comparisonType) == start;
124+
return this.Literal.ValueText.IndexOf(value, start, value.Length, comparisonType) == start;
125125
}
126126
}
127127
}

AspNetCoreAnalyzers/Helpers/TemplateParameter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ namespace AspNetCoreAnalyzers
77
[DebuggerDisplay("{this.Name.ToString()}")]
88
public struct TemplateParameter : IEquatable<TemplateParameter>
99
{
10-
public TemplateParameter(StringLiteralSpan name, ImmutableArray<RouteConstraint> constraints)
10+
public TemplateParameter(Span name, ImmutableArray<RouteConstraint> constraints)
1111
{
1212
this.Name = name;
1313
this.Constraints = constraints;
1414
}
1515

16-
public StringLiteralSpan Name { get; }
16+
public Span Name { get; }
1717

1818
public ImmutableArray<RouteConstraint> Constraints { get; }
1919

@@ -27,7 +27,7 @@ public TemplateParameter(StringLiteralSpan name, ImmutableArray<RouteConstraint>
2727
return !left.Equals(right);
2828
}
2929

30-
public static bool TryParse(StringLiteralSpan span, out TemplateParameter result)
30+
public static bool TryParse(Span span, out TemplateParameter result)
3131
{
3232
var start = span.IndexOf('{');
3333
if (start < 0 ||

AspNetCoreAnalyzers/Helpers/Text.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace AspNetCoreAnalyzers
22
{
33
public static class Text
44
{
5-
public static void SkipWhiteSpace(StringLiteralSpan text, ref int pos)
5+
public static void SkipWhiteSpace(Span text, ref int pos)
66
{
77
while (pos < text.Length &&
88
text[pos] == ' ')
@@ -20,7 +20,7 @@ public static void SkipWhiteSpace(string text, ref int pos)
2020
}
2121
}
2222

23-
public static bool TrySkipPast(StringLiteralSpan text, ref int pos, string substring)
23+
public static bool TrySkipPast(Span text, ref int pos, string substring)
2424
{
2525
var before = pos;
2626
while (pos + substring.Length <= text.Length)
@@ -56,7 +56,7 @@ public static bool TrySkipPast(string text, ref int pos, string substring)
5656
return false;
5757
}
5858

59-
public static void BackWhiteSpace(StringLiteralSpan text, ref int pos)
59+
public static void BackWhiteSpace(Span text, ref int pos)
6060
{
6161
while (pos >= 0 &&
6262
text[pos] == ' ')
@@ -65,7 +65,7 @@ public static void BackWhiteSpace(StringLiteralSpan text, ref int pos)
6565
}
6666
}
6767

68-
private static bool IsAt(StringLiteralSpan text, int pos, string substring)
68+
private static bool IsAt(Span text, int pos, string substring)
6969
{
7070
for (var i = 0; i < substring.Length; i++)
7171
{

0 commit comments

Comments
 (0)