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

skipIf(Predicate) #3432

Open
xenoterracide opened this issue Apr 17, 2024 · 2 comments
Open

skipIf(Predicate) #3432

xenoterracide opened this issue Apr 17, 2024 · 2 comments

Comments

@xenoterracide
Copy link

xenoterracide commented Apr 17, 2024

Feature summary

So the problem with the example is there isn't always a valid greater than, and I'm making up versions that I won't be generating just to fill it in. I've realized it would be nice to have a way to skip or pass a single assertion if it met certain criteria

I'd change the impossible values to null and do

assertThat(...).skipIf(v -> greaterThan == null);

Obviously I can just do an if statement or another test. Somehow to me this feels cleaner, as technically the code still executes.

BDD given/assumeThat isn't the same, because I don't want to skip the entire test, I just want to not fail this 1 assertion.

it'd be almost equivalent to the following, except the assertion wouldn't be called at all here, and it should be with skipIf

    if (greaterThan != null) assertThat(new ComparableVersion(semv.toString()))
      .describedAs("mvn")
      .isGreaterThan(new ComparableVersion(greaterThan));

could also call it passIf, unless (very perl-ish). I thought of using filter too but I think that'd be confusing. I suppose when thinking about chaining though

// would pass even if lessThan is false
assertThat(...).passIf( t -> greaterThan == null).isLessThan(...).isGreaterThan(greaterThan);
// would fail if less than is false but wouldn't run greater than, works like filter
assertThat(...).isLessThan(...).skipIf( t -> greaterThan == null).isGreaterThan(greaterThan);
// honestly seems same as passif to me because perl. Those only make sense at beginning or end of chain. 
assertThat(...).isLessThan(...).isGreaterThan(greaterThan).unless( t -> greaterThan == null );

Possibly has some additional benefit if integrating with Soft Assertions (I find "Soft" weirdly named, and didn't know they were a thing before now), as maybe they can "log" this.

note: I'm fairly confident that this comparison stuff is not the first time I've wanted this.

Example

class SemverBuilderTest {

  @ParameterizedTest
  @ArgumentsSource(VersionProvider.class)
  void gitMetadata(GitMetadata gitMetadata, String expected, String comp, String lessThan, String greaterThan) {
    var semv = new SemverBuilder(gitMetadata).build();
    assertThat(semv).describedAs("equal").isEqualTo(new Semver(expected));
    assertThat(semv).describedAs("comparing").isEqualByComparingTo(new Semver(comp));
    assertThat(semv).describedAs("lessThan").isLessThan(new Semver(lessThan));
    assertThat(new ComparableVersion(semv.toString())).describedAs("mvn").isLessThan(new ComparableVersion(lessThan));
    assertThat(new ComparableVersion(semv.toString()))
      .describedAs("mvn")
      .isGreaterThan(new ComparableVersion(greaterThan));
  }

  static class VersionProvider implements ArgumentsProvider {

    @Override
    public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
      return Stream.of(
        arguments(
          new GitMetadataInfo(0, GitStatus.NO_REPO, null, null),
          "0.0.0-alpha.0.0",
          "0.0.0-alpha.0.0",
          "0.0.0-alpha.0.1",
          "0.0.0-alpha.0.0+0" // not something we're generating
        ),
        arguments(
          new GitMetadataInfo(0, GitStatus.CLEAN, "abcdef10", null),
          "0.0.0-alpha.0.0+abcdef10",
          "0.0.0-alpha.0.0",
          "0.0.0-alpha.0.1",
          "0.0.0-alpha.0" // not something we're generating
        ),
        arguments(
          new GitMetadataInfo(0, GitStatus.DIRTY, "abcdef10", null),
          "0.0.0-alpha.0.0+abcdef10.dirty",
          "0.0.0-alpha.0.0",
          "0.0.0-alpha.0.1",
          "0.0.0-alpha.0"
        )
      );
    }
  }
@joel-costigliola
Copy link
Member

Have you tried using satisfies with a consumer implementing you specific needs ?

@xenoterracide
Copy link
Author

Unless I'm mistaken satisfies doesn't at all do what I'm proposing with a skipIf because I would still just end up using an if statement to not fail.

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