github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/markup/markdown/meta_test.go (about)

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