PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/gems/facets-2.4.5/test/more/test_dictionary.rb

https://bitbucket.org/mediashelf/fedora-migrator
Ruby | 145 lines | 119 code | 19 blank | 7 comment | 0 complexity | 32a1fb5ce522a94982e5df07efe23a88 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, IPL-1.0, AGPL-1.0, LGPL-3.0
  1. # Test facets/dictionary.rb
  2. require 'facets/dictionary.rb'
  3. require 'test/unit'
  4. class TC_Dictionary < Test::Unit::TestCase
  5. def test_create
  6. d = Dictionary['z', 1, 'a', 2, 'c', 3]
  7. assert_equal( ['z','a','c'], d.keys )
  8. end
  9. def test_op_store
  10. d = Dictionary.new
  11. d['z'] = 1
  12. d['a'] = 2
  13. d['c'] = 3
  14. assert_equal( ['z','a','c'], d.keys )
  15. end
  16. def test_push
  17. d = Dictionary['a', 1, 'c', 2, 'z', 3]
  18. assert( d.push('end', 15) )
  19. assert_equal( 15, d['end'] )
  20. assert( ! d.push('end', 30) )
  21. assert( d.unshift('begin', 50) )
  22. assert_equal( 50, d['begin'] )
  23. assert( ! d.unshift('begin', 60) )
  24. assert_equal( ["begin", "a", "c", "z", "end"], d.keys )
  25. assert_equal( ["end", 15], d.pop )
  26. assert_equal( ["begin", "a", "c", "z"], d.keys )
  27. assert_equal( ["begin", 50], d.shift )
  28. end
  29. def test_insert
  30. # front
  31. d = Dictionary['a', 1, 'b', 2, 'c', 3]
  32. r = Dictionary['d', 4, 'a', 1, 'b', 2, 'c', 3]
  33. assert_equal( 4, d.insert(0,'d',4) )
  34. assert_equal( r, d )
  35. # back
  36. d = Dictionary['a', 1, 'b', 2, 'c', 3]
  37. r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
  38. assert_equal( 4, d.insert(-1,'d',4) )
  39. assert_equal( r, d )
  40. end
  41. def test_update
  42. # with other orderred hash
  43. d = Dictionary['a', 1, 'b', 2, 'c', 3]
  44. c = Dictionary['d', 4]
  45. r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
  46. assert_equal( r, d.update(c) )
  47. assert_equal( r, d )
  48. # with other hash
  49. d = Dictionary['a', 1, 'b', 2, 'c', 3]
  50. c = { 'd' => 4 }
  51. r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
  52. assert_equal( r, d.update(c) )
  53. assert_equal( r, d )
  54. end
  55. def test_merge
  56. # with other orderred hash
  57. d = Dictionary['a', 1, 'b', 2, 'c', 3]
  58. c = Dictionary['d', 4]
  59. r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
  60. assert_equal( r, d.merge(c) )
  61. # with other hash
  62. d = Dictionary['a', 1, 'b', 2, 'c', 3]
  63. c = { 'd' => 4 }
  64. r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
  65. assert_equal( r, d.merge(c) )
  66. end
  67. def test_order_by
  68. d = Dictionary['a', 3, 'b', 2, 'c', 1]
  69. d.order_by{ |k,v| v }
  70. assert_equal( [1,2,3], d.values )
  71. assert_equal( ['c','b','a'], d.keys )
  72. end
  73. def test_op_store_again
  74. d = Dictionary[]
  75. d[:a] = 1
  76. d[:c] = 3
  77. assert_equal( [1,3], d.values )
  78. d[:b,1] = 2
  79. assert_equal( [1,2,3], d.values )
  80. assert_equal( [:a,:b,:c], d.keys )
  81. end
  82. def test_reverse!
  83. d = Dictionary['z', 1, 'a', 2, 'c', 3]
  84. d.reverse!
  85. assert_equal( ['c','a','z'], d.keys )
  86. end
  87. def test_enumerable
  88. d = Dictionary[]
  89. d[:a] = "a"
  90. d[:c] = "b"
  91. assert_equal( ["A","B"], d.collect{|k,v| v.capitalize} )
  92. end
  93. def test_autohash
  94. d = Dictionary.new{ |hash,key| hash[key] = 0 }
  95. d[:a] = 0
  96. d[:b] += 1
  97. assert_equal([0, 1], d.values)
  98. assert_equal([:a,:b], d.keys)
  99. end
  100. def test_dup_with_array_values
  101. d = Dictionary.new
  102. d.dup
  103. d[:a]=['t',5]
  104. assert_equal(d, d.dup)
  105. end
  106. def test_first
  107. d = Dictionary[]
  108. d[:a] = "a"
  109. d[:b] = "b"
  110. d[:c] = "c"
  111. assert_equal( "a" , d.first )
  112. assert_equal( [] , d.first(0) )
  113. assert_equal( ["a"] , d.first(1) )
  114. assert_equal( ["a", "b"] , d.first(2) )
  115. end
  116. def test_last
  117. d = Dictionary[]
  118. d[:a] = "a"
  119. d[:b] = "b"
  120. d[:c] = "c"
  121. assert_equal( "c" , d.last )
  122. assert_equal( [] , d.last(0) )
  123. assert_equal( ["c"] , d.last(1) )
  124. assert_equal( ["b", "c"] , d.last(2) )
  125. end
  126. end