forked from kata-containers/tests
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_test.go
149 lines (118 loc) · 3.12 KB
/
utils_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
// Copyright (c) 2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSplitLink(t *testing.T) {
assert := assert.New(t)
type testData struct {
linkName string
file string
section string
valid bool
}
data := []testData{
{"", "", "", false},
{"foo.md", "foo.md", "", true},
{"#bar", "", "bar", true},
{"foo.md#bar", "foo.md", "bar", true},
{"foo.md%%bar", "foo.md%%bar", "", true},
}
for i, d := range data {
file, section, err := splitLink(d.linkName)
if d.valid {
assert.NoErrorf(err, "test[%d]: %+v", i, d)
assert.Equal(file, d.file, "test[%d]: %+v", i, d)
assert.Equal(section, d.section, "test[%d]: %+v", i, d)
} else {
assert.Errorf(err, "test[%d]: %+v", i, d)
}
}
}
func TestValidHeadingIDChar(t *testing.T) {
assert := assert.New(t)
type testData struct {
ch rune
valid bool
}
data := []testData{
{' ', true},
{'\t', true},
{'\n', true},
{'a', true},
{'z', true},
{'A', true},
{'Z', true},
{'0', true},
{'9', true},
{'-', true},
{'_', true},
{'\000', false},
{'\001', false},
}
for i, d := range data {
result := validHeadingIDChar(d.ch)
var outcome bool
if d.valid {
outcome = result != -1
} else {
outcome = result == -1
}
assert.Truef(outcome, "test[%d]: %+v", i, d)
}
// the main list of invalid chars to test
invalid := "!@#$%^&*()+=[]{}\\|:\";'<>?,./"
for i, ch := range invalid {
result := validHeadingIDChar(ch)
outcome := result == -1
assert.Truef(outcome, "invalid[%d]: %+v", i, ch)
}
}
func TestCreateHeadingID(t *testing.T) {
assert := assert.New(t)
type testData struct {
heading string
id string
expecteError bool
}
data := []testData{
{"", "", true},
{"a", "a", false},
{"a.b/c:d", "abcd", false},
{"a ?", "a-", false},
{"a !?!", "a-", false},
{"foo", "foo", false},
{"foo bar", "foo-bar", false},
{"foo_bar", "foo_bar", false},
{"foo_bar()", "foo_bar", false},
{"`foo_bar()`", "foo_bar", false},
{"foo_bar()baz", "foo_barbaz", false},
{"Stability or Performance?", "stability-or-performance", false},
{"Hello - World", "hello---world", false},
{"metrics_json_init()", "metrics_json_init", false},
{"metrics_json_add_array_element(json)", "metrics_json_add_array_elementjson", false},
{"What is it ?", "what-is-it-", false},
{"Sandbox `DeviceInfo`", "sandbox-deviceinfo", false},
{"Build a custom QEMU for aarch64/arm64 - REQUIRED", "build-a-custom-qemu-for-aarch64arm64---required", false},
{"docker --net=host", "docker---nethost", false},
{"Containerd Runtime V2 API (Shim V2 API)", "containerd-runtime-v2-api-shim-v2-api", false},
{"Containerd Runtime V2 API: Shim V2 API", "containerd-runtime-v2-api-shim-v2-api", false},
{"Launch i3.metal instance", "launch-i3metal-instance", false},
{"Deploy!", "deploy", false},
}
for i, d := range data {
id, err := createHeadingID(d.heading)
msg := fmt.Sprintf("test[%d]: %+v, id: %q\n", i, d, id)
if d.expecteError {
assert.Error(err)
continue
}
assert.Equal(id, d.id, msg)
}
}