github.com/blend/go-sdk@v1.20220411.3/stringutil/split_lines_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package stringutil
     9  
    10  import (
    11  	"testing"
    12  
    13  	"github.com/blend/go-sdk/assert"
    14  )
    15  
    16  func TestSplitLines(t *testing.T) {
    17  	assert := assert.New(t)
    18  
    19  	testCases := [...]struct {
    20  		Input    string
    21  		Expected []string
    22  	}{
    23  		{"", nil},
    24  		{"\n", nil},
    25  		{"\n\n", nil},
    26  		{"this", []string{"this"}},
    27  		{"this\nthat", []string{"this", "that"}},
    28  		{"this\nthat\n", []string{"this", "that"}},
    29  		{"this\nthat\nthose", []string{"this", "that", "those"}},
    30  		{"this\nthat\nthose\n", []string{"this", "that", "those"}},
    31  		{"this\nthat\n\nthose\n", []string{"this", "that", "those"}},
    32  		{"this\rthat\nthose\n", []string{"this\rthat", "those"}},
    33  		{"this\rthat\rthose\n", []string{"this\rthat\rthose"}},
    34  		{"this\rthat\rthose\r", []string{"this\rthat\rthose\r"}},
    35  		{"this\r\nthat\rthose\r", []string{"this\r", "that\rthose\r"}},
    36  		{"this\r\nthat\r\nthose\r", []string{"this\r", "that\r", "those\r"}},
    37  		{"this\r\nthat\r\nthose\r\n", []string{"this\r", "that\r", "those\r"}},
    38  	}
    39  
    40  	for _, tc := range testCases {
    41  		assert.Equal(tc.Expected, SplitLines(tc.Input,
    42  			OptSplitLinesIncludeNewLine(false),
    43  			OptSplitLinesIncludeEmptyLines(false),
    44  		))
    45  	}
    46  }
    47  
    48  func TestSplitLinesIncludeNewLine(t *testing.T) {
    49  	assert := assert.New(t)
    50  
    51  	testCases := [...]struct {
    52  		Input    string
    53  		Expected []string
    54  	}{
    55  		{"", nil},
    56  		{"\n", nil},
    57  		{"\n\n", nil},
    58  		{"this", []string{"this"}},
    59  		{"this\nthat", []string{"this\n", "that"}},
    60  		{"this\nthat\n", []string{"this\n", "that\n"}},
    61  		{"this\nthat\nthose", []string{"this\n", "that\n", "those"}},
    62  		{"this\nthat\nthose\n", []string{"this\n", "that\n", "those\n"}},
    63  		{"this\nthat\n\nthose\n", []string{"this\n", "that\n", "those\n"}},
    64  		{"this\rthat\nthose\n", []string{"this\rthat\n", "those\n"}},
    65  		{"this\rthat\rthose\n", []string{"this\rthat\rthose\n"}},
    66  		{"this\rthat\rthose\r", []string{"this\rthat\rthose\r"}},
    67  		{"this\r\nthat\rthose\r", []string{"this\r\n", "that\rthose\r"}},
    68  		{"this\r\nthat\r\nthose\r", []string{"this\r\n", "that\r\n", "those\r"}},
    69  		{"this\r\nthat\r\nthose\r\n", []string{"this\r\n", "that\r\n", "those\r\n"}},
    70  	}
    71  
    72  	for _, tc := range testCases {
    73  		assert.Equal(tc.Expected, SplitLines(tc.Input,
    74  			OptSplitLinesIncludeNewLine(true),
    75  			OptSplitLinesIncludeEmptyLines(false),
    76  		))
    77  	}
    78  }
    79  
    80  func TestSplitLinesIncludeEmptyLines(t *testing.T) {
    81  	assert := assert.New(t)
    82  
    83  	testCases := [...]struct {
    84  		Input    string
    85  		Expected []string
    86  	}{
    87  		{"", nil},
    88  		{"\n", []string{""}},
    89  		{"\n\n", []string{"", ""}},
    90  		{"this", []string{"this"}},
    91  		{"this\nthat", []string{"this", "that"}},
    92  		{"this\nthat\n", []string{"this", "that"}},
    93  		{"this\nthat\nthose", []string{"this", "that", "those"}},
    94  		{"this\nthat\nthose\n", []string{"this", "that", "those"}},
    95  		{"this\nthat\n\nthose\n", []string{"this", "that", "", "those"}},
    96  		{"this\rthat\nthose\n", []string{"this\rthat", "those"}},
    97  		{"this\rthat\rthose\n", []string{"this\rthat\rthose"}},
    98  		{"this\rthat\rthose\r", []string{"this\rthat\rthose\r"}},
    99  		{"this\r\nthat\rthose\r", []string{"this\r", "that\rthose\r"}},
   100  		{"this\r\nthat\r\nthose\r", []string{"this\r", "that\r", "those\r"}},
   101  		{"this\r\nthat\r\nthose\r\n", []string{"this\r", "that\r", "those\r"}},
   102  	}
   103  
   104  	for _, tc := range testCases {
   105  		assert.Equal(tc.Expected, SplitLines(tc.Input,
   106  			OptSplitLinesIncludeNewLine(false),
   107  			OptSplitLinesIncludeEmptyLines(true),
   108  		))
   109  	}
   110  }
   111  
   112  func TestSplitLinesIncludeAll(t *testing.T) {
   113  	assert := assert.New(t)
   114  
   115  	testCases := [...]struct {
   116  		Input    string
   117  		Expected []string
   118  	}{
   119  		{"", nil},
   120  		{"\n", []string{"\n"}},
   121  		{"\n\n", []string{"\n", "\n"}},
   122  		{"this", []string{"this"}},
   123  		{"this\nthat", []string{"this\n", "that"}},
   124  		{"this\nthat\n", []string{"this\n", "that\n"}},
   125  		{"this\nthat\nthose", []string{"this\n", "that\n", "those"}},
   126  		{"this\nthat\nthose\n", []string{"this\n", "that\n", "those\n"}},
   127  		{"this\nthat\n\nthose\n", []string{"this\n", "that\n", "\n", "those\n"}},
   128  		{"this\rthat\nthose\n", []string{"this\rthat\n", "those\n"}},
   129  		{"this\rthat\rthose\n", []string{"this\rthat\rthose\n"}},
   130  		{"this\rthat\rthose\r", []string{"this\rthat\rthose\r"}},
   131  		{"this\r\nthat\rthose\r", []string{"this\r\n", "that\rthose\r"}},
   132  		{"this\r\nthat\r\nthose\r", []string{"this\r\n", "that\r\n", "those\r"}},
   133  		{"this\r\nthat\r\nthose\r\n", []string{"this\r\n", "that\r\n", "those\r\n"}},
   134  	}
   135  
   136  	for _, tc := range testCases {
   137  		assert.Equal(tc.Expected, SplitLines(tc.Input,
   138  			OptSplitLinesIncludeNewLine(true),
   139  			OptSplitLinesIncludeEmptyLines(true),
   140  		))
   141  	}
   142  }