-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathreusable_test.go
More file actions
98 lines (76 loc) · 3.36 KB
/
Copy pathreusable_test.go
File metadata and controls
98 lines (76 loc) · 3.36 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package core
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yaml.in/yaml/v4"
)
func TestReusable_Unmarshal_WithReference_Success(t *testing.T) {
t.Parallel()
yamlContent := `reference: '#/components/parameters/userId'`
var node yaml.Node
err := yaml.Unmarshal([]byte(yamlContent), &node)
require.NoError(t, err, "unmarshal should succeed")
var reusable Reusable[*Parameter]
validationErrs, err := reusable.Unmarshal(t.Context(), "test", node.Content[0])
require.NoError(t, err, "unmarshal should succeed")
require.Empty(t, validationErrs, "validation errors should be empty")
assert.True(t, reusable.GetValid(), "reusable should be valid")
assert.True(t, reusable.Reference.Present, "reference should be present")
assert.NotNil(t, reusable.Reference.Value, "reference value should not be nil")
}
func TestReusable_Unmarshal_NonMappingNode_Error(t *testing.T) {
t.Parallel()
yamlContent := "- item1\n- item2"
var node yaml.Node
err := yaml.Unmarshal([]byte(yamlContent), &node)
require.NoError(t, err, "unmarshal should succeed")
var reusable Reusable[*Parameter]
validationErrs, err := reusable.Unmarshal(t.Context(), "test", node.Content[0])
require.NoError(t, err, "unmarshal error should be nil")
require.NotEmpty(t, validationErrs, "validation errors should not be empty")
assert.Contains(t, validationErrs[0].Error(), "reusable expected `object`", "error message should match")
assert.False(t, reusable.GetValid(), "reusable should not be valid")
}
func TestReusable_SyncChanges_NonStruct_Error(t *testing.T) {
t.Parallel()
var node yaml.Node
err := yaml.Unmarshal([]byte(`reference: '#/test'`), &node)
require.NoError(t, err, "unmarshal should succeed")
reusable := Reusable[*Parameter]{}
_, err = reusable.SyncChanges(t.Context(), "not a struct", node.Content[0])
require.Error(t, err, "SyncChanges should fail")
assert.Contains(t, err.Error(), "Reusable.SyncChanges expected a struct, got `string`", "error message should match")
}
func TestReusable_Unmarshal_NilNode_Error(t *testing.T) {
t.Parallel()
var reusable Reusable[*Parameter]
_, err := reusable.Unmarshal(t.Context(), "test", nil)
require.Error(t, err, "should return error for nil node")
assert.Contains(t, err.Error(), "node is nil", "error should mention nil node")
}
func TestReusable_Unmarshal_InlinedObject_Success(t *testing.T) {
t.Parallel()
yamlContent := `name: petId
in: path
value: "123"`
var node yaml.Node
err := yaml.Unmarshal([]byte(yamlContent), &node)
require.NoError(t, err, "unmarshal should succeed")
var reusable Reusable[*Parameter]
validationErrs, err := reusable.Unmarshal(t.Context(), "test", node.Content[0])
require.NoError(t, err, "unmarshal should succeed")
require.Empty(t, validationErrs, "validation errors should be empty")
assert.True(t, reusable.GetValid(), "reusable should be valid")
require.NotNil(t, reusable.Object, "Object should not be nil")
}
func TestReusable_SyncChanges_Int_Error(t *testing.T) {
t.Parallel()
var node yaml.Node
err := yaml.Unmarshal([]byte(`reference: '#/test'`), &node)
require.NoError(t, err, "unmarshal should succeed")
reusable := Reusable[*Parameter]{}
_, err = reusable.SyncChanges(t.Context(), 42, node.Content[0])
require.Error(t, err, "SyncChanges should fail")
assert.Contains(t, err.Error(), "expected a struct", "error message should mention struct expectation")
}