Created
April 7, 2018 06:52
-
-
Save thelastinuit/f068a95df13cfa9bd263a747e54a97f6 to your computer and use it in GitHub Desktop.
citrusbyte
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
require "test/unit" | |
module ArrayFunctions | |
class << self | |
def flatten(array, flatten_array = []) | |
raise "Nothing to do" unless array | |
array.each do |element| | |
if element.is_a? Integer | |
flatten_array << element | |
elsif element.is_a? Array | |
flatten element, flatten_array | |
else | |
raise "Not a valid object" | |
end | |
end | |
flatten_array | |
end | |
end | |
end | |
class TestArrayFunctions < Test::Unit::TestCase | |
def test_simple_nesting | |
assert_equal [1,2], ArrayFunctions.flatten([1,[2]]) | |
end | |
def test_simple_nesting_with_empty_elements | |
assert_equal [1,2], ArrayFunctions.flatten([1,[2],[],[[]]]) | |
end | |
def test_a_bit_complex_nesting_with_empty_elements | |
assert_equal [1,2,3], ArrayFunctions.flatten([1,[2],[],[[3]]]) | |
end | |
def test_an_apparent_filled_array | |
assert_equal [], ArrayFunctions.flatten([[],[],[],[[]]]) | |
end | |
def test_deep_nesting | |
assert_equal [1,2,3,4,5,6,7], ArrayFunctions.flatten([1,[2,[3,4],5],6,[7]]) | |
end | |
def test_nil_object | |
assert_raise("Nothing to do") { ArrayFunctions.flatten(nil) } | |
end | |
def test_not_a_valid_object | |
assert_raise("Not a valid object") { ArrayFunctions.flatten([2, {}]) } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment