2020-09-12 19:48:47 -06:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-09-12 19:48:47 -06:00
|
|
|
|
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-09-02 01:58:49 -06:00
|
|
|
"strings"
|
2020-09-12 19:48:47 -06:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2024-11-17 22:25:42 -07:00
|
|
|
// 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.
|
2022-11-19 08:22:15 -07:00
|
|
|
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 {
|
2022-09-02 01:58:49 -06:00
|
|
|
return strings.TrimSpace(it.Name) != "" && strings.TrimSpace(it.About) != ""
|
|
|
|
}
|
|
|
|
|
2020-09-12 19:48:47 -06:00
|
|
|
func TestExtractMetadata(t *testing.T) {
|
|
|
|
t.Run("ValidFrontAndBody", func(t *testing.T) {
|
2022-11-19 08:22:15 -07:00
|
|
|
var meta IssueTemplate
|
2020-09-12 19:48:47 -06:00
|
|
|
body, err := ExtractMetadata(fmt.Sprintf("%s\n%s\n%s\n%s", sepTest, frontTest, sepTest, bodyTest), &meta)
|
|
|
|
assert.NoError(t, err)
|
2021-06-06 23:27:09 -06:00
|
|
|
assert.Equal(t, bodyTest, body)
|
2020-09-12 19:48:47 -06:00
|
|
|
assert.Equal(t, metaTest, meta)
|
2022-11-19 08:22:15 -07:00
|
|
|
assert.True(t, meta.Valid())
|
2020-09-12 19:48:47 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NoFirstSeparator", func(t *testing.T) {
|
2022-11-19 08:22:15 -07:00
|
|
|
var meta IssueTemplate
|
2020-09-12 19:48:47 -06:00
|
|
|
_, err := ExtractMetadata(fmt.Sprintf("%s\n%s\n%s", frontTest, sepTest, bodyTest), &meta)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NoLastSeparator", func(t *testing.T) {
|
2022-11-19 08:22:15 -07:00
|
|
|
var meta IssueTemplate
|
2020-09-12 19:48:47 -06:00
|
|
|
_, err := ExtractMetadata(fmt.Sprintf("%s\n%s\n%s", sepTest, frontTest, bodyTest), &meta)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NoBody", func(t *testing.T) {
|
2022-11-19 08:22:15 -07:00
|
|
|
var meta IssueTemplate
|
2020-09-12 19:48:47 -06:00
|
|
|
body, err := ExtractMetadata(fmt.Sprintf("%s\n%s\n%s", sepTest, frontTest, sepTest), &meta)
|
|
|
|
assert.NoError(t, err)
|
2021-06-06 23:27:09 -06:00
|
|
|
assert.Equal(t, "", body)
|
2020-09-12 19:48:47 -06:00
|
|
|
assert.Equal(t, metaTest, meta)
|
2022-11-19 08:22:15 -07:00
|
|
|
assert.True(t, meta.Valid())
|
2020-09-12 19:48:47 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-13 10:33:37 -06:00
|
|
|
func TestExtractMetadataBytes(t *testing.T) {
|
|
|
|
t.Run("ValidFrontAndBody", func(t *testing.T) {
|
2022-11-19 08:22:15 -07:00
|
|
|
var meta IssueTemplate
|
2022-09-13 10:33:37 -06:00
|
|
|
body, err := ExtractMetadataBytes([]byte(fmt.Sprintf("%s\n%s\n%s\n%s", sepTest, frontTest, sepTest, bodyTest)), &meta)
|
|
|
|
assert.NoError(t, err)
|
2022-10-05 12:55:36 -06:00
|
|
|
assert.Equal(t, bodyTest, string(body))
|
2022-09-13 10:33:37 -06:00
|
|
|
assert.Equal(t, metaTest, meta)
|
2022-11-19 08:22:15 -07:00
|
|
|
assert.True(t, meta.Valid())
|
2022-09-13 10:33:37 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NoFirstSeparator", func(t *testing.T) {
|
2022-11-19 08:22:15 -07:00
|
|
|
var meta IssueTemplate
|
2022-09-13 10:33:37 -06:00
|
|
|
_, err := ExtractMetadataBytes([]byte(fmt.Sprintf("%s\n%s\n%s", frontTest, sepTest, bodyTest)), &meta)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NoLastSeparator", func(t *testing.T) {
|
2022-11-19 08:22:15 -07:00
|
|
|
var meta IssueTemplate
|
2022-09-13 10:33:37 -06:00
|
|
|
_, err := ExtractMetadataBytes([]byte(fmt.Sprintf("%s\n%s\n%s", sepTest, frontTest, bodyTest)), &meta)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NoBody", func(t *testing.T) {
|
2022-11-19 08:22:15 -07:00
|
|
|
var meta IssueTemplate
|
2022-09-13 10:33:37 -06:00
|
|
|
body, err := ExtractMetadataBytes([]byte(fmt.Sprintf("%s\n%s\n%s", sepTest, frontTest, sepTest)), &meta)
|
|
|
|
assert.NoError(t, err)
|
2022-10-05 12:55:36 -06:00
|
|
|
assert.Equal(t, "", string(body))
|
2022-09-13 10:33:37 -06:00
|
|
|
assert.Equal(t, metaTest, meta)
|
2022-11-19 08:22:15 -07:00
|
|
|
assert.True(t, meta.Valid())
|
2022-09-13 10:33:37 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-12 19:48:47 -06:00
|
|
|
var (
|
|
|
|
sepTest = "-----"
|
|
|
|
frontTest = `name: Test
|
|
|
|
about: "A Test"
|
|
|
|
title: "Test Title"
|
|
|
|
labels:
|
|
|
|
- bug
|
|
|
|
- "test label"`
|
|
|
|
bodyTest = "This is the body"
|
2022-11-19 08:22:15 -07:00
|
|
|
metaTest = IssueTemplate{
|
2020-09-12 19:48:47 -06:00
|
|
|
Name: "Test",
|
|
|
|
About: "A Test",
|
|
|
|
Title: "Test Title",
|
|
|
|
Labels: []string{"bug", "test label"},
|
|
|
|
}
|
|
|
|
)
|