Permalink
Browse files

rspec support - test_helper and rake task

  • Loading branch information...
1 parent b3ab7d2 commit 281dd1862162e00d5c1944b9c9b61eb226f64f83 @xaviershay xaviershay committed Jul 5, 2008
Showing with 125 additions and 16 deletions.
  1. +5 −7 Rakefile
  2. +11 −0 spec/test_helper.rb
  3. +109 −0 spec/xpath_matchers.rb
  4. +0 −9 test/formtastic_test.rb
View
@@ -1,15 +1,13 @@
require 'rake'
-require 'rake/testtask'
require 'rake/rdoctask'
+require 'spec/rake/spectask'
-desc 'Default: run unit tests.'
-task :default => :test
+desc 'Default: run unit specs.'
+task :default => :spec
desc 'Test the formtastic plugin.'
-Rake::TestTask.new(:test) do |t|
- t.libs << 'lib'
- t.pattern = 'test/**/*_test.rb'
- t.verbose = true
+Spec::Rake::SpecTask.new('spec') do |t|
+ t.spec_files = FileList['spec/**/*_spec.rb']
end
desc 'Generate documentation for the formtastic plugin.'
View
@@ -0,0 +1,11 @@
+require 'rubygems'
+require 'spec'
+require 'activesupport'
+require 'active_support'
+require 'actionpack'
+require 'action_controller'
+require 'action_view'
+require 'rexml/document'
+require File.dirname(__FILE__) + '/xpath_matchers'
+
+$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
View
@@ -0,0 +1,109 @@
+# http://blog.wolfman.com/articles/2008/01/02/xpath-matchers-for-rspec
+
+module Spec
+ module Matchers
+
+ # check if the xpath exists one or more times
+ class HaveXpath
+ def initialize(xpath)
+ @xpath = xpath
+ end
+
+ def matches?(response)
+ @response = response
+ doc = response.is_a?(REXML::Document) ? response : REXML::Document.new(@response)
+ match = REXML::XPath.match(doc, @xpath)
+ not match.empty?
+ end
+
+ def failure_message
+ "Did not find expected xpath #{@xpath}"
+ end
+
+ def negative_failure_message
+ "Did find unexpected xpath #{@xpath}"
+ end
+
+ def description
+ "match the xpath expression #{@xpath}"
+ end
+ end
+
+ def have_xpath(xpath)
+ HaveXpath.new(xpath)
+ end
+
+ # check if the xpath has the specified value
+ # value is a string and there must be a single result to match its
+ # equality against
+ class MatchXpath
+ def initialize(xpath, val)
+ @xpath = xpath
+ @val= val
+ end
+
+ def matches?(response)
+ @response = response
+ doc = response.is_a?(REXML::Document) ? response : REXML::Document.new(@response)
+ ok= true
+ REXML::XPath.each(doc, @xpath) do |e|
+ @actual_val= case e
+ when REXML::Attribute
+ e.to_s
+ when REXML::Element
+ e.text
+ else
+ e.to_s
+ end
+ if @val.is_a?(String)
+ return false unless @val == @actual_val
+ else
+ return false unless @actual_val =~ @val
+ end
+ end
+ return ok
+ end
+
+ def failure_message
+ "The xpath #{@xpath} did not have the value '#{@val}'\nIt was '#{@actual_val}'"
+ end
+
+ def description
+ "match the xpath expression #{@xpath} with #{@val}"
+ end
+ end
+
+ def match_xpath(xpath, val)
+ MatchXpath.new(xpath, val)
+ end
+
+ # checks if the given xpath occurs num times
+ class HaveNodes #:nodoc:
+ def initialize(xpath, num)
+ @xpath= xpath
+ @num = num
+ end
+
+ def matches?(response)
+ @response = response
+ doc = response.is_a?(REXML::Document) ? response : REXML::Document.new(@response)
+ match = REXML::XPath.match(doc, @xpath)
+ @num_found= match.size
+ @num_found == @num
+ end
+
+ def failure_message
+ "Did not find expected number of nodes #{@num} in xpath #{@xpath}\nFound #{@num_found}"
+ end
+
+ def description
+ "match the number of nodes #{@num}"
+ end
+ end
+
+ def have_nodes(xpath, num)
+ HaveNodes.new(xpath, num)
+ end
+
+ end
+end
View
@@ -1,9 +0,0 @@
-require 'test/unit'
-
-class FormtasticTest < Test::Unit::TestCase
-
- def test_this_plugin
- flunk
- end
-
-end

0 comments on commit 281dd18

Please sign in to comment.