Skip to content
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

How to know which Matcher was used when using Matchers.anyOf. #38

Open
xak2000 opened this issue Mar 22, 2016 · 3 comments
Open

How to know which Matcher was used when using Matchers.anyOf. #38

xak2000 opened this issue Mar 22, 2016 · 3 comments
Assignees

Comments

@xak2000
Copy link

xak2000 commented Mar 22, 2016

For example I want to do:

Result result = expect.expect(anyOf(contains("reconfigure"), regexp("# $")));
if (/* first matcher was used */) {
  // The output contains "reconfigure" string.
  // Do something.
} else if (/* second matcher was used */) {
  // The output doesn't contains "reconfigure" string, but contains "# $" regexp.
  // Do something else.
}

How could I know the index of used matcher?

@xak2000
Copy link
Author

xak2000 commented Mar 22, 2016

Now I use:

MultiResult result = expect.expect(anyOf(contains("reconfigure"), regexp("# $")));
if (result.getResults().get(0).isSuccessful()) {
    // The output contains "reconfigure" string.
} else {
    // The output doesn't contains "reconfigure" string, but contains "# $" regexp.
}

Is it correct way?
Is there a way to just get an index of matched pattern (0 or 1 in this case). Something like result.getMatchedIndex(). Maybe then this is a feature request. :)

@agavrilov76 agavrilov76 self-assigned this Apr 6, 2016
@agavrilov76
Copy link
Owner

Sorry for delay. Somehow I missed this github notification among the others.

Note that MultiResult implements Result and delegates their methods to the matched result. So the MultiResult instance will contain the matching information either of contains("reconfigure") or regexp("# $").

You can also consider using interact feature. For example:

        expect.interact()
                .when(contains("reconfigure")).then(r -> System.out.println("A"))
                .when(regexp("# $")).then(r -> System.err.println("B"))
                .until(contains("END"));

@xak2000
Copy link
Author

xak2000 commented Apr 7, 2016

Don't worry! The result.getResults().get(0).isSuccessful() solution works fine.

I know that MultiResult implements Result, but I do not understand how it can help me to recognize which matcher is matched from passed to anyOf. Comparing strings? It is not better then just using isSuccessful().

About interact it is definitely interesting feature! Maybe I will try it later. At the moment I'm entirely happy with the result.getResults().get(0).isSuccessful() solution.

By method getMatchedIndex() in MultiResult I meant something like:

public int getMatchedIndex() {
  for(int i = 0; i < results.size(); i++) {
    if (results.get(i).isSuccessful()) {
      return i;
    }
  }
  return -1;
}

But now I'm not sure if this method is really necessary... It will only return correct result for anyOf matcher but not for allOf matcher. For allOf matcher this method has no sense.

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

No branches or pull requests

2 participants