forked from vdobler/ht
/
boolean.go
104 lines (94 loc) · 2.52 KB
/
boolean.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
101
102
103
104
// Copyright 2016 Volker Dobler. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ht
import "fmt"
func init() {
RegisterCheck(AnyOne{})
RegisterCheck(None{})
}
// Boolean combinations of Checks
// AnyOne checks that at least one Of the embedded checks passes.
// It is the short circuiting boolean OR of the underlying checks.
// Check execution stops once the first passing check is found.
// Example (in JSON5 notation) to check status code for '202 OR 404':
// {
// Check: "AnyOne", Of: [
// {Check: "StatusCode", Expect: 202},
// {Check: "StatusCode", Expect: 404},
// ]
// }
type AnyOne struct {
// Of is the list of checks to execute.
Of CheckList
}
// Prepare implements Checks' Prepare method by forwarding to
// the underlying checks.
func (a AnyOne) Prepare() error {
errs := ErrorList{}
for _, c := range a.Of {
err := c.Prepare()
if err != nil {
errs = append(errs, err)
}
}
if len(errs) == 0 {
return nil
}
return errs
}
// Execute implements Check's Execute method. It executes the underlying checks
// until the first passes. If all underlying checks fail the whole list of
// failures is returned.
func (a AnyOne) Execute(t *Test) error {
errs := ErrorList{}
for _, c := range a.Of {
err := c.Execute(t)
if err == nil {
return nil
}
errs = append(errs, err)
}
return errs
}
// None checks that none Of the embedded checks passes.
// It is the NOT of the short circuiting boolean AND of the underlying checks.
// Check execution stops once the first passing check is found.
// It
// Example (in JSON5 notation) to check for non-occurrence of 'foo' in body:
// {
// Check: "None", Of: [
// {Check: "Body", Contains: "foo"},
// ]
// }
type None struct {
// Of is the list of checks to execute.
Of CheckList
}
// Prepare implements Checks' Prepare method by forwarding to
// the underlying checks.
func (n None) Prepare() error {
errs := ErrorList{}
for _, c := range n.Of {
err := c.Prepare()
if err != nil {
errs = append(errs, err)
}
}
if len(errs) == 0 {
return nil
}
return errs
}
// Execute implements Check's Execute method. It executes the underlying checks
// until the first passes. If all underlying checks fail the whole list of
// failures is returned.
func (n None) Execute(t *Test) error {
for i, c := range n.Of {
err := c.Execute(t)
if err == nil {
return fmt.Errorf("check %d passed", i+1)
}
}
return nil
}