Debian Codegraph @

td/td_empty.go

raw
1 // Copyright (c) 2018, Maxime Soulé
2 // All rights reserved.
3 //
4 // This source code is licensed under the BSD-style license found in the
5 // LICENSE file in the root directory of this source tree.
6
7 package td
8
9 import (
10 "reflect"
11
12 "github.com/maxatome/go-testdeep/internal/ctxerr"
13 "github.com/maxatome/go-testdeep/internal/types"
14 )
15
16 const emptyBadKind = "array OR chan OR map OR slice OR string OR pointer(s) on them"
17
18 type tdEmpty struct {
19 baseOKNil
20 }
21
22 var _ TestDeep = &tdEmpty{}
23
24 // summary(Empty): checks that an array, a channel, a map, a slice or
25 // a string is empty
26 // input(Empty): str,array,slice,map,ptr(ptr on array/slice/map/string),chan
27
28 // Empty operator checks that an array, a channel, a map, a slice or a
29 // string is empty. As a special case (non-typed) nil, as well as nil
30 // channel, map or slice are considered empty.
31 //
32 // Note that the compared data can be a pointer (of pointer of pointer
33 // etc.) on an array, a channel, a map, a slice or a string.
34 //
35 // td.Cmp(t, "", td.Empty()) // succeeds
36 // td.Cmp(t, map[string]bool{}, td.Empty()) // succeeds
37 // td.Cmp(t, []string{"foo"}, td.Empty()) // fails
38 func Empty() TestDeep {
39 return &tdEmpty{
40 baseOKNil: newBaseOKNil(3),
41 }
42 }
43
44 // isEmpty returns (isEmpty, kindError) boolean values with only 3
45 // possible cases:
46 // - true, false → "got" is empty
47 // - false, false → "got" is not empty
48 // - false, true → "got" kind is not compatible with emptiness
49 func isEmpty(got reflect.Value) (bool, bool) {
50 switch got.Kind() {
51 case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
52 return got.Len() == 0, false
53
54 case reflect.Ptr:
55 switch got.Type().Elem().Kind() {
56 case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice,
57 reflect.String:
58 if got.IsNil() {
59 return true, false
60 }
61 fallthrough
62 case reflect.Ptr:
63 return isEmpty(got.Elem())
64 default:
65 return false, true // bad kind
66 }
67
68 default:
69 // nil case
70 if !got.IsValid() {
71 return true, false
72 }
73 return false, true // bad kind
74 }
75 }
76
77 func (e *tdEmpty) Match(ctx ctxerr.Context, got reflect.Value) (err *ctxerr.Error) {
78 ok, badKind := isEmpty(got)
79 if ok {
80 return nil
81 }
82
83 if ctx.BooleanError {
84 return ctxerr.BooleanError
85 }
86
87 if badKind {
88 return ctx.CollectError(ctxerr.BadKind(got, emptyBadKind))
89 }
90
91 return ctx.CollectError(&ctxerr.Error{
92 Message: "not empty",
93 Got: got,
94 Expected: types.RawString("empty"),
95 })
96 }
97
98 func (e *tdEmpty) String() string {
99 return "Empty()"
100 }
101
102 type tdNotEmpty struct {
103 baseOKNil
104 }
105
106 var _ TestDeep = &tdNotEmpty{}
107
108 // summary(NotEmpty): checks that an array, a channel, a map, a slice
109 // or a string is not empty
110 // input(NotEmpty): str,array,slice,map,ptr(ptr on array/slice/map/string),chan
111
112 // NotEmpty operator checks that an array, a channel, a map, a slice
113 // or a string is not empty. As a special case (non-typed) nil, as
114 // well as nil channel, map or slice are considered empty.
115 //
116 // Note that the compared data can be a pointer (of pointer of pointer
117 // etc.) on an array, a channel, a map, a slice or a string.
118 //
119 // td.Cmp(t, "", td.NotEmpty()) // fails
120 // td.Cmp(t, map[string]bool{}, td.NotEmpty()) // fails
121 // td.Cmp(t, []string{"foo"}, td.NotEmpty()) // succeeds
122 func NotEmpty() TestDeep {
123 return &tdNotEmpty{
124 baseOKNil: newBaseOKNil(3),
125 }
126 }
127
128 func (e *tdNotEmpty) Match(ctx ctxerr.Context, got reflect.Value) (err *ctxerr.Error) {
129 ok, badKind := isEmpty(got)
130 if ok {
131 if ctx.BooleanError {
132 return ctxerr.BooleanError
133 }
134 return ctx.CollectError(&ctxerr.Error{
135 Message: "empty",
136 Got: got,
137 Expected: types.RawString("not empty"),
138 })
139 }
140
141 if badKind {
142 if ctx.BooleanError {
143 return ctxerr.BooleanError
144 }
145 return ctx.CollectError(ctxerr.BadKind(got, emptyBadKind))
146 }
147 return nil
148 }
149
150 func (e *tdNotEmpty) String() string {
151 return "NotEmpty()"
152 }