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

     1  package locate
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/aretext/aretext/text"
    10  )
    11  
    12  func TestNextParagraph(t *testing.T) {
    13  	testCases := []struct {
    14  		name        string
    15  		inputString string
    16  		pos         uint64
    17  		expectedPos uint64
    18  	}{
    19  		{
    20  			name:        "empty",
    21  			inputString: "",
    22  			pos:         0,
    23  			expectedPos: 0,
    24  		},
    25  		{
    26  			name:        "end of document",
    27  			inputString: "abcd1234",
    28  			pos:         2,
    29  			expectedPos: 7,
    30  		},
    31  		{
    32  			name:        "from non-empty line to first empty line",
    33  			inputString: "ab\ncd\n\n\nef",
    34  			pos:         1,
    35  			expectedPos: 6,
    36  		},
    37  		{
    38  			name:        "from empty line to next empty line",
    39  			inputString: "ab\n\n\n\ncd\n\nef",
    40  			pos:         3,
    41  			expectedPos: 9,
    42  		},
    43  	}
    44  
    45  	for _, tc := range testCases {
    46  		t.Run(tc.name, func(t *testing.T) {
    47  			textTree, err := text.NewTreeFromString(tc.inputString)
    48  			require.NoError(t, err)
    49  			actualPos := NextParagraph(textTree, tc.pos)
    50  			assert.Equal(t, tc.expectedPos, actualPos)
    51  		})
    52  	}
    53  }
    54  
    55  func TestPrevParagraph(t *testing.T) {
    56  	testCases := []struct {
    57  		name        string
    58  		inputString string
    59  		pos         uint64
    60  		expectedPos uint64
    61  	}{
    62  		{
    63  			name:        "empty",
    64  			inputString: "",
    65  			pos:         0,
    66  			expectedPos: 0,
    67  		},
    68  		{
    69  			name:        "start of document",
    70  			inputString: "abcd1234",
    71  			pos:         4,
    72  			expectedPos: 0,
    73  		},
    74  		{
    75  			name:        "from non-empty line to previous empty line",
    76  			inputString: "ab\n\n\ncd\n\nef",
    77  			pos:         6,
    78  			expectedPos: 4,
    79  		},
    80  		{
    81  			name:        "from empty line to next empty line",
    82  			inputString: "ab\n\n\n\ncd\n\nef",
    83  			pos:         9,
    84  			expectedPos: 5,
    85  		},
    86  	}
    87  
    88  	for _, tc := range testCases {
    89  		t.Run(tc.name, func(t *testing.T) {
    90  			textTree, err := text.NewTreeFromString(tc.inputString)
    91  			require.NoError(t, err)
    92  			actualPos := PrevParagraph(textTree, tc.pos)
    93  			assert.Equal(t, tc.expectedPos, actualPos)
    94  		})
    95  	}
    96  }