-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy pathunmarshaler_internal_test.go
More file actions
55 lines (49 loc) · 1.82 KB
/
Copy pathunmarshaler_internal_test.go
File metadata and controls
55 lines (49 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package toml
import (
"reflect"
"testing"
"github.com/pelletier/go-toml/v2/internal/assert"
)
func TestArrayCountsFreshDecoder(t *testing.T) {
// Whether a decode starts with a nil arrayCounts map depends on decoder
// pooling: a reused decoder keeps the map allocated across documents.
// Exercise the nil-map paths on a guaranteed-fresh decoder so their
// coverage does not depend on sync.Pool behavior.
d := &decoder{}
key := []byte("a")
assert.Equal(t, 0, d.arrayCount(key))
d.resetChildArrayCounts(key)
d.setArrayCount(key, 1)
assert.Equal(t, 1, d.arrayCount(key))
d.setArrayCount(key, 2)
assert.Equal(t, 2, d.arrayCount(key))
assert.Equal(t, 0, d.arrayCount([]byte("b")))
// Child paths are joined with a NUL separator; resetting the parent
// zeroes the child count but leaves the parent untouched.
child := []byte("a\x00b")
d.setArrayCount(child, 3)
d.resetChildArrayCounts(key)
assert.Equal(t, 0, d.arrayCount(child))
assert.Equal(t, 2, d.arrayCount(key))
}
type unexpCaptureInner struct{ C int }
type unexpCaptureOuter struct {
*unexpCaptureInner
}
func TestResolveCaptureUnexportedEmbeddedPointer(t *testing.T) {
// resolveCapture walks the same field paths as walkTable, which has
// either allocated the intermediate embedded pointers or already failed
// by the time captures are resolved. Exercise its defensive branch
// directly: resolving a capture through a nil embedded pointer of
// unexported type must surface fieldByIndexAlloc's error, not panic.
d := &decoder{}
c := rawCapture{
names: []string{"C"},
indexes: []int{-1, -1},
buf: []byte("x = 1\n"),
}
root := reflect.ValueOf(&unexpCaptureOuter{}).Elem()
_, err := d.resolveCapture(root, &c, 0, false)
assert.Error(t, err)
assert.Equal(t, "toml: cannot set embedded pointer to unexported struct: toml.unexpCaptureInner", err.Error())
}