Best Bacon_ruby code snippet using KnockOutput.describe
spec.rb
Source:spec.rb
...169 end170 end171 module TapOutput172 @@count = 1173 @@describe = ""174 def handle_specification_begin(name)175 @@describe << "#{name} "176 end177 def handle_specification_end178 @@describe = ""179 end180 def handle_requirement_begin(description)181 @description = @@describe + description182 @description.sub!(/^[#\s]+/, "")183 ErrorLog.replace ""184 end185 def handle_requirement_end(error)186 if error.empty?187 puts "ok %-3d - %s" % [@@count, @description]188 else189 puts "not ok %d - %s: %s" %190 [@@count, @description, error]191 puts ErrorLog.strip.gsub(/^/, '# ') if Backtraces192 end193 @@count += 1194 end195 def handle_summary196 puts "1..#{Counter[:specifications]}"197 puts "# %d tests, %d assertions, %d failures, %d errors" %198 Counter.values_at(:specifications, :requirements, :failed, :errors)199 end200 end201 module KnockOutput202 def handle_specification_begin(name); end203 def handle_specification_end ; end204 def handle_requirement_begin(description)205 @description = description206 ErrorLog.replace ""207 end208 def handle_requirement_end(error)209 if error.empty?210 puts "ok - %s" % [@description]211 else212 puts "not ok - %s: %s" % [@description, error]213 puts ErrorLog.strip.gsub(/^/, '# ') if Backtraces214 end215 end216 def handle_summary; end217 end218 Outputs = {219 'spec_dox' => SpecDoxOutput,220 'fast' => FastOutput,221 'test_unit' => TestUnitOutput,222 'tap' => TapOutput,223 'knock' => KnockOutput,224 'rubymine' => RubyMineOutput,225 'colorized' => ColorizedOutput,226 }227 class Error < RuntimeError228 attr_accessor :count_as229 def initialize(count_as, message)230 @count_as = count_as231 super message232 end233 end234 class Specification235 attr_reader :description236 def initialize(context, description, block, before_filters, after_filters)237 @context = context238 @description = description239 @block = block240 @before_filters = before_filters.dup241 @after_filters = after_filters.dup242 @postponed_blocks_count = 0243 @ran_spec_block = false244 @ran_after_filters = false245 @exception_occurred = false246 @error = ""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 else...
describe
Using AI Code Generation
1 expect(knock_output.output).to eq "Knock Knock"2 expect(knock_output.output).to eq "Knock Knock"3 expect(knock_output.output).to eq "Knock Knock"4 expect(knock_output.output).to eq "Knock Knock"5 expect(knock_output.output).to eq "Knock Knock"6 expect(knock_output.output).to eq "Knock Knock"7 expect(knock_output.output).to eq "Knock Knock"
describe
Using AI Code Generation
1 expect(KnockOutput.new.describe).to eq("Knock knock!")2 expect(KnockOutput.new.describe).to eq("Who's there?")3 expect(KnockOutput.new.describe).to eq("Boo.")4 expect(KnockOutput.new.describe).to eq("Boo who?")5 expect(KnockOutput.new.describe).to eq("Don't cry, it's only a joke.")6 expect(KnockOutput.new.describe).to eq("Knock knock!")7 expect(KnockOutput.new.describe).to eq("Who's there?")8 expect(KnockOutput.new.describe).to eq("Boo.")9 expect(KnockOutput.new.describe).to eq("Boo who?")10 expect(KnockOutput.new.describe).to eq("Don't cry, it's only a joke.")11 expect(KnockOutput.new.describe).to eq("Knock knock!")12 expect(KnockOutput.new.describe).to eq("Who's there?")13 expect(KnockOutput.new.describe).to eq("Boo.")14 expect(KnockOutput.new.describe).to eq("Boo who?")15 expect(KnockOutput.new.describe).to eq("Don't cry, it's only a joke.")
describe
Using AI Code Generation
1 expect(KnockOutput.new.describe).to eq("Knock knock!")2 expect(KnockOutput.new.describe).to eq("Who's there?")3 expect(KnockOutput.new.describe).to eq("Boo.")4 expect(KnockOutput.new.describe).to eq("Boo who?")5 expect(KnockOutput.new.describe).to eq("Don't cry, it's only a joke.")6 expect(KnockOutput.new.describe).to eq("Knock knock!")7 expect(KnockOutput.new.describe).to eq("Who's there?")8 expect(KnockOutput.new.describe).to eq("Boo.")9 expect(KnockOutput.new.describe).to eq("Boo who?")10 expect(KnockOutput.new.describe).to eq("Don't cry, it's only a joke.")11 expect(KnockOutput.new.describe).to eq("Knock knock!")12 expect(KnockOutput.new.describe).to eq("Who's there?")13 expect(KnockOutput.new.describe).to eq("Boo.")14 expect(KnockOutput.new.describe).to eq("Boo who?")15 expect(KnockOutput.new.describe).to eq("Don't cry, it's only a joke.")
describe
Using AI Code Generation
1 expect(knock_output.output).to eq "Knock Knock"2 expect(knock_output.output).to eq "Knock Knock"3 expect(knock_output.output).to eq "Knock Knock"4 expect(knock_output.output).to eq "Knock Knock"5 expect(knock_output.output).to eq "Knock Knock"6 expect(knock_output.output).to eq "Knock Knock"7 expect(knock_output.output).to eq "Knock Knock"
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!!