Skip to content

Instantly share code, notes, and snippets.

@alexwelch
Forked from devoracer/hashes.rb
Last active April 5, 2016 02:32
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 alexwelch/6d8e25f777d6681fbf527ed16e0749a7 to your computer and use it in GitHub Desktop.
Save alexwelch/6d8e25f777d6681fbf527ed16e0749a7 to your computer and use it in GitHub Desktop.
tests
require File.expand_path(File.dirname(__FILE__) + '/neo')
class AboutHashes < Neo::Koan
def test_creating_hashes
empty_hash = Hash.new
assert_equal Hash, empty_hash.class
assert_equal({}, empty_hash)
assert_equal 0, empty_hash.size
end
def test_hash_literals
hash = { :one => "uno", :two => "dos" }
assert_equal 2, hash.size
end
def test_accessing_hashes
hash = { :one => "uno", :two => 2 }
assert_equal "uno", hash[:one]
assert_equal 2, hash[:two]
assert_equal nil, hash[:doesnt_exist]
end
def test_accessing_hashes_with_fetch
hash = { :one => "uno" }
assert_equal "uno", hash.fetch(:one)
assert_raise(KeyError) do
hash.fetch(:doesnt_exist)
end
assert_equal "booyah", hash.fetch(:noexistajsldfj, "booyah")
# THINK ABOUT IT:
#
# Why might you want to use #fetch instead of #[] when accessing hash keys?
end
def test_changing_hashes
hash = { :one => "uno", :two => "dos" }
hash[:one] = "eins"
expected = { :one => "eins", :two => "dos" }
assert_equal expected, hash
# Bonus Question: Why was "expected" broken out into a variable
# rather than used as a literal?
end
def test_hash_is_unordered
hash1 = { :one => "uno", :two => "dos" }
hash2 = { :two => "dos", :one => "uno" }
assert_equal true, hash1 == hash2
end
def test_hash_keys
hash = { :one => "uno", :two => "dos" }
assert_equal 2, hash.keys.size
assert_equal true, hash.keys.include?(:one)
assert_equal true, hash.keys.include?(:two)
assert_equal Array, hash.keys.class
end
def test_hash_values
hash = { :one => "uno", :two => "dos" }
assert_equal 2, hash.values.size
assert_equal true, hash.values.include?("uno")
assert_equal true, hash.values.include?("dos")
assert_equal Array, hash.values.class
end
def test_combining_hashes
hash = { "jim" => 53, "amy" => 20, "dan" => 23 }
new_hash = hash.merge({ "jim" => 54, "jenny" => 26 })
assert_equal true, hash != new_hash
expected = { "jim" => 54, "amy" => 20, "dan" => 23, "jenny" => 26 }
assert_equal expected, new_hash
end
def test_default_value
hash1 = Hash.new
hash1[:one] = 1
assert_equal 1, hash1[:one]
assert_equal nil, hash1[:two]
hash2 = Hash.new("dos")
hash2[:one] = 1
assert_equal 1, hash2[:one]
assert_equal "dos", hash2[:two]
end
def test_default_value_is_the_same_object
initial_value = []
hash = Hash.new(initial_value)
initial_value << "uno"
initial_value << "dos"
assert_equal ["uno", "dos"], hash[:one]
assert_equal ["uno", "dos"], hash[:two]
assert_equal ["uno", "dos"], hash[:three]
assert_equal true, hash[:one].object_id == hash[:two].object_id
end
def test_default_value_with_block
hash = Hash.new {|hash, key| hash[key] = [] }
hash[:one] << "uno"
hash[:two] << "dos"
assert_equal ["uno"], hash[:one]
assert_equal ["dos"], hash[:two]
assert_equal [], hash[:three]
end
def test_array_of_hashes
rides = [
{
:location => "Denver",
:distance => 40,
:avg_speed => 18,
:type => :road
},
{
location: "Golden",
distance: 15,
avg_speed: 11,
type: :mtb
},
{
location: "Buff Creezy",
distance: 13,
avg_speed: 9,
type: :mtb,
xtra_shit: 'booyah'
}
]
number_of_rides = rides.length
assert_equal number_of_rides, 3
# use a **loop** to add up all the distance
total_distance = 0
rides.each do |ride|
total_distance += ride[:distance]
end
assert_equal total_distance, 68
number_of_mtb_rides = "todo"
sum = 0
rides.each do |ride|
sum += ride [:mtb]
end
assert_equal number_of_mtb_rides, 2
end
total_mtb_distance = "todo"
assert_equal total_mtb_distance, 28
ride_locations = "todo"
# for rides.each |location|
# puts "location"
# end
assert_equal ride_locations, ["Denver", "Golden", "Buff Creezy"]
ride_types = "todo" #unique list of ride types, NO duplicates
assert_equal ride_types, [:mtb, :road]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment