diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-04-20 06:25:08 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-19 18:25:08 -0400 |
commit | 9d99f6ab19ac3f97af3ca126720e9075c127a652 (patch) | |
tree | b817b4582a871f83b91ad7977fe772fc3501c1e8 /modules/markup/html_internal_test.go | |
parent | c9cc6698d2172625854cd063301e63602204a2a1 (diff) | |
download | gitea-9d99f6ab19ac3f97af3ca126720e9075c127a652.tar.gz gitea-9d99f6ab19ac3f97af3ca126720e9075c127a652.zip |
Refactor renders (#15175)
* Refactor renders
* Some performance optimization
* Fix comment
* Transform reader
* Fix csv test
* Fix test
* Fix tests
* Improve optimaziation
* Fix test
* Fix test
* Detect file encoding with reader
* Improve optimaziation
* reduce memory usage
* improve code
* fix build
* Fix test
* Fix for go1.15
* Fix render
* Fix comment
* Fix lint
* Fix test
* Don't use NormalEOF when unnecessary
* revert change on util.go
* Apply suggestions from code review
Co-authored-by: zeripath <art27@cantab.net>
* rename function
* Take NormalEOF back
Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'modules/markup/html_internal_test.go')
-rw-r--r-- | modules/markup/html_internal_test.go | 59 |
1 files changed, 32 insertions, 27 deletions
diff --git a/modules/markup/html_internal_test.go b/modules/markup/html_internal_test.go index 7e4bb6f22f..330750a47a 100644 --- a/modules/markup/html_internal_test.go +++ b/modules/markup/html_internal_test.go @@ -61,8 +61,8 @@ var localMetas = map[string]string{ func TestRender_IssueIndexPattern(t *testing.T) { // numeric: render inputs without valid mentions test := func(s string) { - testRenderIssueIndexPattern(t, s, s, nil) - testRenderIssueIndexPattern(t, s, s, &postProcessCtx{metas: numericMetas}) + testRenderIssueIndexPattern(t, s, s, &RenderContext{}) + testRenderIssueIndexPattern(t, s, s, &RenderContext{Metas: numericMetas}) } // should not render anything when there are no mentions @@ -109,13 +109,13 @@ func TestRender_IssueIndexPattern2(t *testing.T) { links[i] = numericIssueLink(util.URLJoin(setting.AppSubURL, path), "ref-issue", index, marker) } expectedNil := fmt.Sprintf(expectedFmt, links...) - testRenderIssueIndexPattern(t, s, expectedNil, &postProcessCtx{metas: localMetas}) + testRenderIssueIndexPattern(t, s, expectedNil, &RenderContext{Metas: localMetas}) for i, index := range indices { links[i] = numericIssueLink(prefix, "ref-issue", index, marker) } expectedNum := fmt.Sprintf(expectedFmt, links...) - testRenderIssueIndexPattern(t, s, expectedNum, &postProcessCtx{metas: numericMetas}) + testRenderIssueIndexPattern(t, s, expectedNum, &RenderContext{Metas: numericMetas}) } // should render freestanding mentions @@ -150,7 +150,7 @@ func TestRender_IssueIndexPattern3(t *testing.T) { // alphanumeric: render inputs without valid mentions test := func(s string) { - testRenderIssueIndexPattern(t, s, s, &postProcessCtx{metas: alphanumericMetas}) + testRenderIssueIndexPattern(t, s, s, &RenderContext{Metas: alphanumericMetas}) } test("") test("this is a test") @@ -181,25 +181,22 @@ func TestRender_IssueIndexPattern4(t *testing.T) { links[i] = alphanumIssueLink("https://someurl.com/someUser/someRepo/", "ref-issue", name) } expected := fmt.Sprintf(expectedFmt, links...) - testRenderIssueIndexPattern(t, s, expected, &postProcessCtx{metas: alphanumericMetas}) + testRenderIssueIndexPattern(t, s, expected, &RenderContext{Metas: alphanumericMetas}) } test("OTT-1234 test", "%s test", "OTT-1234") test("test T-12 issue", "test %s issue", "T-12") test("test issue ABCDEFGHIJ-1234567890", "test issue %s", "ABCDEFGHIJ-1234567890") } -func testRenderIssueIndexPattern(t *testing.T, input, expected string, ctx *postProcessCtx) { - if ctx == nil { - ctx = new(postProcessCtx) - } - ctx.procs = []processor{issueIndexPatternProcessor} - if ctx.urlPrefix == "" { - ctx.urlPrefix = AppSubURL +func testRenderIssueIndexPattern(t *testing.T, input, expected string, ctx *RenderContext) { + if ctx.URLPrefix == "" { + ctx.URLPrefix = AppSubURL } - res, err := ctx.postProcess([]byte(input)) + var buf strings.Builder + err := postProcess(ctx, []processor{issueIndexPatternProcessor}, strings.NewReader(input), &buf) assert.NoError(t, err) - assert.Equal(t, expected, string(res)) + assert.Equal(t, expected, buf.String()) } func TestRender_AutoLink(t *testing.T) { @@ -207,12 +204,22 @@ func TestRender_AutoLink(t *testing.T) { setting.AppSubURL = AppSubURL test := func(input, expected string) { - buffer, err := PostProcess([]byte(input), setting.AppSubURL, localMetas, false) + var buffer strings.Builder + err := PostProcess(&RenderContext{ + URLPrefix: setting.AppSubURL, + Metas: localMetas, + }, strings.NewReader(input), &buffer) assert.Equal(t, err, nil) - assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer))) - buffer, err = PostProcess([]byte(input), setting.AppSubURL, localMetas, true) + assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer.String())) + + buffer.Reset() + err = PostProcess(&RenderContext{ + URLPrefix: setting.AppSubURL, + Metas: localMetas, + IsWiki: true, + }, strings.NewReader(input), &buffer) assert.Equal(t, err, nil) - assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer))) + assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer.String())) } // render valid issue URLs @@ -235,15 +242,13 @@ func TestRender_FullIssueURLs(t *testing.T) { setting.AppSubURL = AppSubURL test := func(input, expected string) { - ctx := new(postProcessCtx) - ctx.procs = []processor{fullIssuePatternProcessor} - if ctx.urlPrefix == "" { - ctx.urlPrefix = AppSubURL - } - ctx.metas = localMetas - result, err := ctx.postProcess([]byte(input)) + var result strings.Builder + err := postProcess(&RenderContext{ + URLPrefix: AppSubURL, + Metas: localMetas, + }, []processor{fullIssuePatternProcessor}, strings.NewReader(input), &result) assert.NoError(t, err) - assert.Equal(t, expected, string(result)) + assert.Equal(t, expected, result.String()) } test("Here is a link https://git.osgeo.org/gogs/postgis/postgis/pulls/6", "Here is a link https://git.osgeo.org/gogs/postgis/postgis/pulls/6") |