mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-01 07:47:56 +00:00
Add util functions
This commit is contained in:
parent
95cd66d3a0
commit
8ff8559c4c
@ -71,3 +71,30 @@ func updateMacroBlock(lines []string, beginMark, endMark, insertThis string) ([]
|
||||
}
|
||||
return buffer.Bytes(), nil
|
||||
}
|
||||
|
||||
// Tests that a document, represented as a slice of lines, has a line. Ignores
|
||||
// leading and trailing space.
|
||||
func hasLine(lines []string, needle string) bool {
|
||||
for _, line := range lines {
|
||||
trimmedLine := strings.Trim(line, " \n")
|
||||
if trimmedLine == needle {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Tests that a document, represented as a slice of lines, has a macro block.
|
||||
func hasMacroBlock(lines []string, begin string, end string) bool {
|
||||
foundBegin := false
|
||||
for _, line := range lines {
|
||||
trimmedLine := strings.Trim(line, " \n")
|
||||
switch {
|
||||
case !foundBegin && trimmedLine == begin:
|
||||
foundBegin = true
|
||||
case foundBegin && trimmedLine == end:
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
@ -60,3 +60,51 @@ func Test_updateMacroBlock_errors(t *testing.T) {
|
||||
assert.Error(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHasLine(t *testing.T) {
|
||||
cases := []struct {
|
||||
lines []string
|
||||
needle string
|
||||
expected bool
|
||||
}{
|
||||
{[]string{"abc", "def", "ghi"}, "abc", true},
|
||||
{[]string{" abc", "def", "ghi"}, "abc", true},
|
||||
{[]string{"abc ", "def", "ghi"}, "abc", true},
|
||||
{[]string{"\n abc", "def", "ghi"}, "abc", true},
|
||||
{[]string{"abc \n", "def", "ghi"}, "abc", true},
|
||||
{[]string{"abc", "def", "ghi"}, "def", true},
|
||||
{[]string{"abc", "def", "ghi"}, "ghi", true},
|
||||
{[]string{"abc", "def", "ghi"}, "xyz", false},
|
||||
}
|
||||
|
||||
for i, c := range cases {
|
||||
if hasLine(c.lines, c.needle) != c.expected {
|
||||
t.Errorf("case[%d]: %q, expected %t, got %t", i, c.needle, c.expected, !c.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHasMacroBlock(t *testing.T) {
|
||||
cases := []struct {
|
||||
lines []string
|
||||
begin string
|
||||
end string
|
||||
expected bool
|
||||
}{
|
||||
{[]string{"<<<", ">>>"}, "<<<", ">>>", true},
|
||||
{[]string{"<<<", "abc", ">>>"}, "<<<", ">>>", true},
|
||||
{[]string{"<<<", "<<<", "abc", ">>>"}, "<<<", ">>>", true},
|
||||
{[]string{"<<<", "abc", ">>>", ">>>"}, "<<<", ">>>", true},
|
||||
{[]string{"<<<", ">>>", "<<<", ">>>"}, "<<<", ">>>", true},
|
||||
{[]string{"<<<"}, "<<<", ">>>", false},
|
||||
{[]string{">>>"}, "<<<", ">>>", false},
|
||||
{[]string{"<<<", "abc"}, "<<<", ">>>", false},
|
||||
{[]string{"abc", ">>>"}, "<<<", ">>>", false},
|
||||
}
|
||||
|
||||
for i, c := range cases {
|
||||
if hasMacroBlock(c.lines, c.begin, c.end) != c.expected {
|
||||
t.Errorf("case[%d]: %q,%q, expected %t, got %t", i, c.begin, c.end, c.expected, !c.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user