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

Can Generator<...> be injected into Generator's constructor? #234

Open
vlsi opened this issue Sep 17, 2019 · 1 comment
Open

Can Generator<...> be injected into Generator's constructor? #234

vlsi opened this issue Sep 17, 2019 · 1 comment
Labels

Comments

@vlsi
Copy link
Contributor

vlsi commented Sep 17, 2019

Use case: I want to generate test data for java.util.jar.Manifest.

Manifest contains Attributes that is Attributes implements Map<Object,Object>.
Technically speaking attribute keys and values are strings, however the restrictions on keys and values are different.

For instance, keys should match [a-zA-z0-9_-]{1,70}, however keys can contain almost everything except newlines.
However I want not just random strings as keys, but I want the generator to produce well-known key names like Manifest-Version.

So far so good, I think behind the lines of implementing AttributesGenerator extends Generator<Attributes>.

    DefaultAttributes generate(SourceOfRandomness random, GenerationStatus status) {
        def map = gen().make(
            HashMapGenerator.class,
            Gen.frequency(
                Gen.freq(0.4, gen().make(KnownManifestKeyGenerator.class))
                Generator.freq(0.6, gen().make(AsciiStringGenerator.class)),
            ),
            gen().type(String.class))
            .generate(random, status))
        def res = new DefaultAttributes()
        res.putAll(map)
        return res
    }

Frankly speaking, I do not like how gen.make is instantiated at each and every generation attempt.

I wonder if I could "inject" Generator instance with constructor or another configuration-like method.

For instance:

class AttributesGenerator extends Generator<Attributes> {
  private Generator<Map<String, String>> mapGen;
  AttributesGenerator(Generator<Map<@From(KnownManifestKeyGenerator.class, frequency=0.4) 
 @From(AsciiStringGenerator.class, frequency=0.6), String, String>> mapGen) {
    this.mapGen = mapGen;
  }
  // then I could reuse mapGen to produce maps.
}
@vlsi
Copy link
Contributor Author

vlsi commented Sep 17, 2019

Note: Gen.frequency dos not support shrink

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