-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommands_test.go
133 lines (122 loc) · 3.49 KB
/
commands_test.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// -----------------------------------------------------------------------------
// CMDX Utilities Suite cmdx/[commands_test.go]
// (c) balarabe@protonmail.com License: GPLv3
// -----------------------------------------------------------------------------
package main
// to test all items in commands.go use:
// go test --run Test_cmds_
//
// to generate a test coverage report for the whole module use:
// go test -coverprofile cover.out
// go tool cover -html=cover.out
import (
"reflect"
"strings"
"testing"
"github.com/balacode/zr"
)
// -----------------------------------------------------------------------------
// go test --run Test_cmds_consts_
func Test_cmds_consts_(t *testing.T) {
zr.TEqual(t, BB, ("BB"))
zr.TEqual(t, BC, ("BC"))
zr.TEqual(t, BE, ("BE"))
zr.TEqual(t, CB, ("CB"))
zr.TEqual(t, CLL, ("CLL"))
zr.TEqual(t, EB, ("EB"))
zr.TEqual(t, LT, ("<"))
zr.TEqual(t, T, ("T"))
zr.TEqual(t, XE, ("XE"))
}
// -----------------------------------------------------------------------------
// go test --run Test_cmds_AllCategories_
func Test_cmds_AllCategories_(t *testing.T) {
categories := make(map[string]bool, len(AllCategories))
for key, cat := range AllCategories {
if key < 1 || key > 4 {
t.Error("Invalid category key:", key)
}
var exist bool
//
// category must be consistent and not zero-length
if len(strings.TrimSpace(cat)) == 0 {
t.Error("Invalid category: '" + cat + "'")
}
// category must be unique
_, exist = categories[cat]
if exist {
t.Error("Non-unique category '" + cat + "'")
}
categories[cat] = true
}
}
// -----------------------------------------------------------------------------
// go test --run Test_cmds_AllCommands_
func Test_cmds_AllCommands_(t *testing.T) {
var (
shortNames = make(map[string]bool, len(AllCommands))
fullNames = make(map[string]bool, len(AllCommands))
handlers = make(map[uintptr]bool, len(AllCommands))
)
isValidName := func(s string) bool {
for _, ch := range s {
if (ch < 'a' || ch > 'z') && ch != '-' {
return false
}
}
return true
}
addressOf := func(value interface{}) uintptr {
var ret uintptr
refVal := reflect.ValueOf(value)
switch refVal.Kind() {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr,
reflect.Slice, reflect.UnsafePointer:
ret = refVal.Pointer()
}
return ret
}
for _, cmd := range AllCommands {
// ShortName must be consistent and not zero-length
if !isValidName(cmd.ShortName) {
t.Error("Invalid 'ShortName':", zr.DescribeStruct(&cmd))
}
var exist bool
// ShortName must be unique
_, exist = shortNames[cmd.ShortName]
if exist {
t.Error(
"Non-unique ShortName '"+cmd.ShortName+"' in",
zr.DescribeStruct(&cmd),
)
}
shortNames[cmd.ShortName] = true
//
// FullName must be consistent and not zero-length
if !isValidName(cmd.FullName) {
t.Error("Invalid 'FullName':", zr.DescribeStruct(&cmd))
}
// FullName must be unique
_, exist = fullNames[cmd.FullName]
if exist {
t.Error(
"Non-unique FullName '"+cmd.FullName+"' in",
zr.DescribeStruct(&cmd),
)
}
fullNames[cmd.FullName] = true
//
// Handler must not be nil
if cmd.Handler == nil {
t.Error("Handler must not be <nil> in", zr.DescribeStruct(&cmd))
}
// Handler must be unique
addr := addressOf(cmd.Handler)
_, exist = handlers[addr]
if exist {
t.Error("Non-unique Handler in", zr.DescribeStruct(&cmd))
}
handlers[addr] = true
}
}
// end