main
  1should = require('chai').should
  2MovieLibrary = require('../src/movie_library')
  3Movie = require('../src/movie')
  4Studio = require('../src/studio')
  5
  6describe "MovieLibrary", ->
  7  beforeEach ->
  8    @sut = new MovieLibrary
  9    @shawshank_redemption = new Movie(title: "The Shawshank Redemption", studio: Studio.CastleRock, year_published: 1994)
 10    @chasing_amy = new Movie(title: "Chasing Amy", studio: Studio.MiramaxFilms, year_published: 1997)
 11    @man_on_fire = new Movie(title: "Man on Fire", studio: Studio.RegencyEnterprises, year_published: 2004)
 12    @toy_story = new Movie(title: "Toy Story", studio: Studio.Pixar, year_published: 1995)
 13    @up = new Movie(title: "Up", studio: Studio.Pixar, year_published: 2006)
 14    @cars = new Movie(title: "Cars", studio: Studio.Pixar, year_published: 2009)
 15    @monsters_inc = new Movie(title: "Monsters Inc.", studio: Studio.Pixar, year_published: 2001)
 16    @fantasia = new Movie(title: "Fantasia", studio: Studio.Disney, year_published: 1940)
 17    @dumbo = new Movie(title: "Dumbo", studio: Studio.Disney, year_published: 1941)
 18    @pinocchio = new Movie(title: "Pinocchio", studio: Studio.Disney, year_published: 1940)
 19    @all_movies = [@shawshank_redemption, @chasing_amy, @man_on_fire, @toy_story, @up, @cars, @monsters_inc, @fantasia, @dumbo, @pinocchio]
 20
 21  context "when adding a movie to the library", ->
 22    it "increases the total number of movies in the library", ->
 23      @sut.add(@shawshank_redemption)
 24      @sut.add(@chasing_amy)
 25      @sut.count().should.equal(2)
 26
 27    it "does not allow duplicate movies into the library", ->
 28      @sut.add(@man_on_fire)
 29      @sut.add(@man_on_fire)
 30      @sut.count().should.equal(1)
 31
 32    it "does not add two movies that have the same title (logically the same)", ->
 33      @sut.add(new Movie(title: 'old school'))
 34      @sut.add(new Movie(title: 'old school'))
 35      @sut.count().should.equal(1)
 36
 37  context "any?", ->
 38    it "returns true when something matches", ->
 39      @sut.add(new Movie(title: 'old school'))
 40      @sut.add(new Movie(title: 'old school'))
 41      result = @sut.any (movie) ->
 42        movie.title == 'old school'
 43
 44      result.should.equal(true)
 45
 46  context "searching for movies", ->
 47    beforeEach ->
 48      for movie in @all_movies
 49        @sut.add(movie)
 50
 51    it "can find all pixar movies", ->
 52      results = @sut.all(Movie.where(studio: Studio.Pixar))
 53      results.should.include(@toy_story)
 54      results.should.include(@up)
 55      results.should.include(@cars)
 56      results.should.include(@monsters_inc)
 57
 58
 59    it 'finds all movies published by pixar or disney', ->
 60       results = @sut.all(Movie.where(studio: Studio.Pixar).or(Movie.where(studio: Studio.Disney)))
 61       results.length.should.equal(7)
 62       results.should.include(@toy_story)
 63       results.should.include(@up)
 64       results.should.include(@cars)
 65       results.should.include(@monsters_inc)
 66       results.should.include(@fantasia)
 67       results.should.include(@dumbo)
 68       results.should.include(@pinocchio)
 69
 70     it "finds all movies not published by pixar", ->
 71       results = @sut.all(Movie.where(studio: Studio.Pixar).not())
 72       results.length.should.equal(6)
 73       results.should.include(@fantasia)
 74       results.should.include(@dumbo)
 75       results.should.include(@pinocchio)
 76       results.should.include(@shawshank_redemption)
 77       results.should.include(@chasing_amy)
 78       results.should.include(@man_on_fire)
 79
 80     it "finds all movies released after 2004", ->
 81       results = @sut.find_all (movie) -> movie.year_published > 2004
 82       results.length.should.equal(2)
 83       results.should.include(@up)
 84       results.should.include(@cars)
 85
 86     it "finds all movies released between 1982 and 2003 - inclusive", ->
 87       results = @sut.find_all (movie) -> movie.year_published > 1982 && movie.year_published < 2003
 88       results.length.should.equal(4)
 89       results.should.include(@shawshank_redemption)
 90       results.should.include(@chasing_amy)
 91       results.should.include(@toy_story)
 92       results.should.include(@monsters_inc)
 93
 94  context "sorting movies", ->
 95    beforeEach ->
 96      @sut.add(movie) for movie in @all_movies
 97
 98    it "sorts all movies by title ascending", ->
 99      expected_order = [ @cars, @chasing_amy, @dumbo, @fantasia, @man_on_fire, @monsters_inc, @pinocchio, @shawshank_redemption, @toy_story, @up ]
100      results = @sut.sort_by_title_ascending()
101      results.should.eql(expected_order)
102
103    it "sorts all movies by title descending", ->
104      expected_order = [ @up, @toy_story, @shawshank_redemption, @pinocchio, @monsters_inc, @man_on_fire, @fantasia, @dumbo, @chasing_amy, @cars ]
105      results = @sut.sort_by_title_descending()
106      results.should.eql(expected_order)
107
108    it 'Sorts all movies by descending release date', ->
109      expected_order = [@cars, @up, @man_on_fire, @monsters_inc, @chasing_amy, @toy_story, @shawshank_redemption, @dumbo, @fantasia, @pinocchio ]
110      results = @sut.sorted_by_year_published_descending()
111      results.should.eql(expected_order)
112
113    it 'Sorts all movies by ascending release date', ->
114      expected_order = [ @fantasia, @pinocchio, @dumbo, @shawshank_redemption, @toy_story, @chasing_amy, @monsters_inc, @man_on_fire, @up, @cars ]
115      results = @sut.sorted_by_year_published()
116      results.should.eql(expected_order)
117
118    it 'Sorts all movies by preferred studios and release date ascending', ->
119      #rankings: Pixar, Disney, CastleRock, MiramaxFilms, RegenceyEnterprises
120      rankings = [Studio.Pixar, Studio.Disney, Studio.CastleRock, Studio.MiramaxFilms, Studio.RegencyEnterprises]
121      expected_order = [ @toy_story, @monsters_inc, @up, @cars, @fantasia, @pinocchio, @dumbo, @shawshank_redemption, @chasing_amy, @man_on_fire ]
122      results = @sut.sorted_by_studio_rankings_then_by_year_published(rankings)
123      results.should.eql(expected_order)
124