Class: MobyBase::StateObject

Inherits:
Object
  • Object
show all
Includes:
MobyBehaviour::ObjectComposition
Defined in:
lib/tdriver/base/state_object.rb

Overview

Static representation of the state of a TestObject or SUT. StateObject are not refreshed or synchronize etc.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MobyBehaviour::ObjectComposition

#add_child, #add_parent, #remove_child, #remove_parent

Constructor Details

#initialize(*options) ⇒ StateObject

Creation of a new StateObject from source data.

params

options

Hash containing source data describing the object and all other required configuration values e.g. test object factory, -adapter etc.

returns

StateObject

new StateObject instance

raises



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
# File 'lib/tdriver/base/state_object.rb', line 53

def initialize( *options )

  # clone original options array; array is modified below
  options = options.clone

  # determine is method called with new or deprecated API
  
  if options.count == 1 and options.first.kind_of?( Hash )

    # retrieve first array element
    options = options.shift

    # verify options argument type
    options.check_type Hash, 'wrong argument type $1 for StateObject options (expected $2)'

    # verify that :source_data key exists in hash       
    source_data = options.require_key :source_data

    # retrieve reference to parent object
    parent = options[ :parent ]

    # retrieve reference to test object adapter
    test_object_adapter = options[ :test_object_adapter ]
  
  else
  
    # print warning if deprecated API is used
    warn_caller '$1:$2 warning: deprecated API; use hash with :source_data, :parent, :test_object_adapter as argument instead of StateObject.new( source_data, parent, test_object_adapter )'

    # retrieve source data
    source_data = options.shift

    # retrieve reference to parent object
    parent = options.shift

    # retrieve reference to test object adapter
    test_object_adapter = options.shift

  end

  # verify that parent argument type is correct
  parent.check_type [ NilClass, MobyBase::StateObject, MobyBase::TestObject, MobyBase::SUT ], 'wrong argument type $1 for parent object (expected $2)'

  # verify that test object adapter argument type is correct
  test_object_adapter.check_type [ NilClass, Class ], 'wrong argument type $1 for test object adapter (expected $2)'

  # verify that source data argument type is correct
  source_data.check_type [ String, MobyUtil::XML::Element ], 'wrong argument type $1 for source data (expected $2)'

  # parse source data if given argument is type of string
  source_data = MobyUtil::XML.parse_string( source_data ).root if source_data.kind_of?( String )

  # store reference to parent object
  @parent = parent

  # store reference to test object adapter
  if test_object_adapter.nil?

    if @parent.kind_of?( MobyBase::SUT )

      @test_object_adapter = @parent.instance_variable_get( :@test_object_adapter )

    else

      # Load the new xml only, so old is not supported
      @test_object_adapter = TDriver::OptimizedXML::TestObjectAdapter

    end

  else

    @test_object_adapter = test_object_adapter

  end

  # retrieve object attributes
  method( :xml_data= ).call( source_data )

  # initialize child objects cache for state object
  @child_object_cache = TDriver::TestObjectCache.new

  # create accessor methods for any child state objects.
  @test_object_adapter.create_child_accessors!( source_data, self )

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *method_arguments) ⇒ Object

Tries to use the missing method id as a child object type and find an object based on it



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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/tdriver/base/state_object.rb', line 140

def method_missing( method_id, *method_arguments )

  rules_hash = method_arguments.first

  rules_hash = Hash.new unless rules_hash.kind_of? Hash

  rules_hash[ :type ] = method_id 

  begin

    return child( rules_hash )

  rescue MobyBase::TestObjectNotFoundError, MobyBase::TestObjectNotVisibleError

    rules_hash_clone = rules_hash.clone
    
    rules_hash_clone.delete( :type )

    # string representation of used rule hash
    search_attributes_string = rules_hash_clone.collect{ | key, value | ":#{ key } => #{ value.inspect }" }.join( ', ')

    # construct literal representation of object identifiers        
    object_attributes = []        
    object_attributes << "id: #{ @id }" if @id
    object_attributes << "type: #{ @type.inspect }" if @type
    object_attributes << "name: #{ @name.inspect }" if @name
    
    if search_attributes_string.empty?
    
      # do not show any attribute details if none given                
      search_attributes_string = ""
      
    else
  
      # show used attributes      
      search_attributes_string = " (attributes #{ search_attributes_string })"

    end

    # raise exception
    raise MobyBase::TestObjectNotFoundError.new(
      "The state object (#{ object_attributes.join(", ") }) has no child object with type or behaviour method with name #{ method_id.to_s.inspect }#{ search_attributes_string }" 
    )

  end

end

Instance Attribute Details

#idObject (readonly)

TODO: document me



41
42
43
# File 'lib/tdriver/base/state_object.rb', line 41

def id
  @id
end

#nameObject (readonly)

TODO: document me



41
42
43
# File 'lib/tdriver/base/state_object.rb', line 41

def name
  @name
end

#parentObject

description

attr_accessor

returns

Test Object

description: test object that was used as parent when this object was created. Can also be of type SUT if sut was the parent (ie. application objects)
example: "@sut"


38
39
40
# File 'lib/tdriver/base/state_object.rb', line 38

def parent
  @parent
end

#typeObject (readonly)

TODO: document me



41
42
43
# File 'lib/tdriver/base/state_object.rb', line 41

def type
  @type
end

Instance Method Details

#==(test_object) ⇒ Object

Verifies that another StateObject contains the same data as this object. type, id and name must match.

param

other_state_object

StateObject that this object is compared to.

returns

true

The other StateObject contains the same data as this one.

false

The other StateObject does notcontain the same data as this one.

raises

nothing



198
199
200
201
202
203
# File 'lib/tdriver/base/state_object.rb', line 198

def ==( test_object )

  # optimized version
  test_object.instance_of?( MobyBase::StateObject ) && ( @type == test_object.type ) && ( @id == test_object.id ) && ( @name == test_object.name )

end

#attribute(name) ⇒ Object

Function provides access to parameters of the state object

params

name

String defining the name of the attribute to get

returns

String

Value of the attribute as a string

raises

ArgumentError

name is not a String.

AttributeNotFoundError

if the requested attribute can not be found in the xml data of the object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/tdriver/base/state_object.rb', line 250

def attribute( name )

  # check argument variable type
  name.check_type( String, "wrong argument type $1 for attribute name (expected $2)" )

  begin

    # retrieve attribute(s) from test object; never access ui state xml data directly from behaviour implementation
    @test_object_adapter.test_object_attribute( @_xml_data, name.to_s )

  rescue MobyBase::AttributeNotFoundError
  
    raise MobyBase::AttributeNotFoundError, "Could not find attribute '#{ name.to_s }' for state object of type '#{ type.to_s }'"
    
  end

end

#child(attributes) ⇒ Object

Creates a state object for a child object of this state object Associates child object as current object’s child. and associates self as child object’s parent.

NOTE: Subsequent calls to #child always returns reference to same child

params

attributes

Hash object holding information for identifying which child to create, eg. :type => :slider

returns

StateObject

new child state object or reference to existing child



294
295
296
297
298
# File 'lib/tdriver/base/state_object.rb', line 294

def child( attributes )

  get_objects( attributes, false )

end

#children(attributes) ⇒ Object

TODO: document me



301
302
303
304
305
# File 'lib/tdriver/base/state_object.rb', line 301

def children( attributes )

  get_objects( attributes, true )

end

#eql?(other_state_object) ⇒ Boolean

Check to StateObject objects for equality (ie. contents, not if they are the same object).

param

other_state_object

StateObject this object is compared to.

returns

true

other_state_object is equal to this StateObject.

false

other_state_object is not equal to this StateObject.

Returns:

  • (Boolean)


211
212
213
214
215
# File 'lib/tdriver/base/state_object.rb', line 211

def eql? (other_state_object)

  self == other_state_object

end

#get_cached_test_object!(object) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/tdriver/base/state_object.rb', line 268

def get_cached_test_object!( object )

  if @child_object_cache.has_object?( object ) 

    object = @child_object_cache[ object ]

    true

  else

    false

  end

end

#inspectObject

TODO: document me



308
309
310
311
312
# File 'lib/tdriver/base/state_object.rb', line 308

def inspect

  "#<#{ self.class }:0x#{ ( "%x" % ( object_id.to_i << 1 ) )[ 3 .. -1 ] } @id=#{ @id.inspect } @type=\"#{ @type }\" @name=\"#{ @name }\">"

end

#xml_dataObject

Returns a XML element representing this state object.

returns

MobyUtil::XML::Element

XML representation of this state object



236
237
238
239
240
# File 'lib/tdriver/base/state_object.rb', line 236

def xml_data

  @_xml_data

end

#xml_data=(xml_object) ⇒ Object

Sets the XML content of this state object. Also sets identification attributes based on the contents of the XML.

params

xml_object

MobyUtil::XML::Element. State as XML.



223
224
225
226
227
228
229
# File 'lib/tdriver/base/state_object.rb', line 223

def xml_data=( xml_object )
 
  @_xml_data = xml_object
        
  unused_xpath, @name, @type, @id = @test_object_adapter.get_test_object_identifiers( xml_object )
  
end