Skip to content

Instantly share code, notes, and snippets.

View ldaley's full-sized avatar

Luke Daley ldaley

View GitHub Profile
Spock For Non-Believers
Which code would you rather want to read, write, and maintain? Choose the right answer and win a trip to Vulcan!
A. B.
/* Seven lines of imports removed for clarity */ /* Zero lines of imports removed for clarity */
@RunWith(Parameterized.class) class HelloSpock extends spock.lang.Specification {
public class HelloSpock { def "length of Spock and friends"() {
<html>
<head>
</head>
<body>
<div class="spec">
<div class="details">
<div class="name">My first spec</div>
<div class="duration">3 mins 17 seconds</div>
<div class="result">fail</div>
</div>
<html>
<head>
</head>
<body>
<div class="spec pass/some-fail/fail first/last odd/even" id="«spec-class-name»">
<div class="details">
<div class="name">My first spec</div>
<div class="duration">3 mins 17 seconds</div>
<div class="result">fail</div>
</div>
@ldaley
ldaley / gist:1486177
Created December 16, 2011 14:12 — forked from robfletcher/gist:1486156
Geb module methods demonstrating how to wait for jQuery animation to complete
void next() {
nextButton.click()
waitForAnimationComplete()
}
private void waitForAnimationComplete() {
waitFor { this.jquery."find(':animated').length == 0" }
}
@ldaley
ldaley / gist:1854686
Created February 17, 2012 18:12
Transforming HTML in a Gradle build
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
class JSoupFilterReader extends FilterReader {
Closure withDocument
JSoupFilterReader(Reader reader) {
super(new DeferringReader(reader));
this.in.parent = this
@ldaley
ldaley / functional-test.gradle
Created February 22, 2013 11:29
funtional testing config
sourceSets {
functionalTest {
task functionalTest(type: Test) {
testClassesDir = output.classesDir
testSrcDirs = java.srcDirs.toList()
classpath = runtimeClasspath
}
}
}
@ldaley
ldaley / gist:5429603
Last active March 5, 2018 14:59
Groovy @DelegatesTo and type tokens.
Handler<T> handler(@DelegatesTo.Target(asType = true) Class<T> type, @DelegatesTo Closure<?> configurer) {
return new Handler() {
void handle(T object) {
configurer.delegate = object
configure.call(delegate)
}
}
}
@ldaley
ldaley / gist:5559921
Created May 11, 2013 13:09
New capability in Gradle 1.6. Run all verifications (e.g. checkstyle, tests) on all projects (if they are to be executed in this build) before uploading anything. e.g. ./gradlew clean build uploadArchives Will only upload after all projects have been built and tested.
allprojects {
tasks.withType(Upload) {
allprojects {
mustRunAfter tasks.matching { it instanceof VerificationTask }
}
}
}
def "can handle errors on forked threads"() {
given:
def errorHandler = new ErrorHandlingContext() {
void error(Exchange exchange, Exception exception) {
exchange.response.send("Caught: $exception.message")
}
}
when:
app {
public String getBindHost() {
if (boundAddress == null) {
return null;
} else {
InetAddress address = boundAddress.getAddress();
if (address.isAnyLocalAddress()) {
return "localhost";
} else {
return address.getHostAddress();
}