You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.6 KiB

  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package markdown
  5. import (
  6. "fmt"
  7. "testing"
  8. "code.gitea.io/gitea/modules/structs"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestExtractMetadata(t *testing.T) {
  12. t.Run("ValidFrontAndBody", func(t *testing.T) {
  13. var meta structs.IssueTemplate
  14. body, err := ExtractMetadata(fmt.Sprintf("%s\n%s\n%s\n%s", sepTest, frontTest, sepTest, bodyTest), &meta)
  15. assert.NoError(t, err)
  16. assert.Equal(t, body, bodyTest)
  17. assert.Equal(t, metaTest, meta)
  18. assert.True(t, meta.Valid())
  19. })
  20. t.Run("NoFirstSeparator", func(t *testing.T) {
  21. var meta structs.IssueTemplate
  22. _, err := ExtractMetadata(fmt.Sprintf("%s\n%s\n%s", frontTest, sepTest, bodyTest), &meta)
  23. assert.Error(t, err)
  24. })
  25. t.Run("NoLastSeparator", func(t *testing.T) {
  26. var meta structs.IssueTemplate
  27. _, err := ExtractMetadata(fmt.Sprintf("%s\n%s\n%s", sepTest, frontTest, bodyTest), &meta)
  28. assert.Error(t, err)
  29. })
  30. t.Run("NoBody", func(t *testing.T) {
  31. var meta structs.IssueTemplate
  32. body, err := ExtractMetadata(fmt.Sprintf("%s\n%s\n%s", sepTest, frontTest, sepTest), &meta)
  33. assert.NoError(t, err)
  34. assert.Equal(t, body, "")
  35. assert.Equal(t, metaTest, meta)
  36. assert.True(t, meta.Valid())
  37. })
  38. }
  39. var (
  40. sepTest = "-----"
  41. frontTest = `name: Test
  42. about: "A Test"
  43. title: "Test Title"
  44. labels:
  45. - bug
  46. - "test label"`
  47. bodyTest = "This is the body"
  48. metaTest = structs.IssueTemplate{
  49. Name: "Test",
  50. About: "A Test",
  51. Title: "Test Title",
  52. Labels: []string{"bug", "test label"},
  53. }
  54. )