Created
January 18, 2009 19:25
-
-
Save vinbarnes/48750 to your computer and use it in GitHub Desktop.
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 'test/unit' | |
module ArrayExtension | |
def superjoin(args={}) | |
start = args[:start] | |
before = args[:before] | |
joiner = args[:join] | |
after = args[:after] | |
ender = args[:end] | |
"#{start}#{self.map {|e| "#{before}#{e}#{after}"}.join(joiner)}#{ender}" | |
end | |
end | |
class ArrayExtensionTest < Test::Unit::TestCase | |
def setup | |
Array.send(:include, ArrayExtension) | |
@array = %w[a b c] | |
end | |
def test_superjoin_should_accept_no_arguments | |
assert_nothing_raised(ArgumentError) { @array.superjoin() } | |
end | |
def test_superjoin_without_arguments_should_return_a_flattened_string | |
assert_equal('abc', @array.superjoin()) | |
end | |
def test_superjoin_should_accept_before_argument | |
assert_nothing_raised(ArgumentError) { @array.superjoin(:before => '*') } | |
end | |
def test_before_argument_should_prepend_string_to_each_element | |
assert_equal('*a*b*c', @array.superjoin(:before => '*')) | |
end | |
def test_superjoin_should_accept_after_argument | |
assert_nothing_raised(ArgumentError) { @array.superjoin(:after => '~') } | |
end | |
def test_after_argument_should_append_string_to_each_element | |
assert_equal('a~b~c~', @array.superjoin(:after => '~')) | |
end | |
def test_superjoin_should_accept_join_argument | |
assert_nothing_raised(ArgumentError) { @array.superjoin(:join => '-') } | |
end | |
def test_join_argument_should_append_string_between_each_element | |
assert_equal('a-b-c', @array.superjoin(:join => '-')) | |
end | |
def test_superjoin_should_accept_start_argument | |
assert_nothing_raised(ArgumentError) { @array.superjoin(:start => '<') } | |
end | |
def test_start_argument_should_prepend_string_before_all_others | |
assert_equal('<abc', @array.superjoin(:start => '<')) | |
end | |
def test_superjoin_should_accept_end_argument | |
assert_nothing_raised(ArgumentError) { @array.superjoin(:end => '>') } | |
end | |
def test_end_argument_should_append_string_after_all_others | |
assert_equal('abc>', @array.superjoin(:end => '>')) | |
end | |
def test_superjoin_should_correctly_format_string_with_all_arguments_present | |
args = { | |
:start => '<', | |
:before => '*', | |
:join => '-', | |
:after => '~', | |
:end => '>' | |
} | |
assert_equal('<*a~-*b~-*c~>', @array.superjoin(args)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment