Skip to content

Commit 552ceb6

Browse files
committed
Add TestGHDocConvert2HTML(NonPlainText|ErrorConvert|LocalFileNotExists)
1 parent 694a7f4 commit 552ceb6

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

ghdoc_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package ghtoc
22

33
import (
44
"bytes"
5+
"errors"
56
"log"
7+
"os"
68
"testing"
79
)
810

@@ -412,3 +414,71 @@ func TestGHDocConvert2HTML(t *testing.T) {
412414
t.Error("Wrong html. \nGot :", doc.html, "\nWant:", htmlBody)
413415
}
414416
}
417+
418+
func TestGHDocConvert2HTMLNonPlainText(t *testing.T) {
419+
remotePath := "https://github.com/some/readme.md"
420+
token := "some-gh-token"
421+
doc := NewGHDoc(remotePath, true, 0, 0,
422+
true, token, 4, false)
423+
424+
// mock for getting remote raw README text
425+
htmlResponse := []byte("raw md text")
426+
doc.httpGetter = func(_ string) ([]byte, string, error) {
427+
return htmlResponse, "text/html;utf-8", nil
428+
}
429+
// should not call converter to HTML
430+
doc.httpPoster = func(urlPath, filePath, token string) (string, error) {
431+
t.Error("Should not call httpPost (via convertMd2Html)")
432+
return "", nil
433+
}
434+
if err := doc.Convert2HTML(); err != nil {
435+
t.Error("Got error:", err)
436+
}
437+
if doc.html != string(htmlResponse) {
438+
t.Error("Wrong html. \nGot :", doc.html, "\nWant:", string(htmlResponse))
439+
}
440+
}
441+
442+
func TestGHDocConvert2HTMLErrorConvert(t *testing.T) {
443+
remotePath := "https://github.com/some/readme.md"
444+
token := "some-gh-token"
445+
errGet := errors.New("error from http get")
446+
doc := NewGHDoc(remotePath, true, 0, 0,
447+
true, token, 4, false)
448+
449+
// mock for getting remote raw README text
450+
doc.httpGetter = func(urlPath string) ([]byte, string, error) {
451+
return nil, "", errGet
452+
}
453+
454+
err := doc.Convert2HTML()
455+
if err == nil {
456+
t.Error("Should get error from http get!")
457+
}
458+
459+
if !errors.Is(err, errGet) {
460+
t.Error("Wrong error. \nGot :", err, "\nWant:", errGet)
461+
}
462+
}
463+
464+
func TestGHDocConvert2HTMLLocalFileNotExists(t *testing.T) {
465+
localPath := "/some/readme.md"
466+
token := "some-gh-token"
467+
doc := NewGHDoc(localPath, true, 0, 0,
468+
true, token, 4, false)
469+
470+
// should not be called
471+
doc.httpGetter = func(_ string) ([]byte, string, error) {
472+
t.Error("Should not call httpGet")
473+
return nil, "", nil
474+
}
475+
476+
err := doc.Convert2HTML()
477+
if err == nil {
478+
t.Error("Should get error from file checking.")
479+
}
480+
481+
if !errors.Is(err, os.ErrNotExist) {
482+
t.Error("Wrong error. \nGot :", err, "\nWant:", os.ErrNotExist)
483+
}
484+
}

0 commit comments

Comments
 (0)