github.com/ashishbhate/mattermost-server@v5.11.1+incompatible/utils/markdown/text_range_test.go (about)

     1  // Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package markdown
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestTextRanges(t *testing.T) {
    13  	for name, tc := range map[string]struct {
    14  		Markdown       string
    15  		ExpectedRanges []Range
    16  		ExpectedValues []string
    17  	}{
    18  		"simple": {
    19  			Markdown:       "hello",
    20  			ExpectedRanges: []Range{{0, 5}},
    21  			ExpectedValues: []string{"hello"},
    22  		},
    23  		"simple2": {
    24  			Markdown:       "hello!",
    25  			ExpectedRanges: []Range{{0, 6}},
    26  			ExpectedValues: []string{"hello!"},
    27  		},
    28  		"multiline": {
    29  			Markdown:       "hello world\nfoobar",
    30  			ExpectedRanges: []Range{{0, 11}, {12, 18}},
    31  			ExpectedValues: []string{"hello world", "foobar"},
    32  		},
    33  		"code": {
    34  			Markdown:       "hello `code` world",
    35  			ExpectedRanges: []Range{{0, 6}, {12, 18}},
    36  			ExpectedValues: []string{"hello ", " world"},
    37  		},
    38  		"notcode": {
    39  			Markdown:       "hello ` world",
    40  			ExpectedRanges: []Range{{0, 13}},
    41  			ExpectedValues: []string{"hello ` world"},
    42  		},
    43  		"escape": {
    44  			Markdown:       "\\*hello\\*",
    45  			ExpectedRanges: []Range{{1, 7}, {8, 9}},
    46  			ExpectedValues: []string{"*hello", "*"},
    47  		},
    48  		"escapeescape": {
    49  			Markdown:       "\\\\",
    50  			ExpectedRanges: []Range{{1, 2}},
    51  			ExpectedValues: []string{"\\"},
    52  		},
    53  		"notescape": {
    54  			Markdown:       "foo\\x",
    55  			ExpectedRanges: []Range{{0, 5}},
    56  			ExpectedValues: []string{"foo\\x"},
    57  		},
    58  		"notlink": {
    59  			Markdown:       "[foo",
    60  			ExpectedRanges: []Range{{0, 4}},
    61  			ExpectedValues: []string{"[foo"},
    62  		},
    63  		"notlinkend": {
    64  			Markdown:       "[foo]",
    65  			ExpectedRanges: []Range{{0, 5}},
    66  			ExpectedValues: []string{"[foo]"},
    67  		},
    68  		"notimage": {
    69  			Markdown:       "![foo",
    70  			ExpectedRanges: []Range{{0, 5}},
    71  			ExpectedValues: []string{"![foo"},
    72  		},
    73  		"notimage2": {
    74  			Markdown:       "!foo",
    75  			ExpectedRanges: []Range{{0, 4}},
    76  			ExpectedValues: []string{"!foo"},
    77  		},
    78  		"charref": {
    79  			Markdown:       ""test",
    80  			ExpectedRanges: []Range{{0, 1}, {6, 10}},
    81  			ExpectedValues: []string{"\"", "test"},
    82  		},
    83  		"notcharref": {
    84  			Markdown:       "&amp test",
    85  			ExpectedRanges: []Range{{0, 9}},
    86  			ExpectedValues: []string{"&amp test"},
    87  		},
    88  		"notcharref2": {
    89  			Markdown:       "this is &mattermost;",
    90  			ExpectedRanges: []Range{{0, 20}},
    91  			ExpectedValues: []string{"this is &mattermost;"},
    92  		},
    93  		"standalone-ampersand": {
    94  			Markdown:       "Hello & World",
    95  			ExpectedRanges: []Range{{0, 13}},
    96  			ExpectedValues: []string{"Hello & World"},
    97  		},
    98  	} {
    99  		t.Run(name, func(t *testing.T) {
   100  			var ranges []Range
   101  			var values []string
   102  			Inspect(tc.Markdown, func(node interface{}) bool {
   103  				if textNode, ok := node.(*Text); ok {
   104  					ranges = append(ranges, textNode.Range)
   105  					values = append(values, textNode.Text)
   106  				}
   107  				return true
   108  			})
   109  			assert.Equal(t, tc.ExpectedRanges, ranges)
   110  			assert.Equal(t, tc.ExpectedValues, values)
   111  
   112  		})
   113  	}
   114  
   115  }