/** * A combination of {@link #with(Object, Class, Closure)} and {@link #verifyAll(Closure)}. * * @since 1.2 * @param target an implicit target for conditions and/or interactions * @param type the expected type of the target * @param closure a code block containing top-level conditions and/or interactions * @param <U> type of target */ @Beta public <U> void verifyAll( Object target, @DelegatesTo.Target Class<U> type, @DelegatesTo(genericTypeIndex = 0, strategy = Closure.DELEGATE_FIRST) @ClosureParams(SecondParam.FirstGenericType.class) Closure closure) { if (target != null && !type.isInstance(target)) { throw new SpockAssertionError(String.format("Expected target of 'verifyAll' block to have type '%s', but got '%s'", type, target.getClass().getName())); } verifyAll(target, closure); } }
@DelegatesTo.Target Class<U> type, @DelegatesTo(genericTypeIndex = 0, strategy = Closure.DELEGATE_FIRST) @ClosureParams(SecondParam.FirstGenericType.class) Closure closure) {
/** * Allows the closure to be called for the object reference self. * Synonym for 'with()'. * * @param self the object to have a closure act upon * @param closure the closure to call on the object * @return result of calling the closure * @see #with(Object, Closure) * @since 1.0 */ public static <T,U> T identity( @DelegatesTo.Target("self") U self, @DelegatesTo(value=DelegatesTo.Target.class, target="self", strategy=Closure.DELEGATE_FIRST) @ClosureParams(FirstParam.class) Closure<T> closure) { return DefaultGroovyMethods.with(self, closure); }
public static <T,U> U tap( @DelegatesTo.Target("self") U self, @DelegatesTo(value=DelegatesTo.Target.class, target="self", strategy=Closure.DELEGATE_FIRST)
public static <T,U> T with( @DelegatesTo.Target("self") U self, @DelegatesTo(value=DelegatesTo.Target.class, target="self", strategy=Closure.DELEGATE_FIRST)
/** * Creates a stub with the specified type and interactions. If enclosed in a variable assignment, the variable name will be * used as the stub's name. * * Example: * * <pre> * // name is "person", type is Person.class, returns hard-coded values for property {@code name} and method {@code sing()} * def person = Stub(Person) { * name << "Fred" * sing() << "Tra-la-la" * } * </pre> * * @param type the interface or class type of the stub * @param interactions a description of the stub's interactions * @param <T> the interface or class type of the stub * * @return a stub with the specified type and interactions */ @Beta public <T> T Stub( @DelegatesTo.Target Class<T> type, @DelegatesTo(strategy = Closure.DELEGATE_FIRST, genericTypeIndex = 0) @ClosureParams(FirstParam.FirstGenericType.class) Closure interactions) { invalidMockCreation(); return null; }
/** * Creates a Groovy mock with the specified type and interactions. If enclosed in a variable assignment, the variable name will be * used as the mock's name. * * Example: * * <pre> * // name is "person", type is Person.class, returns hard-code value {@code name}, expects one call to {@code sing()} * def person = GroovyMock(Person) { * name << "Fred" * 1 * sing() * } * </pre> * * @param type the interface or class type of the Groovy mock * @param interactions a description of the Groovy mock's interactions * @param <T> the interface or class type of the Groovy mock * * @return a Groovy mock with the specified type and interactions */ @Beta public <T> T GroovyMock( @DelegatesTo.Target Class<T> type, @DelegatesTo(strategy = Closure.DELEGATE_FIRST, genericTypeIndex = 0) @ClosureParams(FirstParam.FirstGenericType.class) Closure interactions) { invalidMockCreation(); return null; }
/** * Creates a Groovy stub with the specified type and interactions. If enclosed in a variable assignment, the variable name will be * used as the stub's name. * * Example: * * <pre> * // name is "person", type is Person.class, returns hard-coded values for property {@code name} and method {@code sing()} * def person = GroovyStub(Person) { * name << "Fred" * sing() << "Tra-la-la" * } * </pre> * * @param type the interface or class type of the Groovy stub * @param interactions a description of the Groovy stub's interactions * @param <T> the interface or class type of the Groovy stub * * @return a Groovy stub with the specified type and interactions */ @Beta public <T> T GroovyStub( @DelegatesTo.Target Class<T> type, @DelegatesTo(strategy = Closure.DELEGATE_FIRST, genericTypeIndex = 0) @ClosureParams(FirstParam.FirstGenericType.class) Closure interactions) { invalidMockCreation(); return null; }
/** * Creates a mock with the specified type and interactions. If enclosed in a variable assignment, the variable name will be * used as the mock's name. * * Example: * * <pre> * // name is "person", type is Person.class, returns hard-code value {@code name}, expects one call to {@code sing()} * def person = Mock(Person) { * name << "Fred" * 1 * sing() * } * </pre> * * @param type the interface or class type of the mock * @param interactions a description of the mock's interactions * @param <T> the interface or class type of the mock * * @return a mock with the specified type and interactions */ @Beta public <T> T Mock( @DelegatesTo.Target Class<T> type, @DelegatesTo(strategy = Closure.DELEGATE_FIRST, genericTypeIndex = 0) @ClosureParams(FirstParam.FirstGenericType.class) Closure interactions) { invalidMockCreation(); return null; }
/** * Creates a spy with the specified type and interactions. If enclosed in a variable assignment, the variable name will be * used as the spy's name. * * Example: * * <pre> * // name is "person", type is Person.class, returns hard-code value {@code name}, calls real method otherwise * def person = Spy(Person) { * name << "Fred" * 1 * sing() * } * </pre> * * @param type the class type of the spy * @param interactions a description of the spy's interactions * @param <T> the class type of the spy * * @return a spy with the specified type and interactions */ @Beta public <T> T Spy( @DelegatesTo.Target Class<T> type, @DelegatesTo(strategy = Closure.DELEGATE_FIRST, genericTypeIndex = 0) @ClosureParams(FirstParam.FirstGenericType.class) Closure interactions) { invalidMockCreation(); return null; }
/** * Creates a Groovy spy with the specified type and interactions. If enclosed in a variable assignment, the variable name will be * used as the spy's name. * * Example: * * <pre> * // name is "person", type is Person.class, returns hard-code value {@code name}, calls real method otherwise * def person = GroovySpy(Person) { * name << "Fred" * 1 * sing() * } * </pre> * * @param type the class type of the Groovy spy * @param interactions a description of the Groovy spy's interactions * @param <T> the class type of the Groovy spy * * @return a Groovy spy with the specified type and interactions */ @Beta public <T> T GroovySpy( @DelegatesTo.Target Class<T> type, @DelegatesTo(strategy = Closure.DELEGATE_FIRST, genericTypeIndex = 0) @ClosureParams(FirstParam.FirstGenericType.class) Closure interactions) { invalidMockCreation(); return null; }
/** * A combination of {@link #with(Object, Closure)} and {@link #verifyAll(Closure)}. * * @since 1.2 * @param target an implicit target for conditions and/or interactions * @param closure a code block containing top-level conditions and/or interactions * @param <U> type of target */ @Beta public <U> void verifyAll( @DelegatesTo.Target U target, @DelegatesTo(strategy = Closure.DELEGATE_FIRST) @ClosureParams(FirstParam.class) Closure<?> closure ){ if (target == null) { throw new SpockAssertionError("Target of 'verifyAll' block must not be null"); } closure.setDelegate(target); // for conditions closure.setResolveStrategy(Closure.DELEGATE_FIRST); GroovyRuntimeUtil.invokeClosure(closure, target); }
@DelegatesTo.Target U target, @DelegatesTo(strategy = Closure.DELEGATE_FIRST) @ClosureParams(FirstParam.class) Closure<?> closure ) {
/** * A collection and closure passed to a JSON builder will create a root JSON array applying * the closure to each object in the collection * <p> * Example: * <pre class="groovyTestCase"> * class Author { * String name * } * def authors = [new Author (name: "Guillaume"), new Author (name: "Jochen"), new Author (name: "Paul")] * * new StringWriter().with { w -> * def json = new groovy.json.StreamingJsonBuilder(w) * json authors, { Author author -> * name author.name * } * * assert w.toString() == '[{"name":"Guillaume"},{"name":"Jochen"},{"name":"Paul"}]' * } * </pre> * @param coll a collection * @param c a closure used to convert the objects of coll */ public Object call(Iterable coll, @DelegatesTo(StreamingJsonDelegate.class) Closure c) throws IOException { return StreamingJsonDelegate.writeCollectionWithClosure(writer, coll, c, generator); }
/** * Delegates to {@link #call(String, Iterable, Closure)} */ public void call(String name, Collection coll, @DelegatesTo(StreamingJsonDelegate.class) Closure c) throws IOException { call(name, (Iterable)coll, c); }
/** * Delegates to {@link #call(String, Iterable, Closure)} */ public void call(String name, Collection coll, @DelegatesTo(StreamingJsonDelegate.class) Closure c) throws IOException { call(name, (Iterable)coll, c); }
/** * Delegates to {@link #call(Iterable, Closure)} */ public Object call(Collection coll, @DelegatesTo(StreamingJsonDelegate.class) Closure c) throws IOException { return call((Iterable)coll, c); }
/** * A closure passed to a JSON builder will create a root JSON object * <p> * Example: * <pre class="groovyTestCase"> * new StringWriter().with { w -> * def json = new groovy.json.StreamingJsonBuilder(w) * json { * name "Tim" * age 39 * } * * assert w.toString() == '{"name":"Tim","age":39}' * } * </pre> * * @param c a closure whose method call statements represent key / values of a JSON object */ public Object call(@DelegatesTo(StreamingJsonDelegate.class) Closure c) throws IOException { writer.write(JsonOutput.OPEN_BRACE); StreamingJsonDelegate.cloneDelegateAndGetContent(writer, c, true, generator); writer.write(JsonOutput.CLOSE_BRACE); return null; }
public static void cloneDelegateAndGetContent(Writer w, @DelegatesTo(StreamingJsonDelegate.class) Closure c) { cloneDelegateAndGetContent(w, c, true); }
public static void curryDelegateAndGetContent(Writer w, @DelegatesTo(StreamingJsonDelegate.class) Closure c, Object o) { curryDelegateAndGetContent(w, c, o, true); }