Skip to content
This repository was archived by the owner on May 31, 2023. It is now read-only.

Commit ac532ed

Browse files
author
Richard Patel
committed
add unit test for attrs
1 parent 055d594 commit ac532ed

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

attrs.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
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+
115
package pyth
216

317
import (
418
"bytes"
519
"fmt"
620
"io"
21+
"sort"
22+
"strings"
723
)
824

925
// AttrsMap is a list of string key-value pairs with stable order.
@@ -24,6 +40,7 @@ func NewAttrsMap(fromGo map[string]string) (out AttrsMap, err error) {
2440
}
2541
out.Pairs = append(out.Pairs, [2]string{k, v})
2642
}
43+
out.Sort()
2744
return
2845
}
2946

@@ -36,6 +53,13 @@ func (a AttrsMap) KVs() map[string]string {
3653
return m
3754
}
3855

56+
// Sort sorts the keys of an AttrsMap by lexicographic order.
57+
func (a AttrsMap) Sort() {
58+
sort.Slice(a.Pairs, func(i, j int) bool {
59+
return strings.Compare(a.Pairs[i][0], a.Pairs[j][0]) < 0
60+
})
61+
}
62+
3963
// UnmarshalBinary unmarshals AttrsMap from its on-chain format.
4064
//
4165
// Will return an error if it fails to consume the entire provided byte slice.

attrs_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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

Comments
 (0)