From be072f5ff12fd45fa6bd34a7bd4f1ec3bbb045dd Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 18 Sep 2012 11:32:23 -0400 Subject: [PATCH] add some new things --- lib/cuke-pack/support/expect_fields.rb | 37 ++++++++++++++++++++++++ lib/cuke-pack/support/rails_generator.rb | 37 ++++++++++++++++++++++++ skel/features/support/cuke-pack.rb | 6 ++++ 3 files changed, 80 insertions(+) create mode 100644 lib/cuke-pack/support/expect_fields.rb create mode 100644 lib/cuke-pack/support/rails_generator.rb diff --git a/lib/cuke-pack/support/expect_fields.rb b/lib/cuke-pack/support/expect_fields.rb new file mode 100644 index 0000000..53170a7 --- /dev/null +++ b/lib/cuke-pack/support/expect_fields.rb @@ -0,0 +1,37 @@ +def expect_fields(object, *fields, &block) + @__expect_stack ||= 0 + @__expect_stack += 1 + + options = {} + + if fields.last.kind_of?(::Hash) + options = fields.pop.dup + end + + search_type = @__expect_stack == 1 ? "#" : "." + + if object.respond_to?(:each) + within "#{search_type}#{object.first.class.name.underscore.pluralize}" do + object.each_with_index do |subobject, index| + expect_fields subobject, fields, options.merge(:index => index), &block + end + end + else + finder = "#{search_type}#{object.class.name.underscore}" + if options[:index] + finder << ":eq(#{options[:index] + 1})" + end + + within finder do + fields.flatten.each do |field| + find(".#{field}").text.should == object.send(field).to_s + end + + block.call(object) if block + end + end + + @__expect_stack -= 1 + @__expect_stack = nil if @__expect_stack == 0 +end + diff --git a/lib/cuke-pack/support/rails_generator.rb b/lib/cuke-pack/support/rails_generator.rb new file mode 100644 index 0000000..3b53483 --- /dev/null +++ b/lib/cuke-pack/support/rails_generator.rb @@ -0,0 +1,37 @@ +Around do |scenario, code| + code.call + + case scenario.exception + when ActionController::RoutingError + if class_name = scenario.exception.message[%r{uninitialized constant (.*Controller)}, 1] + filename = class_name.underscore + + File.open("app/controllers/#{filename}.rb", 'w') { |fh| + fh.print <<-RB +class #{class_name} < ApplicationController +end + RB + } + end + when AbstractController::ActionNotFound + if matches = scenario.exception.message.match(%r{The action '(.*)' could not be found for (.*)Controller}) + _, action, class_name = matches.to_a + + target = Pathname('app/views').join(class_name.underscore).join("#{action}.html.haml") + + if %w{show edit index}.include?(action) + target.parent.mkpath + target.open('w') { |fh| + case action + when 'show', 'edit' + fh.puts "##{class_name.underscore.singular}" + when 'index' + fh.puts "##{class_name.underscore}= render @#{class_name.underscore}" + end + } + end + end + end +end + + diff --git a/skel/features/support/cuke-pack.rb b/skel/features/support/cuke-pack.rb index 4c985b3..30f115e 100644 --- a/skel/features/support/cuke-pack.rb +++ b/skel/features/support/cuke-pack.rb @@ -1,6 +1,7 @@ require 'cuke-pack/support/pause' require 'cuke-pack/support/pending' require 'cuke-pack/support/confirm_js' +require 'cuke-pack/support/expect_fields' Before do # if you want pending steps to pause before marking the step as pending, @@ -28,3 +29,8 @@ require 'cuke-pack/support/flay' # Browser drivers # use with ENV['DRIVER'] # require 'cuke-pack/driver/firefox' +# +# Simple rails controller/view generator +# probably only any good for me +# require 'cuke-pack/support/rails_generator' +