Debian Codegraph @

checkers2.go

raw symbols SCIP
1 // Extensions to the go-check unittest framework.
2 //
3 // NOTE: see https://github.com/go-check/check/pull/6 for reasons why these
4 // checkers live here.
5 package check
6
7 import (
8 "bytes"
9 "reflect"
10 )
11
12 // -----------------------------------------------------------------------
13 // IsTrue / IsFalse checker.
14
15 type isBoolValueChecker struct {
16 *CheckerInfo
17 expected bool
18 }
19
20 func (checker *isBoolValueChecker) Check(
21 params []interface{},
22 names []string) (
23 result bool,
24 error string) {
25
26 obtained, ok := params[0].(bool)
27 if !ok {
28 return false, "Argument to " + checker.Name + " must be bool"
29 }
30
31 return obtained == checker.expected, ""
32 }
33
34 // The IsTrue checker verifies that the obtained value is true.
35 //
36 // For example:
37 //
38 // c.Assert(value, IsTrue)
39 //
40 var IsTrue Checker = &isBoolValueChecker{
41 &CheckerInfo{Name: "IsTrue", Params: []string{"obtained"}},
42 true,
43 }
44
45 // The IsFalse checker verifies that the obtained value is false.
46 //
47 // For example:
48 //
49 // c.Assert(value, IsFalse)
50 //
51 var IsFalse Checker = &isBoolValueChecker{
52 &CheckerInfo{Name: "IsFalse", Params: []string{"obtained"}},
53 false,
54 }
55
56 // -----------------------------------------------------------------------
57 // BytesEquals checker.
58
59 type bytesEquals struct{}
60
61 func (b *bytesEquals) Check(params []interface{}, names []string) (bool, string) {
62 if len(params) != 2 {
63 return false, "BytesEqual takes 2 bytestring arguments"
64 }
65 b1, ok1 := params[0].([]byte)
66 b2, ok2 := params[1].([]byte)
67
68 if !(ok1 && ok2) {
69 return false, "Arguments to BytesEqual must both be bytestrings"
70 }
71
72 return bytes.Equal(b1, b2), ""
73 }
74
75 func (b *bytesEquals) Info() *CheckerInfo {
76 return &CheckerInfo{
77 Name: "BytesEquals",
78 Params: []string{"bytes_one", "bytes_two"},
79 }
80 }
81
82 // BytesEquals checker compares two bytes sequence using bytes.Equal.
83 //
84 // For example:
85 //
86 // c.Assert(b, BytesEquals, []byte("bar"))
87 //
88 // Main difference between DeepEquals and BytesEquals is that BytesEquals treats
89 // `nil` as empty byte sequence while DeepEquals doesn't.
90 //
91 // c.Assert(nil, BytesEquals, []byte("")) // succeeds
92 // c.Assert(nil, DeepEquals, []byte("")) // fails
93 var BytesEquals = &bytesEquals{}
94
95 // -----------------------------------------------------------------------
96 // HasKey checker.
97
98 type hasKey struct{}
99
100 func (h *hasKey) Check(params []interface{}, names []string) (bool, string) {
101 if len(params) != 2 {
102 return false, "HasKey takes 2 arguments: a map and a key"
103 }
104
105 mapValue := reflect.ValueOf(params[0])
106 if mapValue.Kind() != reflect.Map {
107 return false, "First argument to HasKey must be a map"
108 }
109
110 keyValue := reflect.ValueOf(params[1])
111 if !keyValue.Type().AssignableTo(mapValue.Type().Key()) {
112 return false, "Second argument must be assignable to the map key type"
113 }
114
115 return mapValue.MapIndex(keyValue).IsValid(), ""
116 }
117
118 func (h *hasKey) Info() *CheckerInfo {
119 return &CheckerInfo{
120 Name: "HasKey",
121 Params: []string{"obtained", "key"},
122 }
123 }
124
125 // The HasKey checker verifies that the obtained map contains the given key.
126 //
127 // For example:
128 //
129 // c.Assert(myMap, HasKey, "foo")
130 //
131 var HasKey = &hasKey{}