github.com/aretext/aretext@v1.3.0/syntax/languages/criticmarkup_test.go (about)

     1  package languages
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/aretext/aretext/syntax/parser"
     9  )
    10  
    11  func TestCriticMarkupParseFunc(t *testing.T) {
    12  	testCases := []struct {
    13  		name     string
    14  		text     string
    15  		expected []TokenWithText
    16  	}{
    17  		{
    18  			name:     "empty",
    19  			text:     "",
    20  			expected: []TokenWithText{},
    21  		},
    22  		{
    23  			name: "addition",
    24  			text: "Lorem ipsum dolor{++ sit++} amet",
    25  			expected: []TokenWithText{
    26  				{Text: `{++ sit++}`, Role: criticMarkupAddRole},
    27  			},
    28  		},
    29  		{
    30  			name: "deletion",
    31  			text: "Lorem{-- ipsum--} dolor sit amet",
    32  			expected: []TokenWithText{
    33  				{Text: `{-- ipsum--}`, Role: criticMarkupDelRole},
    34  			},
    35  		},
    36  		{
    37  			name: "deletion with hyphen U+2010",
    38  			text: "Lorem{‐‐ ipsum‐‐} dolor sit amet",
    39  			expected: []TokenWithText{
    40  				{Text: `{‐‐ ipsum‐‐}`, Role: criticMarkupDelRole},
    41  			},
    42  		},
    43  		{
    44  			name: "comment",
    45  			text: "Hello {>>This is a comment<<} world",
    46  			expected: []TokenWithText{
    47  				{Text: `{>>This is a comment<<}`, Role: criticMarkupCommentRole},
    48  			},
    49  		},
    50  		{
    51  			name: "highlight",
    52  			text: "Hello {==world!==}{>> classic intro <<}",
    53  			expected: []TokenWithText{
    54  				{Text: `{==world!==}`, Role: criticMarkupHighlightRole},
    55  				{Text: `{>> classic intro <<}`, Role: criticMarkupCommentRole},
    56  			},
    57  		},
    58  		{
    59  			name: "embed in markdown title",
    60  			text: "# foo {++ bar ++} baz",
    61  			expected: []TokenWithText{
    62  				{Text: "# foo ", Role: markdownHeadingRole},
    63  				{Text: "{++ bar ++}", Role: criticMarkupAddRole},
    64  				{Text: " baz", Role: markdownHeadingRole},
    65  			},
    66  		},
    67  		{
    68  			name: "delete between paragraphs",
    69  			text: "abcd{--\n\ne--}fghi",
    70  			expected: []TokenWithText{
    71  				{Text: "{--\n\ne--}", Role: criticMarkupDelRole},
    72  			},
    73  		},
    74  	}
    75  
    76  	for _, tc := range testCases {
    77  		t.Run(tc.name, func(t *testing.T) {
    78  			tokens := ParseTokensWithText(CriticMarkupParseFunc(), tc.text)
    79  			assert.Equal(t, tc.expected, tokens)
    80  		})
    81  	}
    82  }
    83  
    84  func TestCriticMarkupConsolidateTokens(t *testing.T) {
    85  	testCases := []struct {
    86  		name               string
    87  		markdownTokens     []parser.ComputedToken
    88  		criticMarkupTokens []parser.ComputedToken
    89  		expected           []parser.ComputedToken
    90  	}{
    91  		{
    92  			name:               "both empty",
    93  			markdownTokens:     nil,
    94  			criticMarkupTokens: nil,
    95  			expected:           nil,
    96  		},
    97  		{
    98  			name: "markdown without criticmarkup",
    99  			markdownTokens: []parser.ComputedToken{
   100  				{Offset: 0, Length: 2},
   101  			},
   102  			criticMarkupTokens: nil,
   103  			expected: []parser.ComputedToken{
   104  				{Offset: 0, Length: 2},
   105  			},
   106  		},
   107  		{
   108  			name:           "criticmarkup without markdown",
   109  			markdownTokens: nil,
   110  			criticMarkupTokens: []parser.ComputedToken{
   111  				{Offset: 0, Length: 2},
   112  			},
   113  			expected: []parser.ComputedToken{
   114  				{Offset: 0, Length: 2},
   115  			},
   116  		},
   117  		{
   118  			name: "non-overlapping, markdown before criticmarkup",
   119  			markdownTokens: []parser.ComputedToken{
   120  				{Offset: 0, Length: 2},
   121  			},
   122  			criticMarkupTokens: []parser.ComputedToken{
   123  				{Offset: 3, Length: 4},
   124  			},
   125  			expected: []parser.ComputedToken{
   126  				{Offset: 0, Length: 2},
   127  				{Offset: 3, Length: 4},
   128  			},
   129  		},
   130  		{
   131  			name: "non-overlapping, criticmarkup before markdown",
   132  			markdownTokens: []parser.ComputedToken{
   133  				{Offset: 3, Length: 4},
   134  			},
   135  			criticMarkupTokens: []parser.ComputedToken{
   136  				{Offset: 0, Length: 2},
   137  			},
   138  			expected: []parser.ComputedToken{
   139  				{Offset: 0, Length: 2},
   140  				{Offset: 3, Length: 4},
   141  			},
   142  		},
   143  		{
   144  			name: "overlap, truncate",
   145  			markdownTokens: []parser.ComputedToken{
   146  				{Offset: 0, Length: 2},
   147  				{Offset: 4, Length: 2},
   148  			},
   149  			criticMarkupTokens: []parser.ComputedToken{
   150  				{Offset: 1, Length: 4},
   151  			},
   152  			expected: []parser.ComputedToken{
   153  				{Offset: 0, Length: 1},
   154  				{Offset: 1, Length: 4},
   155  				{Offset: 5, Length: 1},
   156  			},
   157  		},
   158  		{
   159  			name: "overlap, replace single token aligned",
   160  			markdownTokens: []parser.ComputedToken{
   161  				{Offset: 1, Length: 2, Role: markdownHeadingRole},
   162  			},
   163  			criticMarkupTokens: []parser.ComputedToken{
   164  				{Offset: 1, Length: 2, Role: criticMarkupAddRole},
   165  			},
   166  			expected: []parser.ComputedToken{
   167  				{Offset: 1, Length: 2, Role: criticMarkupAddRole},
   168  			},
   169  		},
   170  		{
   171  			name: "overlap, replace single token misaligned",
   172  			markdownTokens: []parser.ComputedToken{
   173  				{Offset: 1, Length: 2},
   174  			},
   175  			criticMarkupTokens: []parser.ComputedToken{
   176  				{Offset: 0, Length: 4},
   177  			},
   178  			expected: []parser.ComputedToken{
   179  				{Offset: 0, Length: 4},
   180  			},
   181  		},
   182  		{
   183  			name: "overlap, replace multiple tokens",
   184  			markdownTokens: []parser.ComputedToken{
   185  				{Offset: 1, Length: 1},
   186  				{Offset: 2, Length: 1},
   187  				{Offset: 3, Length: 1},
   188  				{Offset: 4, Length: 1},
   189  				{Offset: 5, Length: 1},
   190  			},
   191  			criticMarkupTokens: []parser.ComputedToken{
   192  				{Offset: 2, Length: 3},
   193  			},
   194  			expected: []parser.ComputedToken{
   195  				{Offset: 1, Length: 1},
   196  				{Offset: 2, Length: 3},
   197  				{Offset: 5, Length: 1},
   198  			},
   199  		},
   200  		{
   201  			name: "overlap, split",
   202  			markdownTokens: []parser.ComputedToken{
   203  				{Offset: 0, Length: 4},
   204  			},
   205  			criticMarkupTokens: []parser.ComputedToken{
   206  				{Offset: 1, Length: 1},
   207  			},
   208  			expected: []parser.ComputedToken{
   209  				{Offset: 0, Length: 1},
   210  				{Offset: 1, Length: 1},
   211  				{Offset: 2, Length: 2},
   212  			},
   213  		},
   214  		{
   215  			name: "multiple criticmarkup tokens",
   216  			markdownTokens: []parser.ComputedToken{
   217  				{Offset: 0, Length: 2},
   218  				{Offset: 3, Length: 5},
   219  			},
   220  			criticMarkupTokens: []parser.ComputedToken{
   221  				{Offset: 1, Length: 1},
   222  				{Offset: 2, Length: 2},
   223  			},
   224  			expected: []parser.ComputedToken{
   225  				{Offset: 0, Length: 1},
   226  				{Offset: 1, Length: 1},
   227  				{Offset: 2, Length: 2},
   228  				{Offset: 4, Length: 4},
   229  			},
   230  		},
   231  	}
   232  
   233  	for _, tc := range testCases {
   234  		t.Run(tc.name, func(t *testing.T) {
   235  			actual := criticMarkupConsolidateTokens(tc.markdownTokens, tc.criticMarkupTokens)
   236  			assert.Equal(t, tc.expected, actual)
   237  		})
   238  	}
   239  }