diff --git a/.editorconfig b/.editorconfig index dbbf539..5e84efc 100644 --- a/.editorconfig +++ b/.editorconfig @@ -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 diff --git a/src/Parlot/Fluent/Parsers.cs b/src/Parlot/Fluent/Parsers.cs index 9a168ce..2a46a20 100644 --- a/src/Parlot/Fluent/Parsers.cs +++ b/src/Parlot/Fluent/Parsers.cs @@ -29,7 +29,7 @@ public static partial class Parsers /// /// Builds a parser that looks for zero or one time the specified parser. /// - public static Parser ZeroOrOne(Parser parser, T defaultValue = default) => new ZeroOrOne(parser); + public static Parser ZeroOrOne(Parser parser, T defaultValue = default) => new ZeroOrOne(parser, defaultValue); /// /// Builds a parser that looks for zero or many times the specified parser. diff --git a/src/Parlot/Fluent/ZeroOrOne.cs b/src/Parlot/Fluent/ZeroOrOne.cs index 5bff419..96d42a4 100644 --- a/src/Parlot/Fluent/ZeroOrOne.cs +++ b/src/Parlot/Fluent/ZeroOrOne.cs @@ -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))) ) ) ); diff --git a/test/Parlot.Tests/CompileTests.cs b/test/Parlot.Tests/CompileTests.cs index 290177d..b20276e 100644 --- a/test/Parlot.Tests/CompileTests.cs +++ b/test/Parlot.Tests/CompileTests.cs @@ -194,7 +194,7 @@ public void ShouldCompileOneOrMany() } [Fact] - public void ShouldCompileZeroOrOne() + public void ShouldZeroOrOne() { var parser = ZeroOrOne(Terms.Text("hello")).Compile(); @@ -202,6 +202,15 @@ public void ShouldCompileZeroOrOne() 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() { diff --git a/test/Parlot.Tests/FluentTests.cs b/test/Parlot.Tests/FluentTests.cs index 424ebf4..1caf8cd 100644 --- a/test/Parlot.Tests/FluentTests.cs +++ b/test/Parlot.Tests/FluentTests.cs @@ -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 _)); } @@ -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")); + } } }