-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathexecution_model_test.go
100 lines (84 loc) · 3.44 KB
/
execution_model_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright © 2009-2011 Esko Luontola <www.orfjackal.net>
// This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0
package examples
import (
"github.com/orfjackal/gospec/src/gospec"
. "github.com/orfjackal/gospec/src/gospec"
"strings"
)
func ExecutionModelSpec(c gospec.Context) {
// "Before block", for example common variables for use in all specs.
commonVariable := ""
c.Specify("The following child specs modify the same variable", func() {
// "Before block", for example initialization for this group of specs.
commonVariable += "x"
// All sibling specs (specs which are declared within a common parent)
// are fully isolated from each other. The following three siblings are
// executed in parallel, each in its own goroutine, and each of them
// has its own copy of the local variables declared in its parent specs.
c.Specify("I modify it, but none of my siblings will know it", func() {
commonVariable += "1"
})
c.Specify("Also I modify it, but none of my siblings will know it", func() {
commonVariable += "2"
})
c.Specify("Also I modify it, but none of my siblings will know it", func() {
commonVariable += "3"
})
// "After block", for example tear down of changes to the file system.
commonVariable += "y"
// Depending on which of the previous siblings was executed this time,
// there are three possible values for the variable:
c.Expect(commonVariable, Satisfies, commonVariable == "x1y" ||
commonVariable == "x2y" ||
commonVariable == "x3y")
})
c.Specify("You can nest", func() {
c.Specify("as many specs", func() {
c.Specify("as you wish.", func() {
c.Specify("GoSpec does not impose artificial limits, "+
"so you can organize your specs freely.", func() {
})
})
})
})
c.Specify("The distinction between 'Expect' and 'Assume'", func() {
// When we have non-trivial test setup code, then it is often useful to
// explicitly state our assumptions about the state of the system under
// test, before the body of the test is executed.
//
// Otherwise it could happen that the test passes even though the code
// is broken, or then we get lots of unhelpful error messages from the
// body of the test, even though the bug was in the test setup.
//
// For this use case, GoSpec provides 'Assume' in addition to 'Expect'.
// Use 'Assume' when the test assumes the correct functioning of some
// behaviour which is not the focus of the current test:
//
// - When an 'Expect' fails, then the child specs are executed normally.
//
// - When an 'Assume' fails, then the child specs are NOT executed. This
// helps to prevent lots of false alarms from the child specs, when
// the real problem was in the test setup.
// Some very complex test setup code
input := ""
for ch := 'a'; ch <= 'c'; ch++ {
input += string(ch)
}
// Uncomment this line to add a bug into the test setup:
//input += " bug"
// Uncomment one of the following asserts to see their difference:
//c.Expect(input, Equals, "abc")
//c.Assume(input, Equals, "abc")
c.Specify("When a string is made all uppercase", func() {
result := strings.ToUpper(input)
c.Specify("it is all uppercase", func() {
c.Expect(result, Equals, "ABC")
})
c.Specify("its length is not changed", func() {
c.Expect(len(result), Equals, 3)
})
})
})
}