Skip to content

Commit

Permalink
Add test for GHToc.Print
Browse files Browse the repository at this point in the history
  • Loading branch information
ekalinin committed Dec 12, 2021
1 parent 9cb84c6 commit 13b05a7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
5 changes: 3 additions & 2 deletions cmd/gh-md-toc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func main() {
toc := <-ch
// #14, check if there's really TOC?
if toc != nil {
toc.Print()
toc.Print(os.Stdout)
}
}

Expand All @@ -80,7 +80,8 @@ func main() {
defer os.Remove(file.Name())

check(ioutil.WriteFile(file.Name(), bytes, 0644))
ghtoc.NewGHDoc(file.Name(), false, *startDepth, *depth, !*noEscape, *token, *indent, *debug).GetToc().Print()
ghtoc.NewGHDoc(file.Name(), false, *startDepth, *depth, !*noEscape, *token, *indent, *debug).
GetToc().Print(os.Stdout)
}

if !*hideFooter {
Expand Down
7 changes: 4 additions & 3 deletions ghdoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ghtoc

import (
"fmt"
"io"
"io/ioutil"
"log"
"net/url"
Expand All @@ -15,11 +16,11 @@ import (
type GHToc []string

// Print TOC to the console
func (toc *GHToc) Print() {
func (toc *GHToc) Print(w io.Writer) {
for _, tocItem := range *toc {
fmt.Println(tocItem)
fmt.Fprint(w, tocItem)
}
fmt.Println()
fmt.Fprintln(w)
}

// GHDoc GitHub document
Expand Down
16 changes: 15 additions & 1 deletion ghdoc_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ghtoc

import "testing"
import (
"bytes"
"testing"
)

func Test_IsUrl(t *testing.T) {
doc1 := &GHDoc{
Expand Down Expand Up @@ -346,3 +349,14 @@ func Test_MinHeaderNumber(t *testing.T) {
t.Error("Res :", toc, "\nExpected :", tocExpected)
}
}

func TestGHToc_Print(t *testing.T) {
toc := GHToc{"one", "two"}
want := "onetwo\n"
var got bytes.Buffer
toc.Print(&got)

if got.String() != want {
t.Error("\nGot :", got.String(), "\nWant:", want)
}
}

0 comments on commit 13b05a7

Please sign in to comment.