Skip to content

Instantly share code, notes, and snippets.

@jeremyf
Created September 18, 2015 19:53
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 jeremyf/0e88e4bd5f12a895264e to your computer and use it in GitHub Desktop.
Save jeremyf/0e88e4bd5f12a895264e to your computer and use it in GitHub Desktop.
class Numbers
def initialize(list)
@list = list
end
attr_reader :list
def includes?(a_number)
return true if list.detect { |element| element.to_i == a_number.to_i }
return false
end
end
require 'minitest/autorun'
class NumbersTest < MiniTest::Test
def test_it_will_return_true_if_user_prompted_number_is_in_initial_list
list_of_numbers = [1,2,3,4,5,6,7,8,9,10]
numbers = Numbers.new(list_of_numbers)
assert_equal(true, numbers.includes?(2))
end
def test_it_will_return_false_if_the_user_prompted_number_is_not_in_initial_list
list_of_numbers = [1,2,3,4,5,6,7,8,9,10]
numbers = Numbers.new(list_of_numbers)
assert_equal(false, numbers.includes?(20))
end
def test_floats_count_in_the_includes
list_of_numbers = [1,2,3,4,5,6,7,8,9,10]
numbers = Numbers.new(list_of_numbers)
assert_equal(true, numbers.includes?(2.0))
end
def test_floats_that_differ_in_the_thousandths_count_in_the_includes
list_of_numbers = [1,2,3,4,5,6,7,8,9,10]
numbers = Numbers.new(list_of_numbers)
assert_equal(true, numbers.includes?(2.001))
end
def test_floats_that_differ_in_the_thousandths_count_in_the_includes_scenario_2
list_of_numbers = [1,2,3,4,5,6,7,8,9,10]
numbers = Numbers.new(list_of_numbers)
assert_equal(true, numbers.includes?(10.005))
end
def test_it_will_fail_when_comparing_a_word
list_of_numbers = [1,2,3,4,5,6,7,8,9,10]
numbers = Numbers.new(list_of_numbers)
assert_equal(false, numbers.includes?("ten"))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment