-
Notifications
You must be signed in to change notification settings - Fork 4
/
serialization_test.go
68 lines (57 loc) · 1.74 KB
/
serialization_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
// Copyright 2014 The Semver Package Authors. 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 (
"database/sql"
"encoding/json"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
var _ sql.Scanner = &Version{}
var _ string = (Version{}).String()
// var _ driver.Valuer = Version{}
func TestSerialization(t *testing.T) {
Convey("Versions within JSON…", t, FailureContinues, func() {
Convey("get parsed into structs", func() {
Convey("if quoted", func() {
in := []byte(`{"ver": "2.31.4"}`)
var out struct{ Ver Version }
expect, _ := NewVersion([]byte("2.31.4"))
So(expect.Bytes(), ShouldResemble, []byte("2.31.4"))
err := json.Unmarshal(in, &out)
So(err, ShouldBeNil)
So(out.Ver, ShouldResemble, expect)
})
Convey("even without quotes", func() {
in := []byte(`{"ver": 2}`)
var out struct{ Ver Version }
expect, _ := NewVersion([]byte("v2"))
err := json.Unmarshal(in, &out)
So(err, ShouldBeNil)
So(out.Ver, ShouldResemble, expect)
})
})
Convey("will be serialized correctly", func() {
for _, str := range []string{
"2.31.4", "14.9", "1.5.3.1", "8",
"8+build66",
"1.5.1-3", "1.12-rc2",
"0-0-0.0.0.4",
} {
given, err := NewVersion([]byte(str))
So(err, ShouldBeNil)
if err != nil {
continue
}
t := struct{ Ver Version }{given}
out, err := json.Marshal(&given)
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `"`+string(str)+`"`) // cast to 'string' for legibility
out, err = json.Marshal(&t)
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `{"Ver":"`+string(str)+`"}`) // cast to 'string' for legibility
}
})
})
}