Module: MobyBehaviour::ObjectBehaviourDescription
- Defined in:
- lib/tdriver/base/behaviour/behaviours/object_behaviour_description.rb
Overview
description
Generic methods for inspecting test object and it’s methods
behaviour
GenericObjectDescription
requires
*
input_type
*
sut_type
*
sut_version
*
objects
*;sut
Instance Method Summary collapse
-
#behaviours(return_indexes = false) ⇒ Object
description Return list of behaviour name(s) which caller object contains.
-
#describe(print = true, return_result = false) ⇒ Object
description Return list of methods and behaviour name(s) which caller object contains.
-
#describe_method(method_name, print = true, return_result = false) ⇒ Object
description Return brief method description either as return value or printed to STDOUT.
-
#object_methods ⇒ Object
description Returns a list of the names of (behaviour) methods publicly accessible in object.
Instance Method Details
#behaviours(return_indexes = false) ⇒ Object
description
Return list of behaviour name(s) which caller object contains. This method may be useful when implementing/testing custom behaviour modules.
arguments
return_indexes
TrueClass
description: Returns result as array of behaviour indexes
example: true
FalseClass
description: Returns result as array of behaviour names
example: false
returns
Array
description: If 'return_indexes' is true, result is a array of indexes (Fixnum)
example: [1,2,3,4,5]
Array
description: If 'return_indexes' is false, result is a array of behaviour names (String)
example: ["GenericApplication", "GenericFind", "GenericObjectComposition"]
footer
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/tdriver/base/behaviour/behaviours/object_behaviour_description.rb', line 68 def behaviours( return_indexes = false ) if return_indexes @object_behaviours else TDriver::BehaviourFactory.collect_behaviours( :index => @object_behaviours ).collect{ | behaviour | behaviour[ :name ] }.uniq.compact end end |
#describe(print = true, return_result = false) ⇒ Object
description
Return list of methods and behaviour name(s) which caller object contains. This method may be useful when implementing/testing custom behaviour modules.
arguments
TrueClass
description: Print result to STDOUT and return as String
example: true
FalseClass
description: Return result as Hash instead of printing to STDOUT
example: false
return_result
TrueClass
description: Pass result as return value
example: true
FalseClass
description: Do not pass result as return value
example: false
returns
String
description: String representation of object details
example: "Object:\n sut => sut_qt\n type => application\n\nBehaviours:\n GenericApplication\n\nMethods:\n application?\n"
Hash
description: Hash representation of object details
example: { :methods=>["application?"], :behaviours=>["GenericApplication"], :object=>{:sut=>:sut_qt, :type=>"application"} }
NilClass
description: When 'return_result' is false
example: nil
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/tdriver/base/behaviour/behaviours/object_behaviour_description.rb', line 229 def describe( print = true, return_result = false ) # print result to stdout if argument not boolean print = true unless [ TrueClass, FalseClass ].include? print.class # return result not printed out to stdout return_result = true unless print result_hash = { :object => { :type => @type, :sut => kind_of?( MobyBase::SUT ) ? id : sut.id }, :methods => object_methods, :behaviours => behaviours } if print result = "" [:object, :behaviours, :methods].each{ | key, value | value = result_hash[ key ] result << "\n#{ key.to_s.capitalize }:\n" case value.class.to_s when "Array" result << "\t" << value.join("\n\t") << "\n" when "Hash" result << value.collect{ | key, value | "\t#{ key } => #{ value }" }.join("\n") << "\n" #else #result << "\t#{ value }\n" end } puts result else result = result_hash end ( return_result ? result : nil ) end |
#describe_method(method_name, print = true, return_result = false) ⇒ Object
description
Return brief method description either as return value or printed to STDOUT. This method may be useful when implementing/testing custom behaviour modules.
arguments
method_name
String
description: Name of the method
example: "type"
TrueClass
description: Print result to STDOUT and return as String
example: true
FalseClass
description: Return result as Hash instead of printing to STDOUT
example: false
return_result
TrueClass
description: Pass result as return value
example: true
FalseClass
description: Do not pass result as return value
example: false
returns
String
description: String representation of object description/details
example: "Description:\nReturns type of the test object\n\nExample:\ntype"
Hash
description: Hash representation of object description/details
example: { :description=>"Returns type of the test object", :example=>"type" }
NilClass
description: When 'return_result' is false
example: nil
exceptions
TypeError
description: Wrong argument type <class> for method name (expected Symbol or String)
ArgumentError
description: Test object type of <type> does not have method <name>
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 187 188 189 190 191 192 193 194 |
# File 'lib/tdriver/base/behaviour/behaviours/object_behaviour_description.rb', line 153 def describe_method( method_name, print = true, return_result = false ) # verify that method_name is type of Symbol or String and convert it to Symbol method_name = method_name.check_type( [ Symbol, String ], "wrong argument type $1 for method name (expected $2)" ).to_s # verify that print argument is boolean print = print.check_type( [ TrueClass, FalseClass ], "wrong argument type $1 for verbose output (expected $2)" ).true? # return result not printed out to stdout return_result = true if print.false? behaviours = TDriver::BehaviourFactory.collect_behaviours( :index => @object_behaviours ).select{ | behaviour | behaviour[ :methods ].keys.include?( method_name ) }.compact.last # verify that method was found behaviours.not_blank "Test object type of #{ @type } does not have method #{ method_name.inspect }" result = { :name => method_name, :description => behaviours[ :methods ][ method_name ][ :description ], :example => behaviours[ :methods ][ method_name ][ :example ] } if print result = [ :name, :description, :example ].inject( "" ){ | tmp_result, key | tmp_result << "\n#{ key.to_s.capitalize }:\n#{ result[ key ] }\n" } puts result end # result return_result ? result : nil end |
#object_methods ⇒ Object
description
Returns a list of the names of (behaviour) methods publicly accessible in object. This method may be useful when implementing/testing custom behaviour modules.
returns
Array
description: List of method names
example: ["application?", "close", "closable?", "describe"]
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/tdriver/base/behaviour/behaviours/object_behaviour_description.rb', line 88 def object_methods TDriver::BehaviourFactory.collect_behaviours( :index => @object_behaviours ).inject( [] ){ | result, behaviour | # append method names to result array result.concat( behaviour[ :methods ].keys.collect{ | key | # make sure that method name is returned in type of string key.to_s } ) }.uniq.compact end |