-
Notifications
You must be signed in to change notification settings - Fork 22
Hw ep6 #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hw ep6 #1
Changes from all commits
ffd02fb
130b982
f33c4e3
2c942ed
360311e
8dc347d
699d063
5c883c8
5700cee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| source 'http://rubygems.org' | ||
|
|
||
| gem 'rake' | ||
| gem 'rspec' | ||
| gem 'activesupport' | ||
| gem 'activerecord' | ||
| gem 'pg' | ||
| gem 'minitest' | ||
| gem 'mynyml-redgreen' | ||
| gem 'fivemat' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,18 @@ | ||
| GEM | ||
| remote: http://rubygems.org/ | ||
| specs: | ||
| activemodel (3.2.3) | ||
| activesupport (= 3.2.3) | ||
| builder (~> 3.0.0) | ||
| activerecord (3.2.3) | ||
| activemodel (= 3.2.3) | ||
| activesupport (= 3.2.3) | ||
| arel (~> 3.0.2) | ||
| tzinfo (~> 0.3.29) | ||
| activesupport (3.2.3) | ||
| i18n (~> 0.6) | ||
| multi_json (~> 1.0) | ||
| arel (3.0.2) | ||
| builder (3.0.0) | ||
| diff-lcs (1.1.3) | ||
| i18n (0.6.0) | ||
| multi_json (1.3.4) | ||
| pg (0.13.2) | ||
| fivemat (1.0.0) | ||
| minitest (3.0.0) | ||
| mynyml-redgreen (0.7.1) | ||
| term-ansicolor (>= 1.0.4) | ||
| rake (0.9.2.2) | ||
| rspec (2.10.0) | ||
| rspec-core (~> 2.10.0) | ||
| rspec-expectations (~> 2.10.0) | ||
| rspec-mocks (~> 2.10.0) | ||
| rspec-core (2.10.0) | ||
| rspec-expectations (2.10.0) | ||
| diff-lcs (~> 1.1.3) | ||
| rspec-mocks (2.10.1) | ||
| tzinfo (0.3.33) | ||
| term-ansicolor (1.0.7) | ||
|
|
||
| PLATFORMS | ||
| ruby | ||
|
|
||
| DEPENDENCIES | ||
| activerecord | ||
| activesupport | ||
| pg | ||
| fivemat | ||
| minitest | ||
| mynyml-redgreen | ||
| rake | ||
| rspec |
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| require_relative 'ep6/vehicle' | ||
| require_relative 'ep6/automobile' | ||
| require_relative 'ep6/motorcycle' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| class Automobile < Vehicle | ||
| attr_accessor :color | ||
| attr_reader :make, :model, :year | ||
|
|
||
| def initialize(features = {}) | ||
| defaults = { | ||
| :color => 'black', | ||
| :make => 'ford', | ||
| :model => 'taurus', | ||
| :year => '2012' | ||
| } | ||
| features = defaults.merge(features) | ||
|
|
||
| @color = features[:color] | ||
| @make = features[:make] | ||
| @model = features[:model] | ||
| @year = features[:year] | ||
|
|
||
| # Now add the new instance to the @all_vehicles array via Vehicle's | ||
| # initialize method. | ||
| super() | ||
| end | ||
|
|
||
| def ==(other) | ||
| if @color == other.color && @make == other.make && | ||
| @model == other.model && @year == other.year | ||
| return true | ||
| else | ||
| return false | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| class Motorcycle < Vehicle | ||
| def self.wheels | ||
| 2 | ||
| end | ||
|
|
||
| def initialize | ||
| # Add the new instance to the @all_vehicles array via Vehicle's | ||
| # initialize method. | ||
| super() | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| class Vehicle | ||
| extend Enumerable | ||
|
|
||
| @all_vehicles = [] | ||
|
|
||
| # create read/write methods for @all_vehicles | ||
| class << self | ||
| attr_accessor :all_vehicles | ||
| end | ||
|
|
||
| def initialize | ||
| Vehicle.all_vehicles.push self | ||
| end | ||
|
|
||
| def self.wheels | ||
| 4 | ||
| end | ||
|
|
||
| def self.each | ||
| @all_vehicles.each do |vehicle| | ||
| yield vehicle | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,11 @@ | ||
| require "rubygems" | ||
| require "bundler/setup" | ||
| require 'bundler/setup' | ||
|
|
||
| require 'rspec/core/rake_task' | ||
| require 'rake/testtask' | ||
|
|
||
| desc 'Default: run specs.' | ||
| task :default => :spec | ||
| desc 'Default: run tests.' | ||
| task :default => :test | ||
|
|
||
| desc "Run specs" | ||
| RSpec::Core::RakeTask.new do |t| | ||
| t.pattern = "**/*_spec.rb" # don't need this, it's default. | ||
| # Put spec opts in a file named .rspec in root | ||
| desc "Run tests" | ||
| Rake::TestTask.new do |t| | ||
| t.pattern = 'test/test_*.rb' | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| require_relative 'test_helper' | ||
|
|
||
| describe 'Automobile class' do | ||
| it 'should exist' do | ||
| Automobile.must_be_kind_of Class | ||
| end | ||
|
|
||
| it 'should inherit from Vehicle' do | ||
| Automobile.superclass.must_equal Vehicle | ||
| end | ||
|
|
||
| it 'should have a class method that returns the number of wheels' do | ||
| Automobile.must_respond_to :wheels | ||
| Automobile.wheels.must_equal 4 | ||
| end | ||
|
|
||
| it 'should have default features when initialized with an empty hash' do | ||
| car = Automobile.new({}) | ||
|
|
||
| car.color.must_equal 'black' | ||
| car.make.must_equal 'ford' | ||
| car.model.must_equal 'taurus' | ||
| car.year.must_equal '2012' | ||
| end | ||
|
|
||
| it 'should override defaults when given various values' do | ||
| white_car = Automobile.new({:color => 'white'}) | ||
| mercedes = Automobile.new({:make => 'mercedes'}) | ||
| charger = Automobile.new({:model => 'charger'}) | ||
| classic = Automobile.new({:year => '1968'}) | ||
|
|
||
| white_car.color.must_equal 'white' | ||
| mercedes.make.must_equal 'mercedes' | ||
| charger.model.must_equal 'charger' | ||
| classic.year.must_equal '1968' | ||
| end | ||
|
|
||
| it 'should accept multiple values in any order' do | ||
| old_charger = Automobile.new({:year => '1968', :model => 'charger'}) | ||
| new_mercedes = Automobile.new({ | ||
| :model => 'SE', | ||
| :make => 'mercedes', | ||
| :color => 'red', | ||
| :year => '2010' | ||
| }) | ||
|
|
||
| old_charger.model.must_equal 'charger' | ||
| old_charger.year.must_equal '1968' | ||
|
|
||
| new_mercedes.color.must_equal 'red' | ||
| new_mercedes.year.must_equal '2010' | ||
| end | ||
|
|
||
| it 'should be able to change its color' do | ||
| car = Automobile.new | ||
| car.color.must_equal 'black' | ||
|
|
||
| car.color = 'white' | ||
| car.color.must_equal 'white' | ||
| end | ||
|
|
||
| it 'should be able to compare two cars' do | ||
| chevy = Automobile.new({ | ||
| :color => 'red', | ||
| :make => 'chevy', | ||
| :model => 'impala', | ||
| :year => 1972 | ||
| }) | ||
| ford = Automobile.new | ||
| chevy_copy = chevy.dup | ||
|
|
||
| chevy.wont_equal ford | ||
| chevy.must_equal chevy_copy | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| gem 'minitest' | ||
| require 'minitest/spec' | ||
| require 'fivemat/minitest/autorun' | ||
| require 'redgreen' | ||
| require_relative '../lib/ep6' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| require_relative 'test_helper' | ||
|
|
||
| describe 'Motorcycle class' do | ||
| it 'should exist' do | ||
| Motorcycle.must_be_kind_of Class | ||
| end | ||
|
|
||
| it 'should inherit from Vehicle' do | ||
| Motorcycle.superclass.must_equal Vehicle | ||
| end | ||
|
|
||
| it 'should have a class method that returns the number of wheels' do | ||
| Motorcycle.must_respond_to :wheels | ||
| Motorcycle.wheels.must_equal 2 | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| describe 'sanity' do | ||
| it 'is minimally capable' do | ||
| true.must_be :==, true | ||
| end | ||
|
|
||
| it 'must know true from false' do | ||
| true.wont_be :==, false | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| require_relative 'test_helper' | ||
|
|
||
| describe 'Vehicle class' do | ||
| it 'should exist' do | ||
| Vehicle.must_be_kind_of Class | ||
| end | ||
|
|
||
| it 'should have a class method that returns the number of wheels' do | ||
| Vehicle.must_respond_to :wheels | ||
| Vehicle.wheels.must_equal 4 | ||
| end | ||
|
|
||
| describe 'class instance variable and enumerability' do | ||
| before do | ||
| # I don't think this should be necessary, but it is. Why? | ||
| Vehicle.all_vehicles = [] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's necessary because whenever we declare an attr_accessor, it defaults to nil Here's an example that shows a way to solve this... Probably a way using meta-programming to handle inside the class definition though: http://rubyfiddle.com/riddles/b2334 |
||
| end | ||
|
|
||
| it 'should store all the vehicles as they are made' do | ||
| car = Automobile.new | ||
| motorcycle = Motorcycle.new | ||
| vehicle = Vehicle.new | ||
|
|
||
| Vehicle.all_vehicles.count.must_equal 3 | ||
| end | ||
|
|
||
| it 'should include enumerable methods' do | ||
| Vehicle.must_respond_to :each | ||
| Vehicle.must_respond_to :select | ||
| Vehicle.must_respond_to :map | ||
| Vehicle.must_respond_to :partition | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this