Skip to content

Instantly share code, notes, and snippets.

@jayzz55
Forked from mattswann/Allergies_test.rb
Last active August 29, 2015 14:21
Show Gist options
  • Save jayzz55/361861737a32f18be5d2 to your computer and use it in GitHub Desktop.
Save jayzz55/361861737a32f18be5d2 to your computer and use it in GitHub Desktop.

Allergies

An allergy test produces a single numeric score which contains the information about all the allergies the person has (that they were tested for).

The list of items (and their value) that were tested are:

  • eggs (1)
  • peanuts (2)
  • shellfish (4)
  • strawberries (8)
  • tomatoes (16)
  • chocolate (32)
  • pollen (64)
  • cats (128)

So if Tom is allergic to peanuts and chocolate, he gets a score of 34.

Write a program that, given a person's score can tell them - use the test file attached to verify your results.

a) whether or not they're allergic to a given item, and b) the full list of allergies.

allergies = Allergies.new(34)
allergies.allergic_to?('chocolate')
=> true
allergies.allergic_to?('cats')
=> false
allergies.list
=> ['peanuts', 'chocolate']
require 'minitest/autorun'
require 'minitest/reporters' # optional
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new() # optional
require_relative './allergies'
class AllergiesTest < MiniTest::Test
def test_no_allergies_at_all
allergies = Allergies.new(0)
assert_equal [], allergies.list
end
def test_allergic_to_just_eggs
allergies = Allergies.new(1)
assert_equal ['eggs'], allergies.list
end
def test_allergic_to_just_peanuts
allergies = Allergies.new(2)
assert_equal ['peanuts'], allergies.list
end
def test_allergic_to_just_strawberries
allergies = Allergies.new(8)
assert_equal ['strawberries'], allergies.list
end
def test_allergic_to_eggs_and_peanuts
allergies = Allergies.new(3)
assert_equal ['eggs', 'peanuts'], allergies.list
end
def test_allergic_to_more_than_eggs_but_not_peanuts
allergies = Allergies.new(5)
assert_equal ['eggs', 'shellfish'], allergies.list
end
def test_allergic_to_lots_of_stuff
allergies = Allergies.new(248)
assert_equal ["strawberries", "tomatoes", "chocolate", "pollen", "cats"], allergies.list
end
def test_allergic_to_everything
allergies = Allergies.new(255)
assert_equal ["eggs", "peanuts", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats"], allergies.list
end
def test_no_allergies_means_not_allergic
allergies = Allergies.new(0)
assert !allergies.allergic_to?('peanuts')
assert !allergies.allergic_to?('cats')
assert !allergies.allergic_to?('strawberries')
end
def test_is_allergic_to_eggs
allergies = Allergies.new(1)
assert allergies.allergic_to?('eggs')
end
def test_allergic_to_eggs_in_addition_to_other_stuff
allergies = Allergies.new(5)
assert allergies.allergic_to?('eggs')
end
def test_ignore_non_allergen_score_parts
allergies = Allergies.new(509)
assert_equal ["eggs", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats"], allergies.list
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment