PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/gems/facets-2.4.5/test/lore/test_ostruct.rb

https://bitbucket.org/mediashelf/fedora-migrator
Ruby | 171 lines | 144 code | 26 blank | 1 comment | 1 complexity | 6bda89de1ca805ac36338d341cb125af MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, IPL-1.0, AGPL-1.0, LGPL-3.0
  1. # Test facets/ostruct.rb
  2. require 'facets/ostruct.rb'
  3. require 'test/unit'
  4. class TestOpenStruct < Test::Unit::TestCase
  5. def setup
  6. @o = OpenStruct.new(:q => 1)
  7. end
  8. def test_update
  9. o = OpenStruct.new( { :a => 1 } )
  10. h = { :b => 2 }
  11. assert_nothing_raised { o.__update__( h ) }
  12. assert_equal( 2, o.b )
  13. end
  14. def test_merge_1
  15. o = OpenStruct.new( { :a => 1 } )
  16. h = { :b => 2 }
  17. q = o.__merge__( h )
  18. assert_equal( 1, q.a )
  19. assert_equal( 2, q.b )
  20. end
  21. def test_merge_2
  22. o1 = OpenStruct.new( { :a => 1 } )
  23. o2 = OpenStruct.new( { :b => 2 } )
  24. q = o1.__merge__( o2 )
  25. assert_equal( 1, q.a )
  26. assert_equal( 2, q.b )
  27. end
  28. def test_store
  29. @o.instance_delegate.store(:a,1)
  30. assert_equal( 1, @o.a )
  31. end
  32. def test_update_using_delegate
  33. @o.instance_delegate.update(:a=>1)
  34. assert_equal( 1, @o.a )
  35. end
  36. def test_op_fetch
  37. o = OpenStruct.new( { :a => 1 } )
  38. assert_equal( 1, o[:a] )
  39. end
  40. def test_op_store_nothing_raised
  41. o = OpenStruct.new( { :a => 1 } )
  42. assert_nothing_raised { o[:b] = 2 }
  43. assert_equal( 2, o.b )
  44. end
  45. def test_update_nothing_raised
  46. o = OpenStruct.new( { :a => 1 } )
  47. h = { :b => 2 }
  48. assert_nothing_raised { o.__update__( h ) }
  49. assert_equal( 2, o.b )
  50. end
  51. def test_merge_with_hash
  52. o = OpenStruct.new( { :a => 1 } )
  53. h = { :b => 2 }
  54. q = o.__merge__( h )
  55. assert_equal( 1, q.a )
  56. assert_equal( 2, q.b )
  57. end
  58. def test_merge_two_open_structs
  59. o1 = OpenStruct.new( { :a => 1 } )
  60. o2 = OpenStruct.new( { :b => 2 } )
  61. q = o1.__merge__( o2 )
  62. assert_equal( 1, q.a )
  63. assert_equal( 2, q.b )
  64. end
  65. end
  66. class TestOpenStructInitialize < Test::Unit::TestCase
  67. class Person < OpenStruct; end
  68. def test_1_old_functionality
  69. o = OpenStruct.new
  70. assert_nil(o.foo)
  71. o.foo = :bar
  72. assert_equal(:bar, o.foo)
  73. o.delete_field(:foo)
  74. assert_nil(o.foo)
  75. o1 = OpenStruct.new(:x => 1, :y => 2)
  76. assert_equal(1, o1.x)
  77. assert_equal(2, o1.y)
  78. o2 = OpenStruct.new(:x => 1, :y => 2)
  79. assert(o1 == o2)
  80. end
  81. def test_2_new_functionality
  82. person = OpenStruct.new do |p|
  83. p.name = 'John Smith'
  84. p.gender = :M
  85. p.age = 71
  86. end
  87. assert_equal('John Smith', person.name)
  88. assert_equal(:M, person.gender)
  89. assert_equal(71, person.age)
  90. assert_equal(nil, person.address)
  91. person = OpenStruct.new(:gender => :M, :age => 71) do |p|
  92. p.name = 'John Smith'
  93. end
  94. assert_equal('John Smith', person.name)
  95. assert_equal(:M, person.gender)
  96. assert_equal(71, person.age)
  97. assert_equal(nil, person.address)
  98. end
  99. def test_3_subclass
  100. person = Person.new do |p|
  101. p.name = 'John Smith'
  102. p.gender = :M
  103. p.age = 71
  104. end
  105. assert_equal('John Smith', person.name)
  106. assert_equal(:M, person.gender)
  107. assert_equal(71, person.age)
  108. assert_equal(nil, person.address)
  109. person = Person.new(:gender => :M, :age => 71) do |p|
  110. p.name = 'John Smith'
  111. end
  112. assert_equal('John Smith', person.name)
  113. assert_equal(:M, person.gender)
  114. assert_equal(71, person.age)
  115. assert_equal(nil, person.address)
  116. end
  117. end
  118. class TestHashToOpenStruct < Test::Unit::TestCase
  119. def test_to_ostruct
  120. a = { :a => 1, :b => 2, :c => 3 }
  121. ao = a.to_ostruct
  122. assert_equal( a[:a], ao.a )
  123. assert_equal( a[:b], ao.b )
  124. assert_equal( a[:c], ao.c )
  125. end
  126. def test_to_ostruct_recurse
  127. a = { :a => 1, :b => 2, :c => { :x => 4 } }
  128. ao = a.to_ostruct_recurse
  129. assert_equal( a[:a], ao.a )
  130. assert_equal( a[:b], ao.b )
  131. assert_equal( a[:c][:x], ao.c.x )
  132. end
  133. def test_to_ostruct_recurse_with_recursion
  134. a = {}
  135. a[:a] = a
  136. ao = a.to_ostruct_recurse
  137. assert_equal( ao, ao.a )
  138. end
  139. def test_to_ostruct_advanced
  140. h = { 'a' => { 'b' => 1 } }
  141. o = h.to_ostruct_recurse( { h['a'] => h['a'] } )
  142. assert_equal( 1, o.a['b'] )
  143. assert( Hash === o.a )
  144. end
  145. end