|
| 1 | +// Copyright 2022 Blockdaemon Inc. |
| 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 pyth |
| 16 | + |
| 17 | +import ( |
| 18 | + "strings" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | +) |
| 24 | + |
| 25 | +func TestAttrsMap(t *testing.T) { |
| 26 | + caseMap := map[string]string{ |
| 27 | + "1pythians": "are", |
| 28 | + "2incredibly": "based", |
| 29 | + } |
| 30 | + attrs, err := NewAttrsMap(caseMap) |
| 31 | + require.NoError(t, err) |
| 32 | + assert.Equal(t, [][2]string{ |
| 33 | + {"1pythians", "are"}, |
| 34 | + {"2incredibly", "based"}, |
| 35 | + }, attrs.Pairs) |
| 36 | + assert.Equal(t, caseMap, attrs.KVs()) |
| 37 | + |
| 38 | + buf, err := attrs.MarshalBinary() |
| 39 | + require.NoError(t, err) |
| 40 | + assert.Equal(t, []byte( |
| 41 | + "\x09"+"1pythians"+"\x03"+"are"+ |
| 42 | + "\x0b"+"2incredibly"+"\x05"+"based", |
| 43 | + ), buf) |
| 44 | + |
| 45 | + var attrs2 AttrsMap |
| 46 | + require.NoError(t, attrs2.UnmarshalBinary(buf)) |
| 47 | + assert.Equal(t, attrs, attrs2) |
| 48 | +} |
| 49 | + |
| 50 | +func TestAttrsMap_LongKey(t *testing.T) { |
| 51 | + longKey := strings.Repeat("A", 256) |
| 52 | + caseMap := map[string]string{ |
| 53 | + longKey: ":)", |
| 54 | + } |
| 55 | + attrs, err := NewAttrsMap(caseMap) |
| 56 | + assert.EqualError(t, err, `key too long (256 > 0xFF): "`+longKey+`"`) |
| 57 | + assert.Len(t, attrs.Pairs, 0) |
| 58 | + assert.Len(t, attrs.KVs(), 0) |
| 59 | +} |
| 60 | + |
| 61 | +func TestAttrsMap_LongValue(t *testing.T) { |
| 62 | + caseMap := map[string]string{ |
| 63 | + "bla": strings.Repeat("A", 256), |
| 64 | + } |
| 65 | + attrs, err := NewAttrsMap(caseMap) |
| 66 | + assert.EqualError(t, err, `value too long (256 > 0xFF): "`+caseMap["bla"]+`"`) |
| 67 | + assert.Len(t, attrs.Pairs, 0) |
| 68 | + assert.Len(t, attrs.KVs(), 0) |
| 69 | +} |
0 commit comments