Created
October 25, 2012 03:12
-
-
Save flakyfilibuster/3950231 to your computer and use it in GitHub Desktop.
switcharoo_test
This file contains 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
# run with: ruby switcharoo_test.rb | |
require 'minitest/autorun' | |
def switcharoo(string, switch_point, delimiter=false) | |
if switch_point < 0 | |
raise "switch_point too small" | |
elsif switch_point > string.size | |
raise "switch_point too big" | |
end | |
# first = string.slice(0...switch_point) | |
# second = string.slice(switch_point..-1) | |
# lasse | |
# if delimiter == true | |
# string.slice(switch_point..-1) << "|" + string.slice(0...switch_point) | |
# elsif delimiter == false | |
# string.slice(switch_point..-1) + string.slice(0...switch_point) | |
# end | |
# ferdi | |
# delimiter ? (second + "|" + first) : (second + first) | |
# ben | |
# second += "|" if delimiter | |
# second + first | |
if delimiter | |
second + "|" + first | |
else | |
second + first | |
end | |
end | |
class SwitcharooTest < MiniTest::Unit::TestCase | |
def test_switch_in_the_middle | |
switch_string = switcharoo("pdqxyz", 3) | |
assert_equal("xyzpdq", switch_string) | |
end | |
def test_switch_point_too_big | |
begin | |
result = switcharoo("pdqxyz", 99) | |
assert false, "switcharoo returned a result of #{result}" | |
rescue => errormessage #Exception | |
puts errormessage.message | |
assert_equal("switch_point too big", errormessage.message) | |
end | |
end | |
def test_switch_point_too_small | |
begin | |
result = switcharoo("pdqxyz", -99) | |
assert false, "switcharoo returned a result of #{result}" | |
rescue => errormessage #Exception | |
puts errormessage.message | |
assert_equal("switch_point too small", errormessage.message) | |
end | |
end | |
def test_pipe_added_when_delimiter_true | |
switch_string = switcharoo("abcd", 1, true) | |
assert_equal("bcd|a", switch_string) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment