Skip to content

Commit

Permalink
cake-buildGH-1547: Fix issue with parsing escaped curly braces in For…
Browse files Browse the repository at this point in the history
…matParser, add tests
  • Loading branch information
kcamp committed Mar 29, 2017
1 parent acc5ee0 commit 3d1f208
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
70 changes: 68 additions & 2 deletions src/Cake.Tests/Unit/Diagnostics/FormatParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,72 @@ public void Returns_Correct_Token_For_Message_With_One_Property()
Assert.Equal(0, ((PropertyToken)result[0]).Position);
}

[Fact]
public void Should_Return_Literal_Tokens_For_Message_With_Nothing_But_Escaped_Braces()
{
// Given, When
var result = FormatParser.Parse("{{}}").ToArray();

// Then
Assert.Equal(2, result.Length);
Assert.IsType<LiteralToken>(result[0]);
Assert.Equal("{", ((LiteralToken)result[0]).Text);
Assert.Equal("}", ((LiteralToken)result[1]).Text);
}

[Fact]
public void Should_Return_Literal_Tokens_For_Message_With_Escaped_Braces_And_Properties()
{
// Given, When
var result = FormatParser.Parse("{{}} {0}").ToArray();

// Then
Assert.Equal(3, result.Length);
Assert.IsType<LiteralToken>(result[0]);
Assert.Equal("{", ((LiteralToken)result[0]).Text);
Assert.Equal("} ", ((LiteralToken)result[1]).Text);

Assert.IsType<PropertyToken>(result[2]);
Assert.Equal(0, ((PropertyToken)result[2]).Position);
Assert.Equal(null, ((PropertyToken)result[2]).Format);
}

[Fact]
public void Should_Return_Literal_Token_For_Unbalanced_Escaped_Opening_Curly_Braces()
{
// Given, When
var result = FormatParser.Parse("{{ test {0}").ToArray();

// Then
Assert.Equal(3, result.Length);
Assert.IsType<LiteralToken>(result[0]);
Assert.Equal("{", ((LiteralToken)result[0]).Text);
Assert.Equal(" test ", ((LiteralToken)result[1]).Text);

Assert.IsType<PropertyToken>(result[2]);
Assert.Equal(0, ((PropertyToken)result[2]).Position);
Assert.Equal(null, ((PropertyToken)result[2]).Format);
}

[Fact]
public void Should_Return_Literal_Token_For_Unbalanced_Escaped_Closing_Curly_Braces()
{
// Given, When
var result = FormatParser.Parse("test {0:d} }}").ToArray();

// Then
Assert.Equal(3, result.Length);
Assert.IsType<LiteralToken>(result[0]);
Assert.Equal("test ", ((LiteralToken)result[0]).Text);

Assert.IsType<PropertyToken>(result[1]);
Assert.Equal(0, ((PropertyToken)result[1]).Position);
Assert.Equal("d", ((PropertyToken)result[1]).Format);

Assert.IsType<LiteralToken>(result[2]);
Assert.Equal(" }", ((LiteralToken)result[2]).Text);
}

[Fact]
public void Should_Return_Literal_Tokens_For_Message_With_Escaped_Braces()
{
Expand All @@ -56,8 +122,8 @@ public void Should_Return_Literal_Tokens_For_Message_With_Escaped_Braces()
// Then
Assert.Equal(2, result.Length);
Assert.IsType<LiteralToken>(result[0]);
Assert.Equal("{{", ((LiteralToken)result[0]).Text);
Assert.Equal("0}}", ((LiteralToken)result[1]).Text);
Assert.Equal("{", ((LiteralToken)result[0]).Text);
Assert.Equal("0}", ((LiteralToken)result[1]).Text);
}

[Fact]
Expand Down
8 changes: 7 additions & 1 deletion src/Cake/Diagnostics/Formatting/FormatParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private static FormatToken ParseProperty(TextReader reader)
if ((char)reader.Peek() == '{')
{
reader.Read();
return new LiteralToken("{{");
return new LiteralToken("{");
}
var builder = new StringBuilder();
while (true)
Expand Down Expand Up @@ -106,6 +106,12 @@ private static FormatToken ParseText(TextReader reader)
{
break;
}
if (character == '}' && reader.Peek() == '}')
{
// escaped curly sequence, consume the first character,
// let the iteration/append continue below.
reader.Read();
}
builder.Append((char)reader.Read());
}
return new LiteralToken(builder.ToString());
Expand Down

0 comments on commit 3d1f208

Please sign in to comment.