How to use raise method of KnockOutput Package

Best Bacon_ruby code snippet using KnockOutput.raise

bacon.rb

Source:bacon.rb Github

copy

Full Screen

...9 VERSION = "1.1"10 Counter = Hash.new(0)11 ErrorLog = ""12 Shared = Hash.new { |_, name|13 raise NameError, "no such context: #{name.inspect}"14 }15 RestrictName = // unless defined? RestrictName16 RestrictContext = // unless defined? RestrictContext17 Backtraces = true unless defined? Backtraces18 def self.summary_on_exit19 return if Counter[:installed_summary] > 020 at_exit {21 handle_summary22 if $!23 raise $!24 elsif Counter[:errors] + Counter[:failed] > 025 exit 1 unless SILVERLIGHT26 end27 }28 Counter[:installed_summary] += 129 end30 class <<self; alias summary_at_exit summary_on_exit; end31 module SpecDoxOutput32 def handle_specification(name)33 puts name34 yield35 puts36 end37 def handle_requirement(description)38 print "- #{description}"39 error = yield40 puts error.empty? ? "" : " [#{error}]"41 end42 def handle_summary43 print ErrorLog if Backtraces44 puts "%d specifications (%d requirements), %d failures, %d errors" %45 Counter.values_at(:specifications, :requirements, :failed, :errors)46 end47 end48 module TestUnitOutput49 def handle_specification(name) yield end50 def handle_requirement(description)51 error = yield52 if error.empty?53 print "."54 else55 print error[0..0]56 end57 end58 def handle_summary59 puts60 puts ErrorLog if Backtraces61 puts "%d tests, %d assertions, %d failures, %d errors" %62 Counter.values_at(:specifications, :requirements, :failed, :errors)63 end64 end65 module TapOutput66 def handle_specification(name) yield end67 def handle_requirement(description)68 ErrorLog.replace ""69 error = yield70 if error.empty?71 puts "ok %-3d - %s" % [Counter[:specifications], description]72 else73 puts "not ok %d - %s: %s" %74 [Counter[:specifications], description, error]75 puts ErrorLog.strip.gsub(/^/, '# ') if Backtraces76 end77 end78 def handle_summary79 puts "1..#{Counter[:specifications]}"80 puts "# %d tests, %d assertions, %d failures, %d errors" %81 Counter.values_at(:specifications, :requirements, :failed, :errors)82 end83 end84 module KnockOutput85 def handle_specification(name) yield end86 def handle_requirement(description)87 ErrorLog.replace ""88 error = yield89 if error.empty?90 puts "ok - %s" % [description]91 else92 puts "not ok - %s: %s" % [description, error]93 puts ErrorLog.strip.gsub(/^/, '# ') if Backtraces94 end95 end96 def handle_summary; end97 end98 extend SpecDoxOutput # default99 class Error < RuntimeError100 attr_accessor :count_as101 def initialize(count_as, message)102 @count_as = count_as103 super message104 end105 end106 class Context107 attr_reader :name, :block108 109 def initialize(name, &block)110 @name = name111 @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 self261 else262 be(*args, &block)263 end264 end265 def be(*args, &block)266 if args.empty?267 self268 else269 block = args.shift unless block_given?270 satisfy(*args, &block)271 end272 end273 alias a be274 alias an be275 def satisfy(*args, &block)276 if args.size == 1 && String === args.first277 description = args.shift278 else279 description = ""280 end281 r = yield(@object, *args)282 if Bacon::Counter[:depth] > 0283 Bacon::Counter[:requirements] += 1284 raise Bacon::Error.new(:failed, description) unless @negated ^ r285 end286 @negated ^ r ? r : false287 end288 def method_missing(name, *args, &block)289 name = "#{name}?" if name.to_s =~ /\w[^?]\z/290 desc = @negated ? "not " : ""291 desc << @object.inspect << "." << name.to_s292 desc << "(" << args.map{|x|x.inspect}.join(", ") << ") failed"293 satisfy(desc) { |x| x.__send__(name, *args, &block) }294 end295 def equal(value) self == value end296 def match(value) self =~ value end297 def identical_to(value) self.equal? value end298 alias same_as identical_to299 def flunk(reason="Flunked")300 raise Bacon::Error.new(:failed, reason)301 end302end...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful