MCPcopy Index your code
hub / github.com/go-openapi/jsonpointer / TestDashToken_GetAlwaysErrors

Function TestDashToken_GetAlwaysErrors

dash_token_test.go:18–69  ·  view source on GitHub ↗

RFC 6901 §4: the "-" token refers to the (nonexistent) element after the last array element. It is always an error on Get/Offset, valid only as the terminal token of a Set against a slice (append, per RFC 6902).

(t *testing.T)

Source from the content-addressed store, hash-verified

16// (append, per RFC 6902).
17
18func TestDashToken_GetAlwaysErrors(t *testing.T) {
19 t.Parallel()
20
21 t.Run("terminal dash on slice in map", func(t *testing.T) {
22 doc := map[string]any{"arr": []any{1, 2, 3}}
23 p, err := New("/arr/-")
24 require.NoError(t, err)
25
26 _, _, err = p.Get(doc)
27 require.Error(t, err)
28 require.ErrorIs(t, err, ErrDashToken)
29 require.ErrorIs(t, err, ErrPointer)
30 })
31
32 t.Run("terminal dash on top-level slice", func(t *testing.T) {
33 doc := []int{1, 2, 3}
34 p, err := New("/-")
35 require.NoError(t, err)
36
37 _, _, err = p.Get(doc)
38 require.Error(t, err)
39 require.ErrorIs(t, err, ErrDashToken)
40 })
41
42 t.Run("intermediate dash during get", func(t *testing.T) {
43 doc := map[string]any{"arr": []any{map[string]any{"x": 1}}}
44 p, err := New("/arr/-/x")
45 require.NoError(t, err)
46
47 _, _, err = p.Get(doc)
48 require.Error(t, err)
49 require.ErrorIs(t, err, ErrDashToken)
50 })
51
52 t.Run("GetForToken on slice with dash", func(t *testing.T) {
53 _, _, err := GetForToken([]int{1, 2}, "-")
54 require.Error(t, err)
55 require.ErrorIs(t, err, ErrDashToken)
56 })
57
58 t.Run("dash on map key is a regular lookup, not an error", func(t *testing.T) {
59 // "-" is only special for arrays.
60 // A literal "-" key in a map is fine.
61 doc := map[string]any{"-": 42}
62 p, err := New("/-")
63 require.NoError(t, err)
64
65 v, _, err := p.Get(doc)
66 require.NoError(t, err)
67 assert.Equal(t, 42, v)
68 })
69}
70
71func TestDashToken_OffsetErrors(t *testing.T) {
72 t.Parallel()

Callers

nothing calls this directly

Calls 4

NewFunction · 0.85
GetForTokenFunction · 0.85
GetMethod · 0.80
ErrorMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…