Best Rr_ruby code snippet using TestCase.call
callback_test.rb
Source:callback_test.rb
...22 end23 24 def test_should_raise_exception_if_no_methods_specified25 exception = assert_raise(ArgumentError) { StateMachine::Callback.new(:before) }26 assert_equal 'Method(s) for callback must be specified', exception.message27 end28 29 def test_should_not_raise_exception_if_method_specified_in_do_option30 assert_nothing_raised { StateMachine::Callback.new(:before, :do => :run) }31 end32 33 def test_should_not_raise_exception_if_method_specified_as_argument34 assert_nothing_raised { StateMachine::Callback.new(:before, :run) }35 end36 37 def test_should_not_raise_exception_if_method_specified_as_block38 assert_nothing_raised { StateMachine::Callback.new(:before, :run) {} }39 end40 41 def test_should_not_raise_exception_if_implicit_option_specified42 assert_nothing_raised { StateMachine::Callback.new(:before, :do => :run, :invalid => :valid) }43 end44 45 def test_should_not_bind_to_objects46 assert !StateMachine::Callback.bind_to_object47 end48 49 def test_should_not_have_a_terminator50 assert_nil StateMachine::Callback.terminator51 end52end53class CallbackByDefaultTest < Test::Unit::TestCase54 def setup55 @callback = StateMachine::Callback.new(:before) {}56 end57 58 def test_should_have_type59 assert_equal :before, @callback.type60 end61 62 def test_should_not_have_a_terminator63 assert_nil @callback.terminator64 end65 66 def test_should_have_a_branch_with_all_matcher_requirements67 assert_equal StateMachine::AllMatcher.instance, @callback.branch.event_requirement68 assert_equal StateMachine::AllMatcher.instance, @callback.branch.state_requirements.first[:from]69 assert_equal StateMachine::AllMatcher.instance, @callback.branch.state_requirements.first[:to]70 end71 72 def test_should_not_have_any_known_states73 assert_equal [], @callback.known_states74 end75end76class CallbackWithMethodArgumentTest < Test::Unit::TestCase77 def setup78 @callback = StateMachine::Callback.new(:before, lambda {|*args| @args = args})79 80 @object = Object.new81 @result = @callback.call(@object)82 end83 84 def test_should_be_successful85 assert @result86 end87 88 def test_should_call_with_empty_context89 assert_equal [@object], @args90 end91end92class CallbackWithMultipleMethodArgumentsTest < Test::Unit::TestCase93 def setup94 @callback = StateMachine::Callback.new(:before, :run_1, :run_2)95 96 class << @object = Object.new97 attr_accessor :callbacks98 99 def run_1100 (@callbacks ||= []) << :run_1101 end102 103 def run_2104 (@callbacks ||= []) << :run_2105 end106 end107 108 @result = @callback.call(@object)109 end110 111 def test_should_be_successful112 assert @result113 end114 115 def test_should_call_each_callback_in_order116 assert_equal [:run_1, :run_2], @object.callbacks117 end118end119class CallbackWithDoMethodTest < Test::Unit::TestCase120 def setup121 @callback = StateMachine::Callback.new(:before, :do => lambda {|*args| @args = args})122 123 @object = Object.new124 @result = @callback.call(@object)125 end126 127 def test_should_be_successful128 assert @result129 end130 131 def test_should_call_with_empty_context132 assert_equal [@object], @args133 end134end135class CallbackWithMultipleDoMethodsTest < Test::Unit::TestCase136 def setup137 @callback = StateMachine::Callback.new(:before, :do => [:run_1, :run_2])138 139 class << @object = Object.new140 attr_accessor :callbacks141 142 def run_1143 (@callbacks ||= []) << :run_1144 end145 146 def run_2147 (@callbacks ||= []) << :run_2148 end149 end150 151 @result = @callback.call(@object)152 end153 154 def test_should_be_successful155 assert @result156 end157 158 def test_should_call_each_callback_in_order159 assert_equal [:run_1, :run_2], @object.callbacks160 end161end162class CallbackWithBlockTest < Test::Unit::TestCase163 def setup164 @callback = StateMachine::Callback.new(:before) do |*args|165 @args = args166 end167 168 @object = Object.new169 @result = @callback.call(@object)170 end171 172 def test_should_be_successful173 assert @result174 end175 176 def test_should_call_with_empty_context177 assert_equal [@object], @args178 end179end180class CallbackWithMixedMethodsTest < Test::Unit::TestCase181 def setup182 @callback = StateMachine::Callback.new(:before, :run_argument, :do => :run_do) do |object|183 object.callbacks << :block184 end185 186 class << @object = Object.new187 attr_accessor :callbacks188 189 def run_argument190 (@callbacks ||= []) << :argument191 end192 193 def run_do194 (@callbacks ||= []) << :do195 end196 end197 198 @result = @callback.call(@object)199 end200 201 def test_should_be_successful202 assert @result203 end204 205 def test_should_call_each_callback_in_order206 assert_equal [:argument, :do, :block], @object.callbacks207 end208end209class CallbackWithExplicitRequirementsTest < Test::Unit::TestCase210 def setup211 @object = Object.new212 @callback = StateMachine::Callback.new(:before, :from => :parked, :to => :idling, :on => :ignite, :do => lambda {})213 end214 215 def test_should_call_with_empty_context216 assert @callback.call(@object, {})217 end218 219 def test_should_not_call_if_from_not_included220 assert !@callback.call(@object, :from => :idling)221 end222 223 def test_should_not_call_if_to_not_included224 assert !@callback.call(@object, :to => :parked)225 end226 227 def test_should_not_call_if_on_not_included228 assert !@callback.call(@object, :on => :park)229 end230 231 def test_should_call_if_all_requirements_met232 assert @callback.call(@object, :from => :parked, :to => :idling, :on => :ignite)233 end234 235 def test_should_include_in_known_states236 assert_equal [:parked, :idling], @callback.known_states237 end238end239class CallbackWithImplicitRequirementsTest < Test::Unit::TestCase240 def setup241 @object = Object.new242 @callback = StateMachine::Callback.new(:before, :parked => :idling, :on => :ignite, :do => lambda {})243 end244 245 def test_should_call_with_empty_context246 assert @callback.call(@object, {})247 end248 249 def test_should_not_call_if_from_not_included250 assert !@callback.call(@object, :from => :idling)251 end252 253 def test_should_not_call_if_to_not_included254 assert !@callback.call(@object, :to => :parked)255 end256 257 def test_should_not_call_if_on_not_included258 assert !@callback.call(@object, :on => :park)259 end260 261 def test_should_call_if_all_requirements_met262 assert @callback.call(@object, :from => :parked, :to => :idling, :on => :ignite)263 end264 265 def test_should_include_in_known_states266 assert_equal [:parked, :idling], @callback.known_states267 end268end269class CallbackWithIfConditionTest < Test::Unit::TestCase270 def setup271 @object = Object.new272 end273 274 def test_should_call_if_true275 callback = StateMachine::Callback.new(:before, :if => lambda {true}, :do => lambda {})276 assert callback.call(@object)277 end278 279 def test_should_not_call_if_false280 callback = StateMachine::Callback.new(:before, :if => lambda {false}, :do => lambda {})281 assert !callback.call(@object)282 end283end284class CallbackWithUnlessConditionTest < Test::Unit::TestCase285 def setup286 @object = Object.new287 end288 289 def test_should_call_if_false290 callback = StateMachine::Callback.new(:before, :unless => lambda {false}, :do => lambda {})291 assert callback.call(@object)292 end293 294 def test_should_not_call_if_true295 callback = StateMachine::Callback.new(:before, :unless => lambda {true}, :do => lambda {})296 assert !callback.call(@object)297 end298end299class CallbackWithoutTerminatorTest < Test::Unit::TestCase300 def setup301 @object = Object.new302 end303 304 def test_should_not_halt_if_result_is_false305 callback = StateMachine::Callback.new(:before, :do => lambda {false}, :terminator => nil)306 assert_nothing_thrown { callback.call(@object) }307 end308end309class CallbackWithTerminatorTest < Test::Unit::TestCase310 def setup311 @object = Object.new312 end313 314 def test_should_not_halt_if_terminator_does_not_match315 callback = StateMachine::Callback.new(:before, :do => lambda {false}, :terminator => lambda {|result| result == true})316 assert_nothing_thrown { callback.call(@object) }317 end318 319 def test_should_halt_if_terminator_matches320 callback = StateMachine::Callback.new(:before, :do => lambda {false}, :terminator => lambda {|result| result == false})321 assert_throws(:halt) { callback.call(@object) }322 end323 324 def test_should_halt_if_terminator_matches_any_method325 callback = StateMachine::Callback.new(:before, :do => [lambda {true}, lambda {false}], :terminator => lambda {|result| result == false})326 assert_throws(:halt) { callback.call(@object) }327 end328end329class CallbackWithoutArgumentsTest < Test::Unit::TestCase330 def setup331 @callback = StateMachine::Callback.new(:before, :do => lambda {|object| @arg = object})332 333 @object = Object.new334 @callback.call(@object, {}, 1, 2, 3)335 end336 337 def test_should_call_method_with_object_as_argument338 assert_equal @object, @arg339 end340end341class CallbackWithArgumentsTest < Test::Unit::TestCase342 def setup343 @callback = StateMachine::Callback.new(:before, :do => lambda {|*args| @args = args})344 345 @object = Object.new346 @callback.call(@object, {}, 1, 2, 3)347 end348 349 def test_should_call_method_with_all_arguments350 assert_equal [@object, 1, 2, 3], @args351 end352end353class CallbackWithUnboundMethodTest < Test::Unit::TestCase354 def setup355 @callback = StateMachine::Callback.new(:before, :do => lambda {|*args| @context = args.unshift(self)})356 357 @object = Object.new358 @callback.call(@object, {}, 1, 2, 3)359 end360 361 def test_should_call_method_outside_the_context_of_the_object362 assert_equal [self, @object, 1, 2, 3], @context363 end364end365class CallbackWithBoundMethodTest < Test::Unit::TestCase366 def setup367 @object = Object.new368 end369 370 def test_should_call_method_within_the_context_of_the_object_for_block_methods371 context = nil372 callback = StateMachine::Callback.new(:before, :do => lambda {|*args| context = [self] + args}, :bind_to_object => true)373 callback.call(@object, {}, 1, 2, 3)374 375 assert_equal [@object, 1, 2, 3], context376 end377 378 def test_should_ignore_option_for_symbolic_methods379 class << @object380 attr_reader :context381 382 def after_ignite(*args)383 @context = args384 end385 end386 387 callback = StateMachine::Callback.new(:before, :do => :after_ignite, :bind_to_object => true)388 callback.call(@object)389 390 assert_equal [], @object.context391 end392 393 def test_should_ignore_option_for_string_methods394 callback = StateMachine::Callback.new(:before, :do => '[1, 2, 3]', :bind_to_object => true)395 assert callback.call(@object)396 end397end398class CallbackWithMultipleBoundMethodsTest < Test::Unit::TestCase399 def setup400 @object = Object.new401 402 first_context = nil403 second_context = nil404 405 @callback = StateMachine::Callback.new(:before, :do => [lambda {first_context = self}, lambda {second_context = self}], :bind_to_object => true)406 @callback.call(@object)407 408 @first_context = first_context409 @second_context = second_context410 end411 412 def test_should_call_each_method_within_the_context_of_the_object413 assert_equal @object, @first_context414 assert_equal @object, @second_context415 end416end417class CallbackWithApplicationBoundObjectTest < Test::Unit::TestCase418 def setup419 @original_bind_to_object = StateMachine::Callback.bind_to_object420 StateMachine::Callback.bind_to_object = true421 422 context = nil423 @callback = StateMachine::Callback.new(:before, :do => lambda {|*args| context = self})424 425 @object = Object.new426 @callback.call(@object)427 @context = context428 end429 430 def test_should_call_method_within_the_context_of_the_object431 assert_equal @object, @context432 end433 434 def teardown435 StateMachine::Callback.bind_to_object = @original_bind_to_object436 end437end438class CallbackWithBoundMethodAndArgumentsTest < Test::Unit::TestCase439 def setup440 @object = Object.new441 end442 443 def test_should_include_single_argument_if_specified444 context = nil445 callback = StateMachine::Callback.new(:before, :do => lambda {|arg1| context = [arg1]}, :bind_to_object => true)446 callback.call(@object, {}, 1)447 assert_equal [1], context448 end449 450 def test_should_include_multiple_arguments_if_specified451 context = nil452 callback = StateMachine::Callback.new(:before, :do => lambda {|arg1, arg2, arg3| context = [arg1, arg2, arg3]}, :bind_to_object => true)453 callback.call(@object, {}, 1, 2, 3)454 assert_equal [1, 2, 3], context455 end456 457 def test_should_include_arguments_if_splat_used458 context = nil459 callback = StateMachine::Callback.new(:before, :do => lambda {|*args| context = args}, :bind_to_object => true)460 callback.call(@object, {}, 1, 2, 3)461 assert_equal [1, 2, 3], context462 end463end464class CallbackWithApplicationTerminatorTest < Test::Unit::TestCase465 def setup466 @original_terminator = StateMachine::Callback.bind_to_object467 StateMachine::Callback.terminator = lambda {|result| result == false}468 469 @object = Object.new470 end471 472 def test_should_not_halt_if_terminator_does_not_match473 callback = StateMachine::Callback.new(:before, :do => lambda {true})474 assert_nothing_thrown { callback.call(@object) }475 end476 477 def test_should_halt_if_terminator_matches478 callback = StateMachine::Callback.new(:before, :do => lambda {false})479 assert_throws(:halt) { callback.call(@object) }480 end481 482 def teardown483 StateMachine::Callback.bind_to_object = @original_bind_to_object484 end485end486class CallbackWithAroundTypeAndBlockTest < Test::Unit::TestCase487 def setup488 @object = Object.new489 @callbacks = []490 end491 492 def test_should_evaluate_before_without_after493 callback = StateMachine::Callback.new(:around, lambda {|*args| block = args.pop; @args = args; block.call})494 assert callback.call(@object)495 assert_equal [@object], @args496 end497 498 def test_should_evaluate_after_without_before499 callback = StateMachine::Callback.new(:around, lambda {|*args| block = args.pop; block.call; @args = args})500 assert callback.call(@object)501 assert_equal [@object], @args502 end503 504 def test_should_halt_if_not_yielded505 callback = StateMachine::Callback.new(:around, lambda {|block|})506 assert_throws(:halt) { callback.call(@object) }507 end508 509 def test_should_call_block_after_before510 callback = StateMachine::Callback.new(:around, lambda {|block| @callbacks << :before; block.call})511 assert callback.call(@object) { @callbacks << :block }512 assert_equal [:before, :block], @callbacks513 end514 515 def test_should_call_block_before_after516 @callbacks = []517 callback = StateMachine::Callback.new(:around, lambda {|block| block.call; @callbacks << :after})518 assert callback.call(@object) { @callbacks << :block }519 assert_equal [:block, :after], @callbacks520 end521 522 def test_should_halt_if_block_halts523 callback = StateMachine::Callback.new(:around, lambda {|block| block.call; @callbacks << :after})524 assert_throws(:halt) { callback.call(@object) { throw :halt } }525 assert_equal [], @callbacks526 end527end528class CallbackWithAroundTypeAndMultipleMethodsTest < Test::Unit::TestCase529 def setup530 @callback = StateMachine::Callback.new(:around, :run_1, :run_2)531 532 class << @object = Object.new533 attr_accessor :before_callbacks534 attr_accessor :after_callbacks535 536 def run_1537 (@before_callbacks ||= []) << :run_1538 yield539 (@after_callbacks ||= []) << :run_1540 end541 542 def run_2543 (@before_callbacks ||= []) << :run_2544 yield545 (@after_callbacks ||= []) << :run_2546 end547 end548 end549 550 def test_should_succeed551 assert @callback.call(@object)552 end553 554 def test_should_evaluate_before_callbacks_in_order555 @callback.call(@object)556 assert_equal [:run_1, :run_2], @object.before_callbacks557 end558 559 def test_should_evaluate_after_callbacks_in_reverse_order560 @callback.call(@object)561 assert_equal [:run_2, :run_1], @object.after_callbacks562 end563 564 def test_should_call_block_after_before_callbacks565 @callback.call(@object) { (@object.before_callbacks ||= []) << :block }566 assert_equal [:run_1, :run_2, :block], @object.before_callbacks567 end568 569 def test_should_call_block_before_after_callbacks570 @callback.call(@object) { (@object.after_callbacks ||= []) << :block }571 assert_equal [:block, :run_2, :run_1], @object.after_callbacks572 end573 574 def test_should_halt_if_first_doesnt_yield575 class << @object576 def run_1577 (@before_callbacks ||= []) << :run_1578 end579 end580 581 catch(:halt) do582 @callback.call(@object) { (@object.before_callbacks ||= []) << :block }583 end584 585 assert_equal [:run_1], @object.before_callbacks586 assert_nil @object.after_callbacks587 end588 589 def test_should_halt_if_last_doesnt_yield590 class << @object591 def run_2592 (@before_callbacks ||= []) << :run_2593 end594 end595 596 catch(:halt) { @callback.call(@object) }597 assert_equal [:run_1, :run_2], @object.before_callbacks598 assert_nil @object.after_callbacks599 end600 601 def test_should_not_evaluate_further_methods_if_after_halts602 class << @object603 def run_2604 (@before_callbacks ||= []) << :run_2605 yield606 (@after_callbacks ||= []) << :run_2607 throw :halt608 end609 end610 611 catch(:halt) { @callback.call(@object) }612 assert_equal [:run_1, :run_2], @object.before_callbacks613 assert_equal [:run_2], @object.after_callbacks614 end615end616class CallbackWithAroundTypeAndArgumentsTest < Test::Unit::TestCase617 def setup618 @object = Object.new619 end620 621 def test_should_include_object_if_specified622 callback = StateMachine::Callback.new(:around, lambda {|object, block| @args = [object]; block.call})623 callback.call(@object)624 assert_equal [@object], @args625 end626 627 def test_should_include_arguments_if_specified628 callback = StateMachine::Callback.new(:around, lambda {|object, arg1, arg2, arg3, block| @args = [object, arg1, arg2, arg3]; block.call})629 callback.call(@object, {}, 1, 2, 3)630 assert_equal [@object, 1, 2, 3], @args631 end632 633 def test_should_include_arguments_if_splat_used634 callback = StateMachine::Callback.new(:around, lambda {|*args| block = args.pop; @args = args; block.call})635 callback.call(@object, {}, 1, 2, 3)636 assert_equal [@object, 1, 2, 3], @args637 end638end639class CallbackWithAroundTypeAndTerminatorTest < Test::Unit::TestCase640 def setup641 @object = Object.new642 end643 644 def test_should_not_halt_if_terminator_does_not_match645 callback = StateMachine::Callback.new(:around, :do => lambda {|block| block.call(false); false}, :terminator => lambda {|result| result == true})646 assert_nothing_thrown { callback.call(@object) }647 end648 649 def test_should_not_halt_if_terminator_matches650 callback = StateMachine::Callback.new(:around, :do => lambda {|block| block.call(false); false}, :terminator => lambda {|result| result == false})651 assert_nothing_thrown { callback.call(@object) }652 end653end654class CallbackWithAroundTypeAndBoundMethodTest < Test::Unit::TestCase655 def setup656 @object = Object.new657 end658 659 def test_should_call_method_within_the_context_of_the_object660 context = nil661 callback = StateMachine::Callback.new(:around, :do => lambda {|block| context = self; block.call}, :bind_to_object => true)662 callback.call(@object, {}, 1, 2, 3)663 664 assert_equal @object, context665 end666 667 def test_should_include_arguments_if_specified668 context = nil669 callback = StateMachine::Callback.new(:around, :do => lambda {|*args| block = args.pop; context = args; block.call}, :bind_to_object => true)670 callback.call(@object, {}, 1, 2, 3)671 672 assert_equal [1, 2, 3], context673 end674end...
tcms.rb
Source:tcms.rb
...103 xmlrpc_client.password = @options[:password]104 @client = xmlrpc_client105 return xmlrpc_client106 end107 def call(method, *args)108 @logger.info("TCMS: #{method} #{args}")109 begin110 return client.call(method, *args)111 rescue => e112 @logger.error("Error: #{e.to_s}")113 # @client = nil # client automatically reconnects114 raise "TCMS: Unable to call #{method}(#{args})"115 end116 end117 def multicall(*methods)118 @logger.info("TCMS: calling #{methods.size} methods")119 return client.multicall(*methods)120 rescue => e121 @logger.error("Error: #{e.to_s}")122 raise "TCMS: ERROR calling #{methods.size} methods"123 end124 # need to run in a new thread125 def call2_async(method, *args)126 @logger.info("TCMS async: #{method} #{args}")127 not_err, ret = client.call2_async(method, *args)128 @logger.error(exception_to_string(ret)) unless not_err129 return not_err, ret130 end131 # need to run in a new thread132 def multicall2_async(*methods)133 @logger.info("TCMS async: calling #{methods.size} methods")134 not_err, ret = client.multicall2_async(*methods)135 @logger.error(exception_to_string(ret)) unless not_err136 return not_err, ret137 end138 def version139 return self.call('Version.get')140 end141 def get_testrun(testrun_id)142 return self.call('TestRun.get', testrun_id.to_i)143 end144 alias_method :get_run, :get_testrun145 # @param [Array of Integer]146 # @return [Array of TestCase]147 def get_cases2(case_ids)148 return self.call('TestCase.filter', {'case_id__in'=>case_ids})149 end150 alias_method :filter_cases_by_id, :get_cases2151 def filter_cases_by_tag(tags_ids)152 return self.call('TestCase.filter', {'tag__in'=>tags_ids})153 end154 # @param [Integer]155 # @return [TestCase]156 def get_blessed_case(case_id)157 return self.call('TestCase.get', case_id.to_i)158 end159 alias get_case get_blessed_case160 def get_blessed_cases(*cases)161 return self.multicall(162 *cases.map { |case_id| ['TestCase.get', case_id.to_i] }163 )164 end165 def get_case_status(case_id)166 return self.call('TestCase.get_case_status', case_id.to_i)167 end168 def get_case_script(case_id)169 testcase = self.get_case(case_id)170 begin171 json = JSON.load(testcase['script'])172 rescue JSON::ParserError, NoMethodError173 raise "Invalid json in script field of test case #{case_id}"174 end175 if json['ruby']176 return json['ruby']177 else178 raise "No ruby script for test case #{case_id}"179 end180 end181 alias_method :get_script, :get_case_script182 # @param [Integer] the testrun ID183 # @return [Array<Hash>] array of hashes containing test case intermixed184 # with caserun properties185 def get_runs_cases(testrun_ids)186 testrun_ids.reduce([]) { |all, testrun_id|187 all.concat(get_run_cases(testrun_id))188 }189 end190 # @param [Integer] the testrun ID191 # @return [Array of TestCase]192 def get_run_cases(testrun_id)193 testcases = self.call('TestRun.get_test_cases', testrun_id.to_i)194 testcases.each do |testcase|195 testcase['run_id'] = testrun_id.to_i196 end197 return testcases198 end199 def get_cases_by_id(case_ids)200 case_ids = [case_ids] unless case_ids.class == Array201 testcases = []202 case_ids.each do |id|203 testcases.push(self.get_case(id))204 end205 return testcases206 end207 alias_method :get_cases_by_ids, :get_cases_by_id208 def get_cases_by_tags(tag_names, plan_id=@options[:plan])209 if @@tags.empty?210 self.get_all_cases_tags(plan_id)211 end212 tag_names = [tag_names] unless tag_names.class == Array213 tag_ids = []214 tag_names.each do |tag_name|215 tag_ids.push(@@tags[tag_name])216 end217 cases = []218 tag_ids.each do |tag_id|219 testcases = self.filter_cases({'tag'=>tag_id, 'plan'=>plan_id})220 testcases.select! {|item| item['tag'].include?(tag_id) }221 # merge the arrays222 cases += testcases223 end224 # now we need to consolidate and weed out the duplicates but only if225 # multiple tags are given226 unique_case_ids = []227 unique_testcases = []228 if tag_ids.count > 1229 cases.each do |tc|230 if not unique_case_ids.include? tc['case_id']231 unique_case_ids.push(tc['case_id'])232 unique_testcases.push(tc)233 end234 end235 else236 unique_testcases = cases237 end238 return unique_testcases239 end240 def filter_cases(options={})241 options[:plan] = @options[:plan] unless options[:plan]242 options[:case_status] = CASE_STATUS['CONFIRMED'] unless options[:case_status]243 return self.call('TestCase.filter', options)244 end245 alias_method :get_cases_by_filter, :filter_cases246 def get_all_cases_tags(plan_id=@options[:plan])247 tags = self.call('TestPlan.get_all_cases_tags', plan_id)248 @@tags.clear249 tags.each do |tag|250 @@tags[tag['name']] = tag['id']251 end252 return tags253 end254 def get_tags(values)255 return self.call('Tag.get_tags', values)256 end257 def get_tag_id(tag_name)258 if @@tags.empty?259 self.get_all_cases_tags(@options[:plan])260 end261 return @@tags[tag_name]262 end263 def get_tag_name(tag_id)264 if @@tags.empty?265 self.get_all_cases_tags(@options[:plan])266 end267 @@tags.each do |name, id|268 return name if id == tag_id269 end270 return nil271 end272 def add_testcase_tags(case_ids, tags)273 return self.call('TestCase.add_tag', case_ids, tags)274 end275 def remove_testcase_tags(case_ids, tags)276 return self.call('TestCase.remove_tag', case_ids, tags)277 end278 # @return [TestCaseRun merged with TestCase]279 def get_caserun(caserun_id)280 caserun = self.call('TestCaseRun.get', caserun_id.to_i)281 testcase = self.get_case(caserun['case_id'])282 return testcase.merge(caserun)283 end284 def get_caserun_raw(caserun_id)285 return self.call('TestCaseRun.get', caserun_id.to_i)286 end287 def get_testcase_bugs(case_id)288 return self.call('TestCase.get_bugs', Integer(case_id))289 end290 # get all bugs associated with a caserun_id291 def get_caserun_bugs(caserun_id)292 return self.call('TestCaseRun.get_bugs', Integer(caserun_id))293 end294 # get all bugs associated with a testrun295 def get_testrun_bugs(testrun_id)296 return self.call('TestRun.get_bugs', Integer(testrun_id))297 end298 def get_test_case_runs(testrun_id)299 return self.call('TestRun.get_test_case_runs', testrun_id)300 end301 # Given a testrun_id, reset all status that's not IDLE to302 # @param [Integer] testrun_id to be modified303 # @param [String] target_status, the status of the runs you are looking to change from304 # @param [String] case_status, the status that you want to set the targets to305 def reset_testrun(options)306 #testrun_id, status_to_be='IDLE', status_current=nil)307 # set all (if status.nil?) caseruns' status to IDLE308 testrun_id = options[:testrun_id]309 target_status_id = CASE_RUN_STATUS[options[:status_target]]310 testcase_runs = self.get_test_case_runs(testrun_id)311 caseruns2update = []312 testcase_runs.each do |testcase|313 if options[:status_from].nil?314 if options[:case_ids]315 if options[:case_ids].include? testcase['case_id']316 caseruns2update << testcase['case_run_id']317 end318 else319 if testcase['case_run_status_id'] != target_status_id320 caseruns2update << testcase['case_run_id']321 end322 end323 elsif options[:case_ids]324 if options[:case_ids].include? testcase['case_id']325 caseruns2update << testcase['case_run_id']326 end327 else328 # user wants to target a specific type of status to update329 if options[:status_from].include? testcase['case_run_status']330 caseruns2update << testcase['case_run_id']331 end332 end333 end334 if caseruns2update.count > 0335 update_caserun_status(caseruns2update, options[:status_target])336 else337 @logger.info("No matching caserun to update")338 end339 end340 # Get realtime case run status341 def get_caserun_status(caserun_id)342 caserun = self.call('TestCaseRun.get', caserun_id.to_i)343 return caserun['case_run_status']344 end345 # Get the URL of the latest log from a testcase run id346 def get_caserun_logs(caserun_id)347 self.call('TestCaseRun.get_logs', Integer(caserun_id))348 end349 def get_latest_log_url(caserun_id)350 logs = self.get_caserun_logs(caserun_id)351 if logs.count == 0352 @logger.info("There are no run logs stored for caserun_id #{caserun_id}")353 return nil354 else355 return logs[-1]["url"]356 end357 end358 # @param [Array of Integer] integers which represetns caserun_id359 # @return [{TestCaseRun+TestCase}, ...]360 def get_caseruns(caserun_ids)361 hash = {}362 # Get case runs and store them in hash363 caserun_ids = [caserun_ids] unless caserun_ids.class == Array364 case_ids = []365 caseruns = self.call('TestCaseRun.filter', {'case_run_id__in'=>caserun_ids})366 caseruns.each do |caserun|367 hash[caserun['case_id']] = caserun368 case_ids.push(caserun['case_id'])369 end370 # Get test cases and merge them to hash371 cases = self.call('TestCase.filter', {'case_id__in' => case_ids})372 cases.each do |testcase|373 hash[testcase['case_id']].merge!(testcase)374 end375 return hash.values376 end377 alias_method :get_caseruns_by_id, :get_caseruns378 def update_caserun(caserun_ids, options)379 return self.call('TestCaseRun.update', caserun_ids, options)380 end381 def update_caserun_status(caserun_ids, status)382 return self.call('TestCaseRun.update', caserun_ids,383 {'case_run_status' => CASE_RUN_STATUS[status]})384 end385 def add_caserun_comment(caserun_ids, comment)386 return self.call('TestCaseRun.add_comment', caserun_ids, comment.to_s)387 end388 alias_method :update_caserun_comments, :add_caserun_comment389 def attach_caserun_log(caserun_id, name, url)390 return self.call('TestCaseRun.attach_log', caserun_id, name, url)391 end392 alias_method :update_caserun_testlog, :attach_caserun_log393 def detach_caserun_log(caserun_id, link_id)394 return self.call('TestCaseRun.detach_log', caserun_id, link_id)395 end396 # Detach all the logs of the caserun397 def detach_caserun_logs(caserun_id)398 caserun = self.call('TestCaseRun.get', caserun_id)399 caserun['links'].each do |link_id|400 self.detach_caserun_log(caserun_id, link_id)401 end402 end403 alias_method :detach_logs, :detach_caserun_logs404 def create_testrun(options)405 options = @options.merge(options)406 ['timeout', 'xmlrpc_url'].each do |key|407 options.delete(key)408 end409 return self.call('TestRun.create', options)410 end411 alias_method :create_run, :create_testrun412 def filter_user(options={})413 return self.call('User.filter', options)414 end415 def update_testcases(case_ids, params)416 return self.call('TestCase.update', case_ids, params)417 end418 def get_plan_info_by_name(plan_name)419 return self.call('TestPlan.filter', {"name"=> plan_name})420 end421 end422end...
testFile.rb
Source:testFile.rb
...64 case expectedValue65 when Symbol66 return solution67 when Proc68 return call_lambda(expectedValue,inputs).to_s69 else70 return expectedValue71 end72 end73 def testCases74 testCases = []75 if values.count > 0 76 number_of_tests = values[0].count77 number_of_tests.times do |testIndex|78 testCase = TestCase.new79 # add test specific values80 symbols.each_with_index do |symbol, index|81 test_values = values[index]82 83 testCase.declarations << symbol.to_s84 testCase.inputs << test_values[testIndex]85 end86 init_test testCase87 testCases << testCase88 end89 else90 testCase = TestCase.new91 init_test testCase92 testCases << testCase93 end94 return testCases95 end96 def init_test testCase97 if solution_lambda98 testCase.expectedOutput = call_lambda(solution_lambda,testCase.inputs).to_s99 else100 testCase.expectedOutput = nil101 end102 testCase.beforeERB = beforeERB103 testCase.afterERB = afterERB104 testCase.funcCallERB = funcCallERB105 106 testCase.expectedDeclarations = expectedDeclarations107 testCase.expectedFunctions = expectedFunctions108 testCase.expectedEnums = expectedEnums109 testCase.expectedFunctionCalls = expectedFunctionCalls110 testCase.expectedFunctionCallCounts = expectedFunctionCallCounts111 testCase.expectedTypeCounts = expectedTypeCounts112 testCase.includeFunctionCall = includeFunctionCall113 testCase.htmlHide = htmlHide114 testCase.htmlExtra = htmlExtra115 expectedValues.each do |name, value|116 expected = [name, valueFor(value,117 testCase.inputs,118 testCase.expectedOutput)]119 testCase.expectedDeclationValues << expected120 end121 testCase.solutionLambda = solution_lambda122 testCase.evalLambda = evalLambda123 testCase.requiresOutput = self.requiresOutput124 testCase.floatOutput = self.floatOutput125 end126 # provides values for a declaration. Each value represents a test case127 def values_for(symbol,test_values)128 symbols << symbol129 values << test_values130 end131 # variable should be defined132 def expects(symbol,message=nil)133 unless message134 message = "You did not declare <code>#{symbol.to_s}</code>"135 end136 expectedDeclarations << [symbol.to_s, message]137 end138 # Symbol should be value after code is executed139 def expects_value(symbol,value=nil,&lambda)140 #array, value or lambda. symbol141 if lambda142 expectedValues << [symbol.to_s,lambda]143 return144 end145 expectedValues << [symbol.to_s,value]146 end147 # a lambda that provides a solution for the problem. i.e. solves the problem given the input data148 def solution(&lambda)149 self.solution_lambda = lambda150 end151 # need to think about this one152 def check_with(&lambda)153 self.evalLambda = lambda154 end155 def has_no_output()156 self.requiresOutput = false157 end158 def expects_func(definition, options = {})159 self.expectedFunctions << [definition, options]160 end161 def test_func(definition, params)162 # params[:with]163 end164 def before template165 self.beforeERB = template166 end167 168 def after template169 self.afterERB = template170 end171 def add_func_call func, params = "", options = {}172 if func.is_a? Symbol 173 func = func.to_s174 end175 self.includeFunctionCall = true176 func_call = "#{func}(#{params})"177 final_call = func_call178 if options[:call]179 final_call = options[:call]180 end181 if options[:print] 182 final_call = "print(#{final_call})"183 end184 setup = ""185 if options[:setup]186 setup = options[:setup]187 end188 self.funcCallERB = <<-EOF189<% if has[#{func.inspect}] %>190#{func_call}191<% end %>192 EOF193 self.afterERB = <<-EOF194<% if has[#{func.inspect}] %>195#{setup}196#{final_call}197<% end %>198 EOF199 end200 def after_tests(&lambda)201 self.afterTestsLambda = lambda202 end203 def html_hide columns204 self.htmlHide = columns205 end206 def html_add name, &lambda207 self.htmlExtra << [name, lambda]208 end209 def html(&lambda)210 self.htmlGenerator = lambda211 end212 def float_output213 self.floatOutput = true214 end215 def expects_enum name, values 216 self.expectedEnums << {217 name: name,218 values: values219 }220 end221 def expects_function_call name, message="You did not call the <code>_FUNCTION_</code> function."222 self.expectedFunctionCalls << [name, message]223 end224 def expects_function_call_count functions, count, message225 if functions.kind_of?(Array) == false226 functions = [functions]227 end228 self.expectedFunctionCallCounts << [functions, count, message]229 end230 def expects_type_count type, count, message231 self.expectedTypeCounts << [type, count, message]232 end233 def uid iid234 self._uid = iid235 end236 # HTML Generation237 def process results 238 allOK = true239 message = nil240 results.each do |result|241 status = result[:status]242 if status != :correct243 allOK = false244 end245 if status == :compilation_error246 message = "Compilation Error"247 break248 end249 # if status == :runtime_error250 # message = "Runtime Error"251 # break252 # end253 if status == :typo254 message = result[:message]255 break256 end257 if status == :missing_variables258 message = result[:message]259 break260 end261 if status == :missing_functions262 message = result[:message]263 break264 end265 if status == :wrong_function_definition266 message = result[:message]267 break268 end269 if status == :not_recursive270 message = result[:message]271 end272 if status == :missing_function_calls273 message = result[:message]274 end275 if status == :invalid_function_call_count276 message = result[:message]277 end278 if status == :invalid_type_count279 message = result[:message]280 end281 if requiresOutput and result[:status] == :no_output282 message = result[:message]283 end284 if status == :missing_enums285 message = result[:message]286 end287 end288 html = ""289 if message == nil...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!