summaryrefslogtreecommitdiffstats
path: root/modules/markup/markdown/meta_test.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel@debian.org>2024-10-18 20:33:49 +0200
committerDaniel Baumann <daniel@debian.org>2024-12-12 23:57:56 +0100
commite68b9d00a6e05b3a941f63ffb696f91e554ac5ec (patch)
tree97775d6c13b0f416af55314eb6a89ef792474615 /modules/markup/markdown/meta_test.go
parentInitial commit. (diff)
downloadforgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.tar.xz
forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.zip
Adding upstream version 9.0.3.
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to 'modules/markup/markdown/meta_test.go')
-rw-r--r--modules/markup/markdown/meta_test.go110
1 files changed, 110 insertions, 0 deletions
diff --git a/modules/markup/markdown/meta_test.go b/modules/markup/markdown/meta_test.go
new file mode 100644
index 0000000..d341ae4
--- /dev/null
+++ b/modules/markup/markdown/meta_test.go
@@ -0,0 +1,110 @@
+// Copyright 2020 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package markdown
+
+import (
+ "fmt"
+ "strings"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+/*
+IssueTemplate is a legacy to keep the unit tests working.
+Copied from structs.IssueTemplate, the original type has been changed a lot to support yaml template.
+*/
+type IssueTemplate struct {
+ Name string `json:"name" yaml:"name"`
+ Title string `json:"title" yaml:"title"`
+ About string `json:"about" yaml:"about"`
+ Labels []string `json:"labels" yaml:"labels"`
+ Ref string `json:"ref" yaml:"ref"`
+}
+
+func (it *IssueTemplate) Valid() bool {
+ return strings.TrimSpace(it.Name) != "" && strings.TrimSpace(it.About) != ""
+}
+
+func TestExtractMetadata(t *testing.T) {
+ t.Run("ValidFrontAndBody", func(t *testing.T) {
+ var meta IssueTemplate
+ body, err := ExtractMetadata(fmt.Sprintf("%s\n%s\n%s\n%s", sepTest, frontTest, sepTest, bodyTest), &meta)
+ require.NoError(t, err)
+ assert.Equal(t, bodyTest, body)
+ assert.Equal(t, metaTest, meta)
+ assert.True(t, meta.Valid())
+ })
+
+ t.Run("NoFirstSeparator", func(t *testing.T) {
+ var meta IssueTemplate
+ _, err := ExtractMetadata(fmt.Sprintf("%s\n%s\n%s", frontTest, sepTest, bodyTest), &meta)
+ require.Error(t, err)
+ })
+
+ t.Run("NoLastSeparator", func(t *testing.T) {
+ var meta IssueTemplate
+ _, err := ExtractMetadata(fmt.Sprintf("%s\n%s\n%s", sepTest, frontTest, bodyTest), &meta)
+ require.Error(t, err)
+ })
+
+ t.Run("NoBody", func(t *testing.T) {
+ var meta IssueTemplate
+ body, err := ExtractMetadata(fmt.Sprintf("%s\n%s\n%s", sepTest, frontTest, sepTest), &meta)
+ require.NoError(t, err)
+ assert.Equal(t, "", body)
+ assert.Equal(t, metaTest, meta)
+ assert.True(t, meta.Valid())
+ })
+}
+
+func TestExtractMetadataBytes(t *testing.T) {
+ t.Run("ValidFrontAndBody", func(t *testing.T) {
+ var meta IssueTemplate
+ body, err := ExtractMetadataBytes([]byte(fmt.Sprintf("%s\n%s\n%s\n%s", sepTest, frontTest, sepTest, bodyTest)), &meta)
+ require.NoError(t, err)
+ assert.Equal(t, bodyTest, string(body))
+ assert.Equal(t, metaTest, meta)
+ assert.True(t, meta.Valid())
+ })
+
+ t.Run("NoFirstSeparator", func(t *testing.T) {
+ var meta IssueTemplate
+ _, err := ExtractMetadataBytes([]byte(fmt.Sprintf("%s\n%s\n%s", frontTest, sepTest, bodyTest)), &meta)
+ require.Error(t, err)
+ })
+
+ t.Run("NoLastSeparator", func(t *testing.T) {
+ var meta IssueTemplate
+ _, err := ExtractMetadataBytes([]byte(fmt.Sprintf("%s\n%s\n%s", sepTest, frontTest, bodyTest)), &meta)
+ require.Error(t, err)
+ })
+
+ t.Run("NoBody", func(t *testing.T) {
+ var meta IssueTemplate
+ body, err := ExtractMetadataBytes([]byte(fmt.Sprintf("%s\n%s\n%s", sepTest, frontTest, sepTest)), &meta)
+ require.NoError(t, err)
+ assert.Equal(t, "", string(body))
+ assert.Equal(t, metaTest, meta)
+ assert.True(t, meta.Valid())
+ })
+}
+
+var (
+ sepTest = "-----"
+ frontTest = `name: Test
+about: "A Test"
+title: "Test Title"
+labels:
+ - bug
+ - "test label"`
+ bodyTest = "This is the body"
+ metaTest = IssueTemplate{
+ Name: "Test",
+ About: "A Test",
+ Title: "Test Title",
+ Labels: []string{"bug", "test label"},
+ }
+)