main
  1require "spec_helper"
  2
  3describe MovieLibrary do
  4  let(:library) { MovieLibrary.new }
  5  let(:shawshank_redemption) { create_movie(title: "The Shawshank Redemption", studio: Studio::CastleRock, year_published: 1994) }
  6  let(:chasing_amy) { create_movie(title: "Chasing Amy", studio: Studio::MiramaxFilms, year_published: 1997) }
  7  let(:man_on_fire) { create_movie(title: "Man on Fire", studio: Studio::RegencyEnterprises, year_published: 2004) }
  8
  9  let(:toy_story) { create_movie(title: "Toy Story", studio: Studio::Pixar, year_published: 1995) }
 10  let(:up) { create_movie(title: "Up", studio: Studio::Pixar, year_published: 2006) }
 11  let(:cars) { create_movie(title: "Cars", studio: Studio::Pixar, year_published: 2009) }
 12  let(:monsters_inc) { create_movie(title: "Monsters Inc.", studio: Studio::Pixar, year_published: 2001) }
 13
 14  let(:fantasia) { create_movie(title: "Fantasia", studio: Studio::Disney, year_published: 1940) }
 15  let(:dumbo) { create_movie(title: "Dumbo", studio: Studio::Disney, year_published: 1941) }
 16  let(:pinocchio) { create_movie(title: "Pinocchio", studio: Studio::Disney, year_published: 1940) }
 17
 18  let(:all_movies) { [shawshank_redemption, chasing_amy, man_on_fire, toy_story, up, cars, monsters_inc, fantasia, dumbo, pinocchio] }
 19
 20  def create_movie(details)
 21    Movie.new(details)
 22  end
 23
 24  context "when adding a movie to the library" do
 25    it "should increase the total number of movies in the library" do
 26      library.add(shawshank_redemption)
 27      library.add(chasing_amy)
 28      library.total_count.should == 2
 29    end
 30
 31    it "should not allow duplicate movies into the library" do
 32      library.add(man_on_fire)
 33      library.add(man_on_fire)
 34      library.total_count.should == 1
 35    end
 36
 37    it 'cannot add two movies that have the same title (logically the same)' do
 38      library.add(create_movie(:title => 'Old School'))
 39      library.add(create_movie(:title => 'Old School'))
 40      library.total_count.should == 1
 41    end
 42  end
 43
 44  context 'Searching for movies' do
 45    before :each do
 46      all_movies.each { |x| library.add(x) }
 47    end
 48
 49    it 'Can find all pixar movies' do
 50      results = library.find_all_movies_by_pixar
 51      results.count.should == 4
 52      results.should include(toy_story)
 53      results.should include(up)
 54      results.should include(cars)
 55      results.should include(monsters_inc)
 56    end
 57
 58    it 'Can find all movies published by pixar or disney' do
 59      results = library.find_all_movies_by_pixar_or_disney
 60      results.count.should == 7
 61      results.should include(toy_story)
 62      results.should include(up)
 63      results.should include(cars)
 64      results.should include(monsters_inc)
 65      results.should include(fantasia)
 66      results.should include(dumbo)
 67      results.should include(pinocchio)
 68    end
 69
 70    it 'Can find all movies not published by pixar' do
 71      results = library.find_all_movies_not_published_by_pixar
 72      results.length.should == 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    end
 80
 81    it 'Can find all movies released after 2004' do
 82      results = library.find_all_movies_published_after_2004
 83      results.length.should == 2
 84      results.should include(up)
 85      results.should include(cars)
 86    end
 87
 88    it 'Can find all movies released between 1982 and 2003 - Inclusive' do
 89      results = library.find_all_movies_between_1982_and_2003
 90      results.length.should == 4
 91      results.should include(shawshank_redemption)
 92      results.should include(chasing_amy)
 93      results.should include(toy_story)
 94      results.should include(monsters_inc)
 95    end
 96  end
 97
 98  context 'Sorting movies' do
 99    before :each do
100      all_movies.each { |x| library.add(x) }
101    end
102
103    it 'Sorts all movies by descending title' do
104      expected_order = [ cars, chasing_amy, dumbo, fantasia, man_on_fire, monsters_inc, pinocchio, shawshank_redemption, toy_story, up]
105      results = library.sort_movies_by_title_descending
106      results.should == expected_order
107    end
108
109    it 'Sorts all movies by ascending title' do
110      expected_order = [up, toy_story, shawshank_redemption, pinocchio, monsters_inc, man_on_fire, fantasia, dumbo, chasing_amy, cars]
111      results = library.sort_movies_by_title_ascending
112      results.should == expected_order
113    end
114
115    it 'Sorts all movies by descending release date' do
116      expected_order = [cars, up, man_on_fire, monsters_inc, chasing_amy, toy_story, shawshank_redemption, dumbo, fantasia, pinocchio ]
117      results = library.sort_movies_by_descending_release_date
118      results.should == expected_order
119    end
120
121    it 'Sorts all movies by ascending release date' do
122      expected_order = [pinocchio, fantasia, dumbo, shawshank_redemption, toy_story, chasing_amy, monsters_inc, man_on_fire, up, cars]
123      results = library.sort_movies_by_ascending_release_date
124      results.should == expected_order
125    end
126
127    it 'Sorts all movies by preferred studios and release date ascending' do
128      #rankings: Pixar, Disney, CastleRock, MiramaxFilms, RegenceyEnterprises
129      expected_order = [ toy_story, monsters_inc, up, cars, fantasia, pinocchio, dumbo, shawshank_redemption, chasing_amy, man_on_fire]
130      results = library.sort_movies_by_preferred_studios_and_release_date_ascending
131      results.should == expected_order
132    end
133  end
134
135  context "equality" do
136    it "should not equal" do
137      blah = create_movie(title: 'blah')
138      huh = create_movie(title: 'huh')
139      blah.should_not == huh
140    end
141
142    it "should equal" do
143      huh1 = create_movie(title: 'huh')
144      huh2 = create_movie(title: 'huh')
145      huh1.should == huh2
146    end
147  end
148end