Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
8ff8559c
Commit
8ff8559c
authored
Jul 10, 2015
by
Tim Hockin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add util functions
parent
95cd66d3
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
75 additions
and
0 deletions
+75
-0
util.go
cmd/mungedocs/util.go
+27
-0
util_test.go
cmd/mungedocs/util_test.go
+48
-0
No files found.
cmd/mungedocs/util.go
View file @
8ff8559c
...
@@ -71,3 +71,30 @@ func updateMacroBlock(lines []string, beginMark, endMark, insertThis string) ([]
...
@@ -71,3 +71,30 @@ func updateMacroBlock(lines []string, beginMark, endMark, insertThis string) ([]
}
}
return
buffer
.
Bytes
(),
nil
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
}
cmd/mungedocs/util_test.go
View file @
8ff8559c
...
@@ -60,3 +60,51 @@ func Test_updateMacroBlock_errors(t *testing.T) {
...
@@ -60,3 +60,51 @@ func Test_updateMacroBlock_errors(t *testing.T) {
assert
.
Error
(
t
,
err
)
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
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment