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