Guest User

Untitled

a guest
Jul 16th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. require 'test/unit'
  2. require 'flatten'
  3.  
  4. # This test may be run in three modes:
  5. # 1) With no arguments, tests the built-in Array method flatten replaced with Flatten module implementation
  6. # 2) With a single argument 'newclass', tests a subclass of Array with Flatten module implementation
  7. # 2) With a single argument 'original', tests the built-in Array flatten method
  8. if ARGV[0] == 'newclass'
  9. class MyArray < Array
  10. include Flatten
  11. end
  12. CLASS = MyArray
  13. elsif ARGV[0] == 'original'
  14. CLASS = Array
  15. else
  16. class Array
  17. include Flatten
  18. end
  19. CLASS = Array
  20. end
  21.  
  22. puts "Working with array like class: #{CLASS.name}"
  23.  
  24. class FlattenTest < Test::Unit::TestCase
  25.  
  26. def test_one_level
  27. assert_equal [1,2,3], CLASS.new([1,[2,3]]).flatten
  28. end
  29.  
  30. def test_flatten_keeps_class
  31. my_array = CLASS.new([1,[2,3],4])
  32. flattened = my_array.flatten
  33. assert_equal my_array.class, flattened.class
  34. assert_equal CLASS.name, flattened.class.name
  35. assert_equal flattened, [1,2,3,4]
  36. end
  37.  
  38. def test_flatten_many_levels
  39. assert_equal [0,0,0,1,2,3,4,5,6,7,8,9,10,11,12], CLASS.new([0,0,0,[1,[2,3],4],5,[[[[[[6,7]],8]],9],10,11],12]).flatten
  40. end
  41.  
  42. def test_flatten_self_referencing_fails
  43. a = CLASS.new([1,2])
  44. b = CLASS.new([3,4,a])
  45. a << b
  46. assert_raises(ArgumentError) { a.flatten }
  47. end
  48.  
  49. end
Add Comment
Please, Sign In to add comment