...

Source file src/github.com/subosito/gotenv/scanner_test.go

Documentation: github.com/subosito/gotenv

     1  package gotenv
     2  
     3  import (
     4  	"bufio"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestScanner(t *testing.T) {
    12  	type testCase struct {
    13  		name string
    14  		in   string
    15  		exp  []string
    16  	}
    17  
    18  	testCases := []testCase{
    19  		{
    20  			"regular LF split with trailing LF",
    21  			"aa\nbb\ncc\n",
    22  			[]string{"aa", "bb", "cc", ""},
    23  		},
    24  		{
    25  			"regular LF split with no trailing LF",
    26  			"aa\nbb\ncc",
    27  			[]string{"aa", "bb", "cc"},
    28  		},
    29  
    30  		{
    31  			"regular CR split with trailing CR",
    32  			"aa\rbb\rcc\r",
    33  			[]string{"aa", "bb", "cc", ""},
    34  		},
    35  		{
    36  			"regular CR split with no trailing CR",
    37  			"aa\rbb\rcc",
    38  			[]string{"aa", "bb", "cc"},
    39  		},
    40  
    41  		{
    42  			"regular CRLF split with trailing CRLF",
    43  			"aa\r\nbb\r\ncc\r\n",
    44  			[]string{"aa", "bb", "cc", ""},
    45  		},
    46  		{
    47  			"regular CRLF split with no trailing CRLF",
    48  			"aa\r\nbb\r\ncc",
    49  			[]string{"aa", "bb", "cc"},
    50  		},
    51  
    52  		{
    53  			"mix of possible line endings",
    54  			"aa\r\nbb\ncc\rdd",
    55  			[]string{"aa", "bb", "cc", "dd"},
    56  		},
    57  	}
    58  
    59  	for _, tc := range testCases {
    60  		s := bufio.NewScanner(strings.NewReader(tc.in))
    61  		s.Split(splitLines)
    62  
    63  		i := 0
    64  		for s.Scan() {
    65  			if i >= len(tc.exp) {
    66  				assert.Fail(t, "unexpected line", "testCase: %s - got extra line: %q", tc.name, s.Text())
    67  			} else {
    68  				got := s.Text()
    69  				assert.Equal(t, tc.exp[i], got, "testCase: %s - line %d", tc.name, i)
    70  			}
    71  			i++
    72  		}
    73  
    74  		assert.NoError(t, s.Err(), "testCase: %s", tc.name)
    75  		assert.Equal(t, len(tc.exp), i, "testCase: %s - expected to have the correct line count", tc.name)
    76  	}
    77  }
    78  

View as plain text