Class JsObjGen

  • All Implemented Interfaces:
    fun.gen.Gen<JsObj>, Function<Random,​Supplier<JsObj>>

    public final class JsObjGen
    extends Object
    implements fun.gen.Gen<JsObj>
    Represents a JsObj generator. It can be created using the static factory methods of or inserting new key-generator pairs to an existing JsObj generator with the method set(String, Gen). Each generator of the Json is created from a new seed that is calculated passing the original one to the split generator

    There are factory methods to create generators of up to 20-key Json objects.

    Optional and nullable keys are specified with the methods setOptionals and setNullable.

    Given the following optional fields a,b and c, all the possible permutations (2^n = 8) are generated with the same probability:

      - a, b and c missing
      - a and b missing
      - a and c missing
      - b and c missing
      - a missing
      - b missing
      - c missing
      - none of the missing
     

    The same applies for nullable fields.

    • Method Detail

      • setNullables

        public JsObjGen setNullables​(Collection<String> nullables)
        Returns a brand new JsObj generator with the same key-generators pairs that this instance and the specified nullable keys. The value associated to a nullable key may or not be null
        Parameters:
        nullables - the optional keys
        Returns:
        a brand new JsObj generator
      • setNullables

        public JsObjGen setNullables​(String... nullables)
        Returns a brand new JsObj generator with the same key-generators pairs that this instance and the specified nullable keys. The value associated to a nullable key may or not be null
        Parameters:
        nullables - the optional keys
        Returns:
        a brand new JsObj generator
      • setAllNullable

        public JsObjGen setAllNullable()
        Returns a brand new JsObj generator with the same key-generators pairs that this instance and all keys nullable. The value associated to a nullable key may or not be null
        Returns:
        a new generator
      • setOptionals

        public JsObjGen setOptionals​(Collection<String> optionals)
        Returns a brand new JsObj generator with the same key-generators pairs that this instance and the specified optional keys. An optional key may or not appear in the Json objects generated
        Parameters:
        optionals - the optional keys
        Returns:
        a brand new JsObj generator
      • setAllOptional

        public JsObjGen setAllOptional()
        Returns a brand new JsObj generator with the same key-generators pairs that this instance and all keys optimal. An optional key may or not appear in the Json objects generated
        Returns:
        a brand new JsObj generator
      • setOptionals

        public JsObjGen setOptionals​(String... optional)
        Returns a brand new JsObj generator with the same key-generators pairs that this instance and the specified optional keys. An optional key may or not appear in the Json objects generated
        Parameters:
        optional - the optional keys
        Returns:
        a brand new JsObj generator
      • set

        public JsObjGen set​(String key,
                            fun.gen.Gen<? extends JsValue> gen)
        Returns a brand new JsObj generator with the same key-generators pairs that this instance plus the specified by the params key and gen
        Parameters:
        key - the new key
        gen - the generator associated to the new key
        Returns:
        a brand new JsObj generator
      • apply

        public Supplier<JsObj> apply​(Random seed)
        Returns a supplier from the specified seed that generates a new JsObj each time it's called
        Specified by:
        apply in interface Function<Random,​Supplier<JsObj>>
        Parameters:
        seed - the generator seed
        Returns:
        a JsObj supplier
      • suchThat

        public fun.gen.Gen<JsObj> suchThat​(JsObjSpec spec)
        Creates a generator that generates values from this gen that satisfy the specified spec. Care is needed to ensure there is a high chance that will satisfy the spec. By default, it will try 1000 times. If no value conforms the spec after this number of iterations, a runtime exception will be thrown.
        Parameters:
        spec - the spec that will satisfy the generated values
        Returns:
        a new JsObj generator
        Throws:
        RuntimeException - if a value is not generated after 1000 tries
      • suchThat

        public fun.gen.Gen<JsObj> suchThat​(JsObjSpec spec,
                                           int tries)
        Creates a generator that generates values from this gen that satisfy the specified spec. Care is needed to ensure there is a high chance that will satisfy the spec. It will try the specified number of tries. If no value passes this predicate after this number of iterations, a runtime exception will be thrown.
        Parameters:
        spec - the spec that will satisfy the generated values
        tries - the max number of iterations to generate a value that satisfies the spec
        Returns:
        a new JsObj generator
        Throws:
        RuntimeException - if a value is not generated after the specified number of tries
      • suchThatNo

        public fun.gen.Gen<JsObj> suchThatNo​(JsObjSpec spec)
        Creates a generator that generates values from this gen that doesn't satisfy the specified spec. Care is needed to ensure there is a high chance that won't satisfy the spec. By default, it will try 1000 times. If all values conform the spec after this number of iterations, a runtime exception will be thrown.
        Parameters:
        spec - the spec that won't satisfy the generated values
        Returns:
        a new JsObj generator
        Throws:
        RuntimeException - if a value is not generated after 1000 tries
      • suchThatNo

        public fun.gen.Gen<JsObj> suchThatNo​(JsObjSpec spec,
                                             int tries)
        Creates a generator that generates values from this gen that doesn't satisfy the specified spec. Care is needed to ensure there is a high chance that won't satisfy the spec. It will try the specified number of tries. If all values conform the spec after this number of iterations, a runtime exception will be thrown.
        Parameters:
        spec - the spec that won't satisfy the generated values
        tries - max number of tries
        Returns:
        a new JsObj generator
        Throws:
        RuntimeException - if a value is not generated after the specified number of tries