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

Allow configuration annotations from a generator used with @From onto the parameter #279

Open
SimY4 opened this issue Oct 8, 2020 · 4 comments
Labels

Comments

@SimY4
Copy link

SimY4 commented Oct 8, 2020

Consider a generator like this:

@MyAnnotation(value = "Some default")
class MyClassGenerator extends Generator<MyClass> {
  private MyAnnotation myAnnotation;

  ...

  public MyClass generate(SourceOfRandomness random, GenerationStatus status) {
    MyClassFieldGenerator myClassFieldGenerator = gen().make(MyClassFieldGenerator.class);
    myClassFieldGenerator.configure(myAnnotation);
    return new MyClass(myClassFieldGenerator.generate(random, status))
  }

  public void configure(MyAnnotation myAnnotation) {
    this.myAnnotation = myAnnotation;
  }
}

What's interesting about it is that by calling gen().make(MyClassFieldGenerator.class) I can put my configuration annotation on generator class itself to configure defaults on it without calling to myClassFieldGenerator.configure(myAnnotation); explicitly.

But I can't do the same for Generator that is referred from a Property method itself:

@Property
public void myProperty(@From(MyClassGenerator.class) MyClass myClass) {
  // here MyClassGenerator will not receive defaults from annotation present on Generator class.
}

In practice, this means that I have to instantiate default annotation instance myself if it's not provided by the framework which looks like something that could be easily avoided.

@pholser
Copy link
Owner

pholser commented Oct 13, 2020

@SimY4 Thanks for your interest in junit-quickcheck.

You're right -- when you say gen().make(MyClassFieldGenerator.class), it will configure that generator with whatever config annotations live on MyClassFieldGenerator. On a property method, junit-quickcheck decides on generators to use in producing values for a parameter by examining annotations on the parameter itself; and if one of those annotations is From, it doesn't necessarily also scrape annotations from the generator class named in From. I suppose junit-quickcheck could be made to do that....Another possibility I want to examine in the future is pulling annotations on a prescribed path from a parameter, to its declaring method, its enclosing class(es), and so on.

Which path would better address your need? Maybe there are other options I haven't considered?

@pholser pholser changed the title Default Configuration for Generators Allow configuration annotations from a generator used with @From onto the parameter Oct 14, 2020
@pholser
Copy link
Owner

pholser commented Oct 26, 2020

@SimY4 Checking in to see how you'd like to proceed with this issue. Let me know....thanks!

@SimY4
Copy link
Author

SimY4 commented Nov 16, 2020

@pholser Hi, sorry I missed your message. I tried to approach this issue myself but failed to find a solution. Currently, I'm using workaround where my generators configuring themselves at the moment of creation:

@MyAnnotation(value = "Some default")
class MyClassGenerator extends Generator<MyClass> {
  private MyAnnotation myAnnotation;

  public MyClassGenerator() {
    super(MyClass.class);
    configure(getClass());
  }

  ...

  public void configure(MyAnnotation myAnnotation) {
    this.myAnnotation = myAnnotation;
  }
}

@pholser
Copy link
Owner

pholser commented Nov 16, 2020

@SimY4 Thanks for getting back to me. I'm considering going ahead and scraping config annotations from generators from @From. Can't see any harm or confusion it would cause.

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

No branches or pull requests

2 participants