All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ar.com.dgarcia.javaspec.api.variable.Let Maven / Gradle / Ivy

The newest version!
package ar.com.dgarcia.javaspec.api.variable;

import ar.com.dgarcia.javaspec.api.contexts.TestContext;

import java.util.function.Supplier;

/**
 * This class allows variable definitions in tests suites that are lazily accessed and can be redefined in subcontexts
 * Created by nrainhart on 15/03/19.
 */
public class Let {

  private String variableName;
  private Supplier context;

  public static  Let create(String variableName, Supplier context) {
    Let let = new Let<>();
    let.variableName = variableName;
    let.context = context;
    return let;
  }

  /**
   * Defines the value in the current context, which may redefine previous value of broader context,
   * or be redefined by a subcontext.
An exception is thrown if a variable is tried to be defined twice in same context * * @param definition A value supplier that can be used to lazily define the initial value of the variable */ public Let let(Supplier definition) { context().let(variableName(), definition); return (Let) this; } /** * Gets the value defined in the current context or parent context.
* The value of the variable is lazily defined the first time accessed. If there's no previous * definition of the variable, then an exception will be thrown. * * @return The value of the variable */ public T get() { return context().get(variableName()); } String variableName() { return variableName; } TestContext context() { return context.get(); } }