diff --git a/cmd/mungedocs/util.go b/cmd/mungedocs/util.go index f26df9b714c..8581f5152b9 100644 --- a/cmd/mungedocs/util.go +++ b/cmd/mungedocs/util.go @@ -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 +} diff --git a/cmd/mungedocs/util_test.go b/cmd/mungedocs/util_test.go index 49fbcee6381..30d85d5a2b4 100644 --- a/cmd/mungedocs/util_test.go +++ b/cmd/mungedocs/util_test.go @@ -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) + } + } +}