-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathyaml_test.go
97 lines (75 loc) · 2.49 KB
/
yaml_test.go
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
//
// Copyright 2018-2023 Cristian Maglie. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
package semver
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
func TestYAMLParseVersion(t *testing.T) {
var versionIsYamlUnmarshaler yaml.Unmarshaler = MustParse("1.0.0")
var versionIsYamlMarshaler yaml.Marshaler = MustParse("1.0.0")
_ = versionIsYamlUnmarshaler
_ = versionIsYamlMarshaler
testVersion := "1.2.3-aaa.4.5.6+bbb.7.8.9"
v, err := Parse(testVersion)
require.NoError(t, err)
data, err := yaml.Marshal(v)
require.Equal(t, "1.2.3-aaa.4.5.6+bbb.7.8.9\n", string(data))
require.NoError(t, err)
var u Version
err = yaml.Unmarshal(data, &u)
require.NoError(t, err)
dump := fmt.Sprintf("%v,%v,%v,%v,%v,%v", u.raw, u.major, u.minor, u.patch, u.prerelease, u.build)
require.Equal(t, "1.2.3-aaa.4.5.6+bbb.7.8.9,1,3,5,15,25", dump)
require.Equal(t, testVersion, u.String())
err = yaml.Unmarshal([]byte(`"invalid"`), &u)
require.Error(t, err)
err = yaml.Unmarshal([]byte(`invalid:`), &u)
require.Error(t, err)
require.NoError(t, yaml.Unmarshal([]byte(`"1.6.2"`), &v))
require.NoError(t, yaml.Unmarshal([]byte(`"1.6.3"`), &u))
require.True(t, u.GreaterThan(v))
}
func TestYAMLParseRelaxedVersion(t *testing.T) {
var relaxedVersionIsYamlUnmarshaler yaml.Unmarshaler = ParseRelaxed("1.0.0")
var relaxedVersionIsYamlMarshaler yaml.Marshaler = ParseRelaxed("1.0.0")
_ = relaxedVersionIsYamlUnmarshaler
_ = relaxedVersionIsYamlMarshaler
testVersion := "1.2.3-aaa.4.5.6+bbb.7.8.9"
v := ParseRelaxed(testVersion)
data, err := yaml.Marshal(v)
require.NoError(t, err)
require.Equal(t, "1.2.3-aaa.4.5.6+bbb.7.8.9\n", string(data))
var u RelaxedVersion
err = yaml.Unmarshal(data, &u)
require.NoError(t, err)
require.Equal(t, testVersion, u.String())
err = yaml.Unmarshal([]byte(`"invalid"`), &u)
require.NoError(t, err)
require.Equal(t, "invalid", u.String())
err = yaml.Unmarshal([]byte(`invalid:`), &u)
require.Error(t, err)
}
func BenchmarkYAMLDecoding(b *testing.B) {
testVersion := "1.2.3-aaa.4.5.6+bbb.7.8.9"
v, _ := Parse(testVersion)
data, _ := yaml.Marshal(v)
var u Version
for i := 0; i < b.N; i++ {
yaml.Unmarshal(data, &u)
}
}
func BenchmarkYAMLDecodingRelaxed(b *testing.B) {
testVersion := "1.2.3-aaa.4.5.6+bbb.7.8.9"
v := ParseRelaxed(testVersion)
data, _ := yaml.Marshal(v)
var u RelaxedVersion
for i := 0; i < b.N; i++ {
yaml.Unmarshal(data, &u)
}
}