github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/primitive/string_list_test.go (about) 1 // Copyright 2020 DataStax 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package primitive 16 17 import ( 18 "bytes" 19 "errors" 20 "fmt" 21 "io/ioutil" 22 "testing" 23 24 "github.com/stretchr/testify/assert" 25 ) 26 27 func TestReadStringList(t *testing.T) { 28 tests := []struct { 29 name string 30 source []byte 31 expected []string 32 remaining []byte 33 err error 34 }{ 35 {"empty string list", []byte{0, 0}, []string{}, []byte{}, nil}, 36 {"singleton string list", []byte{ 37 0, 1, // length 38 0, 5, h, e, l, l, o, // hello 39 }, []string{"hello"}, []byte{}, nil}, 40 {"simple string list", []byte{ 41 0, 2, // length 42 0, 5, h, e, l, l, o, // hello 43 0, 5, w, o, r, l, d, // world 44 }, []string{"hello", "world"}, []byte{}, nil}, 45 {"empty elements", []byte{ 46 0, 2, // length 47 0, 0, // elt 1 48 0, 0, // elt 2 49 }, []string{"", ""}, []byte{}, nil}, 50 { 51 "cannot read list length", 52 []byte{0}, 53 nil, 54 []byte{}, 55 fmt.Errorf("cannot read [string list] length: %w", fmt.Errorf("cannot read [short]: %w", errors.New("unexpected EOF"))), 56 }, 57 { 58 "cannot read list element", 59 []byte{0, 1, 0, 5, h, e, l, l}, 60 nil, 61 []byte{}, 62 fmt.Errorf("cannot read [string list] element 0: %w", fmt.Errorf("cannot read [string] content: %w", errors.New("unexpected EOF"))), 63 }, 64 } 65 for _, tt := range tests { 66 t.Run(tt.name, func(t *testing.T) { 67 buf := bytes.NewReader(tt.source) 68 actual, err := ReadStringList(buf) 69 assert.Equal(t, tt.expected, actual) 70 assert.Equal(t, tt.err, err) 71 remaining, _ := ioutil.ReadAll(buf) 72 assert.Equal(t, tt.remaining, remaining) 73 }) 74 } 75 } 76 77 func TestWriteStringList(t *testing.T) { 78 tests := []struct { 79 name string 80 input []string 81 expected []byte 82 err error 83 }{ 84 { 85 "empty string list", 86 []string{}, 87 []byte{0, 0}, 88 nil, 89 }, 90 { 91 "nil string list", 92 nil, 93 []byte{0, 0}, 94 nil, 95 }, 96 { 97 "singleton string list", 98 []string{"hello"}, 99 []byte{ 100 0, 1, // length 101 0, 5, h, e, l, l, o, // hello 102 }, 103 nil, 104 }, 105 { 106 "simple string list", 107 []string{"hello", "world"}, 108 []byte{ 109 0, 2, // length 110 0, 5, h, e, l, l, o, // hello 111 0, 5, w, o, r, l, d, // world 112 }, 113 nil, 114 }, 115 { 116 "empty elements", 117 []string{"", ""}, 118 []byte{ 119 0, 2, // length 120 0, 0, // elt 1 121 0, 0, // elt 2 122 }, 123 nil, 124 }, 125 } 126 for _, tt := range tests { 127 t.Run(tt.name, func(t *testing.T) { 128 buf := &bytes.Buffer{} 129 err := WriteStringList(tt.input, buf) 130 assert.Equal(t, tt.expected, buf.Bytes()) 131 assert.Equal(t, tt.err, err) 132 }) 133 } 134 }