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

Language Ruby Lines 172
MD5 Hash 6bda89de1ca805ac36338d341cb125af Estimated Cost $3,531 (why?)
Repository https://bitbucket.org/mediashelf/fedora-migrator View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Test facets/ostruct.rb

require 'facets/ostruct.rb'
require 'test/unit'

class TestOpenStruct < Test::Unit::TestCase

  def setup
    @o = OpenStruct.new(:q => 1)
  end

  def test_update
    o = OpenStruct.new( { :a => 1 } )
    h = { :b => 2 }
    assert_nothing_raised { o.__update__( h ) }
    assert_equal( 2, o.b )
  end

  def test_merge_1
    o = OpenStruct.new( { :a => 1 } )
    h = { :b => 2 }
    q = o.__merge__( h )
    assert_equal( 1, q.a )
    assert_equal( 2, q.b )
  end

  def test_merge_2
    o1 = OpenStruct.new( { :a => 1 } )
    o2 = OpenStruct.new( { :b => 2 } )
    q = o1.__merge__( o2 )
    assert_equal( 1, q.a )
    assert_equal( 2, q.b )
  end

  def test_store
    @o.instance_delegate.store(:a,1)
    assert_equal( 1, @o.a )
  end

  def test_update_using_delegate
    @o.instance_delegate.update(:a=>1)
    assert_equal( 1, @o.a )
  end

  def test_op_fetch
    o = OpenStruct.new( { :a => 1 } )
    assert_equal( 1, o[:a] )
  end

  def test_op_store_nothing_raised
    o = OpenStruct.new( { :a => 1 } )
    assert_nothing_raised { o[:b] = 2 }
    assert_equal( 2, o.b )
  end

  def test_update_nothing_raised
    o = OpenStruct.new( { :a => 1 } )
    h = { :b => 2 }
    assert_nothing_raised { o.__update__( h ) }
    assert_equal( 2, o.b )
  end

  def test_merge_with_hash
    o = OpenStruct.new( { :a => 1 } )
    h = { :b => 2 }
    q = o.__merge__( h )
    assert_equal( 1, q.a )
    assert_equal( 2, q.b )
  end

  def test_merge_two_open_structs
    o1 = OpenStruct.new( { :a => 1 } )
    o2 = OpenStruct.new( { :b => 2 } )
    q = o1.__merge__( o2 )
    assert_equal( 1, q.a )
    assert_equal( 2, q.b )
  end

end

class TestOpenStructInitialize < Test::Unit::TestCase

  class Person < OpenStruct; end

  def test_1_old_functionality
    o = OpenStruct.new
    assert_nil(o.foo)
    o.foo = :bar
    assert_equal(:bar, o.foo)
    o.delete_field(:foo)
    assert_nil(o.foo)
    o1 = OpenStruct.new(:x => 1, :y => 2)
    assert_equal(1, o1.x)
    assert_equal(2, o1.y)
    o2 = OpenStruct.new(:x => 1, :y => 2)
    assert(o1 == o2)
  end

  def test_2_new_functionality
    person = OpenStruct.new do |p|
      p.name = 'John Smith'
      p.gender  = :M
      p.age     = 71
    end
    assert_equal('John Smith', person.name)
    assert_equal(:M, person.gender)
    assert_equal(71, person.age)
    assert_equal(nil, person.address)
    person = OpenStruct.new(:gender => :M, :age => 71) do |p|
      p.name = 'John Smith'
    end
    assert_equal('John Smith', person.name)
    assert_equal(:M, person.gender)
    assert_equal(71, person.age)
    assert_equal(nil, person.address)
  end

  def test_3_subclass
    person = Person.new do |p|
      p.name = 'John Smith'
      p.gender  = :M
      p.age     = 71
    end
    assert_equal('John Smith', person.name)
    assert_equal(:M, person.gender)
    assert_equal(71, person.age)
    assert_equal(nil, person.address)
    person = Person.new(:gender => :M, :age => 71) do |p|
      p.name = 'John Smith'
    end
    assert_equal('John Smith', person.name)
    assert_equal(:M, person.gender)
    assert_equal(71, person.age)
    assert_equal(nil, person.address)
  end
end

class TestHashToOpenStruct < Test::Unit::TestCase

  def test_to_ostruct
    a = { :a => 1, :b => 2, :c => 3 }
    ao = a.to_ostruct
    assert_equal( a[:a], ao.a )
    assert_equal( a[:b], ao.b )
    assert_equal( a[:c], ao.c )
  end

  def test_to_ostruct_recurse
    a = { :a => 1, :b => 2, :c => { :x => 4 } }
    ao = a.to_ostruct_recurse
    assert_equal(  a[:a], ao.a )
    assert_equal(  a[:b], ao.b )
    assert_equal(  a[:c][:x], ao.c.x )
  end

  def test_to_ostruct_recurse_with_recursion
    a = {}
    a[:a] = a
    ao = a.to_ostruct_recurse
    assert_equal( ao, ao.a )
  end

  def test_to_ostruct_advanced
    h = { 'a' => { 'b' => 1 } }
    o = h.to_ostruct_recurse( { h['a'] => h['a'] } )
    assert_equal( 1, o.a['b'] )
    assert( Hash === o.a )
  end

end
Back to Top