Skip to content

Commit a03c2c3

Browse files
committed
Add test data generator
1 parent 76664a6 commit a03c2c3

File tree

5 files changed

+656
-0
lines changed

5 files changed

+656
-0
lines changed

test/mock/mock.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type Env struct {
3535
ArrayOfFoo []*Foo
3636
MapOfFoo map[string]Foo
3737
MapOfAny map[string]interface{}
38+
MapIntAny map[int]string
3839
FuncParam func(_ bool, _ int, _ string) bool
3940
FuncParamAny func(_ interface{}) bool
4041
FuncTooManyReturns func() (int, int, error)

test/playground/data.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package playground
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
func ExampleData() interface{} {
9+
profileJohn := UserProfile{
10+
Birthday: time.Date(1990, 1, 1, 0, 0, 0, 0, time.UTC),
11+
Biography: "A passionate writer about Go.",
12+
Website: "https://johndoe.com",
13+
}
14+
15+
profileJane := UserProfile{
16+
Birthday: time.Date(1985, 2, 15, 0, 0, 0, 0, time.UTC),
17+
Biography: "A web developer and writer.",
18+
Website: "https://jane.com",
19+
}
20+
21+
authorJohn := Author{
22+
ID: 1,
23+
FirstName: "John",
24+
LastName: "Doe",
25+
26+
Profile: profileJohn,
27+
}
28+
29+
authorJane := Author{
30+
ID: 2,
31+
FirstName: "Jane",
32+
LastName: "Smith",
33+
34+
Profile: profileJane,
35+
}
36+
37+
posts := []Post{
38+
{
39+
ID: 1,
40+
Title: "Understanding Golang",
41+
Content: "Go is an open-source programming language...",
42+
PublishDate: time.Now().AddDate(0, -1, 0),
43+
Author: authorJohn,
44+
Comments: generateComments(3),
45+
Tags: []string{"Go", "Programming"},
46+
Likes: 100,
47+
},
48+
{
49+
ID: 2,
50+
Title: "Exploring Python",
51+
Content: "Python is versatile...",
52+
PublishDate: time.Now().AddDate(0, -2, 0),
53+
Author: authorJane,
54+
Comments: generateComments(4),
55+
Tags: []string{"Python", "Development"},
56+
Likes: 150,
57+
},
58+
{
59+
ID: 3,
60+
Title: "Web Development Basics",
61+
Content: "The world of web development...",
62+
PublishDate: time.Now().AddDate(0, -3, 0),
63+
Author: authorJane,
64+
Comments: generateComments(5),
65+
Tags: []string{"Web", "HTML", "CSS"},
66+
Likes: 125,
67+
},
68+
{
69+
ID: 4,
70+
Title: "Machine Learning in a Nutshell",
71+
Content: "ML is revolutionizing industries...",
72+
PublishDate: time.Now().AddDate(0, -5, 0),
73+
Author: authorJohn,
74+
Comments: generateComments(6),
75+
Tags: []string{"ML", "AI"},
76+
Likes: 200,
77+
},
78+
{
79+
ID: 5,
80+
Title: "JavaScript: The Good Parts",
81+
Content: "JavaScript powers the web...",
82+
PublishDate: time.Now().AddDate(0, -4, 0),
83+
Author: authorJane,
84+
Comments: generateComments(3),
85+
Tags: []string{"JavaScript", "Web"},
86+
Likes: 170,
87+
},
88+
}
89+
90+
blog := Blog{
91+
Posts: make([]Post, len(posts)),
92+
Authors: map[int]Author{authorJohn.ID: authorJohn, authorJane.ID: authorJane},
93+
TotalViews: 10000,
94+
TotalPosts: len(posts),
95+
TotalLikes: 0,
96+
}
97+
98+
for i, post := range posts {
99+
blog.Posts[i] = post
100+
blog.TotalLikes += post.Likes
101+
}
102+
103+
return blog
104+
}
105+
106+
func generateComments(count int) []Comment {
107+
var comments []Comment
108+
for i := 1; i <= count; i++ {
109+
comment := Comment{
110+
ID: i,
111+
AuthorName: fmt.Sprintf("Commenter %d", i),
112+
Content: fmt.Sprintf("This is comment %d!", i),
113+
CommentDate: time.Now().AddDate(0, 0, -i),
114+
Upvotes: i * 5,
115+
}
116+
comments = append(comments, comment)
117+
}
118+
return comments
119+
}

test/playground/env.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ type UserProfile struct {
1010
Website string
1111
}
1212

13+
func (u UserProfile) Age() int {
14+
return time.Now().Year() - u.Birthday.Year()
15+
}
16+
1317
type Author struct {
1418
ID int
1519
FirstName string
@@ -18,6 +22,14 @@ type Author struct {
1822
Profile UserProfile
1923
}
2024

25+
func (a Author) FullName() string {
26+
return a.FirstName + " " + a.LastName
27+
}
28+
29+
func (a Author) IsAdmin() bool {
30+
return a.ID == 1
31+
}
32+
2133
type Post struct {
2234
ID int
2335
Title string
@@ -29,6 +41,35 @@ type Post struct {
2941
Likes int
3042
}
3143

44+
func (p Post) Published() bool {
45+
return !p.PublishDate.IsZero()
46+
}
47+
48+
func (p Post) After(date time.Time) bool {
49+
return p.PublishDate.After(date)
50+
}
51+
52+
func (p Post) Before(date time.Time) bool {
53+
return p.PublishDate.Before(date)
54+
}
55+
56+
func (p Post) Compare(other Post) int {
57+
if p.PublishDate.Before(other.PublishDate) {
58+
return -1
59+
} else if p.PublishDate.After(other.PublishDate) {
60+
return 1
61+
}
62+
return 0
63+
}
64+
65+
func (p Post) Equal(other Post) bool {
66+
return p.Compare(other) == 0
67+
}
68+
69+
func (p Post) IsZero() bool {
70+
return p.ID == 0 && p.Title == "" && p.Content == "" && p.PublishDate.IsZero()
71+
}
72+
3273
type Comment struct {
3374
ID int
3475
AuthorName string
@@ -37,10 +78,94 @@ type Comment struct {
3778
Upvotes int
3879
}
3980

81+
func (c Comment) Upvoted() bool {
82+
return c.Upvotes > 0
83+
}
84+
85+
func (c Comment) AuthorEmail() string {
86+
return c.AuthorName + "@example.com"
87+
}
88+
4089
type Blog struct {
4190
Posts []Post
4291
Authors map[int]Author
4392
TotalViews int
4493
TotalPosts int
4594
TotalLikes int
4695
}
96+
97+
func (b Blog) RecentPosts() []Post {
98+
var posts []Post
99+
for _, post := range b.Posts {
100+
if post.Published() {
101+
posts = append(posts, post)
102+
}
103+
}
104+
return posts
105+
}
106+
107+
func (b Blog) PopularPosts() []Post {
108+
var posts []Post
109+
for _, post := range b.Posts {
110+
if post.Likes > 150 {
111+
posts = append(posts, post)
112+
}
113+
}
114+
return posts
115+
}
116+
117+
func (b Blog) TotalUpvotes() int {
118+
var upvotes int
119+
for _, post := range b.Posts {
120+
for _, comment := range post.Comments {
121+
upvotes += comment.Upvotes
122+
}
123+
}
124+
return upvotes
125+
}
126+
127+
func (b Blog) TotalComments() int {
128+
var comments int
129+
for _, post := range b.Posts {
130+
comments += len(post.Comments)
131+
}
132+
return comments
133+
}
134+
135+
func (Blog) Add(a, b float64) float64 {
136+
return a + b
137+
}
138+
139+
func (Blog) Sub(a, b float64) float64 {
140+
return a - b
141+
}
142+
143+
func (Blog) Title(post Post) string {
144+
return post.Title
145+
}
146+
147+
func (Blog) HasTag(post Post, tag string) bool {
148+
for _, t := range post.Tags {
149+
if t == tag {
150+
return true
151+
}
152+
}
153+
return false
154+
}
155+
156+
func (Blog) IsAdmin(author Author) bool {
157+
return author.IsAdmin()
158+
}
159+
160+
func (Blog) IsZero(post Post) bool {
161+
return post.IsZero()
162+
}
163+
164+
func (Blog) WithID(posts []Post, id int) Post {
165+
for _, post := range posts {
166+
if post.ID == id {
167+
return post
168+
}
169+
}
170+
return Post{}
171+
}

0 commit comments

Comments
 (0)