Skip to content

Commit 7efa51a

Browse files
committed
Add TextScanner tests
* Fix off-by-one error in error location reported by TextScanner.Assert * Fix error handling for invalid comments
1 parent b732303 commit 7efa51a

3 files changed

Lines changed: 62 additions & 5 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.LightJson.Serialization
5+
{
6+
using global::LightJson;
7+
using global::LightJson.Serialization;
8+
using Xunit;
9+
10+
public class TextScannerTests
11+
{
12+
[Fact]
13+
public void TestUnexpectedLookahead()
14+
{
15+
JsonParseException ex;
16+
17+
ex = Assert.ThrowsAny<JsonParseException>(() => JsonValue.Parse("trUe"));
18+
Assert.Equal(JsonParseException.ErrorType.InvalidOrUnexpectedCharacter, ex.Type);
19+
Assert.Equal(0, ex.Position.Line);
20+
Assert.Equal(2, ex.Position.Column);
21+
22+
ex = Assert.ThrowsAny<JsonParseException>(() => JsonValue.Parse("tr"));
23+
Assert.Equal(JsonParseException.ErrorType.IncompleteMessage, ex.Type);
24+
Assert.Equal(0, ex.Position.Line);
25+
Assert.Equal(2, ex.Position.Column);
26+
}
27+
28+
[Fact]
29+
public void TestIncompleteComment()
30+
{
31+
var ex = Assert.ThrowsAny<JsonParseException>(() => JsonValue.Parse("{ /1 }"));
32+
Assert.Equal(JsonParseException.ErrorType.InvalidOrUnexpectedCharacter, ex.Type);
33+
Assert.Contains("'1'", ex.Message);
34+
Assert.Equal(0, ex.Position.Line);
35+
Assert.Equal(3, ex.Position.Column);
36+
37+
ex = Assert.ThrowsAny<JsonParseException>(() => JsonValue.Parse("{ // ignored text }"));
38+
Assert.Equal(JsonParseException.ErrorType.IncompleteMessage, ex.Type);
39+
40+
ex = Assert.ThrowsAny<JsonParseException>(() => JsonValue.Parse("{ /* ignored text }"));
41+
Assert.Equal(JsonParseException.ErrorType.IncompleteMessage, ex.Type);
42+
}
43+
44+
[Fact]
45+
public void TestBlockCommentTermination()
46+
{
47+
var obj = JsonValue.Parse("{ /* * / */ }");
48+
Assert.Equal(JsonValueType.Object, obj.Type);
49+
Assert.Equal(0, obj.AsJsonObject.Count);
50+
}
51+
}
52+
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/StyleCop.Analyzers.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@
239239
<Compile Include="LightJson\JsonObjectTests.cs" />
240240
<Compile Include="LightJson\JsonReaderTests.cs" />
241241
<Compile Include="LightJson\JsonValueTests.cs" />
242+
<Compile Include="LightJson\Serialization\TextScannerTests.cs" />
242243
<Compile Include="Lightup\AccessorDeclarationSyntaxExtensionsTests.cs" />
243244
<Compile Include="Lightup\AutoWrapSeparatedSyntaxListTests.cs" />
244245
<Compile Include="Lightup\BaseMethodDeclarationSyntaxExtensionsTests.cs" />

StyleCop.Analyzers/StyleCop.Analyzers/LightJson/Serialization/TextScanner.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public void SkipWhitespace()
110110
}
111111
else if (next == '/')
112112
{
113-
this.SkipCommentOrInvalidSlash();
113+
this.SkipComment();
114114
continue;
115115
}
116116
else
@@ -127,12 +127,13 @@ public void SkipWhitespace()
127127
/// <param name="next">The expected character.</param>
128128
public void Assert(char next)
129129
{
130+
var errorPosition = this.position;
130131
if (this.Read() != next)
131132
{
132133
throw new JsonParseException(
133134
string.Format("Parser expected '{0}'", next),
134135
ErrorType.InvalidOrUnexpectedCharacter,
135-
this.position);
136+
errorPosition);
136137
}
137138
}
138139

@@ -149,9 +150,9 @@ public void Assert(string next)
149150
}
150151
}
151152

152-
private void SkipCommentOrInvalidSlash()
153+
private void SkipComment()
153154
{
154-
// First character is the a slash
155+
// First character is the first slash
155156
this.Read();
156157
switch (this.Peek())
157158
{
@@ -164,7 +165,10 @@ private void SkipCommentOrInvalidSlash()
164165
return;
165166

166167
default:
167-
return;
168+
throw new JsonParseException(
169+
string.Format("Parser expected '{0}'", this.Peek()),
170+
ErrorType.InvalidOrUnexpectedCharacter,
171+
this.position);
168172
}
169173
}
170174

0 commit comments

Comments
 (0)