Skip to content

Commit

Permalink
Fix ZeroOrOne with default value (#125)
Browse files Browse the repository at this point in the history
Co-authored-by: Gustavo Mauricio de Barros <gustavomauriciodebarros@gmail.com>
  • Loading branch information
sebastienros and gumbarros authored May 28, 2024
1 parent 9aa53da commit 69bce37
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ dotnet_style_prefer_simplified_interpolation = true:suggestion
dotnet_style_readonly_field = true:suggestion

# Parameter preferences
dotnet_code_quality_unused_parameters = all:suggestion
dotnet_code_quality_unused_parameters = all:warning

# Suppression preferences
dotnet_remove_unnecessary_suppression_exclusions = none
Expand Down
2 changes: 1 addition & 1 deletion src/Parlot/Fluent/Parsers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static partial class Parsers
/// <summary>
/// Builds a parser that looks for zero or one time the specified parser.
/// </summary>
public static Parser<T> ZeroOrOne<T>(Parser<T> parser, T defaultValue = default) => new ZeroOrOne<T>(parser);
public static Parser<T> ZeroOrOne<T>(Parser<T> parser, T defaultValue = default) => new ZeroOrOne<T>(parser, defaultValue);

/// <summary>
/// Builds a parser that looks for zero or many times the specified parser.
Expand Down
2 changes: 1 addition & 1 deletion src/Parlot/Fluent/ZeroOrOne.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public CompilationResult Compile(CompilationContext context)
: Expression.IfThenElse(
parserCompileResult.Success,
Expression.Assign(value, parserCompileResult.Value),
Expression.Assign(value, Expression.Default(typeof(T)))
Expression.Assign(value, Expression.Constant(_defaultValue, typeof(T)))
)
)
);
Expand Down
11 changes: 10 additions & 1 deletion test/Parlot.Tests/CompileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,23 @@ public void ShouldCompileOneOrMany()
}

[Fact]
public void ShouldCompileZeroOrOne()
public void ShouldZeroOrOne()
{
var parser = ZeroOrOne(Terms.Text("hello")).Compile();

Assert.Equal("hello", parser.Parse(" hello world hello"));
Assert.Null(parser.Parse(" foo"));
}

[Fact]
public void ShouldZeroOrOneWithDefault()
{
var parser = ZeroOrOne(Terms.Text("hello"), "world").Compile();

Assert.Equal("world", parser.Parse(" this is an apple"));
Assert.Equal("hello", parser.Parse(" hello world"));
}

[Fact]
public void ShouldCompileBetweens()
{
Expand Down
22 changes: 20 additions & 2 deletions test/Parlot.Tests/FluentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ public void BetweenShouldParseBetweenTwoString()
[Fact]
public void TextShouldResetPosition()
{
var code = OneOf(Terms.Text("substract"), Terms.Text("substitute"));
var code = OneOf(Terms.Text("subtract"), Terms.Text("substitute"));

Assert.False(code.TryParse("sublime", out _));
Assert.True(code.TryParse("substract", out _));
Assert.True(code.TryParse("subtract", out _));
Assert.True(code.TryParse("substitute", out _));
}

Expand Down Expand Up @@ -872,5 +872,23 @@ public void ShouldParsePrefix(string expression, double result)

Assert.Equal(result, unary.Parse(expression));
}

[Fact]
public void ShouldZeroOrOne()
{
var parser = ZeroOrOne(Terms.Text("hello"));

Assert.Equal("hello", parser.Parse(" hello world hello"));
Assert.Null(parser.Parse(" foo"));
}

[Fact]
public void ShouldZeroOrOneWithDefault()
{
var parser = ZeroOrOne(Terms.Text("hello"), "world");

Assert.Equal("world", parser.Parse(" this is an apple"));
Assert.Equal("hello", parser.Parse(" hello world"));
}
}
}

0 comments on commit 69bce37

Please sign in to comment.