DEV Community

Discussion on: Daily Challenge #1 - String Peeler

Collapse
 
andrewbrown profile image
Andrew Brown 🇨🇦 • Edited

I am surprised no one wrote test code. Sometimes in interviews with challenge this simple and you have access to run ruby they are expecting to see test code. Check out my ruby test code tutorials buds.

I notice lots of people are raising an error instead of ignoring or returning null so they have failed the challenge's instructions.

require "test/unit"

# remove the first and last letter of a string
# if there is less than 2 characters return zero.
def quartered value
  raise ArgumentError, 'Argument is not a string' unless value.is_a? String
  return value unless value.size > 2
  value[0] = ''
  value.chop
end


class QaurteredTest < Test::Unit::TestCase
  def test_quartered
    assert_equal 'orl', quartered('world'), "quartered('world') should return a string called 'orl'"
  end

  def test_quartered_ignore
    assert_equal 'hi', quartered('hi'), "quartered('hi') should return 'hi'"
  end

  def test_quartered_invalid
    assert_raise_message('Argument is not a string', "quartered(2) should raise exception") do
      quartered(2)
    end
  end
end