coverage for the name tokenizer reading hex

This commit is contained in:
Eliot Jones
2018-01-15 21:16:36 +00:00
parent 4fbea58657
commit 54b6374e7d
2 changed files with 41 additions and 11 deletions

View File

@@ -112,6 +112,42 @@
Assert.Equal(expected, AssertNameToken(token).Data.Name); Assert.Equal(expected, AssertNameToken(token).Data.Name);
} }
[Fact]
public void IgnoredInvalidHex()
{
var input = StringBytesTestConverter.Convert("/Invalid#AZBadHex");
var result = tokenizer.TryTokenize(input.First, input.Bytes, out var token);
Assert.True(result);
Assert.Equal("Invalid#AZBadHex", AssertNameToken(token).Data.Name);
}
[Fact]
public void IgnoreInvalidSingleHex()
{
var input = StringBytesTestConverter.Convert("/Invalid#Z");
var result = tokenizer.TryTokenize(input.First, input.Bytes, out var token);
Assert.True(result);
Assert.Equal("Invalid#Z", AssertNameToken(token).Data.Name);
}
[Fact]
public void EndsNameFollowingInvalidHex()
{
var input = StringBytesTestConverter.Convert("/Hex#/Name");
var result = tokenizer.TryTokenize(input.First, input.Bytes, out var token);
Assert.True(result);
Assert.Equal("Hex#", AssertNameToken(token).Data.Name);
}
private static NameToken AssertNameToken(IToken token) private static NameToken AssertNameToken(IToken token)
{ {
Assert.NotNull(token); Assert.NotNull(token);

View File

@@ -25,7 +25,7 @@
bool escapeActive = false; bool escapeActive = false;
int postEscapeRead = 0; int postEscapeRead = 0;
var escapedChars = new char[2]; var escapedChars = new char[2];
while (inputBytes.MoveNext()) while (inputBytes.MoveNext())
{ {
var b = inputBytes.CurrentByte; var b = inputBytes.CurrentByte;
@@ -43,16 +43,10 @@
if (postEscapeRead == 2) if (postEscapeRead == 2)
{ {
string hex = new string(escapedChars); var hex = new string(escapedChars);
try
{ var characterToWrite = (byte)Convert.ToInt32(hex, 16);
var characterToWrite = (byte)Convert.ToInt32(hex, 16); bytes.Add(characterToWrite);
bytes.Add(characterToWrite);
}
catch (FormatException e)
{
throw new InvalidOperationException("Error: expected hex digit, actual='" + hex + "'", e);
}
escapeActive = false; escapeActive = false;
postEscapeRead = 0; postEscapeRead = 0;