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>
This commit is contained in:
parent
c9cc6698d2
commit
9d99f6ab19
41 changed files with 1027 additions and 627 deletions
|
@ -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")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue