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