Debian Codegraph @

testing/condition/condition.go

raw
1 /*
2 Copyright 2013-2014 Canonical Ltd.
3
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License version 3, as published
6 by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranties of
10 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 // Package condition implements a strategy family for use in testing.
18 package condition
19
20 import (
21 "fmt"
22 "strings"
23 "sync"
24 )
25
26 type Interface interface {
27 OK() bool
28 String() string
29 }
30
31 // Work is a simple boolean condition; either it works all the time
32 // (when true), or it fails all the time (when false).
33 func Work(wk bool) work {
34 return work(wk)
35 }
36
37 type work bool
38
39 func (c work) OK() bool {
40 if c {
41 return true
42 } else {
43 return false
44 }
45 }
46 func (c work) String() string {
47 if c {
48 return "Always Working."
49 } else {
50 return "Never Working."
51 }
52 }
53
54 var _ Interface = work(false)
55
56 // Fail2Work fails for the first n times its OK() method is checked,
57 // and then mysteriously starts working.
58 func Fail2Work(left int32) *fail2Work {
59 c := new(fail2Work)
60 c.Left = left
61 return c
62 }
63
64 type fail2Work struct {
65 Left int32
66 lock sync.RWMutex
67 }
68
69 func (c *fail2Work) OK() bool {
70 c.lock.Lock()
71 defer c.lock.Unlock()
72 if c.Left > 0 {
73 c.Left--
74 return false
75 } else {
76 return true
77 }
78 }
79
80 func (c *fail2Work) String() string {
81 c.lock.RLock()
82 defer c.lock.RUnlock()
83 if c.Left > 0 {
84 return fmt.Sprintf("Still Broken, %d to go.", c.Left)
85 } else {
86 return "Working."
87 }
88 }
89
90 var _ Interface = &fail2Work{}
91
92 // Not builds a condition that negates the one passed in.
93 func Not(sub Interface) *not {
94 return &not{sub}
95 }
96
97 type not struct{ sub Interface }
98
99 func (c *not) OK() bool { return !c.sub.OK() }
100 func (c *not) String() string { return fmt.Sprintf("Not %s", c.sub) }
101
102 var _ Interface = &not{}
103
104 type _iter struct {
105 cond Interface
106 remaining int
107 }
108
109 func (i _iter) String() string { return fmt.Sprintf("%d of %s", i.remaining, i.cond) }
110
111 type chain struct {
112 subs []*_iter
113 lock sync.RWMutex
114 }
115
116 func (c *chain) OK() bool {
117 var sub *_iter
118 c.lock.Lock()
119 defer c.lock.Unlock()
120 for _, sub = range c.subs {
121 if sub.remaining > 0 {
122 sub.remaining--
123 return sub.cond.OK()
124 }
125 }
126 return sub.cond.OK()
127 }
128
129 func (c *chain) String() string {
130 ss := make([]string, len(c.subs))
131 c.lock.RLock()
132 defer c.lock.RUnlock()
133 for i, sub := range c.subs {
134 ss[i] = sub.String()
135 }
136 return strings.Join(ss, " Then: ")
137 }
138
139 var _ Interface = new(chain)
140
141 // Chain(n1, cond1, n2, cond2, ...) returns cond1.OK() the first n1
142 // times OK() is called, cond2.OK() the following n2 times, etc.
143 func Chain(args ...interface{}) *chain {
144 iters := make([]*_iter, 0, len(args)/2)
145 for len(args) > 1 {
146 rem := args[0].(int)
147 sub := args[1].(Interface)
148 iters = append(iters, &_iter{sub, rem})
149 args = args[2:]
150 }
151
152 return &chain{subs: iters}
153 }