github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/primitive/string_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 const ( 28 d = byte('d') 29 e = byte('e') 30 h = byte('h') 31 k = byte('k') 32 l = byte('l') 33 m = byte('m') 34 n = byte('n') 35 o = byte('o') 36 r = byte('r') 37 u = byte('u') 38 w = byte('w') 39 ) 40 41 func TestReadString(t *testing.T) { 42 tests := []struct { 43 name string 44 source []byte 45 expected string 46 remaining []byte 47 err error 48 }{ 49 {"simple string", []byte{0, 5, h, e, l, l, o}, "hello", []byte{}, nil}, 50 {"string with remaining", []byte{0, 5, h, e, l, l, o, 1, 2, 3, 4}, "hello", []byte{1, 2, 3, 4}, nil}, 51 {"empty string", []byte{0, 0}, "", []byte{}, nil}, 52 {"non-ASCII string", []byte{ 53 0, 15, // length 54 0xce, 0xb3, 0xce, 0xb5, 0xce, 0xb9, 0xce, 0xac, //γειά 55 0x20, // space 56 0xcf, 0x83, 0xce, 0xbf, 0xcf, 0x85, // σου 57 }, "γειά σου", []byte{}, nil}, 58 { 59 "cannot read length", 60 []byte{0}, 61 "", 62 []byte{}, 63 fmt.Errorf("cannot read [string] length: %w", fmt.Errorf("cannot read [short]: %w", errors.New("unexpected EOF"))), 64 }, 65 { 66 "cannot read string", 67 []byte{0, 5, h, e, l, l}, 68 "", 69 []byte{}, 70 fmt.Errorf("cannot read [string] content: %w", errors.New("unexpected EOF")), 71 }, 72 } 73 for _, tt := range tests { 74 t.Run(tt.name, func(t *testing.T) { 75 buf := bytes.NewReader(tt.source) 76 actual, err := ReadString(buf) 77 assert.Equal(t, tt.expected, actual) 78 assert.Equal(t, tt.err, err) 79 remaining, _ := ioutil.ReadAll(buf) 80 assert.Equal(t, tt.remaining, remaining) 81 }) 82 } 83 } 84 85 func TestWriteString(t *testing.T) { 86 tests := []struct { 87 name string 88 input string 89 expected []byte 90 err error 91 }{ 92 { 93 "simple string", 94 "hello", 95 []byte{0, 5, h, e, l, l, o}, 96 nil, 97 }, 98 {"empty string", "", []byte{0, 0}, nil}, 99 {"non-ASCII string", "γειά σου", []byte{ 100 0, 15, // length 101 0xce, 0xb3, 0xce, 0xb5, 0xce, 0xb9, 0xce, 0xac, //γειά 102 0x20, // space 103 0xcf, 0x83, 0xce, 0xbf, 0xcf, 0x85, // σου 104 }, nil}, 105 } 106 for _, tt := range tests { 107 t.Run(tt.name, func(t *testing.T) { 108 buf := &bytes.Buffer{} 109 err := WriteString(tt.input, buf) 110 assert.Equal(t, tt.expected, buf.Bytes()) 111 assert.Equal(t, tt.err, err) 112 }) 113 } 114 }