-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcapture.go
More file actions
62 lines (51 loc) · 1.23 KB
/
capture.go
File metadata and controls
62 lines (51 loc) · 1.23 KB
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
package schema
import "fmt"
// Capture can be used once or more to capture values and to make sure a value stays the same
func Capture(name string) CaptureMatcher {
return &captureMatcher{
name: name,
value: nil,
}
}
// CaptureMatcher is the exposed interface for Capture
type CaptureMatcher interface {
Matcher
Equals(interface{}) bool
CapturedValue() interface{}
}
type captureMatcher struct {
name string
value interface{}
}
func (m *captureMatcher) CapturedValue() interface{} {
return m.value
}
func (m *captureMatcher) Equals(expected interface{}) bool {
if i, isInt := expected.(int); isInt {
valueAsFloat, valueIsFloat := m.value.(float64)
if !valueIsFloat {
// actual is no number
return false
}
if float64(int(valueAsFloat)) != valueAsFloat {
// actual can not be used as int
return false
}
// int values are distinct?
return int(valueAsFloat) == i
}
return m.value == expected
}
func (m *captureMatcher) Match(data interface{}) *Error {
if m.value == nil {
m.value = data
return nil
}
if m.value != data {
return SelfError(fmt.Sprintf("%s: %v != %v", m.String(), data, m.value))
}
return nil
}
func (m *captureMatcher) String() string {
return fmt.Sprintf("Capture(%s)", m.name)
}