Skip to content

Instantly share code, notes, and snippets.

@aarontc
Last active February 19, 2016 19:19
Show Gist options
  • Save aarontc/d549ee4a82d21d263c9b to your computer and use it in GitHub Desktop.
Save aarontc/d549ee4a82d21d263c9b to your computer and use it in GitHub Desktop.
numeric?
require 'minitest'
require 'minitest/autorun'
require 'minitest/pride'
class TestNumeric < Minitest::Test
def numeric?(arg)
!/\A[+-]?\d+\z/.match(arg.to_s).nil?
end
def test_int
(1..1_000_000).each do |i|
assert numeric?(i)
assert numeric?("+#{i}")
assert numeric?("-#{i}")
end
end
def test_float
refute numeric?(1.2)
refute numeric?(12.2)
refute numeric?(1.32)
refute numeric?('15.2')
refute numeric?(131.12)
refute numeric?(12_222_222_222_222.21232)
refute numeric?(111.2331)
end
def test_string
assert numeric?('0')
assert numeric?('1')
assert numeric?('121234')
end
def test_special_cases
actual = numeric?('/jobs/12g34/new/')
refute actual
assert_kind_of FalseClass, actual
end
def test_with_sign
assert numeric?(-12)
assert numeric?(12)
assert numeric?(+12)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment