Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Ramstack.Parsing/Parser.Choice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public override bool TryParse(ref ParseContext context, [NotNullWhen(true)] out
}

context.RestoreDiagnosticState(state);
context.ReportExpected(Name);
value = default;
return false;
}
Expand Down
22 changes: 22 additions & 0 deletions tests/Ramstack.Parsing.Tests/ParsersTests.Choice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,26 @@ public void ChoiceTest_2()
parsers.Map(m => (m.Index, m.Length)).Parse("Sunset").Value,
Is.EqualTo((0, 3)));
}

[Test]
public void Choice_NamedParser_ReportsName()
{
// The unnamed alternative makes this a ChoiceParser rather than
// DeferredDiagnosticChoiceParser.
var parser = Choice(L('a'), L('b').Between(L('('), L(')'))).As("value");

Assert.That(
parser.Parse("?").ErrorMessage,
Is.EqualTo("(1:1) Expected value"));
}

[Test]
public void Choice_UnnamedParser_ReportsAlternatives()
{
var parser = Choice(L('a'), L('b').Between(L('('), L(')')));

Assert.That(
parser.Parse("?").ErrorMessage,
Is.EqualTo("(1:1) Expected 'a' or '('"));
}
}
Loading