Guest User

Untitled

a guest
Jun 18th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. module UnsortedList
  2.  
  3. def sort_ul(list)
  4. html = ""
  5. while list.size > 0
  6. row = list.shift
  7. link = get_link(row[:link])
  8. if list.size > 0
  9. if row[:nivel] == list[0][:nivel] #estamos en el mismo nivel?
  10. html << "<li>#{link}</li>"
  11. elsif row[:nivel] < list[0][:nivel] #estamos en un nivel superior?
  12. html << "<li>#{link}<ul>" + sort_ul(list) + "</ul></li>"
  13. else
  14. end
  15. else
  16. html << "<li>#{link}</li>"
  17. end
  18. end
  19. if row[:nivel] == 1
  20. "<ul>" + html + "</ul>"
  21. else
  22. html
  23. end
  24. end
  25.  
  26. def sort_ul2(list)
  27. current_level = 0
  28. html = ""
  29. list.each do |item|
  30. level, link = item[:nivel], item[:link]
  31. link = get_link(link)
  32. if level > current_level
  33. html<< "<ul><li>"
  34. html<< "#{link}"
  35. elsif level == current_level
  36. html<< "</li><li>#{link}"
  37. else
  38. (current_level - level).times { html<< "</li></ul>" }
  39. html<< "</li><li>#{link}"
  40. end
  41.  
  42. current_level = level
  43. end
  44.  
  45. # Close ULs left open
  46. current_level.times.each do
  47. html<< "</li></ul>"
  48. end
  49. html
  50. end
  51.  
  52. def get_link(link)
  53. "<a href='##{link.downcase}'>#{link}</a>"
  54. end
  55.  
  56. end
  57.  
  58. require 'test/unit'
  59.  
  60. class TestNestedList < Test::Unit::TestCase
  61.  
  62. include UnsortedList
  63.  
  64. def setup
  65. @simple = [{:nivel => 1, :link => "1"},{:nivel => 1, :link => "2"}]
  66. @simple_r = "<ul><li><a href='#1'>1</a></li><li><a href='#2'>2</a></li></ul>"
  67. @complex = [{:nivel => 1, :link => "1"},{:nivel => 2, :link => "1.1"},{:nivel => 3, :link => "1.1.1"}]
  68. @complex_r = "<ul><li><a href='#1'>1</a><ul><li><a href='#1.1'>1.1</a><ul><li><a href='#1.1.1'>1.1.1</a></li></ul></li></ul></li></ul>"
  69. @more_complex = [{:nivel => 1, :link => "1"},{:nivel => 2, :link => "1.1"},{:nivel => 2, :link => "1.2"},{:nivel => 3, :link => "1.2.1"},{:nivel => 3, :link => "1.2.2"},{:nivel => 1, :link => "2"}]
  70. @more_complex_r = "<ul><li><a href='#1'>1</a><ul><li><a href='#1.1'>1.1</a></li><li><a href='#1.2'>1.2</a><ul><li><a href='#1.2.1'>1.2.1</a></li><li><a href='#1.2.2'>1.2.2</a></li></ul></li></ul></li><li><a href='#2'>2</a></li></ul>"
  71. end
  72.  
  73. # prueba con sort_ul
  74. def test_it_should_return_simple_list
  75. assert_equal @simple_r, sort_ul(@simple)
  76. end
  77.  
  78. def test_it_should_return_nested_list
  79. assert @complex_r, sort_ul(@complex)
  80. end
  81.  
  82. def test_it_should_return_complex_nested_list
  83. assert @more_complex_r, sort_ul(@more_complex)
  84. end
  85.  
  86. # prueba con sort_ul2
  87. def test_it_should_return_simple_list2
  88. assert_equal @simple_r, sort_ul2(@simple)
  89. end
  90.  
  91. def test_it_should_return_nested_list2
  92. assert @complex_r, sort_ul2(@complex)
  93. end
  94.  
  95. def test_it_should_return_complex_nested_list2
  96. assert @more_complex_r, sort_ul2(@more_complex)
  97. end
  98.  
  99. end
Add Comment
Please, Sign In to add comment