Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
takaishi committed Jan 28, 2019
1 parent 67c9815 commit 95b70a6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 45 deletions.
1 change: 1 addition & 0 deletions mdtoc/mdtoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@ func (mdtoc *MDToc) GenerateTOC(input []byte) string {
}
return blackfriday.GoToNext
})
toc = fmt.Sprintf("%s\n", toc)
return toc
}
85 changes: 40 additions & 45 deletions mdtoc/mdtoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"testing"
)

var inputTextValid = `# This is example
var inputTextValid = `
# This is example
<!-- toc -->
Expand All @@ -17,7 +18,34 @@ aaa
bbb
`

var inputTextInvalid1 = `# This is example
var expectTOC = `
* [foo](#foo)
* [bar](#bar)
`

var outputTextValid = `
# This is example
<!-- toc -->
<!-- toc:start -->
* [foo](#foo)
* [bar](#bar)
<!-- toc:end -->
## foo
aaa
## bar
bbb
`

var inputTextInvalidWithoutTOCComment = `
# This is example
## foo
Expand All @@ -28,7 +56,8 @@ aaa
bbb
`

var inputTextInvalid2 = `# This is example
var inputTextInvalidWithoutTOCEndComment = `
# This is example
<!-- toc -->
<!-- toc:start -->
Expand All @@ -50,79 +79,45 @@ func TestGenerateTOC(t *testing.T) {
mt := MDToc{File: "", InFile: false, Level: 2}

toc := mt.GenerateTOC([]byte(inputTextValid))
expect := `
* [foo](#foo)
* [bar](#bar)`

if toc != expect {
if toc != expectTOC {
t.Error(toc)
t.Error(expect)
t.Error(expectTOC)
}
}

func TestInsertTOC(t *testing.T) {
mt := MDToc{File: "", InFile: false, Level: 2}

toc := mt.GenerateTOC([]byte(inputTextValid))
expect := `
* [foo](#foo)
* [bar](#bar)`

if toc != expect {
if toc != expectTOC {
t.Error(toc)
t.Error(expect)
t.Error(expectTOC)
}

output, err := mt.InsertTOC(inputTextValid, toc)
if err != nil {
t.Error(err)
}
expect = `# This is example
<!-- toc -->
<!-- toc:start -->
* [foo](#foo)
* [bar](#bar)
<!-- toc:end -->
## foo
aaa
## bar
bbb
`

if output != expect {
if output != outputTextValid {
t.Error("unmatch")
t.Error(output)
t.Error(expect)
t.Error(expectTOC)
}
}

func TestInsertTOCWithInvalidString(t *testing.T) {
mt := MDToc{File: "", InFile: false, Level: 2}

toc := mt.GenerateTOC([]byte(inputTextValid))
expect := `
* [foo](#foo)
* [bar](#bar)`

if toc != expect {
t.Error(toc)
t.Error(expect)
}

_, err := mt.InsertTOC(inputTextInvalid1, toc)

_, err := mt.InsertTOC(inputTextInvalidWithoutTOCComment, toc)
if err.Error() != "Can not find toc_pos comment `<!-- toc -->`" {
t.Error()
}

_, err = mt.InsertTOC(inputTextInvalid2, toc)
_, err = mt.InsertTOC(inputTextInvalidWithoutTOCEndComment, toc)

if err.Error() != "Can not find toc end position comment `<!-- toc:end -->`." {
t.Error()
Expand Down

0 comments on commit 95b70a6

Please sign in to comment.