-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathcontext_generic_test.go
More file actions
70 lines (49 loc) · 1.42 KB
/
context_generic_test.go
File metadata and controls
70 lines (49 loc) · 1.42 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors
package echo
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestContextGetOK(t *testing.T) {
c := NewContext(nil, nil)
c.Set("key", int64(123))
v, err := ContextGet[int64](c, "key")
assert.NoError(t, err)
assert.Equal(t, int64(123), v)
}
func TestContextGetNonExistentKey(t *testing.T) {
c := NewContext(nil, nil)
c.Set("key", int64(123))
v, err := ContextGet[int64](c, "nope")
assert.ErrorIs(t, err, ErrNonExistentKey)
assert.Equal(t, int64(0), v)
}
func TestContextGetInvalidCast(t *testing.T) {
c := NewContext(nil, nil)
c.Set("key", int64(123))
v, err := ContextGet[bool](c, "key")
assert.ErrorIs(t, err, ErrInvalidKeyType)
assert.Equal(t, false, v)
}
func TestContextGetOrOK(t *testing.T) {
c := NewContext(nil, nil)
c.Set("key", int64(123))
v, err := ContextGetOr[int64](c, "key", 999)
assert.NoError(t, err)
assert.Equal(t, int64(123), v)
}
func TestContextGetOrNonExistentKey(t *testing.T) {
c := NewContext(nil, nil)
c.Set("key", int64(123))
v, err := ContextGetOr[int64](c, "nope", 999)
assert.NoError(t, err)
assert.Equal(t, int64(999), v)
}
func TestContextGetOrInvalidCast(t *testing.T) {
c := NewContext(nil, nil)
c.Set("key", int64(123))
v, err := ContextGetOr[float32](c, "key", float32(999))
assert.ErrorIs(t, err, ErrInvalidKeyType)
assert.Equal(t, float32(0), v)
}