Skip to content

Added optional error message to the Where combinator #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 14, 2021
Merged

Added optional error message to the Where combinator #131

merged 3 commits into from
Apr 14, 2021

Conversation

AndrewSav
Copy link
Contributor

Resolves #117

/// <returns>The resulting parser.</returns>
public static TokenListParser<TKind, T> Where<TKind, T>(this TokenListParser<TKind, T> parser, Func<T, bool> predicate)
public static TokenListParser<TKind, T> Where<TKind, T>(this TokenListParser<TKind, T> parser, Func<T, bool> predicate, string message = "unsatisfied condition")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If message is nullable then the type should be string?. Or if not, then an argument check is still required to catch misuse by consumers on earlier language versions. (Making it string? seems reasonable to me, as it would match Result<T>.ErrorMessage.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nblumhardt In which cases does it make sense to have the message to be null?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 - probably aren't any. string and the null check would be fine 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nblumhardt added.

@nblumhardt
Copy link
Member

Would it be possible to tack a test on with this one?

@AndrewSav
Copy link
Contributor Author

@nblumhardt the current Where does not have a test. IMO, the change is trivial enough, so if the method did not have the test before the change itself does not warrant it now. What do you think?

@nblumhardt
Copy link
Member

Thanks for the reply!

Originally, this codebase evolved pretty quickly, and test coverage is lower than it should be. I'm trying to bump it up a little with each change :-)

How about:

    public class WhereCombinatorTests
    {
        [Fact]
        public void WhereFailsIfPrecedingParserFails()
        {
            AssertParser.Fails(Character.EqualTo('a').Where(_ => true), "b");
        }

        [Fact]
        public void WhereSucceedsWhenPredicateMatches()
        {
            AssertParser.SucceedsWith(Character.EqualTo('a').Where(a => a == 'a'), "a", 'a');
        }

        [Fact]
        public void WhereFailsWhenPredicateDoesNotMatch()
        {
            AssertParser.FailsWithMessage(
                Character.EqualTo('a').Where(a => a != 'a', "character should be an A"), 
                "a",
                "Syntax error (line 1, column 1): character should be an A");
        }
    }

@AndrewSav
Copy link
Contributor Author

@nblumhardt thank you for this, I have added it.

@nblumhardt nblumhardt merged commit 9be4ea0 into datalust:dev Apr 14, 2021
@nblumhardt
Copy link
Member

Great, thanks 👍

@AndrewSav AndrewSav deleted the where-message branch April 14, 2021 08:46
@nblumhardt nblumhardt mentioned this pull request Sep 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom error message for Where combinator
2 participants