Skip to content

Commit

Permalink
Merge pull request #33 from vikram7/vr-random-hash
Browse files Browse the repository at this point in the history
add tests & refactor
  • Loading branch information
vikram7 committed Feb 14, 2016
2 parents d0abfba + 2b752d3 commit a209965
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 18 deletions.
27 changes: 9 additions & 18 deletions lib/rubies/random_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,16 @@ def deep_traverse(&block)

class RandomHash < MyHash
def initialize
@ds = MyHash.new
@data_structure = MyHash.new
end

def children
array = Array.new
rand(1..3).times do
array << Faker::Name.first_name
end
array
rand = rand(1..3)
Array.new(rand) { Faker::Name.first_name }
end

def has_kids?
rand(2) == 1
[true, false].sample
end

def hash_one
Expand All @@ -51,24 +48,18 @@ def hash_two

def hash_three
hash = MyHash.new
length = rand(1..5)
count = 1
while count <= length
rand(1..5).times do
details = Hash.new
name = Faker::Name.name
phone = Faker::PhoneNumber.cell_phone
company = Faker::Company.name
details["phone"] = phone
details["company"] = company
details["phone"] = Faker::PhoneNumber.cell_phone
details["company"] = Faker::Company.name
details["children"] = children if has_kids?
hash[name] = details
count += 1
hash[Faker::Name.name] = details
end
hash
end

def generate
@ds = [hash_one, hash_two, hash_three].sample
@data_structure = [hash_one, hash_two, hash_three].sample
end
end
end
72 changes: 72 additions & 0 deletions spec/rubies/random_hash_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require "spec_helper"

module Rubies
describe RandomHash do
before do
@random_hash = Rubies::RandomHash.new
end

it "#children returns an array" do
children = @random_hash.children
expect(children).to be_instance_of(Array)
end

it "#children returns an array of names" do
children = @random_hash.children.map(&:class).uniq
expect(children.length).to eq(1)
expect(children).to eq([String])
end

it "#has_kids? returns a boolean" do
expect([true, false]).to include @random_hash.has_kids?
end

it "#hash_one returns a hash" do
hash_one = @random_hash.hash_one
expect(hash_one).to be_instance_of(Hash)
end

it "#hash_one returns a hash of 10 elements" do
hash_one = @random_hash.hash_one
expect(hash_one.count).to eq(10)
end

it "#hash_one returns hash with String keys" do
hash_one = @random_hash.hash_one
keys = hash_one.keys
expect(keys.map(&:class).uniq.length).to eq(1)
expect(keys.map(&:class).uniq).to eq([String])
end

it "#hash_one returns hash with String values" do
hash_one = @random_hash.hash_one
values = hash_one.values
expect(values.map(&:class).uniq.length).to eq(1)
expect(values.map(&:class).uniq).to eq([String])
end

it "#hash_two returns a hash of 10 elements" do
hash_two = @random_hash.hash_two
expect(hash_two.count).to eq(10)
end

it "#hash_two returns hash with String keys" do
hash_two = @random_hash.hash_two
keys = hash_two.keys
expect(keys.map(&:class).uniq.length).to eq(1)
expect(keys.map(&:class).uniq).to eq([String])
end

it "#hash_one returns hash with Fixnum values" do
hash_two = @random_hash.hash_two
values = hash_two.values
expect(values.map(&:class).uniq.length).to eq(1)
expect(values.map(&:class).uniq).to eq([Fixnum])
end

it "#hash_three returns an instance of MyHash" do
hash_three = @random_hash.hash_three
expect(hash_three).to be_instance_of(Rubies::MyHash)
end
end
end

0 comments on commit a209965

Please sign in to comment.