Skip to content

Commit

Permalink
Removing escape handling from tokeniser and refine tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick-Lucas committed Dec 21, 2017
1 parent 2855319 commit d0e4387
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 44 deletions.
22 changes: 5 additions & 17 deletions src/EntryPoint/Parsing/Tokeniser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ static IEnumerable<Token> TokeniseArgs(string[] args) {

static IEnumerable<Token> BasicTokenise(string args) {
bool isOption = false;
bool escaped = false;

StringBuilder token = new StringBuilder();
List<Token> tokens = new List<Token>();
Action StoreToken = () => {
void StoreToken() {
if (token.Length > 0) {
var t = token.ToString().Trim('"');
tokens.Add(new Token(t, isOption));
Expand All @@ -65,31 +64,20 @@ static IEnumerable<Token> BasicTokenise(string args) {
};

foreach (var c in args.ToCharArray()) {
if (!escaped && c == '\\') {
// If char is an unescaped escape, then set the escape state and continue
escaped = true;
continue;
}

if (!escaped && (c == '=')) {
// if char is an unescaped = then store the token and start again
if (c == '=') {
// if char is = then store the token and start again
// Whitespace splitting was already handled by .Net
StoreToken();

} else {
if (!escaped && token.Length == 0 && c == '-') {
if (token.Length == 0 && c == '-') {
// Mark the new token as an option
isOption = true;
}

// Otherwise append thiss char to the current token
// Otherwise append this char to the current token
token.Append(c);
}

if (escaped) {
// If this char was escape then switch off for the next char
escaped = false;
}
}
StoreToken();

Expand Down
30 changes: 3 additions & 27 deletions test/EntryPointTests/ParsingTests/TokeniserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,13 @@
namespace EntryPointTests.ParsingTests {
public class TokeniserTests {

[Fact]
public void Tokenise_EscaleSingleDash() {
string[] args = new string[] { "--hello", @"\-world" };
List<Token> expectedTokens = new List<Token>() {
new Token("--hello", true),
new Token("-world", false)
};

var tokens = Tokeniser.MakeTokens(args);
Assert.Equal(expectedTokens, tokens, new TokenEqualityComparer());
}

[Fact]
public void Tokenise_EscapeNonQuotedArg() {
string[] args = new string[] { "--hello", @"\--world" };
List<Token> expectedTokens = new List<Token>() {
new Token("--hello", true),
new Token(@"\--world", false)
};

var tokens = Tokeniser.MakeTokens(args);
Assert.Equal(expectedTokens, tokens, new TokenEqualityComparer());
}

// the whole world argument is in quotes here
// this text would be received if the user passed the argument in quotes (quotes would be stripped)
[Fact]
public void Tokenise_EscapeQuotedArg() {
string[] args = new string[] { "--hello", "\"--w\\orld\"" };
string[] args = new string[] { "--hello", @"w\orld" };
List<Token> expectedTokens = new List<Token>() {
new Token("--hello", true),
new Token("--world", false)
new Token(@"w\orld", false)
};

var tokens = Tokeniser.MakeTokens(args);
Expand Down

0 comments on commit d0e4387

Please sign in to comment.