package store import ( "testing" "time" "github.com/stretchr/testify/assert" ) type mockConverter struct{} func (m mockConverter) Convert(text string) string { return text + "!converted" } func TestFormatter_FormatText(t *testing.T) { tbl := []struct { in, out string name string }{ {"", "!converted", "empty"}, {"12345 abc", "
12345 abc
\n!converted", "simple"}, {"**xyz** _aaa_ - \"sfs\"", "xyz aaa – «sfs»
\n!converted", "format"}, { "http://127.0.0.1/some-long-link/12345/678901234567890", "http://127.0.0." + "1/some-long-link/12345/6789012...
\n!converted", "links", }, {"— not translated #354", "— not translated #354
\n!converted", "mdash"}, {"smth\n```go\nfunc main(aa string) int {return 0}\n```", `smth
func main(aa string) int {return 0}!converted`, "code with language"}, {"```\ntest_code\n```", `
test_code!converted`, "code without language"}, } f := NewCommentFormatter(mockConverter{}) for _, tt := range tbl { t.Run(tt.name, func(t *testing.T) { assert.Equal(t, tt.out, f.FormatText(tt.in)) }) } } func TestFormatter_FormatTextNoConverter(t *testing.T) { f := NewCommentFormatter() assert.Equal(t, "
12345
\n", f.FormatText("12345")) } func TestFormatter_FormatTextConverterFunc(t *testing.T) { fn := CommentConverterFunc(func(text string) string { return "zz!" + text }) f := NewCommentFormatter(fn) assert.Equal(t, "zz!12345
\n", f.FormatText("12345")) } func TestFormatter_FormatComment(t *testing.T) { comment := Comment{ Text: "blah\n\nxyz", User: User{ID: "username"}, ParentID: "p123", ID: "123", Locator: Locator{SiteID: "site", URL: "url"}, Score: 10, Pin: true, Deleted: true, Timestamp: time.Date(2018, 1, 1, 9, 30, 0, 0, time.Local), Votes: map[string]bool{"uu": true}, } f := NewCommentFormatter(mockConverter{}) exp := comment exp.Text = "blah
\n\nxyz
\n!converted" assert.Equal(t, exp, f.Format(comment)) } func TestFormatter_ShortenAutoLinks(t *testing.T) { f := NewCommentFormatter(nil) tbl := []struct { max int in, out string }{ {32, "", ""}, {32, "text", "text"}, {32, "asd
", "asd
"}, {5, `incorrect-url`, `incorrect-url`}, {32, `some text, not href`, `some text, not href`}, { 32, `https://blah.com/a/b/c/d?g=123#anc`, `https://blah.com/a/b/c/d?g=123#anc`, }, { 31, `https://blah.com/a/b/c/d?g=123#anc`, `https://blah.com/a/b/c/d?g=1...`, }, { 15, `https://blah.com/a/b/c/d?g=123#anc`, `https://blah.com...`, }, { 3, `https://blah.com/a/b/c/d?g=123#anc`, `https://blah.com...`, }, { -1, `https://blah.com/a/b/c/d?g=123#anc`, `https://blah.com/a/b/c/d?g=123#anc`, }, } for n, tt := range tbl { got := f.shortenAutoLinks(tt.in, tt.max) assert.Equalf(t, tt.out, got, "check #%d", n) } }