lib/futest.rb in futest-0.0.1 vs lib/futest.rb in futest-0.0.2

- old
+ new

@@ -6,13 +6,14 @@ ############## # Prints error message and stops execution def halt(str, obj = nil, n = x(caller)) m = "#{n}: #{str}" - if obj and obj.errors.any? - n = obj.errors.messages - m += ":\n=> " + n.each{|k, v| n[k] = v.join(', ')}.to_json[1..-2].gsub('","', '", "') + # Support for errors when using Object DB ORM + if obj and obj.errors and obj.errors.any? and obj.errors.messages + q = obj.errors.messages + m += ":\n=> " + q.each{|k, v| q[k] = v.join(', ')}.to_json[1..-2].gsub('","', '", "') end puts red(%{#{m}}) puts exit(0) end @@ -24,21 +25,32 @@ puts green("#{n}: #{args[0]}") end # Equality tester def is(v1, v2, n = x(caller)) - halt("#{v1} is #{v2}", nil, n) unless v1 == v2 - end + v2 = {:eq => v2} unless v2.is_a?(Hash) + # Extract options here with delete. + # No options available at the moment. - # Greater than tester - def gt(v1, v2, n = x(caller)) - halt("#{v1} gt #{v2}", nil, n) unless v1 > v2 - end + # For key output + def fs(y);{:eq => '==', :gt => '>', :lt => '<', :a? => 'is a'}[y] rescue y;end - # Less than tester - def lt(v1, v2, n = x(caller)) - halt("#{v1} lt #{v2}", nil, n) unless v1 < v2 + # Symbolize keys and extract values + k, v = v2.inject({}){|q,(k,v)|q[k.to_sym] = v; q}.to_a.flatten + s = ["#{v1.class} #{v1} #{fs(k)} #{v}", nil, n] + case k + when :eq + halt(*s) unless v1 == v + when :gt + halt(*s) unless v1 > v + when :lt + halt(*s) unless v1 < v + when :a? + halt(*s) unless v1.is_a?(v) + else + puts "#{k}: Command not supported." + end end ############## # HELPER METHODS ############## @@ -50,10 +62,10 @@ def green(text);"\e[33m#{text}\e[0m";end # Print error message def e(y) y.backtrace.first.match(/(\/.+\/.*.rb):(\d{1,9}):/) - halt(%{#{y.message}\n=> ~/#{$1.split('/')[3..-1].join('/')}}, nil, $2) if $1.present? and $2.present? + halt(%{#{y.message}\n=> ~/#{$1.split('/')[3..-1].join('/')}}, nil, $2) if $1 and $2 halt(%{#{y.message}\n=> #{y.backtrace.join("\n")}}) end # Get the line number def x(q)