Skip to content

prints should accept an optional extra param "then" or similar to validate the result of the provided function #994

Open

Description

@kevmoo
No description provided.

Activity

  1. grouma commented on Mar 4, 2019

    @grouma
    Member

    I'm not sure I follow this request. Can you elaborate with a brief example?

  2. kevmoo commented on Mar 4, 2019

    @kevmoo
    MemberAuthor
    expect(() => process(1,2,3), prints('hello'));

    Fine, but what if I want to check a thrown exception or the result?

    expect(() => process(1,2,3), prints('wth, yo?').then(throwsArgumentError));
  3. lrhn commented on Mar 4, 2019

    @lrhn
    Member

    This is a reasonable request since prints is not checking the result, but checking side-effects created during the evaluation. You can have many side effects, and still have one result afterward. Other matchers check the result (value or thrown) and there can only be one of those.

    Maybe there could be a SideEffectMatcher with an optional Matcher then(Matcher) method and perhaps an SideEffectMatcher and(SideEffectMatcher) for chaining multiple side effects. Example:

    abstract class SideEffectMatcher implements Matcher {
      SideEffectMatcher and(SideEffectMatcher nextSideEffect);
      Matcher then(dynamic result);
    }

    Then prints would be a SideEffectMatcher.

  4. kevmoo commented on Mar 4, 2019

    @kevmoo
    MemberAuthor

    @lrhn – SGTM!

  5. natebosch commented on Mar 5, 2019

    @natebosch
    Member

    Does this work today with allOf?

    expect(() => process(1,2,3), allOf(prints('hello'), throwsArgumentError));

    Would the side effects need to be ordered?

  6. kevmoo commented on Mar 5, 2019

    @kevmoo
    MemberAuthor

    Tried that. It doesn't work.

  7. natebosch commented on Mar 5, 2019

    @natebosch
    Member

    We could consider trying to make the allof version work, though taking a look at the implementation that most likely means that the function would get run twice - once to check the prints and once to check the throwsArgumentError.

    Making multiple matchers cooperate and only run a Function() once does not look immediately feasible to me.

    If it's acceptable to let the function run once per matcher I can run with that. Otherwise we'll likely need a separate approach and provide a different utility.

    var printCapture = PrintCapture();
    expect(printCapture.wrap(() => process(1,2,3)), throwsArgumentError);
    expect(printCapture.captured, contains('hello'));
  8. kevmoo commented on Mar 5, 2019

    @kevmoo
    MemberAuthor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions