-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>() | ||
{ | ||
{"&", "&"}, {"<", "<"}, {">", ">"} | ||
}; | ||
|
||
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("&", "&", TokenType.Separator).Equals(result)); | ||
} | ||
|
||
[Test] | ||
public void replace_less_with_lt() | ||
{ | ||
var result = Reader.ReadToken("<."); | ||
Assert.True(new Token("<", "<", TokenType.Separator).Equals(result)); | ||
} | ||
|
||
[Test] | ||
public void replace_greater_with_gt() | ||
{ | ||
var result = Reader.ReadToken(">we"); | ||
Assert.True(new Token(">", ">", TokenType.Separator).Equals(result)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters