-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainernodes_test.go
123 lines (99 loc) · 2.85 KB
/
containernodes_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package books_test
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"example.com/books"
)
// NOTE: Remember that Describe, Context, and When are functionally equivalent aliases.
var _ = Describe("Organizing in container nodes", func() {
var book *books.Book
BeforeEach(func() {
book = &books.Book{
Title: "Les Miserables",
Author: "Victor Hugo",
Pages: 2783,
}
Expect(book.IsValid()).To(BeTrue())
})
Describe("Extracting the author's first and last name", func() {
Context("When the author has both names", func() {
It("can extract the author's last name", func() {
Expect(book.AuthorLastName()).To(Equal("Hugo"))
})
It("can extract the author's first name", func() {
Expect(book.AuthorFirstName()).To(Equal("Victor"))
})
})
Context("When the author only has one name", func() {
BeforeEach(func() {
book.Author = "Hugo"
})
It("interprets the single author name as a last name", func() {
Expect(book.AuthorLastName()).To(Equal("Hugo"))
})
It("returns empty for the first name", func() {
Expect(book.AuthorFirstName()).To(BeZero())
})
})
Context("When the author has a middle name", func() {
BeforeEach(func() {
book.Author = "Victor Marie Hugo"
})
It("can extract the author's last name", func() {
Expect(book.AuthorLastName()).To(Equal("Hugo"))
})
It("can extract the author's first name", func() {
Expect(book.AuthorFirstName()).To(Equal("Victor"))
})
})
Context("When the author has no name", func() {
It("should not be a valid book and returns empty for first and last name", func() {
book.Author = ""
Expect(book.IsValid()).To(BeFalse())
Expect(book.AuthorLastName()).To(BeZero())
Expect(book.AuthorFirstName()).To(BeZero())
})
})
})
Describe("JSON encoding and decoding", func() {
It("survives the round trip", func() {
encoded, err := book.AsJSON()
Expect(err).NotTo(HaveOccurred())
decoded, err := books.NewBookFromJSON(encoded)
Expect(err).NotTo(HaveOccurred())
Expect(decoded).To(Equal(book))
})
Describe("some JSON decoding edge cases", func() {
var err error
When("the JSON fails to parse", func() {
BeforeEach(func() {
book, err = books.NewBookFromJSON(`{
"title":"Les Miserables",
"author":"Victor Hugo",
"pages":2783oops
}`)
})
It("returns a nil book", func() {
Expect(book).To(BeNil())
})
It("errors", func() {
Expect(err).To(MatchError(books.ErrInvalidJSON))
})
})
When("the JSON is incomplete", func() {
BeforeEach(func() {
book, err = books.NewBookFromJSON(`{
"title":"Les Miserables",
"author":"Victor Hugo"
}`)
})
It("returns a nil book", func() {
Expect(book).To(BeNil())
})
It("errors", func() {
Expect(err).To(MatchError(books.ErrIncompleteJSON))
})
})
})
})
})