code.gitea.io/gitea@v1.22.3/modules/markup/html_codepreview_test.go (about)

     1  // Copyright 2024 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package markup_test
     5  
     6  import (
     7  	"context"
     8  	"html/template"
     9  	"strings"
    10  	"testing"
    11  
    12  	"code.gitea.io/gitea/modules/git"
    13  	"code.gitea.io/gitea/modules/markup"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestRenderCodePreview(t *testing.T) {
    19  	markup.Init(&markup.ProcessorHelper{
    20  		RenderRepoFileCodePreview: func(ctx context.Context, opts markup.RenderCodePreviewOptions) (template.HTML, error) {
    21  			return "<div>code preview</div>", nil
    22  		},
    23  	})
    24  	test := func(input, expected string) {
    25  		buffer, err := markup.RenderString(&markup.RenderContext{
    26  			Ctx:  git.DefaultContext,
    27  			Type: "markdown",
    28  		}, input)
    29  		assert.NoError(t, err)
    30  		assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
    31  	}
    32  	test("http://localhost:3000/owner/repo/src/commit/0123456789/foo/bar.md#L10-L20", "<p><div>code preview</div></p>")
    33  	test("http://other/owner/repo/src/commit/0123456789/foo/bar.md#L10-L20", `<p><a href="http://other/owner/repo/src/commit/0123456789/foo/bar.md#L10-L20" rel="nofollow">http://other/owner/repo/src/commit/0123456789/foo/bar.md#L10-L20</a></p>`)
    34  }