code.gitea.io/gitea@v1.19.3/modules/markup/mdstripper/mdstripper_test.go (about)

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package mdstripper
     5  
     6  import (
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestMarkdownStripper(t *testing.T) {
    14  	type testItem struct {
    15  		markdown      string
    16  		expectedText  []string
    17  		expectedLinks []string
    18  	}
    19  
    20  	list := []testItem{
    21  		{
    22  			`
    23  ## This is a title
    24  
    25  This is [one](link) to paradise.
    26  This **is emphasized**.
    27  This: should coalesce.
    28  
    29  ` + "```" + `
    30  This is a code block.
    31  This should not appear in the output at all.
    32  ` + "```" + `
    33  
    34  * Bullet 1
    35  * Bullet 2
    36  
    37  A HIDDEN ` + "`" + `GHOST` + "`" + ` IN THIS LINE.
    38  		`,
    39  			[]string{
    40  				"This is a title",
    41  				"This is",
    42  				"to paradise.",
    43  				"This",
    44  				"is emphasized",
    45  				".",
    46  				"This: should coalesce.",
    47  				"Bullet 1",
    48  				"Bullet 2",
    49  				"A HIDDEN",
    50  				"IN THIS LINE.",
    51  			},
    52  			[]string{
    53  				"link",
    54  			},
    55  		},
    56  		{
    57  			"Simply closes: #29 yes",
    58  			[]string{
    59  				"Simply closes: #29 yes",
    60  			},
    61  			[]string{},
    62  		},
    63  		{
    64  			"Simply closes: !29 yes",
    65  			[]string{
    66  				"Simply closes: !29 yes",
    67  			},
    68  			[]string{},
    69  		},
    70  	}
    71  
    72  	for _, test := range list {
    73  		text, links := StripMarkdown([]byte(test.markdown))
    74  		rawlines := strings.Split(text, "\n")
    75  		lines := make([]string, 0, len(rawlines))
    76  		for _, line := range rawlines {
    77  			line := strings.TrimSpace(line)
    78  			if line != "" {
    79  				lines = append(lines, line)
    80  			}
    81  		}
    82  		assert.EqualValues(t, test.expectedText, lines)
    83  		assert.EqualValues(t, test.expectedLinks, links)
    84  	}
    85  }