-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_test.go
103 lines (100 loc) · 3.65 KB
/
base_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
98
99
100
101
102
103
package golangcustomjsontypesexample_test
import (
"encoding/json"
"fmt"
"testing"
gcjte "github.com/Rhaseven7h/golangcustomjsontypesexample"
. "github.com/smartystreets/goconvey/convey"
)
func TestJSONSpike(t *testing.T) {
Convey("Given a Profession Type", t, func() {
Convey("When converted to string", func() {
doctor := gcjte.ProfessionDoctor
Convey("Then it should return a string representation", func() {
s := doctor.String()
So(s, ShouldEqual, "A Fine Doctor")
})
})
Convey("When marshalling a profession", func() {
strReps := map[string]gcjte.Profession{
`"A Fine Doctor"`: gcjte.ProfessionDoctor,
`"A Sweet Engineer"`: gcjte.ProfessionEngineer,
`"An Awful Lawyer"`: gcjte.ProfessionLawyer,
`"A Smart Mathematician"`: gcjte.ProfessionMathematician,
`"A Brilliant Physicist"`: gcjte.ProfessionPhysicist,
}
for str, prof := range strReps {
s, err := json.Marshal(prof)
Convey(fmt.Sprintf("Then we should get no error, and a valid string representation of a %s", str), func() {
So(err, ShouldBeNil)
So(string(s), ShouldEqual, str)
})
}
})
Convey("When unmarshalling a valid profession", func() {
strReps := map[string]gcjte.Profession{
`"A Fine Doctor"`: gcjte.ProfessionDoctor,
`"A Sweet Engineer"`: gcjte.ProfessionEngineer,
`"An Awful Lawyer"`: gcjte.ProfessionLawyer,
`"A Smart Mathematician"`: gcjte.ProfessionMathematician,
`"A Brilliant Physicist"`: gcjte.ProfessionPhysicist,
}
for str, prof := range strReps {
var v gcjte.Profession
err := json.Unmarshal([]byte(str), &v)
Convey(fmt.Sprintf("Then it should recognize a %s", str), func() {
So(err, ShouldBeNil)
So(v, ShouldEqual, prof)
})
}
})
Convey("When unmarshalling an invalid profession string", func() {
var v gcjte.Profession
err := json.Unmarshal([]byte(`"something invalid"`), &v)
Convey("Then it should give an error, and announce unknown profession", func() {
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "unknown profession")
})
})
Convey("When unmarshalling an invalid profession number", func() {
var v gcjte.Profession
err := json.Unmarshal([]byte(`45`), &v)
Convey("Then it should give an error, and announce unknown profession", func() {
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "unknown profession")
})
})
Convey("When unmarshalling an invalid profession boolean value", func() {
var v gcjte.Profession
err := json.Unmarshal([]byte(`true`), &v)
Convey("Then it should give an error, and announce unknown profession", func() {
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "unknown profession")
})
})
Convey("And given a struct using a Profession type", func() {
type SampleUser struct {
Name string `json:"name"`
Activity gcjte.Profession `json:"activity"`
}
sampleUser1 := &SampleUser{
Name: "John Doe",
Activity: gcjte.ProfessionMathematician,
}
Convey("When struct is marshalled", func() {
b, err := json.Marshal(sampleUser1)
Convey("Then we should get a correctly marshalled string", func() {
So(err, ShouldBeNil)
So(string(b), ShouldEqual, `{"name":"John Doe","activity":"A Smart Mathematician"}`)
})
})
Convey("When a json using it is unmarshalled", func() {
var sampleUser2 SampleUser
err := json.Unmarshal([]byte(`{"name":"Jane Doe","activity":"An Awful Lawyer"}`), &sampleUser2)
So(err, ShouldBeNil)
So(sampleUser2.Name, ShouldEqual, "Jane Doe")
So(sampleUser2.Activity, ShouldEqual, gcjte.ProfessionLawyer)
})
})
})
}