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