Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@obcode
Created October 14, 2019 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obcode/80a56d9d316611a7a1bb6fee5e599a0e to your computer and use it in GitHub Desktop.
Save obcode/80a56d9d316611a7a1bb6fee5e599a0e to your computer and use it in GitHub Desktop.
package moviestore
import "testing"
func newMoviestoreImpl() *moviestoreImpl {
ms := new(moviestoreImpl)
ms.available = make(map[Serial]Movie)
ms.users = make(map[UserID]User)
ms.rented = make(map[UserID][]Movie)
return ms
}
func TestMoviestore_AddMovie(t *testing.T) {
cases := []struct {
title string
fsk FSK
}{
{"Am Limit", FSK0},
{"Texas Chainsaw Massacre", FSK18},
{"Inglourious Basterds", FSK16},
}
moviestore := newMoviestoreImpl()
for _, c := range cases {
gotSerial := moviestore.AddMovie(c.title, c.fsk)
gotMovie, present := moviestore.available[gotSerial]
if !present || gotMovie.Title != c.title || gotMovie.Fsk != c.fsk {
t.Errorf("Added %v, but got %v", c, gotMovie)
}
}
}
func TestMoviestore_AddUser(t *testing.T) {
cases := []struct {
name string
age Age
}{
{"Hugo", 12},
{"Helga", 19},
{"Ronja", 17},
}
moviestore := newMoviestoreImpl()
for _, c := range cases {
gotUserID := moviestore.AddUser(c.name, c.age)
gotUser, present := moviestore.users[gotUserID]
if !present || gotUser.Name != c.name || gotUser.Age != c.age {
t.Errorf("Added %v, but got %v", c, gotUser)
}
}
}
func TestMoviestore_AddAndRentMovie(t *testing.T) {
movies := []struct {
title string
fsk FSK
}{
{"Am Limit", FSK0},
{"Texas Chainsaw Massacre", FSK18},
{"Inglourious Basterds", FSK16},
}
users := []struct {
name string
age Age
}{
{"Hugo", 12},
{"Helga", 19},
{"Ronja", 17},
}
moviestore := newMoviestoreImpl()
movieSerials := make([]Serial, 0)
for _, c := range movies {
gotSerial := moviestore.AddMovie(c.title, c.fsk)
movieSerials = append(movieSerials, gotSerial)
}
userSerials := make([]UserID, 0)
for _, u := range users {
gotSerial := moviestore.AddUser(u.name, u.age)
userSerials = append(userSerials, gotSerial)
}
movieSerial := movieSerials[0]
userID := userSerials[0]
_, _, err := moviestore.Rent(movieSerial, userID)
if err != nil {
t.Errorf("Rent(%v,%v) does not work\nmoviestore: %v", movieSerial, userID, moviestore)
}
for _, c := range movieSerials {
gotMovie, present := moviestore.available[c]
if !present && c != movieSerial {
t.Errorf("Movie %v rented, but still available", gotMovie)
}
}
rentedMovies := moviestore.rented[userID]
if len(rentedMovies) != 1 {
t.Errorf("No movies rented or too much %v", rentedMovies)
}
if rentedMovies[0].Serial != movieSerial {
t.Errorf("Wrong movie rented, have %d, wanted %d", rentedMovies[0].Serial, movieSerial)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment