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