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

Add content() to LogAssert #237

Open
filiphr opened this issue Jan 22, 2022 · 2 comments
Open

Add content() to LogAssert #237

filiphr opened this issue Jan 22, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@filiphr
Copy link

filiphr commented Jan 22, 2022

Currently it is not easy to test whether the log file contains some kind of a string sub sequence.

It would be good if there is:

assertThat(result)
    .out()
    .content()
    .contains("Partial string");

Another alternative would be to override asString from AbstractObject assert and provide the same functionality.

e.g.

assertThat(result)
    .out()
    .asString()
    .contains("Partial string");
@filiphr filiphr added the enhancement New feature or request label Jan 22, 2022
@khmarbaise
Copy link
Owner

khmarbaise commented Jan 22, 2022

There is already a way to get the whole output simply by using .plain() like this:

assertThat(result)
 .out()
 .plain()
 .contains("WhatEver");

The .plain() is a ListAssert<String>..It is also possible to get the plain output from stderr like this:

assertThat(result)
 .err()
 .plain()
 .contains("WhatEver");

The .out() represents the output via stdout where as the .err() represents the output via stdout.

Apart from that this is also described in the users guide: https://khmarbaise.github.io/maven-it-extension/itf-documentation/usersguide/usersguide.html#_assertion_for_maven_log

Furthermore you can of course use:

assertThat(result)
 .out()
 .asString()
 .contains("WhatEver");

or:

assertThat(result)
 .err()
 .asString()
 .contains("WhatEver");

but to convert a list into a string (what asString() exactly does) would not really make sense to me...because the stdout as well as stderr are line oriented.

So it could be used like this:

assertThat(result)
 .out()
 .plain()
 .containsExactly("WhatEver", "Another");

Furthermore to get only parts of a line you have to filter out the appropriate line via filteredOn(...)(because you have a list and in the allSatisfy the condition for the single line...There you can check for sequence etc.

assertThat(result).out().plain()
   .filteredOn(s -> s.startsWith("XXX")).allSatisfy(s -> {
            assertThat(s).contains("partialString");
   });

If you are interested only in having a single line (of stdout) contain a partial content:

assertThat(result).out().plain().filteredOn(s -> s.contains("XXX")).hasSize(1);

@filiphr
Copy link
Author

filiphr commented Jan 22, 2022

Thanks for the detailed response @khmarbaise. I know about plain() and that is what I am using currently. However, the fact that it returns a ListAssert<String> means that I need to write things like:

 assertThat(result)
                .out()
                .error()
                .anySatisfy(error -> {
                    Assertions.assertThat(error)
                            .contains(
                                    "Partial"
                            );
                });

My contains example was only a small example. StringAssert from AssertJ offers a lot of other functionality that I can use in my assertions. That will provide more descriptive assertion messages. When I use the one I am currently using I get all the lines that did not match the condition. That is not super readable right now.

If I use

assertThat(result).out().plain().filteredOn(s -> s.contains("XXX")).hasSize(1);

The error message will only say that I am expecting to have a size of 1, but I had a size of 0. I won't be able to see immediately what the actual problem was.

Regarding the line endings, perhaps something could be done like the ``PathAssert#contentandPathAssert#content(Charset)`.

e.g.


  public AbstractStringAssert<?> content() {
    return return content(Charset.defaultCharset());
  }

  public AbstractStringAssert<?> content(Charset charset) {
    return Assertions.assertThat(charset);
  }

  private String readPath(Charset charset) {
    try {
      return new String(Files.readAllBytes(this.actual.getStdout()), charset);
    } catch (IOException e) {
      throw new UncheckedIOException(format("Failed to read %s content with %s charset", actual, charset), e);
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants