Skip to content

Commit

Permalink
Fixing unexpected exception types
Browse files Browse the repository at this point in the history
Fixes #148
  • Loading branch information
sebastienros committed Sep 26, 2019
1 parent 1b5a4e0 commit 4931af9
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 6 deletions.
11 changes: 11 additions & 0 deletions Fluid.Tests/NumberFiltersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ public void Modulo()
Assert.Equal(2, result.ToNumberValue());
}

[Fact]
public void ModuloWithNoArgumentThrows()
{
var input = NumberValue.Create(6);

var arguments = FilterArguments.Empty;
var context = new TemplateContext();

Assert.Throws<ParseException>(() => NumberFilters.Modulo(input, arguments, context));
}

[Fact]
public void Plus()
{
Expand Down
23 changes: 22 additions & 1 deletion Fluid.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Fluid.Tests
{
public class ParserTests
{
private static LanguageData _language = new LanguageData(new FluidGrammar());
private static readonly LanguageData _language = new LanguageData(new FluidGrammar());

private List<Statement> Parse(string source)
{
Expand Down Expand Up @@ -328,5 +328,26 @@ public void ShouldBeAbleToCompareNilValues()
result.Render(new TemplateContext { Model = model });
}
}

[Theory]
[InlineData("{% for %}")]
[InlineData("{% endfor %}")]
[InlineData("{% case %}")]
[InlineData("{% endcase %}")]
[InlineData("{% if %}")]
[InlineData("{% endif %}")]
[InlineData("{% unless %}")]
[InlineData("{% endunless %}")]
[InlineData("{% comment %}")]
[InlineData("{% endcomment %}")]
[InlineData("{% raw %}")]
[InlineData("{% endraw %}")]
[InlineData("{% capture %}")]
[InlineData("{% endcapture %}")]

public void ShouldThrowParseExceptionMissingTag(string template)
{
Assert.Throws<ParseException>(() => FluidTemplate.Parse(template));
}
}
}
15 changes: 13 additions & 2 deletions Fluid.Tests/StringFiltersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,19 @@ public void Remove()
var result = StringFilters.Remove(input, arguments, context);

Assert.Equal("acac", result.ToStringValue());
}

}

[Fact]
public void RemovesReturnsInputWhenArgumentIsEmpty()
{
var input = new StringValue("abcabc");

var arguments = FilterArguments.Empty;
var context = new TemplateContext();

var result = StringFilters.Remove(input, arguments, context);
}

[Fact]
public void ReplaceFirst()
{
Expand Down
40 changes: 38 additions & 2 deletions Fluid/DefaultFluidParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,7 @@ private StringSegment ConsumeTag(StringSegment segment, int start, string endTag

// We reached the end of the segment without finding the matched tag.
// Ideally we could return a parsing error, right now we just return the text.
end = segment.Length - 1;
return segment.Subsegment(start, index - start);
throw new ParseException($"Expected '{endTag}'");
}

/// <summary>
Expand Down Expand Up @@ -549,6 +548,12 @@ public virtual Statement BuildTagStatement(ParseTreeNode node)
break;

case "endcomment":

if (!_isComment)
{
throw new ParseException($"Expected tag 'comment' before 'endcomment'");
}

_isComment = false;
break;

Expand All @@ -557,6 +562,12 @@ public virtual Statement BuildTagStatement(ParseTreeNode node)
break;

case "endraw":

if (!_isRaw)
{
throw new ParseException($"Expected tag 'raw' before 'endraw'");
}

_isRaw = false;
break;

Expand Down Expand Up @@ -621,6 +632,11 @@ public virtual Statement BuildTagStatement(ParseTreeNode node)

public static CaptureStatement BuildCaptureStatement(BlockContext context)
{
if (context.Tag == null)
{
throw new ParseException($"Expected tag 'capture' before 'endcapture'");
}

if (context.Tag.Term.Name != "capture")
{
throw new ParseException($"Unexpected tag: '{context.Tag.Term.Name}' not matching 'capture' tag.");
Expand Down Expand Up @@ -722,6 +738,11 @@ public virtual OutputStatement BuildOutputStatement(ParseTreeNode node)

public static IfStatement BuildIfStatement(BlockContext context)
{
if (context.Tag == null)
{
throw new ParseException($"Expected tag 'if' before 'endif'");
}

if (context.Tag.Term.Name != "if")
{
throw new ParseException($"Unexpected tag: '{context.Tag.Term.Name}' not matching 'if' tag.");
Expand All @@ -742,6 +763,11 @@ public static IfStatement BuildIfStatement(BlockContext context)

public static CaseStatement BuildCaseStatement(BlockContext context)
{
if (context.Tag == null)
{
throw new ParseException($"Expected tag 'case' before 'endcase'");
}

if (context.Tag.Term.Name != "case")
{
throw new ParseException($"Unexpected tag: '{context.Tag.Term.Name}' not matching 'case' tag.");
Expand All @@ -766,6 +792,11 @@ public static CaseStatement BuildCaseStatement(BlockContext context)

public virtual UnlessStatement BuildUnlessStatement(BlockContext context)
{
if (context.Tag == null)
{
throw new ParseException($"Expected tag 'unless' before 'endunless'");
}

if (context.Tag.Term.Name != "unless")
{
throw new ParseException($"Unexpected tag: '{context.Tag.Term.Name}' not matching 'unless' tag.");
Expand Down Expand Up @@ -794,6 +825,11 @@ public virtual UnlessStatement BuildUnlessStatement(BlockContext context)

public static Statement BuildForStatement(BlockContext context)
{
if (context.Tag == null)
{
throw new ParseException($"Expected tag 'for' before 'endfor'");
}

if (context.Tag.Term.Name != "for")
{
throw new ParseException($"Unexpected tag: '{context.Tag.Term.Name}' not matching 'for' tag.");
Expand Down
5 changes: 5 additions & 0 deletions Fluid/Filters/NumberFilters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public static FluidValue Minus(FluidValue input, FilterArguments arguments, Temp

public static FluidValue Modulo(FluidValue input, FilterArguments arguments, TemplateContext context)
{
if (arguments.Count == 0)
{
throw new ParseException("The filter 'modulo' requires an argument.");
}

return NumberValue.Create(Convert.ToInt32(input.ToNumberValue()) % Convert.ToInt32(arguments.At(0).ToNumberValue()));
}

Expand Down
9 changes: 8 additions & 1 deletion Fluid/Filters/StringFilters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,14 @@ public static FluidValue RemoveFirst(FluidValue input, FilterArguments arguments

public static FluidValue Remove(FluidValue input, FilterArguments arguments, TemplateContext context)
{
return new StringValue(input.ToStringValue().Replace(arguments.At(0).ToStringValue(), ""));
var argument = arguments.At(0).ToStringValue();

if (String.IsNullOrEmpty(argument))
{
return new StringValue(input.ToStringValue());
}

return new StringValue(input.ToStringValue().Replace(argument, ""));
}

public static FluidValue ReplaceFirst(FluidValue input, FilterArguments arguments, TemplateContext context)
Expand Down

0 comments on commit 4931af9

Please sign in to comment.