Skip to content

Commit

Permalink
separators reader added
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton92nd committed Nov 10, 2014
1 parent 9e7fc66 commit ba12e0e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
32 changes: 32 additions & 0 deletions HTMLParser/SeparatorReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Mark.HTMLParser
{
public class SeparatorReader : ITokenReader
{
private static readonly char[] Separators = new char[] {'.', ',', '\'', '\"', '!', '?', '%', '@',
'#', '$', '^', '&', '*', '/', '<', '>', '-', '=' };

private static readonly Dictionary<string, string> NeedEscape = new Dictionary<string, string>()
{
{"&", "&amp;"}, {"<", "&lt;"}, {">", "&gt;"}
};

public Token ReadToken(string from)
{
if (from.Length == 0)
return null;
string result = from.Substring(0, 1);
if (NeedEscape.ContainsKey(result))
{
result = NeedEscape[result];
}
return Separators.Contains(from[0]) ? new Token(from.Substring(0, 1), result, TokenType.Separator) : null;
}
}
}
1 change: 1 addition & 0 deletions Mark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<Compile Include="HTMLParser\EscapeReader.cs" />
<Compile Include="HTMLParser\LineEndReader.cs" />
<Compile Include="HTMLParser\Parser.cs" />
<Compile Include="HTMLParser\SeparatorReader.cs" />
<Compile Include="HTMLParser\Token.cs" />
<Compile Include="HTMLParser\ITokenReader.cs" />
<Compile Include="HTMLParser\UnderscoreReader.cs" />
Expand Down
51 changes: 51 additions & 0 deletions Tests/SeparatorReader_should.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using Mark.HTMLParser;

namespace Tests
{
[TestFixture]
class SeparatorReader_should
{
private static readonly SeparatorReader Reader = new SeparatorReader();

[Test]
public void return_null_if_empty_string()
{
var result = Reader.ReadToken("");
Assert.IsNull(result);
}

[Test]
public void read_single_separator()
{
var result = Reader.ReadToken(",.");
Assert.True(new Token(",", TokenType.Separator).Equals(result));
}

[Test]
public void replace_ampersand_with_amp()
{
var result = Reader.ReadToken("&.");
Assert.True(new Token("&", "&amp;", TokenType.Separator).Equals(result));
}

[Test]
public void replace_less_with_lt()
{
var result = Reader.ReadToken("<.");
Assert.True(new Token("<", "&lt;", TokenType.Separator).Equals(result));
}

[Test]
public void replace_greater_with_gt()
{
var result = Reader.ReadToken(">we");
Assert.True(new Token(">", "&gt;", TokenType.Separator).Equals(result));
}
}
}
1 change: 1 addition & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<Compile Include="LineEndReader_should.cs" />
<Compile Include="Parser_should.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SeparatorReader_should.cs" />
<Compile Include="UnderscoreReader_should.cs" />
<Compile Include="WhitespaceReader_should.cs" />
<Compile Include="WordReader_should.cs" />
Expand Down

0 comments on commit ba12e0e

Please sign in to comment.