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

extracting(Function...) propagates Object instead of the common base type of all extractors #3372

Open
xenoterracide opened this issue Feb 21, 2024 · 6 comments
Labels
type: improvement A general improvement

Comments

@xenoterracide
Copy link

Feature summary

So, I'm noticing that my attempts to validate that this toString() and getVersion are returning the same thing is kind of painful.

Example

// using semver4j, note this won't pass because my code subclasses and stuff even then, but compiling


      var v010 = Semver.coerce("v0.1.0");

     // asserthat(v010).matchesToString(...) nope doesn't exist

      assertThat(v010)
        .extracting(Semver::getVersion, Semver::toString)
        .allSatisfy(o -> {
          assertThat(o).isInstanceOf(String.class); // I guess chaining off this assertion won't convince allowance of matches
          if (o instanceof String s) { // but couldn't o already have determined we're a string?
            assertThat(s).matches("^0\\.1\\.0-SNAPSHOT-1-g\\p{XDigit}{7}$");
          }
        });
@xenoterracide xenoterracide changed the title better comparison casting better comparison casting, and matching toString Feb 21, 2024
@joel-costigliola
Copy link
Member

joel-costigliola commented Feb 21, 2024

Have you given a try to extracting(String propertyOrField, InstanceOfAssertFactory) or asInstanceOf ?

Should look like

// ... import static InstanceOfAssertFactories.STRING
assertThat(v010)
        .extracting(Semver::getVersion, Semver::toString)
        .allSatisfy(o -> assertThat(o).asInstanceOf(STRING).matches("^0\\.1\\.0-SNAPSHOT-1-g\\p{XDigit}{7}$"));

@scordio
Copy link
Member

scordio commented Feb 21, 2024

What you're looking for is probably asInstanceOf(InstanceOfAssertFactory), as Joel already mentioned.

However, there could be a way to make extracting(Function...) and the following allSatisfy smarter, maybe with some tuning of the type parameters for cases where all the extractors produce a common type which is more specific than Object, with a strategy similar to #1551.

@scordio scordio changed the title better comparison casting, and matching toString extracting(Function...) does not allow type propagation when all extractors produce the same base type Feb 21, 2024
@scordio scordio added the type: improvement A general improvement label Feb 21, 2024
@scordio scordio changed the title extracting(Function...) does not allow type propagation when all extractors produce the same base type extracting(Function...) propagates Object instead of the common base type of all extractors Feb 21, 2024
@xenoterracide
Copy link
Author

both things sound great, hadn't heard of asInstanceOf before. Even less thinking about it though sounds like a better solution.

xenoterracide added a commit to xenoterracide/gradle-semver that referenced this issue Feb 23, 2024
@joel-costigliola
Copy link
Member

@xenoterracide shall we close this issue or is there something more to discuss?

@scordio
Copy link
Member

scordio commented Feb 23, 2024

I would keep it open to see if we can do something cheap like in #1551.

@xenoterracide
Copy link
Author

^ same

xenoterracide added a commit to xenoterracide/gradle-semver that referenced this issue Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: improvement A general improvement
Projects
None yet
Development

No branches or pull requests

3 participants