-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathspec_test.go
More file actions
120 lines (107 loc) · 2.44 KB
/
spec_test.go
File metadata and controls
120 lines (107 loc) · 2.44 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package dalec
import (
"encoding/json"
"testing"
"time"
"github.com/goccy/go-yaml"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
)
func TestDate(t *testing.T) {
expect := "2023-10-01"
expectTime, err := time.Parse(time.DateOnly, expect)
assert.NilError(t, err)
d := Date{Time: expectTime}
assert.Check(t, cmp.Equal(d.Format(time.DateOnly), expect))
dtJSON, err := json.Marshal(d)
assert.NilError(t, err)
dtYAML, err := yaml.Marshal(d)
assert.NilError(t, err)
var d2 Date
err = json.Unmarshal(dtJSON, &d2)
assert.NilError(t, err)
assert.Check(t, cmp.Equal(d2.Format(time.DateOnly), expect))
d3 := Date{}
err = yaml.Unmarshal(dtYAML, &d3)
assert.NilError(t, err)
assert.Check(t, cmp.Equal(d3.Format(time.DateOnly), expect))
}
func TestSourceGeneratorValidateGomodEdits(t *testing.T) {
t.Parallel()
tests := []struct {
name string
gen *SourceGenerator
expectErr bool
expectedErrSubstr string
}{
{
name: "valid gomod edits",
gen: &SourceGenerator{
Gomod: &GeneratorGomod{
Edits: &GomodEdits{
Replace: []GomodReplace{
{Original: "github.com/old/module", Update: "github.com/new/module@v1.0.0"},
},
},
},
},
expectErr: false,
},
{
name: "invalid replace - empty old",
gen: &SourceGenerator{
Gomod: &GeneratorGomod{
Edits: &GomodEdits{
Replace: []GomodReplace{
{Original: "", Update: "github.com/new/module@v1.0.0"},
},
},
},
},
expectErr: true,
expectedErrSubstr: "must be non-empty",
},
{
name: "invalid replace - empty new",
gen: &SourceGenerator{
Gomod: &GeneratorGomod{
Edits: &GomodEdits{
Replace: []GomodReplace{
{Original: "github.com/old/module", Update: ""},
},
},
},
},
expectErr: true,
expectedErrSubstr: "must be non-empty",
},
{
name: "multiple errors",
gen: &SourceGenerator{
Gomod: &GeneratorGomod{
Edits: &GomodEdits{
Replace: []GomodReplace{
{Original: "", Update: ""}, // Both invalid
},
},
},
},
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.gen.Validate()
if tt.expectErr {
if err == nil {
t.Fatal("expected error, got nil")
}
if tt.expectedErrSubstr != "" {
assert.Check(t, cmp.Contains(err.Error(), tt.expectedErrSubstr))
}
return
}
assert.NilError(t, err)
})
}
}