parent
4136775a1f
commit
38a6eeb889
@ -1,57 +0,0 @@
|
||||
/*
|
||||
* Copyright 2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import spock.lang.*
|
||||
|
||||
class DataDrivenSpec extends Specification {
|
||||
def "maximum of two numbers"() {
|
||||
expect:
|
||||
Math.max(a, b) == c
|
||||
|
||||
where:
|
||||
a << [3, 5, 9]
|
||||
b << [7, 4, 9]
|
||||
c << [7, 5, 9]
|
||||
}
|
||||
|
||||
def "minimum of #a and #b is #c"() {
|
||||
expect:
|
||||
Math.min(a, b) == c
|
||||
|
||||
where:
|
||||
a | b || c
|
||||
3 | 7 || 3
|
||||
5 | 4 || 4
|
||||
9 | 9 || 9
|
||||
}
|
||||
|
||||
def "#person.name is a #sex.toLowerCase() person"() {
|
||||
expect:
|
||||
person.getSex() == sex
|
||||
|
||||
where:
|
||||
person || sex
|
||||
new Person(name: "Fred") || "Male"
|
||||
new Person(name: "Wilma") || "Female"
|
||||
}
|
||||
|
||||
static class Person {
|
||||
String name
|
||||
String getSex() {
|
||||
name == "Fred" ? "Male" : "Female"
|
||||
}
|
||||
}
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
/*
|
||||
* Copyright 2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import groovy.sql.Sql
|
||||
import spock.lang.Shared
|
||||
import spock.lang.Specification
|
||||
|
||||
class DatabaseDrivenSpec extends Specification {
|
||||
@Shared sql = Sql.newInstance("jdbc:h2:mem:", "org.h2.Driver")
|
||||
|
||||
// insert data (usually the database would already contain the data)
|
||||
def setupSpec() {
|
||||
sql.execute("create table maxdata (id int primary key, a int, b int, c int)")
|
||||
sql.execute("insert into maxdata values (1, 3, 7, 7), (2, 5, 4, 5), (3, 9, 9, 9), (4, 2, -3, 2)")
|
||||
}
|
||||
|
||||
def "Maximum of #a and #b is #c"() {
|
||||
expect:
|
||||
Math.max(a, b) == c
|
||||
|
||||
where:
|
||||
[a, b, c] << sql.rows("select a, b, c from maxdata")
|
||||
}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import spock.lang.Specification
|
||||
|
||||
import static spock.util.matcher.HamcrestMatchers.closeTo
|
||||
|
||||
/**
|
||||
* Hamcrest matchers are deeply integrated with Spock's condition mechanism.
|
||||
* The syntax for using a matcher in a condition is simple:
|
||||
* {@code <expected-value> <matcher>}. If the condition fails, both the usual
|
||||
* condition output and the matcher's output will be shown.
|
||||
*
|
||||
* @author Peter Niederwiser
|
||||
* @since 0.5
|
||||
*/
|
||||
class HamcrestMatchersSpec extends Specification {
|
||||
def "comparing two decimal numbers"() {
|
||||
def myPi = 3.14
|
||||
|
||||
expect:
|
||||
myPi closeTo(Math.PI, 0.01)
|
||||
}
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
/*
|
||||
* Copyright 2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import spock.lang.Specification
|
||||
|
||||
class HelloSpockSpec extends Specification {
|
||||
def "length of Spock's and his friends' names"() {
|
||||
expect:
|
||||
name.size() == length
|
||||
|
||||
where:
|
||||
name | length
|
||||
"Spock" | 5
|
||||
"Kirk" | 4
|
||||
"Scotty" | 6
|
||||
}
|
||||
}
|
@ -1,74 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import java.lang.annotation.*
|
||||
|
||||
import spock.lang.Specification
|
||||
|
||||
/**
|
||||
* Shows how methods can be included and excluded from a spec run. For
|
||||
* demonstration purposes, the configuration script's location is set
|
||||
* programmatically in a static initializer. Usually, the configuration
|
||||
* script's location would be set externally (via the same system property),
|
||||
* or the configuration script would reside in one of the two default locations:
|
||||
* <li>
|
||||
* <ul><tt>SpockConfig.groovy</tt> on the class path</ul>
|
||||
* <ul><tt>$user_home/.spock/SpockConfig.groovy</tt> on the file system</ul>
|
||||
* </li>
|
||||
*
|
||||
* <p>Note that the configuration script referenced by the system property
|
||||
* may either reside on the class path or on the file system (make sure to
|
||||
* include the whole path).
|
||||
*
|
||||
* <p>What's not shown here is that filtering also works for classes.
|
||||
* Whereas methods can only be filtered based on annotations, classes can
|
||||
* also be filtered based on their base class.
|
||||
*
|
||||
* <p>If you directly want to see the effect of different Spock configurations,
|
||||
* just run this spec from your IDE with an additional VM parameter
|
||||
* <li>
|
||||
* <ul><tt>-Dspock.configuration=IncludeFastConfig.groovy</tt> or</ul>
|
||||
* <ul><tt>-Dspock.configuration=ExcludeSlowConfig.groovy</tt></ul>
|
||||
* </li>
|
||||
*
|
||||
* <p>See also the two corresponding resource files provided with this spec.
|
||||
*
|
||||
* @since 0.4
|
||||
*/
|
||||
class IncludeExcludeExtensionSpec extends Specification {
|
||||
@Fast
|
||||
def "a fast method"() {
|
||||
println "fast"
|
||||
expect: true
|
||||
}
|
||||
|
||||
@Slow
|
||||
def "a slow method"() {
|
||||
println "slow"
|
||||
expect: true
|
||||
}
|
||||
|
||||
def "a neither fast nor slow method"() {
|
||||
println "neither fast nor slow"
|
||||
expect: true
|
||||
}
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
@interface Fast {}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
@interface Slow {}
|
@ -1,45 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import spock.lang.*
|
||||
|
||||
/**
|
||||
* The expected order of interactions can now be specified by using multiple
|
||||
* then-blocks. Interactions in a later then-block must take place after
|
||||
* interactions in an earlier then-block. The order of interactions within
|
||||
* the same then-block is unspecified.
|
||||
*
|
||||
* @since 0.4
|
||||
*/
|
||||
class OrderedInteractionsSpec extends Specification {
|
||||
def "collaborators must be invoked in order"() {
|
||||
def coll1 = Mock(Collaborator)
|
||||
def coll2 = Mock(Collaborator)
|
||||
|
||||
when:
|
||||
// try to reverse the order of these invocations and see what happens
|
||||
coll1.collaborate()
|
||||
coll2.collaborate()
|
||||
|
||||
then:
|
||||
1 * coll1.collaborate()
|
||||
|
||||
then:
|
||||
1 * coll2.collaborate()
|
||||
}
|
||||
}
|
||||
|
||||
interface Collaborator {
|
||||
def collaborate()
|
||||
}
|
@ -1,64 +0,0 @@
|
||||
/*
|
||||
* Copyright 2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import spock.lang.Specification
|
||||
|
||||
class Publisher {
|
||||
def subscribers = []
|
||||
|
||||
def send(event) {
|
||||
subscribers.each {
|
||||
try {
|
||||
it.receive(event)
|
||||
} catch (Exception e) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface Subscriber {
|
||||
def receive(event)
|
||||
}
|
||||
|
||||
class PublisherSpec extends Specification {
|
||||
def pub = new Publisher()
|
||||
def sub1 = Mock(Subscriber)
|
||||
def sub2 = Mock(Subscriber)
|
||||
|
||||
def setup() {
|
||||
pub.subscribers << sub1 << sub2
|
||||
}
|
||||
|
||||
def "delivers events to all subscribers"() {
|
||||
when:
|
||||
pub.send("event")
|
||||
|
||||
then:
|
||||
1 * sub1.receive("event")
|
||||
1 * sub2.receive("event")
|
||||
}
|
||||
|
||||
def "can cope with misbehaving subscribers"() {
|
||||
sub1.receive(_) >> { throw new Exception() }
|
||||
|
||||
when:
|
||||
pub.send("event1")
|
||||
pub.send("event2")
|
||||
|
||||
then:
|
||||
1 * sub2.receive("event1")
|
||||
1 * sub2.receive("event2")
|
||||
}
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
/*
|
||||
* Copyright 2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import spock.lang.*
|
||||
|
||||
abstract class BaseSpec extends Specification {
|
||||
def x = { println 'base field initializer' }()
|
||||
|
||||
def setupSpec() { println 'base setupSpec()' }
|
||||
def cleanupSpec() { println 'base cleanupSpec()' }
|
||||
|
||||
def setup() { println 'base setup()' }
|
||||
def cleanup() { println 'base cleanup()' }
|
||||
|
||||
def baseSpecMethod() { setup: println 'base spec method' }
|
||||
}
|
||||
|
||||
class DerivedSpec extends BaseSpec {
|
||||
def y = { println 'derived field initializer' }()
|
||||
|
||||
def setupSpec() { println 'derived setupSpec()' }
|
||||
def cleanupSpec() { println 'derived cleanupSpec()' }
|
||||
|
||||
def setup() { println 'derived setup()' }
|
||||
def cleanup() { println 'derived cleanup()' }
|
||||
|
||||
def derivedSpecMethod() { setup: println 'derived spec method' }
|
||||
}
|
@ -1,120 +0,0 @@
|
||||
/*
|
||||
* Copyright 2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import spock.lang.Specification
|
||||
|
||||
class EmptyStackSpec extends Specification {
|
||||
def stack = new Stack()
|
||||
|
||||
def "size"() {
|
||||
expect: stack.size() == 0
|
||||
}
|
||||
|
||||
def "pop"() {
|
||||
when: stack.pop()
|
||||
then: thrown(EmptyStackException)
|
||||
}
|
||||
|
||||
def "peek"() {
|
||||
when: stack.peek()
|
||||
then: thrown(EmptyStackException)
|
||||
}
|
||||
|
||||
def "push"() {
|
||||
when:
|
||||
stack.push("elem")
|
||||
|
||||
then:
|
||||
stack.size() == old(stack.size()) + 1
|
||||
stack.peek() == "elem"
|
||||
}
|
||||
}
|
||||
|
||||
class StackWithOneElementSpec extends Specification {
|
||||
def stack = new Stack()
|
||||
|
||||
def setup() {
|
||||
stack.push("elem")
|
||||
}
|
||||
|
||||
def "size"() {
|
||||
expect: stack.size() == 1
|
||||
}
|
||||
|
||||
def "pop"() {
|
||||
when:
|
||||
def x = stack.pop()
|
||||
|
||||
then:
|
||||
x == "elem"
|
||||
stack.size() == 0
|
||||
}
|
||||
|
||||
def "peek"() {
|
||||
when:
|
||||
def x = stack.peek()
|
||||
|
||||
then:
|
||||
x == "elem"
|
||||
stack.size() == 1
|
||||
}
|
||||
|
||||
def "push"() {
|
||||
when:
|
||||
stack.push("elem2")
|
||||
|
||||
then:
|
||||
stack.size() == 2
|
||||
stack.peek() == "elem2"
|
||||
}
|
||||
}
|
||||
|
||||
class StackWithThreeElementsSpec extends Specification {
|
||||
def stack = new Stack()
|
||||
|
||||
def setup() {
|
||||
["elem1", "elem2", "elem3"].each { stack.push(it) }
|
||||
}
|
||||
|
||||
def "size"() {
|
||||
expect: stack.size() == 3
|
||||
}
|
||||
|
||||
def "pop"() {
|
||||
expect:
|
||||
stack.pop() == "elem3"
|
||||
stack.pop() == "elem2"
|
||||
stack.pop() == "elem1"
|
||||
stack.size() == 0
|
||||
}
|
||||
|
||||
def "peek"() {
|
||||
expect:
|
||||
stack.peek() == "elem3"
|
||||
stack.peek() == "elem3"
|
||||
stack.peek() == "elem3"
|
||||
stack.size() == 3
|
||||
}
|
||||
|
||||
def "push"() {
|
||||
when:
|
||||
stack.push("elem4")
|
||||
|
||||
then:
|
||||
stack.size() == 4
|
||||
stack.peek() == "elem4"
|
||||
}
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import spock.lang.*
|
||||
|
||||
/**
|
||||
* Demonstrates how {@code @Stepwise} causes a spec to be run in incremental steps.
|
||||
* Change a step's condition from {@literal true} to {@literal false}, and observe
|
||||
* how the remaining steps will be skipped automatically on the next run.
|
||||
* Also notice that if you run a single step (e.g. from the IDE's context menu),
|
||||
* all prior steps will also be run.
|
||||
*
|
||||
* <p>{@code @Stepwise} is particularly useful for higher-level specs whose
|
||||
* methods have logical dependencies.
|
||||
*
|
||||
* @since 0.4
|
||||
*/
|
||||
@Stepwise
|
||||
class StepwiseExtensionSpec extends Specification {
|
||||
def "step 1"() {
|
||||
expect: true // try to change this to 'false'
|
||||
}
|
||||
|
||||
def "step 2"() {
|
||||
expect: true
|
||||
}
|
||||
|
||||
def "step 3"() {
|
||||
expect: true
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/*
|
||||
* Copyright 2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import org.junit.Rule
|
||||
import org.junit.rules.TestName
|
||||
import spock.lang.Specification
|
||||
|
||||
class UsingJUnitRulesSpec extends Specification {
|
||||
@Rule TestName name
|
||||
|
||||
def "retrieve test name at runtime"() {
|
||||
println "entering '$name.methodName'"
|
||||
expect: 1 + 1 == 2
|
||||
println "leaving '$name.methodName'"
|
||||
}
|
||||
}
|
Loading…
Reference in new issue