Created
February 25, 2015 02:40
-
-
Save jesseract/5b141eef3e5a54735c86 to your computer and use it in GitHub Desktop.
Split test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'minitest/autorun' | |
require 'minitest/pride' | |
# Write two methods: | |
# | |
# * `first_name`: given a name in string, return the first name. | |
# * `last_name`: given a name in string, return the last name. | |
# WRITE YOUR CODE HERE. | |
def first_name(name) | |
array = name.split(" ") | |
if array.length <= 1 | |
"" | |
elsif array.length == 2 | |
array.first | |
else array.length >= 2 | |
end | |
end | |
def last_name(name) | |
name.split(" ").last | |
end | |
class StringSplitChallenge < MiniTest::Test | |
def test_first_name | |
assert_equal "Mason", first_name("Mason Matthews") | |
end | |
def test_last_name | |
assert_equal "Matthews", last_name("Mason Matthews") | |
end | |
def test_one_word_name | |
assert_equal "", first_name("deadmou5") | |
assert_equal "deadmou5", last_name("deadmou5") | |
end | |
def test_three_word_name | |
assert_equal "John Quincy", first_name("John Quincy Adams") | |
assert_equal "Adams", last_name("John Quincy Adams") | |
end | |
def test_no_word_name | |
assert_equal "", first_name("") | |
assert_equal "", last_name("") | |
end | |
def test_nil_name | |
assert_equal "", first_name(nil) | |
assert_equal "", last_name(nil) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment