Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

created a helper for replacing sexp insttances in a sexp ... ie. for …

…replacing something like the local variable :name with the string 'Dog' all throughout a Sexp tree.
  • Loading branch information...
commit 59d635db56ba59e2efc323ad905f852a5b44cecc 1 parent f5aad4b
@remi remi authored
View
14 cucumber2rspec/lib/cucumber2rspec.rb
@@ -28,6 +28,20 @@ def self.translate feature_text
Cucumber2RSpec::Feature.new(feature_text).code
end
+ def self.replace_in_sexp sexp, to_replace, replace_with
+ while sexp.include?(to_replace)
+ index = sexp.index(to_replace)
+ sexp.delete_at index
+ sexp.insert index, replace_with
+ end
+
+ sexp.each do|obj|
+ replace_in_sexp(obj, to_replace, replace_with) if obj.is_a?(Sexp)
+ end
+
+ sexp
+ end
+
end
require 'cucumber2rspec/feature'
View
49 cucumber2rspec/spec/basic_with_variable_cucumber2rspec_spec.rb
@@ -0,0 +1,49 @@
+require File.dirname(__FILE__) + '/spec_helper'
+
+describe Cucumber2RSpec, 'basic' do
+
+ before do
+ code = File.read File.dirname(__FILE__) + '/features/basic_with_variable.feature'
+ @feature = Cucumber2RSpec::Feature.new code
+ end
+
+ it 'should be able to load up a feature and get its name' do
+ @feature.name.should == 'Manage dogs'
+ end
+
+ it 'should be able to get the scenarios for a feature' do
+ @feature.scenarios.length.should == 1
+ @feature.scenarios.first.name.should == 'Create a dog'
+ end
+
+=begin
+ it "Create a dog" do
+ @dogs.should(be_nil)
+ @your_mom = "Mommy"
+
+ puts "name is the name of the block argument above"
+ @dogs ||= []
+ @dogs << "Rover"
+ @view = @dogs.inspect
+
+ @your_mom.should_not(be_nil)
+ @view.should_not(be_nil)
+ @view.should(include("A dog"))
+ end
+=end
+ it 'should be able to get the code for a step' do
+ scenario = @feature.scenarios.first
+
+ scenario.steps[0].keyword.should == 'Given'
+ scenario.steps[0].code.should == '@dogs.should(be_nil)'
+ scenario.steps[1].keyword.should == 'Given'
+ scenario.steps[1].code.should == '@your_mom = "Mommy"'
+
+ scenario.steps[2].keyword.should == 'When'
+ scenario.steps[2].code.should == 'puts "name is the name of the block argument above"' +
+ "@dogs ||= []\n(@dogs << \"Rover\")" # <--- we strip spaces
+
+ #scenario.steps[3].keyword.should == 'Then'
+ #scenario.steps[3].code.should == '@your_mom.should_not(be_nil)'
+ end
+end
View
17 cucumber2rspec/spec/desired_specs/basic_with_variable_spec.rb
@@ -0,0 +1,17 @@
+describe "Manage dogs" do
+
+ it "Create a dog" do
+ @dogs.should(be_nil)
+ @your_mom = "Mommy"
+
+ puts "name is the name of the block argument above"
+ @dogs ||= []
+ @dogs << "Rover"
+ @view = @dogs.inspect
+
+ @your_mom.should_not(be_nil)
+ @view.should_not(be_nil)
+ @view.should(include("A dog"))
+ end
+
+end
View
11 cucumber2rspec/spec/features/basic_with_variable.feature
@@ -0,0 +1,11 @@
+Feature: Manage dogs
+ In order to keep track of dogs
+ As a user
+ I want to be able to manage dogs
+
+Scenario: Create a dog
+ Given there are no dogs
+ And your mom
+ When I create a dog named "Rover"
+ And I view all dogs
+ Then your mom should see a dog
View
1  cucumber2rspec/spec/features/the_steps.rb
@@ -7,6 +7,7 @@
end
When /^I create a dog named "([^\"]*)"$/ do |name|
+ puts "name is the name of the block argument above"
@dogs ||= []
@dogs << name
end
View
60 cucumber2rspec/spec/sexp_stuff_spec.rb
@@ -0,0 +1,60 @@
+require File.dirname(__FILE__) + '/spec_helper'
+
+describe 'Sexp stuff' do
+
+ def do_stuff(sexp)
+ Cucumber2RSpec.replace_in_sexp sexp, Sexp.new(:lvar, :name), Sexp.new(:str, "Rover")
+ end
+
+ it 'should be able to replace a local variable in a simple Sexp' do
+ real = ParseTree.new.process(proc { |name| puts name }.to_ruby)
+ desired = ParseTree.new.process(proc { |name| puts "Rover" }.to_ruby)
+
+ real.should_not == desired
+ do_stuff(real).should == desired
+ end
+
+ it 'should be able to replace a local variable in a bigger Sexp' do
+ real = ParseTree.new.process(proc { |name| if 1 == 2 then name end }.to_ruby)
+ desired = ParseTree.new.process(proc { |name| if 1 == 2 then "Rover" end }.to_ruby)
+
+ real.should_not == desired
+ do_stuff(real).should == desired
+ end
+
+ it 'should be able to replace a local variable in a even bigger Sexp' do
+ real = ParseTree.new.process(proc { |name|
+ if name.include?('hi')
+ puts 'w00t ' + name + "includes 'hi'"
+ else
+ puts name + " does not include 'hi'"
+ end
+ }.to_ruby)
+
+ desired = ParseTree.new.process(proc { |name|
+ if "Rover".include?('hi')
+ puts 'w00t ' + "Rover" + "includes 'hi'"
+ else
+ puts "Rover" + " does not include 'hi'"
+ end
+ }.to_ruby)
+
+ real.should_not == desired
+ do_stuff(real).should == desired
+ end
+
+ # creating the desired result fails for this one because ParseTree takes "hi #{"String"}"
+ # and squishes them down into "hi String" which is kind of ideal but it's not super
+ # easy to reproduce so I'd like to hold off on trying to implement that!
+ it 'should be able to replace a local variable in a interpolated string' do
+ real = ParseTree.new.process(proc { |name| puts "the name is: #{ name }" }.to_ruby)
+
+ do_stuff(real)
+
+ Ruby2Ruby.new.process(real).should == 'proc { |name| puts("the name is: #{"Rover"}") }'
+
+ #real.should_not == desired
+ #do_stuff(real).should == desired
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.