github.com/aretext/aretext@v1.3.0/locate/string_test.go (about)

     1  package locate
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/aretext/aretext/syntax"
     9  )
    10  
    11  func TestStringObject(t *testing.T) {
    12  	testCases := []struct {
    13  		name           string
    14  		inputString    string
    15  		pos            uint64
    16  		syntaxLanguage syntax.Language
    17  		quoteRune      rune
    18  		includeQuotes  bool
    19  		expectStartPos uint64
    20  		expectEndPos   uint64
    21  	}{
    22  		{
    23  			name:           "empty",
    24  			inputString:    "",
    25  			pos:            0,
    26  			expectStartPos: 0,
    27  			expectEndPos:   0,
    28  		},
    29  		{
    30  			name:           "on start quote",
    31  			inputString:    `x "abcd" y`,
    32  			pos:            2,
    33  			quoteRune:      '"',
    34  			includeQuotes:  true,
    35  			expectStartPos: 2,
    36  			expectEndPos:   8,
    37  		},
    38  		{
    39  			name:           "include quotes",
    40  			inputString:    `"abcd"`,
    41  			pos:            3,
    42  			quoteRune:      '"',
    43  			includeQuotes:  true,
    44  			expectStartPos: 0,
    45  			expectEndPos:   6,
    46  		},
    47  		{
    48  			name:           "exclude quotes",
    49  			inputString:    `"abcd"`,
    50  			pos:            3,
    51  			quoteRune:      '"',
    52  			includeQuotes:  false,
    53  			expectStartPos: 1,
    54  			expectEndPos:   5,
    55  		},
    56  		{
    57  			name:           "include quotes, empty string",
    58  			inputString:    `""`,
    59  			pos:            1,
    60  			quoteRune:      '"',
    61  			includeQuotes:  true,
    62  			expectStartPos: 0,
    63  			expectEndPos:   2,
    64  		},
    65  		{
    66  			name:           "exclude quotes, empty string",
    67  			inputString:    `""`,
    68  			pos:            1,
    69  			quoteRune:      '"',
    70  			includeQuotes:  false,
    71  			expectStartPos: 1,
    72  			expectEndPos:   1,
    73  		},
    74  		{
    75  			name:           "string syntax token, include quotes",
    76  			inputString:    `"ab\"cd"`,
    77  			pos:            2,
    78  			quoteRune:      '"',
    79  			includeQuotes:  true,
    80  			expectStartPos: 0,
    81  			expectEndPos:   8,
    82  			syntaxLanguage: syntax.LanguageGo,
    83  		},
    84  		{
    85  			name:           "string syntax token, exclude quotes",
    86  			inputString:    `"ab\"cd"`,
    87  			pos:            2,
    88  			quoteRune:      '"',
    89  			includeQuotes:  false,
    90  			expectStartPos: 1,
    91  			expectEndPos:   7,
    92  			syntaxLanguage: syntax.LanguageGo,
    93  		},
    94  	}
    95  
    96  	for _, tc := range testCases {
    97  		t.Run(tc.name, func(t *testing.T) {
    98  			textTree, syntaxParser := textTreeAndSyntaxParser(t, tc.inputString, tc.syntaxLanguage)
    99  			actualStartPos, actualEndPos := StringObject(tc.quoteRune, textTree, syntaxParser, tc.includeQuotes, tc.pos)
   100  			assert.Equal(t, tc.expectStartPos, actualStartPos)
   101  			assert.Equal(t, tc.expectEndPos, actualEndPos)
   102  		})
   103  	}
   104  }