Permalink
Browse files

Better exceptions

  • Loading branch information...
1 parent 281e79f commit bf8ff23fc1dd7dc8cf5732245b95b9c8ba8cdc98 @jnicklas committed Apr 20, 2012
Showing with 29 additions and 88 deletions.
  1. +8 −0 examples/errors.feature
  2. +11 −5 lib/turnip.rb
  3. +10 −3 lib/turnip/builder.rb
  4. +0 −65 test.rb
  5. +0 −15 test_spec.rb
View
@@ -0,0 +1,8 @@
+Feature: raises errors
+ Scenario: Step missing
+ When a step just does not exist
+ Scenario: Step raises error
+ When raise error
+ Scenario: Incorrect expectation
+ Given there is a monster
+ Then it should die
View
@@ -44,11 +44,14 @@ module Steps
extend Define
include Execute
- def step(description, extra_arg=nil)
+ def run_step(feature_file, step)
begin
- super
+ step(step.description, step.extra_arg)
rescue Turnip::Pending
- pending("No such step: '#{description}'")
+ pending("No such step: '#{step.description}'")
+ rescue StandardError => e
+ e.backtrace.unshift "#{feature_file}:#{step.line}:in `#{step.description}'"
+ raise e
end
end
end
@@ -60,15 +63,18 @@ def run(feature_file)
Turnip::Builder.build(feature_file).features.each do |feature|
describe feature.name, feature.metadata_hash do
before do
+ # This is kind of a hack, but it will make RSpec throw way nicer exceptions
+ example.metadata[:file_path] = feature_file
+
feature.backgrounds.map(&:steps).flatten.each do |step|
- step(step.description, step.extra_arg)
+ run_step(feature_file, step)
end
end
feature.scenarios.each do |scenario|
describe scenario.name, scenario.metadata_hash do
it scenario.steps.map(&:description).join(' -> ') do
scenario.steps.each do |step|
- step(step.description, step.extra_arg)
+ run_step(feature_file, step)
end
end
end
View
@@ -33,6 +33,10 @@ def initialize(raw)
@backgrounds = []
end
+ def line
+ @raw.line
+ end
+
def metadata_hash
super.merge(:type => Turnip.type, :turnip => true)
end
@@ -76,14 +80,17 @@ def to_scenarios(examples)
Scenario.new(@raw).tap do |scenario|
scenario.steps = steps.map do |step|
new_description = step.description.gsub(/<([^>]*)>/) { |_| Hash[headers.zip(row)][$1] }
- Step.new(new_description, step.extra_arg)
+ Step.new(new_description, step.extra_arg, step.line)
end
end
end
end
end
- class Step < Struct.new(:description, :extra_arg)
+ class Step < Struct.new(:description, :extra_arg, :line)
+ def to_s
+ description
+ end
end
attr_reader :features
@@ -131,7 +138,7 @@ def step(step)
elsif step.rows
extra_arg = Turnip::Table.new(step.rows.map { |row| row.cells(&:value) })
end
- @current_step_context.steps << Step.new(step.name, extra_arg)
+ @current_step_context.steps << Step.new(step.name, extra_arg, step.line)
end
def eof
View
65 test.rb
@@ -1,65 +0,0 @@
-require "turnip/step_definition"
-require "turnip/placeholder"
-
-step = Turnip::StepDefinition.new("this is a :test") { |test| "foo #{test}" }
-
-module Step
- def self.define(object, text, &block)
- step = Turnip::StepDefinition.new(text, &block)
- object.send(:define_method, "step: #{step.expression}") { step }
- object.send(:define_method, "execute: #{step.expression}", &block)
- end
-
- def self.execute(object, text)
- match = find_step(object, text)
- object.send("execute: #{match.expression}", *match.params)
- end
-
- def self.find_step(object, text)
- object.methods.each do |method|
- method = method.to_s
- next unless method.start_with?("step:")
- match = object.send(method).match(text)
- return match if match
- end
- end
-end
-
-module DSL
- def step(text, &block)
- Step.define(self, text, &block)
- end
-end
-
-class Object
- def turnip_step(text)
- Step.execute(self, text)
- end
-end
-
-module Thing
- extend DSL
-
- step "foo (bar) :baz" do |baz|
- puts "in step #{baz}"
- end
-end
-
-module Schmoo
- include Thing
-end
-
-class Foo
- extend DSL
- include Schmoo
-
- step "foo (bar) :baz" do |baz|
- puts "inheritance"
- super(baz)
- end
-end
-
-foo = Foo.new
-foo.turnip_step("foo bar baz")
-foo.turnip_step('foo bar "Schmoo bar"')
-foo.turnip_step("foo baz")
View
@@ -1,15 +0,0 @@
-module Foo
- def foo
- puts "WorkS!"
- end
-end
-
-RSpec.configure do |config|
- config.include Foo, :bar => true
-end
-
-describe "test", :foo => true do
- it "does", :bar => true do
- foo
- end
-end

0 comments on commit bf8ff23

Please sign in to comment.