-
-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
864 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package gofakeit | ||
|
||
import "math/rand" | ||
|
||
func BookTitle() string { return bookTitle(globalFaker.Rand) } | ||
|
||
func (f *Faker) BookTitle() string { return bookTitle(f.Rand) } | ||
|
||
func bookTitle(r *rand.Rand) string { return getRandValue(r, []string{"book", "title"}) } | ||
|
||
func BookAuthor() string { return bookAuthor(globalFaker.Rand) } | ||
|
||
func (f *Faker) BookAuthor() string { return bookAuthor(f.Rand) } | ||
|
||
func bookAuthor(r *rand.Rand) string { return getRandValue(r, []string{"book", "author"}) } | ||
|
||
func BookGenre() string { return bookGenre(globalFaker.Rand) } | ||
|
||
func (f *Faker) BookGenre() string { return bookGenre(f.Rand) } | ||
|
||
func bookGenre(r *rand.Rand) string { return getRandValue(r, []string{"book", "genre"}) } | ||
|
||
type BookInfo struct { | ||
Title string `json:"title" xml:"name"` | ||
Author string `json:"author" xml:"author"` | ||
Genre string `json:"genre" xml:"genre"` | ||
} | ||
|
||
func Book() *BookInfo { return book(globalFaker.Rand) } | ||
|
||
func (f *Faker) Book() *BookInfo { return book(f.Rand) } | ||
|
||
func book(r *rand.Rand) *BookInfo { | ||
return &BookInfo{ | ||
Title: bookTitle(r), | ||
Author: bookAuthor(r), | ||
Genre: bookGenre(r), | ||
} | ||
} | ||
|
||
func addBookLookup() { | ||
AddFuncLookup("book", Info{ | ||
Display: "Book", | ||
Category: "book", | ||
Description: "Random Book data set", | ||
Example: `{title: "Hamlet", author: "Mark Twain", genre: "Adventure"}`, | ||
Output: "map[string]string", | ||
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) { | ||
return book(r), nil | ||
}, | ||
}) | ||
|
||
AddFuncLookup("booktitle", Info{ | ||
Display: "Title", | ||
Category: "book", | ||
Description: "Random Book title", | ||
Example: "Hamlet", | ||
Output: "string", | ||
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) { | ||
return bookTitle(r), nil | ||
}, | ||
}) | ||
|
||
AddFuncLookup("bookauthor", Info{ | ||
Display: "Author", | ||
Category: "book", | ||
Description: "Random Book author", | ||
Example: "Mark Twain", | ||
Output: "string", | ||
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) { | ||
return bookAuthor(r), nil | ||
}, | ||
}) | ||
|
||
AddFuncLookup("bookgenre", Info{ | ||
Display: "Genre", | ||
Category: "book", | ||
Description: "Random Book genre", | ||
Example: "Adventure", | ||
Output: "string", | ||
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) { | ||
return bookGenre(r), nil | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package gofakeit | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func EnableBook() { | ||
Seed(11) | ||
book := Book() | ||
fmt.Println(book.Title) | ||
fmt.Println(book.Author) | ||
fmt.Println(book.Genre) | ||
// Output: Anna Karenina | ||
// Toni Morrison | ||
// Thriller | ||
} | ||
|
||
func ExampleFaker_Book() { | ||
f := New(11) | ||
book := f.Book() | ||
fmt.Println(book.Title) | ||
fmt.Println(book.Author) | ||
fmt.Println(book.Genre) | ||
// Output: Anna Karenina | ||
// Toni Morrison | ||
// Thriller | ||
} | ||
|
||
func BenchmarkBook(b *testing.B) { | ||
b.Run("package", func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
Book() | ||
} | ||
}) | ||
|
||
b.Run("Faker math", func(b *testing.B) { | ||
f := New(0) | ||
|
||
for i := 0; i < b.N; i++ { | ||
f.Book() | ||
} | ||
}) | ||
|
||
b.Run("Faker crypto", func(b *testing.B) { | ||
f := NewCrypto() | ||
|
||
for i := 0; i < b.N; i++ { | ||
f.Book() | ||
} | ||
}) | ||
} | ||
|
||
func TestBook(t *testing.T) { | ||
for i := 0; i < 100; i++ { | ||
Book() | ||
} | ||
} | ||
|
||
func ExampleBookTitle() { | ||
Seed(11) | ||
fmt.Println(BookTitle()) | ||
// Output: Anna Karenina | ||
} | ||
|
||
func ExampleFaker_BookTitle() { | ||
f := New(11) | ||
fmt.Println(f.BookTitle()) | ||
// Output: Anna Karenina | ||
} | ||
|
||
func BenchmarkBookTitle(b *testing.B) { | ||
b.Run("package", func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
BookTitle() | ||
} | ||
}) | ||
|
||
b.Run("Faker math", func(b *testing.B) { | ||
f := New(0) | ||
|
||
for i := 0; i < b.N; i++ { | ||
f.BookTitle() | ||
} | ||
}) | ||
|
||
b.Run("Faker crypto", func(b *testing.B) { | ||
f := NewCrypto() | ||
|
||
for i := 0; i < b.N; i++ { | ||
f.BookTitle() | ||
} | ||
}) | ||
} | ||
|
||
func ExampleBookAuthor() { | ||
Seed(11) | ||
fmt.Println(BookAuthor()) | ||
// Output: James Joyce | ||
} | ||
|
||
func ExampleFaker_BookAuthor() { | ||
f := New(11) | ||
fmt.Println(f.BookAuthor()) | ||
// Output: James Joyce | ||
} | ||
|
||
func BenchmarkBookAuthor(b *testing.B) { | ||
b.Run("package", func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
BookAuthor() | ||
} | ||
}) | ||
|
||
b.Run("Faker math", func(b *testing.B) { | ||
f := New(0) | ||
|
||
for i := 0; i < b.N; i++ { | ||
f.BookAuthor() | ||
} | ||
}) | ||
|
||
b.Run("Faker crypto", func(b *testing.B) { | ||
f := NewCrypto() | ||
|
||
for i := 0; i < b.N; i++ { | ||
f.BookAuthor() | ||
} | ||
}) | ||
} | ||
|
||
|
||
func ExampleBookGenre() { | ||
Seed(11) | ||
fmt.Println(BookGenre()) | ||
// Output: Crime | ||
} | ||
|
||
func ExampleFaker_BookGenre() { | ||
f := New(11) | ||
fmt.Println(f.BookGenre()) | ||
// Output: Crime | ||
} | ||
|
||
func BenchmarkBookGenre(b *testing.B) { | ||
b.Run("package", func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
BookGenre() | ||
} | ||
}) | ||
|
||
b.Run("Faker math", func(b *testing.B) { | ||
f := New(0) | ||
|
||
for i := 0; i < b.N; i++ { | ||
f.BookGenre() | ||
} | ||
}) | ||
|
||
b.Run("Faker crypto", func(b *testing.B) { | ||
f := NewCrypto() | ||
|
||
for i := 0; i < b.N; i++ { | ||
f.BookGenre() | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.