Commit 9ad0a26
Changed files (3)
domain
test
domain/movie_library.go
@@ -4,7 +4,24 @@ type MovieLibrary struct {
Movies []Movie
}
+func (self *MovieLibrary) Find(fn Predicate) *Movie {
+ for i := range self.Movies {
+ movie := self.Movies[i]
+ if fn(movie) {
+ return &movie
+ }
+ }
+
+ return nil
+}
+
func (self *MovieLibrary) Add(movie Movie) {
+ found := self.Find(func(x Movie) bool { return x.Title == movie.Title })
+
+ if found != nil {
+ return
+ }
+
self.Movies = append(self.Movies, movie)
}
domain/predicate.go
@@ -0,0 +1,3 @@
+package domain
+
+type Predicate func(Movie) bool
test/unit/movie_library_test.go
@@ -25,19 +25,26 @@ func TestMovieLibrary(t *testing.T) {
pinocchio := domain.Movie{Title: "Pinocchio", Studio: disney, Year: 1940}
t.Run("Add", func(t *testing.T) {
- subject.Add(shawshank_redemption)
- subject.Add(chasing_amy)
- subject.Add(man_on_fire)
- subject.Add(toy_story)
- subject.Add(up)
- subject.Add(cars)
- subject.Add(monsters_inc)
- subject.Add(fantasia)
- subject.Add(dumbo)
- subject.Add(pinocchio)
+ t.Run("when adding a movie to the library", func(t *testing.T) {
+ subject.Add(shawshank_redemption)
+ subject.Add(chasing_amy)
+ subject.Add(man_on_fire)
+ subject.Add(toy_story)
+ subject.Add(up)
+ subject.Add(cars)
+ subject.Add(monsters_inc)
+ subject.Add(fantasia)
+ subject.Add(dumbo)
+ subject.Add(pinocchio)
- t.Run("Length", func(t *testing.T) {
- assert.Equal(t, 10, subject.Count())
+ t.Run("increases the total number of movies in the library", func(t *testing.T) {
+ assert.Equal(t, 10, subject.Count())
+ })
+
+ t.Run("does not allow duplicates", func(t *testing.T) {
+ subject.Add(man_on_fire)
+ assert.Equal(t, 10, subject.Count())
+ })
})
})
}