Guest User

Untitled

a guest
Jul 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. ## lib/iteration.rb
  2.  
  3. # To not overload namespaces
  4. module Iteration
  5. # Add 2 to each element and return the new array.
  6. def add_2(array)
  7. array.map { |e| e+2 }
  8. end
  9.  
  10. # Delete all smaller than 4 and sort it from lowest to highest
  11. def del_sort(array)
  12. array.reject { |e| e<4 }.sort
  13. end
  14.  
  15. # To be able to call the methods via Iteration.method
  16. module_function :add_2, :del_sort
  17. end
  18.  
  19.  
  20. ## test/suite/lib/interation.rb
  21.  
  22. Baretest.suite "Hands-on" do
  23.  
  24. suite 'Iteration' do
  25.  
  26. setup :input => 1, "first example" do # input, variante 1 (kann irgend ein objekt sein, z.B. auch ein String)
  27. @input = [*0..10]
  28. end
  29.  
  30. setup :input => 2, "second example" do
  31. @input = [10, 2, 1, -2, 14, -1, 13, -4, 6, 8, -5, 3, 7, 12, 9, -3, 0, 15, 11, 5, 4]
  32. end
  33.  
  34. suite "#add2" do
  35. setup :input => 1 do # ergänze :input variante 1
  36. @result = [*2..12]
  37. end
  38.  
  39. setup :input => 2 do # ergänze :input variante 2
  40. @result = [12, 4, 3, 0, 16, 1, 15, -2, 8, 10, -3, 5, 9, 14, 11, -1, 2, 17, 13, 7, 6] # mit sort_by { rand } wüsst ich nicht wirklich wie...
  41. end
  42.  
  43. assert ":input" do
  44. @result == Iteration.add_2(@input)
  45. end
  46. end
  47.  
  48. suite "#del_sort" do
  49. setup :input => 1 do # ergänze :input variante 1
  50. @result = [*4..10]
  51. end
  52.  
  53. setup :input => 2 do # ergänze :input variante 2
  54. @result = [*4..15]
  55. end
  56.  
  57. assert "del_sort with :input" do
  58. @result_del_sort == Iteration.del_sort(@input)
  59. end
  60. end
  61. end
  62. end
Add Comment
Please, Sign In to add comment