package store import ( "strconv" "strings" "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", }, { "something https://amp.dw.com/ru/илон-маск-купил-twitter/a-61590911", "

something https://amp.dw.com/ru/илон-маск-купил-twitter...

\n!converted", "links", }, { "something _aaa_", "

something aaa

\n!converted", "lazy image", }, {"— not translated #354", "

— not translated #354

\n!converted", "mdash"}, {`no_smartpants "quoted" text`, "

no_smartpants "quoted" text

\n!converted", "normal quotes without smartpants"}, {`"quoted" text`, "

«quoted» text

\n!converted", "normal quotes with smartpants"}, {`no_smartpants “quoted” text`, "

no_smartpants “quoted” text

\n!converted", "curly quotes without smartpants"}, {`“quoted” text`, "

“quoted” text

\n!converted", "curly quotes with smartpants"}, {`no_smartpants «quoted» text`, "

no_smartpants «quoted» text

\n!converted", "French guillemets without smartpants"}, {`«quoted» text`, "

«quoted» text

\n!converted", "French guillemets with smartpants"}, {"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) { raw := strings.HasPrefix(tt.in, `no_smartpants`) t.Logf("raw: %v", raw) assert.Equal(t, tt.out, f.FormatText(tt.in, raw)) }) } } func TestFormatter_FormatTextNoConverter(t *testing.T) { f := NewCommentFormatter() assert.Equal(t, "

12345

\n", f.FormatText("12345", false)) } 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", false)) } 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: "http://example.com?foo=bar&x=123"}, 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\n

xyz

\n!converted" assert.Equal(t, exp, f.Format(comment, false)) } 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) } } func TestCommentFormatter_lazyImage(t *testing.T) { tbl := []struct { inp, out string }{ {"", ""}, {`blah `, `blah `}, {`blah `, `blah `}, {`blah ххх `, `blah ххх `}, } f := NewCommentFormatter(nil) for i, tt := range tbl { t.Run(strconv.Itoa(i), func(t *testing.T) { assert.Equal(t, tt.out, f.lazyImage(tt.inp)) }) } }