code.gitea.io/gitea@v1.19.3/modules/templates/helper_test.go (about)

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package templates
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestSubjectBodySeparator(t *testing.T) {
    13  	test := func(input, subject, body string) {
    14  		loc := mailSubjectSplit.FindIndex([]byte(input))
    15  		if loc == nil {
    16  			assert.Empty(t, subject, "no subject found, but one expected")
    17  			assert.Equal(t, body, input)
    18  		} else {
    19  			assert.Equal(t, subject, input[0:loc[0]])
    20  			assert.Equal(t, body, input[loc[1]:])
    21  		}
    22  	}
    23  
    24  	test("Simple\n---------------\nCase",
    25  		"Simple\n",
    26  		"\nCase")
    27  	test("Only\nBody",
    28  		"",
    29  		"Only\nBody")
    30  	test("Minimal\n---\nseparator",
    31  		"Minimal\n",
    32  		"\nseparator")
    33  	test("False --- separator",
    34  		"",
    35  		"False --- separator")
    36  	test("False\n--- separator",
    37  		"",
    38  		"False\n--- separator")
    39  	test("False ---\nseparator",
    40  		"",
    41  		"False ---\nseparator")
    42  	test("With extra spaces\n-----   \t   \nBody",
    43  		"With extra spaces\n",
    44  		"\nBody")
    45  	test("With leading spaces\n   -------\nOnly body",
    46  		"",
    47  		"With leading spaces\n   -------\nOnly body")
    48  	test("Multiple\n---\n-------\n---\nSeparators",
    49  		"Multiple\n",
    50  		"\n-------\n---\nSeparators")
    51  	test("Insuficient\n--\nSeparators",
    52  		"",
    53  		"Insuficient\n--\nSeparators")
    54  }