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

Decouple Specimen from SpecimenType #109

Open
Nylle opened this issue Aug 25, 2024 · 0 comments
Open

Decouple Specimen from SpecimenType #109

Nylle opened this issue Aug 25, 2024 · 0 comments
Assignees

Comments

@Nylle
Copy link
Owner

Nylle commented Aug 25, 2024

I noticed that *Specimen-classes have a weird relationship to the SpecimenType-class.

In order to decide whether a specific Specimen can be used for a given SpecimenType, the SpecimenFactory asks the SpecimenType.

Example:
SpecimenFactory:

if (type.isTimeType()) {
    return new TimeSpecimen<>(type, context);
}

SpecimenType:

public boolean isTimeType() {
    if (Temporal.class.isAssignableFrom(asClass())) {
        return true;
    }
    if (TemporalAdjuster.class.isAssignableFrom(asClass())) {
        return true;
    }
    if (TemporalAmount.class.isAssignableFrom(asClass())) {
        return true;
    }
    if (asClass().equals(ZoneId.class)) {
        return true;
    }
    if (asClass().equals(java.util.Date.class)) {
        return true;
    }
    if (asClass().equals(java.sql.Date.class)) {
        return true;
    }
    return false;
}

It seems weird to me that a specimen cannot decide for itself. It also introduces some oddly specific "configuration" in the SpecimenType like:

public boolean isSpecialType() {
    if (asClass().equals(java.math.BigInteger.class)) {
        return true;
    }
    if (asClass().equals(java.math.BigDecimal.class)) {
        return true;
    }
    if (asClass().equals(java.io.File.class)) {
        return true;
    }
    if (asClass().equals(java.net.URI.class)) {
        return true;
    }
    return false;
}

...which in this case covers some classes Fixture cannot instantiate out-of-the-box and hence has to defer to the SpecialSpecimen.

Some helper-methods in SpecimenType seem reasonable as they are simply forwarding class-information, e.g.

    public boolean isEnum() {
        return asClass().isEnum();
    }

The above mentioned helpers isTimeType() and isSpecialType() however seem out of place as Specimen-specific information is shared across classes, one of which should not have the responsibility to "know" this information.

I'd like to refactor the code to allow each Specimen to decide on its own whether it can handle a specific type, removing this responsibility from SpecimenType.

This will have the following advantages:

  • Specimens are self-contained
  • SpecimenType can be simplified by removing methods
  • SpecimenFactory can be simplified by replacing the if-else-chain by a simple loop
  • It will be easier to register custom Specimens in the future, e.g. for expanded support of KotlinFixture
@Nylle Nylle self-assigned this Aug 25, 2024
Nylle added a commit that referenced this issue Aug 25, 2024
This will add two things to each specimen: a static method to determine
what type it supports, and a "Spec" that can create the related
specimen.

Refs: #109
Nylle added a commit that referenced this issue Aug 25, 2024
Nylle added a commit that referenced this issue Aug 25, 2024
Nylle added a commit that referenced this issue Aug 25, 2024
Also, create and return IMeta on the fly and statically.

Refs: #109
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant