-
Notifications
You must be signed in to change notification settings - Fork 11
Method Grouping and Triggering
For methods in your API which are not required (those declared with any()
and atMost(..)
) it is possible to assign them to a group. Grouping methods allows for more complicated (A|B) types of scenarios, where the user can pick one method over another, but not both. The first time a method with a group is called all other methods in the group are removed from play. This is also a nice way to support method overloading, which is really just two or more methods with different signatures which could be called. Flapi's setReturnType(..)
method is an example of this, being two methods setReturnType(String)
and setReturnType(Class)
grouped together.
Methods are grouped by calling the atMost(..)
and any()
methods accepting an integer group number. The effect is scoped to the current block.
Building off of the group numbers, it is also possible to have a method appear after a method in a group has been called. The method will be hidden initially, and will only enter play after its group has been hit for the first time. This functionality is available to all methods in a block via the after(int group)
method.
We can enhance the previous example to use method triggering. Let's say that if the developer selects an InputStream as their upload choice then we want to enable some settings, such as the length of the stream and a file name to use. In the previous example we provided the stream length via an overloaded method, but with more optional parameters this becomes cumbersome. In the case of the file name, it is optional for file uploads (since we have the name) but required for the stream. How can we capture all of these rules in our builder?
Descriptor descriptor = Flapi.builder()
.setPackage("unquietcode.tools.flapi.examples")
.setDescriptorName("Something")
.build();