Skip to content

Commit

Permalink
added norwegian examples and extracted step node
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed May 26, 2008
1 parent 17f44a5 commit b45f461
Show file tree
Hide file tree
Showing 21 changed files with 305 additions and 390 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
nbproject
pkg
pkg
log/*.log
110 changes: 60 additions & 50 deletions Manifest.txt
Original file line number Diff line number Diff line change
@@ -1,50 +1,60 @@
History.txt
License.txt
Manifest.txt
README.txt
Rakefile
bin/cucumber
config/hoe.rb
config/requirements.rb
examples/simple/addition.story
examples/simple/steps/addition_steps.rb
examples/web/run_stories.story
examples/web/steps/stories_steps.rb
lib/cucumber.rb
lib/cucumber/ansi_colours.rb
lib/cucumber/cli.rb
lib/cucumber/executor.rb
lib/cucumber/parser/languages.yml
lib/cucumber/parser/story_parser.rb
lib/cucumber/parser/story_parser.treetop.erb
lib/cucumber/parser/story_parser_en.rb
lib/cucumber/parser/story_parser_fr.rb
lib/cucumber/parser/story_parser_no.rb
lib/cucumber/parser/story_parser_pt.rb
lib/cucumber/pretty_printer.rb
lib/cucumber/progress_formatter.rb
lib/cucumber/step_methods.rb
lib/cucumber/version.rb
script/console
script/console.cmd
script/destroy
script/destroy.cmd
script/generate
script/generate.cmd
script/txt2html
script/txt2html.cmd
setup.rb
spec/cucumber/executor_spec.rb
spec/cucumber/sell_cucumbers.story
spec/spec.opts
spec/spec_helper.rb
tasks/deployment.rake
tasks/environment.rake
tasks/rspec.rake
tasks/treetop.rake
tasks/website.rake
website/index.html
website/index.txt
website/javascripts/rounded_corners_lite.inc.js
website/stylesheets/screen.css
website/template.html.erb
History.txt
License.txt
Manifest.txt
README.txt
Rakefile
bin/cucumber
config/hoe.rb
config/requirements.rb
examples/simple/addition.story
examples/simple/division.story
examples/simple/steps/addition_steps.rb
examples/simple_norwegian/steg/matte_steg.rb.rb
examples/simple_norwegian/summering.story
examples/web/run_stories.story
examples/web/steps/stories_steps.rb
lib/cucumber.rb
lib/cucumber/ansi_colours.rb
lib/cucumber/cli.rb
lib/cucumber/executor.rb
lib/cucumber/parser/languages.yml
lib/cucumber/parser/nodes.rb
lib/cucumber/parser/story_parser.rb
lib/cucumber/parser/story_parser.treetop.erb
lib/cucumber/parser/story_parser_en.rb
lib/cucumber/parser/story_parser_fr.rb
lib/cucumber/parser/story_parser_no.rb
lib/cucumber/parser/story_parser_pt.rb
lib/cucumber/pretty_printer.rb
lib/cucumber/progress_formatter.rb
lib/cucumber/rails/world.rb
lib/cucumber/step_methods.rb
lib/cucumber/stories.rb
lib/cucumber/version.rb
lib/cucumber/visitors/html_formatter.rb
log/debug.log
script/console
script/console.cmd
script/destroy
script/destroy.cmd
script/generate
script/generate.cmd
script/txt2html
script/txt2html.cmd
setup.rb
spec/cucumber/executor_spec.rb
spec/cucumber/sell_cucumbers.story
spec/cucumber/visitors/html_formatter_spec.rb
spec/cucumber/visitors/stories.html
spec/spec.opts
spec/spec_helper.rb
tasks/deployment.rake
tasks/environment.rake
tasks/rspec.rake
tasks/treetop.rake
tasks/website.rake
website/index.html
website/index.txt
website/javascripts/rounded_corners_lite.inc.js
website/stylesheets/screen.css
website/template.html.erb
31 changes: 31 additions & 0 deletions examples/simple_norwegian/steg/matte_steg.rb.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'spec'

class Calculator
def push(n)
@args ||= []
@args << n
end

def add
@args.inject(0){|n,sum| sum+=n}
end
end

Before do
@calc = Calculator.new
end

After do
end

Given /at jeg har tastet inn (\d+)/ do |n|
@calc.push n.to_i
end

When 'jeg summerer' do
@result = @calc.add
end

Then /skal resultatet være (\d*)/ do |result|
@result.should == result.to_i
end
10 changes: 10 additions & 0 deletions examples/simple_norwegian/summering.story
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Historie: Addition
Som en matteidiot
Vil jeg bli fortalt summen av to tall
Slik at jeg ikke gjør dumme feil

Scenario: 5+7
Gitt at jeg har tastet inn 5
Og at jeg har tastet inn 7
Når jeg summerer
skal resultatet være 12
3 changes: 2 additions & 1 deletion lib/cucumber.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
$:.unshift(File.dirname(__FILE__)) unless
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
require 'rubygems'
require 'treetop'
require 'treetop/runtime'
require 'treetop/ruby_extensions'
require 'cucumber/version'
require 'cucumber/cli'
require 'cucumber/executor'
Expand Down
12 changes: 10 additions & 2 deletions lib/cucumber/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ def execute!
require "cucumber/parser/story_parser_#{@options[:lang]}"
$executor = Executor.new(formatter)
libs = @options[:require].map{|path| File.directory?(path) ? Dir["#{path}/**/*.rb"] : path}.flatten
libs.each{|lib| require lib}
libs.each do |lib|
begin
require lib
rescue LoadError => e
e.message << "\nFailed to load #{lib}"
raise e
end
end

$executor.visit_stories(stories)
end
Expand All @@ -59,7 +66,8 @@ def stories
def formatter
klass = {
'progress' => ProgressFormatter,
'html' => Visitors::HtmlFormatter
'html' => Visitors::HtmlFormatter,
'pretty' => PrettyPrinter,
}[@options[:format]]
klass.new(STDOUT)
end
Expand Down
25 changes: 18 additions & 7 deletions lib/cucumber/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@ def mod

def initialize(formatter)
@formatter = formatter
@world_proc = lambda{ Object.new }
@step_procs = {}
@before_procs = []
@after_procs = []
end

def register_world_proc(&proc)
@world_proc = proc
end

def register_before_proc(&proc)
proc.extend(CallIn)
@before_procs << proc
Expand All @@ -60,7 +65,7 @@ def register_after_proc(&proc)
def register_step_proc(key, &proc)
regexp = case(key)
when String
Regexp.new(key)
Regexp.new("^#{key}$")
when Regexp
key
else
Expand All @@ -82,24 +87,27 @@ def visit_story(story)
end

def visit_header(header)
@formatter.header_executing(header) if @formatter.respond_to?(:header_executing)
end

def visit_narrative(narrative)
@formatter.narratrive_executing(narrative) if @formatter.respond_to?(:narratrive_executing)
end

def visit_scenario(scenario)
@error = nil
@context = Object.new
@before_procs.each{|p| p.call_in(@context, *[])}
@world = @world_proc.call
@formatter.scenario_executing(scenario) if @formatter.respond_to?(:scenario_executing)
@before_procs.each{|p| p.call_in(@world, *[])}
scenario.accept(self)
@after_procs.each{|p| p.call_in(@context, *[])}
@after_procs.each{|p| p.call_in(@world, *[])}
end

def visit_step(step)
if @error.nil?
proc, args = find_step_proc(step.name)
begin
proc.call_in(@context, *args)
proc.call_in(@world, *args)
rescue ArgCountError => e
e.backtrace[0] = proc.backtrace_line
strip_pos = e.backtrace.index("#{__FILE__}:#{__LINE__-3}:in `visit_step'")
Expand All @@ -116,12 +124,15 @@ def visit_step(step)

def find_step_proc(name)
args = nil
regexp_proc_arr = @step_procs.select do |regexp, _| # TODO: Fix for Ruby 1.9
regexp_proc_arr = @step_procs.select do |regexp, _|
if name =~ regexp
args = $~.captures
end
end
raise "Too many" if regexp_proc_arr.length > 1
if regexp_proc_arr.length > 1
regexen = regexp_proc_arr.transpose[0].map{|re| re.inspect}.join("\n ")
raise "\"#{name}\" matches several steps:\n\n #{regexen}\n\nPlease give your steps unambiguous names\n"
end
regexp_proc = regexp_proc_arr[0]
regexp_proc.nil? ? [PENDING, []] : [regexp_proc[1], args]
end
Expand Down
34 changes: 33 additions & 1 deletion lib/cucumber/parser/nodes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Cucumber
module Parser
class StoryNode
class StoryNode < Treetop::Runtime::SyntaxNode
def self.parse(file, parser)
story = parser.parse(IO.read(file))
if story.nil?
Expand All @@ -20,5 +20,37 @@ def accept(visitor)
end
end
end

class StepNode < Treetop::Runtime::SyntaxNode
class << self
def new_id!
@next_id ||= -1
@next_id += 1
end
end

attr_accessor :error

def line
input.line_of(interval.first)
end

def name
sentence.text_value.strip
end

def keyword
step_type.text_value.strip
end

def file
parent.parent.file
end

def id
@id ||= self.class.new_id!
end

end
end
end
20 changes: 1 addition & 19 deletions lib/cucumber/parser/story_parser.treetop.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,7 @@ grammar Story
end

rule step
space step_type:('<%= words['given'] %>' / '<%= words['when'] %>' / '<%= words['then'] %>'/ '<%= words['and'] %>' / '<%= words['given_scenario'] %>') space sentence:sentence_line {
attr_accessor :error

def line
input.line_of(interval.first)
end

def name
sentence.text_value.strip
end

def keyword
step_type.text_value.strip
end

def file
parent.parent.file
end
}
space step_type:('<%= words['given'] %>' / '<%= words['when'] %>' / '<%= words['then'] %>'/ '<%= words['and'] %>' / '<%= words['given_scenario'] %>') space sentence:sentence_line <Cucumber::Parser::StepNode>
end

rule sentence_line
Expand Down
Loading

0 comments on commit b45f461

Please sign in to comment.