Best Bacon_ruby code snippet using KnockOutput.run
spec.rb
Source:spec.rb
...247 end248 def postponed?249 @postponed_blocks_count != 0250 end251 def run_before_filters252 execute_block { @before_filters.each { |f| @context.instance_eval(&f) } }253 end254 def run_spec_block255 @ran_spec_block = true256 # If an exception occurred, we definitely don't need to perform the actual spec anymore257 unless @exception_occurred258 execute_block { @context.instance_eval(&@block) }259 end260 finish_spec unless postponed?261 end262 def run_after_filters263 @ran_after_filters = true264 execute_block { @after_filters.each { |f| @context.instance_eval(&f) } }265 end266 def run267 Bacon.handle_requirement_begin(@description)268 Counter[:depth] += 1269 run_before_filters270 @number_of_requirements_before = Counter[:requirements]271 run_spec_block unless postponed?272 end273 def schedule_block(seconds, &block)274 # If an exception occurred, we definitely don't need to schedule any more blocks275 unless @exception_occurred276 @postponed_blocks_count += 1277 unless Platform.android?278 performSelector("run_postponed_block:", withObject:block, afterDelay:seconds)279 else280 sleep seconds281 run_postponed_block(block)282 end283 end284 end285 def postpone_block(timeout = 1, &block)286 # If an exception occurred, we definitely don't need to schedule any more blocks287 unless @exception_occurred288 if @postponed_block289 raise "Only one indefinite `wait' block at the same time is allowed!"290 else291 @postponed_blocks_count += 1292 @postponed_block = block293 unless Platform.android?294 performSelector("postponed_block_timeout_exceeded", withObject:nil, afterDelay:timeout)295 else296 sleep timeout297 postponed_block_timeout_exceeded298 end299 end300 end301 end302 def postpone_block_until_change(object_to_observe, key_path, timeout = 1, &block)303 # If an exception occurred, we definitely don't need to schedule any more blocks304 unless @exception_occurred305 if @postponed_block306 raise "Only one indefinite `wait' block at the same time is allowed!"307 else308 @postponed_blocks_count += 1309 @postponed_block = block310 @observed_object_and_key_path = [object_to_observe, key_path]311 object_to_observe.addObserver(self, forKeyPath:key_path, options:0, context:nil)312 unless Platform.android?313 performSelector("postponed_change_block_timeout_exceeded", withObject:nil, afterDelay:timeout)314 else315 sleep timeout316 postponed_change_block_timeout_exceeded317 end318 end319 end320 end321 def observeValueForKeyPath(key_path, ofObject:object, change:_, context:__)322 resume323 end324 def postponed_change_block_timeout_exceeded325 remove_observer!326 postponed_block_timeout_exceeded327 end328 def remove_observer!329 if @observed_object_and_key_path330 object, key_path = @observed_object_and_key_path331 object.removeObserver(self, forKeyPath:key_path)332 @observed_object_and_key_path = nil333 end334 end335 def postponed_block_timeout_exceeded336 cancel_scheduled_requests!337 execute_block { raise Error.new(:failed, "timeout exceeded: #{@context.name} - #{@description}") }338 @postponed_blocks_count = 0339 finish_spec340 end341 def resume342 unless Platform.android?343 NSObject.cancelPreviousPerformRequestsWithTarget(self, selector:'postponed_block_timeout_exceeded', object:nil)344 NSObject.cancelPreviousPerformRequestsWithTarget(self, selector:'postponed_change_block_timeout_exceeded', object:nil)345 end346 remove_observer!347 block = @postponed_block348 @postponed_block = nil349 run_postponed_block(block)350 end351 def run_postponed_block(block)352 # If an exception occurred, we definitely don't need execute any more blocks353 execute_block(&block) unless @exception_occurred354 @postponed_blocks_count -= 1355 unless postponed?356 if @ran_after_filters357 exit_spec358 elsif @ran_spec_block359 finish_spec360 else361 run_spec_block362 end363 end364 end365 def finish_spec366 if !@exception_occurred && Counter[:requirements] == @number_of_requirements_before367 # the specification did not contain any requirements, so it flunked368 execute_block { raise Error.new(:missing, "empty specification: #{@context.name} #{@description}") }369 end370 run_after_filters371 exit_spec unless postponed?372 end373 def cancel_scheduled_requests!374 unless Platform.android?375 NSObject.cancelPreviousPerformRequestsWithTarget(@context)376 NSObject.cancelPreviousPerformRequestsWithTarget(self)377 end378 end379 def exit_spec380 cancel_scheduled_requests!381 Counter[:depth] -= 1382 Bacon.handle_requirement_end(@error)383 @context.specification_did_finish(self)384 end385 def execute_block386 begin387 yield388 rescue Object => e389 @exception_occurred = true390 if e.is_a?(Exception)391 ErrorLog << "#{e.class}: #{e.message}\n"392 lines = $DEBUG ? e.backtrace : e.backtrace.find_all { |line| line !~ /bin\/macbacon|\/mac_bacon\.rb:\d+/ }393 lines.each_with_index { |line, i|394 ErrorLog << "\t#{line}#{i==0 ? ": #{@context.name} - #{@description}" : ""}\n"395 }396 ErrorLog << "\n"397 else398 unless Platform.android?399 # Pure NSException.400 ErrorLog << "#{e.name}: #{e.reason}\n"401 else402 # Pure Java exception.403 ErrorLog << "#{e.class.toString} : #{e.getMessage}"404 end405 end406 @error = if e.is_a? Error407 Counter[e.count_as] += 1408 "#{e.count_as.to_s.upcase} - #{e}"409 else410 Counter[:errors] += 1411 "ERROR: #{e.class} - #{e}"412 end413 end414 end415 end416 class Platform417 def self.android?418 defined?(NSObject) ? false : true419 end420 end421 def self.add_context(context)422 (@contexts ||= []) << context423 end424 def self.current_context_index425 @current_context_index ||= 0426 end427 def self.current_context428 @contexts[current_context_index]429 end430 def self.run(arg=nil)431 unless respond_to?(:handle_specification_begin)432 extend(Outputs[ENV['output']] || SpecDoxOutput)433 end434 @timer ||= Time.now435 unless Platform.android?436 Counter[:context_depth] += 1437 handle_specification_begin(current_context.name)438 current_context.performSelector("run", withObject:nil, afterDelay:0)439 else440 @main_activity ||= arg441 @contexts.each do |context|442 Counter[:context_depth] += 1443 handle_specification_begin(context.name)444 context.run445 handle_specification_end446 Counter[:context_depth] -= 1447 end448 handle_summary449 end450 end451 # Android-only.452 def self.main_activity453 @main_activity454 end455 def self.context_did_finish(context)456 unless Platform.android?457 handle_specification_end458 Counter[:context_depth] -= 1459 if (@current_context_index + 1) < @contexts.size460 @current_context_index += 1461 run462 else463 # DONE464 handle_summary465 unless Platform.android?466 exit(Counter.values_at(:failed, :errors).inject(:+))467 else468 # In Android there is no need to exit as we terminate the activity right after Bacon.469 end470 end471 end472 end473 class Context474 attr_reader :name, :block475 def initialize(name, before = nil, after = nil, &block)476 @name = name477 @before = (before ? before.dup : [])478 @after = (after ? after.dup : [])479 @block = block480 @specifications = []481 @current_specification_index = 0482 Bacon.add_context(self)483 instance_eval(&block)484 end485 def run486 # TODO487 #return unless name =~ RestrictContext488 unless Platform.android?489 if spec = current_specification490 spec.performSelector("run", withObject:nil, afterDelay:0)491 else492 Bacon.context_did_finish(self)493 end494 else495 @specifications.each do |spec|496 spec.run497 end498 Bacon.context_did_finish(self)499 end500 end501 def current_specification502 @specifications[@current_specification_index]503 end504 def specification_did_finish(spec)505 unless Platform.android?506 if (@current_specification_index + 1) < @specifications.size507 @current_specification_index += 1508 run509 else510 Bacon.context_did_finish(self)511 end512 end513 end514 def before(&block); @before << block; end515 def after(&block); @after << block; end516 def behaves_like(*names)517 names.each { |name| instance_eval(&Shared[name]) }518 end519 def it(description, &block)520 return unless description =~ RestrictName521 block ||= proc { should.flunk "not implemented" }522 Counter[:specifications] += 1523 @specifications << Specification.new(self, description, block, @before, @after)524 end525 def should(*args, &block)526 if Counter[:depth]==0527 it('should '+args.first, &block)528 else529 super(*args, &block)530 end531 end532 def describe(*args, &block)533 context = Bacon::Context.new(args.join(' '), @before, @after, &block)534 # FIXME: fix RM-879 and RM-806535 unless Platform.android?536 (parent_context = self).methods(false).each { |e|537 class<<context; self end.send(:define_method, e) { |*args| parent_context.send(e, *args) }538 }539 end540 context541 end542 def wait(seconds = nil, &block)543 if seconds544 current_specification.schedule_block(seconds, &block)545 else546 current_specification.postpone_block(&block)547 end548 end549 def wait_max(timeout, &block)550 current_specification.postpone_block(timeout, &block)551 end552 def wait_for_change(object_to_observe, key_path, timeout = 1, &block)553 current_specification.postpone_block_until_change(object_to_observe, key_path, timeout, &block)554 end555 def resume556 current_specification.resume557 end558 def raise?(*args, &block); block.raise?(*args); end559 def throw?(*args, &block); block.throw?(*args); end560 def change?(*args, &block); block.change?(*args); end561 alias_method :context, :describe562 # Android-only.563 def main_activity; Bacon.main_activity; end564 end565end566class Object567 def true?; false; end568 def false?; false; end569end570class TrueClass571 def true?; true; end572end573class FalseClass574 def false?; true; end575end576class Proc577 def raise?(*exceptions)578 call579 rescue *(exceptions.empty? ? RuntimeError : exceptions) => e580 e581 else582 false583 end584 def throw?(sym)585 catch(sym) {586 call587 return false588 }589 true590 end591 def change?592 pre_result = yield593 call594 post_result = yield595 pre_result != post_result596 end597end598class Numeric599 def close?(to, delta)600 (to.to_f - self).abs <= delta.to_f rescue false601 end602end603class Object604 def should(*args, &block) Should.new(self).be(*args, &block) end605end606module Kernel607 private608 def describe(*args, &block) Bacon::Context.new(args.join(' '), &block) end609 def shared(name, &block) Bacon::Shared[name] = block end610 alias_method :context, :describe611end612class Should613 # Kills ==, ===, =~, eql?, equal?, frozen?, instance_of?, is_a?,614 # kind_of?, nil?, respond_to?, tainted?615 instance_methods.each { |name| undef_method name if name =~ /\?|^\W+$/ }616 def initialize(object)617 @object = object618 @negated = false619 end620 def not(*args, &block)621 @negated = !@negated622 if args.empty?623 self624 else625 be(*args, &block)626 end627 end628 def be(*args, &block)629 if args.empty?630 self631 else632 block = args.shift unless block_given?633 satisfy(*args, &block)634 end635 end636 alias a be637 alias an be638 def satisfy(*args, &block)639 if args.size == 1 && String === args.first640 description = args.shift641 else642 description = ""643 end644 r = yield(@object, *args)645 if Bacon::Counter[:depth] > 0646 Bacon::Counter[:requirements] += 1647 raise Bacon::Error.new(:failed, description) unless @negated ^ r648 r649 else650 @negated ? !r : !!r651 end652 end653 def method_missing(name, *args, &block)654 name = "#{name}?" if name.to_s =~ /\w[^?]\z/655 desc = @negated ? "not " : ""656 desc << @object.inspect << "." << name.to_s657 desc << "(" << args.map{ |x| x.inspect }.join(", ") << ") failed"658 satisfy(desc) { |x| x.__send__(name, *args, &block) }659 end660 def equal(value) self == value end661 def match(value) self =~ value end662 def identical_to(value) self.equal? value end663 alias same_as identical_to664 def flunk(reason="Flunked")665 raise Bacon::Error.new(:failed, reason)666 end667end668# Do not log all exceptions when running the specs.669Exception.log_exceptions = false...
bacon.rb
Source:bacon.rb
...111 @before, @after = [], []112 @block = block113 end114 115 def run116 return unless name =~ RestrictContext117 Bacon.handle_specification(name) { instance_eval(&block) }118 self119 end120 def before(&block); @before << block; end121 def after(&block); @after << block; end122 def behaves_like(*names)123 names.each { |name| instance_eval(&Shared[name]) }124 end125 def it(description, &block)126 return unless description =~ RestrictName127 block ||= lambda { should.flunk "not implemented" }128 Counter[:specifications] += 1129 run_requirement description, block130 end131 132 def should(*args, &block)133 if Counter[:depth]==0134 it('should '+args.first,&block)135 else136 super(*args,&block)137 end138 end139 def run_requirement(description, spec)140 Bacon.handle_requirement description do141 begin142 Counter[:depth] += 1143 rescued = false144 begin145 @before.each { |block| instance_eval(&block) }146 prev_req = Counter[:requirements]147 instance_eval(&spec)148 rescue Object => e149 rescued = true150 raise e151 ensure152 if Counter[:requirements] == prev_req and not rescued153 raise Error.new(:missing,154 "empty specification: #{@name} #{description}")155 end156 begin157 @after.each { |block| instance_eval(&block) }158 rescue Object => e159 raise e unless rescued160 end161 end162 rescue => e163 ErrorLog << "#{e.class}: #{e.message}\n"164 if e.backtrace165 e.backtrace.find_all do |line| 166 line !~ /bin\/bacon|\/bacon\.rb:\d+/ 167 end.each_with_index do |line, i|168 ErrorLog << "\t#{line}#{i==0 ? ": #@name - #{description}" : ""}\n"169 end170 end171 ErrorLog << "\n"172 if e.kind_of? Error173 Counter[e.count_as] += 1174 e.count_as.to_s.upcase175 else176 Counter[:errors] += 1177 "ERROR: #{e.class}"178 end179 else180 ""181 ensure182 Counter[:depth] -= 1183 end184 end185 end186 def describe(*args, &block)187 context = Bacon::Context.new(args.join(' '), &block)188 @before.each { |b| context.before(&b) }189 @after.each { |b| context.after(&b) }190 context.run191 end192 def raise?(*args, &block); block.raise?(*args); end193 def throw?(*args, &block); block.throw?(*args); end194 def change?(*args, &block); block.change?(*args); end195 end196end197class Object198 def true?; false; end199 def false?; false; end200end201class TrueClass202 def true?; true; end203end204class FalseClass205 def false?; true; end206end207class Proc208 def raise?(*exceptions)209 exceptions = [RuntimeError] if exceptions.empty?210 call211 # Only to work in 1.9.0, rescue with splat doesn't work there right now212 rescue Object => e213 case e214 when *exceptions215 e216 else217 raise e218 end219 else220 false221 end222 def throw?(sym)223 catch(sym) {224 call225 return false226 }227 return true228 end229 def change?230 pre_result = yield231 called = call232 post_result = yield233 pre_result != post_result234 end235end236class Numeric237 def close?(to, delta)238 (to.to_f - self).abs <= delta.to_f rescue false239 end240end241class Object242 def should(*args, &block) Should.new(self).be(*args, &block) end243end244module Kernel245 private246 def describe(*args, &block) Bacon::Context.new(args.join(' '), &block).run end247 def shared(name, &block) Bacon::Shared[name] = block end248end249class Should250 # Kills ==, ===, =~, eql?, equal?, frozen?, instance_of?, is_a?,251 # kind_of?, nil?, respond_to?, tainted?252 instance_methods.each { |name| undef_method name if name =~ /\?|^\W+$/ }253 def initialize(object)254 @object = object255 @negated = false256 end257 def not(*args, &block)258 @negated = !@negated259 if args.empty?260 self...
run
Using AI Code Generation
1 def message=(new_message)2 def message=(new_message)3 def message=(new_message)
run
Using AI Code Generation
1KnockOutput.new.run("Hello World!")2 def run(str)3KnockOutput.new.run("Hello World!")4 def run(str)
run
Using AI Code Generation
1 puts File.read(__FILE__)2 puts File.read(__FILE__)3 puts File.read(__FILE__)4 put File.read(__FILE__)5 puts File.read(__FILE__)6 puts File.read(__FILE__)7KnockOutput.new.run("Hello World!")8 def run(str)9KnockOutput.new.run("Hello World!")10 def run(str)11KnockOutput.new.run("Hello World!")12 def run(str)13KnockOutput.new.run("Hello World!")14 def run(str)
run
Using AI Code Generation
1 puts File.read(__FILE__)2 puts File.read(__FILE__)3 puts File.read(__FILE__)4 puts File.read(__FILE__)5 puts File.read(__FILE__)6 puts File.read(__FILE__)
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!!