-
Notifications
You must be signed in to change notification settings - Fork 2
/
script_test.go
132 lines (120 loc) · 3.07 KB
/
script_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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package statedb
import (
"context"
"maps"
"slices"
"strings"
"testing"
"github.com/cilium/hive"
"github.com/cilium/hive/cell"
"github.com/cilium/hive/hivetest"
"github.com/cilium/hive/script"
"github.com/cilium/hive/script/scripttest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestScript(t *testing.T) {
log := hivetest.Logger(t)
h := hive.New(
Cell, // DB
cell.Invoke(func(db *DB) {
t1 := newTestObjectTable(t, "test1", tagsIndex)
require.NoError(t, db.RegisterTable(t1), "RegisterTable")
t2 := newTestObjectTable(t, "test2", tagsIndex)
require.NoError(t, db.RegisterTable(t2), "RegisterTable")
}),
)
t.Cleanup(func() {
assert.NoError(t, h.Stop(log, context.TODO()))
})
cmds, err := h.ScriptCommands(log)
require.NoError(t, err, "ScriptCommands")
maps.Insert(cmds, maps.All(script.DefaultCmds()))
engine := &script.Engine{
Cmds: cmds,
}
scripttest.Test(t,
context.Background(),
func(t testing.TB, args []string) *script.Engine {
return engine
},
[]string{},
"testdata/*.txtar",
)
}
func TestHeaderLine(t *testing.T) {
type retrieval struct {
header string
idxs []int
}
testCases := []struct {
line string
names []string
pos []int
get []retrieval
}{
{
"Foo Bar ",
[]string{"Foo", "Bar"},
[]int{0, 6},
[]retrieval{
{"Foo", []int{0}},
{"Bar", []int{1}},
{"Bar Foo Bar", []int{1, 0, 1}},
},
},
{
"Foo Bar Quux",
[]string{"Foo", "Bar", "Quux"},
[]int{0, 4, 10},
[]retrieval{
{"Foo", []int{0}},
{"Bar", []int{1}},
{"Bar Foo", []int{1, 0}},
{"Quux", []int{2}},
{"Quux Foo", []int{2, 0}},
},
},
}
for _, tc := range testCases {
// Parse header line into names and positions
names, pos := splitHeaderLine(tc.line)
require.Equal(t, tc.names, names)
require.Equal(t, tc.pos, pos)
// Split the header line with the parsed positions.
header := splitByPositions(tc.line, pos)
require.Equal(t, tc.names, header)
// Join the headers with the positions.
line := joinByPositions(header, pos)
require.Equal(t, strings.TrimRight(tc.line, " \t"), line)
// Test retrievals
for _, r := range tc.get {
names, pos = splitHeaderLine(r.header)
idxs, err := getColumnIndexes(names, header)
require.NoError(t, err)
require.Equal(t, r.idxs, idxs)
row := slices.Clone(header)
cols := takeColumns(row, idxs)
line := joinByPositions(cols, pos)
require.Equal(t, line, r.header)
}
}
}
func TestSortedFlags(t *testing.T) {
cases := []struct{ input, expected string }{
{"foo bar", "foo bar"},
{"bar foo", "bar foo"},
{"foo bar -baz=1", "-baz=1 foo bar"},
{"bar foo -baz=1", "-baz=1 bar foo"},
{"foo -baz=1 bar", "-baz=1 foo bar"},
{"-baz=1 foo bar", "-baz=1 foo bar"},
{"-baz=1 foo -quux=2 bar", "-baz=1 -quux=2 foo bar"},
{"-baz=1 bar foo -quux=2", "-baz=1 -quux=2 bar foo"},
}
for _, tc := range cases {
actual := strings.Join(sortedArgs(strings.Split(tc.input, " ")), " ")
assert.Equal(t, tc.expected, actual)
}
}