Repository navigation
Expand file tree
/
Copy pathtiq_test.go
More file actions
63 lines (49 loc) · 1.42 KB
/
Copy pathtiq_test.go
File metadata and controls
63 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
package tiq
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGet(t *testing.T) {
type TestStruct struct {
Field1 string `json:"field1" db:"field_1"`
Field2 int `json:"field2"`
}
t.Run("returns tag value when field and tag exist", func(t *testing.T) {
testStruct := TestStruct{}
value, ok := Get(testStruct, "Field1", "json")
assert.True(t, ok)
assert.Equal(t, "field1", value)
})
t.Run("returns false when field does not exist", func(t *testing.T) {
testStruct := TestStruct{}
value, ok := Get(testStruct, "NonExistentField", "json")
assert.False(t, ok)
assert.Empty(t, value)
})
t.Run("returns false when Inspect fails", func(t *testing.T) {
value, ok := Get(nil, "Field1", "json")
assert.False(t, ok)
assert.Empty(t, value)
})
}
func TestSet(t *testing.T) {
type TestStruct struct {
Field1 string
Field2 int
}
t.Run("sets field value", func(t *testing.T) {
testStruct := &TestStruct{}
err := Set(testStruct, "Field1", "new value")
assert.NoError(t, err)
assert.Equal(t, "new value", testStruct.Field1)
})
t.Run("returns ErrFieldNotFound when field does not exist", func(t *testing.T) {
testStruct := &TestStruct{}
err := Set(testStruct, "NonExistentField", "value")
assert.ErrorIs(t, err, ErrFieldNotFound)
})
t.Run("returns error when Inspect fails", func(t *testing.T) {
err := Set(nil, "Field1", "value")
assert.ErrorIs(t, err, ErrNilValue)
})
}