diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..7e2586f --- /dev/null +++ b/Rakefile @@ -0,0 +1,7 @@ +require 'rspec/core/rake_task' + +RSpec::Core::RakeTask::new(:spec) do |spec| + spec.fail_on_error = false +end + +task :default => :spec diff --git a/gemfiles/Gemfile-rails2 b/gemfiles/Gemfile-rails2 new file mode 100644 index 0000000..18bfa99 --- /dev/null +++ b/gemfiles/Gemfile-rails2 @@ -0,0 +1,5 @@ +source "http://rubygems.org" + +gemspec :path => File.expand_path('../..', __FILE__) +gem "rails", "~> 2.3" +File.open(File.expand_path("../Gemfile-shared", __FILE__)){|f| eval(f.read)} diff --git a/gemfiles/Gemfile-rails2.lock b/gemfiles/Gemfile-rails2.lock new file mode 100644 index 0000000..3d241dc --- /dev/null +++ b/gemfiles/Gemfile-rails2.lock @@ -0,0 +1,49 @@ +PATH + remote: /home/lite/work/validates_xml + specs: + validates_xml (1.0.2) + activerecord (>= 2.0.0) + +GEM + remote: http://rubygems.org/ + specs: + actionmailer (2.3.14) + actionpack (= 2.3.14) + actionpack (2.3.14) + activesupport (= 2.3.14) + rack (~> 1.1.0) + activerecord (2.3.14) + activesupport (= 2.3.14) + activeresource (2.3.14) + activesupport (= 2.3.14) + activesupport (2.3.14) + diff-lcs (1.1.3) + rack (1.1.2) + rails (2.3.14) + actionmailer (= 2.3.14) + actionpack (= 2.3.14) + activerecord (= 2.3.14) + activeresource (= 2.3.14) + activesupport (= 2.3.14) + rake (>= 0.8.3) + rake (0.9.2) + rspec (2.6.0) + rspec-core (~> 2.6.0) + rspec-expectations (~> 2.6.0) + rspec-mocks (~> 2.6.0) + rspec-core (2.6.4) + rspec-expectations (2.6.0) + diff-lcs (~> 1.1.2) + rspec-mocks (2.6.0) + sqlite3 (1.3.4) + sqlite3-ruby (1.3.3) + sqlite3 (>= 1.3.3) + +PLATFORMS + ruby + +DEPENDENCIES + rails (~> 2.3) + rspec (~> 2.6.0) + sqlite3-ruby + validates_xml! diff --git a/gemfiles/Gemfile-rails3 b/gemfiles/Gemfile-rails3 new file mode 100644 index 0000000..e8b2a9b --- /dev/null +++ b/gemfiles/Gemfile-rails3 @@ -0,0 +1,5 @@ +source "http://rubygems.org" + +gemspec :path => File.expand_path('../..', __FILE__) +gem "rails", "~> 3.0.0" +File.open(File.expand_path("../Gemfile-shared", __FILE__)){|f| eval(f.read)} diff --git a/gemfiles/Gemfile-rails3.lock b/gemfiles/Gemfile-rails3.lock new file mode 100644 index 0000000..c92623e --- /dev/null +++ b/gemfiles/Gemfile-rails3.lock @@ -0,0 +1,95 @@ +PATH + remote: /home/lite/work/validates_xml + specs: + validates_xml (1.0.2) + activerecord (>= 2.0.0) + +GEM + remote: http://rubygems.org/ + specs: + abstract (1.0.0) + actionmailer (3.0.10) + actionpack (= 3.0.10) + mail (~> 2.2.19) + actionpack (3.0.10) + activemodel (= 3.0.10) + activesupport (= 3.0.10) + builder (~> 2.1.2) + erubis (~> 2.6.6) + i18n (~> 0.5.0) + rack (~> 1.2.1) + rack-mount (~> 0.6.14) + rack-test (~> 0.5.7) + tzinfo (~> 0.3.23) + activemodel (3.0.10) + activesupport (= 3.0.10) + builder (~> 2.1.2) + i18n (~> 0.5.0) + activerecord (3.0.10) + activemodel (= 3.0.10) + activesupport (= 3.0.10) + arel (~> 2.0.10) + tzinfo (~> 0.3.23) + activeresource (3.0.10) + activemodel (= 3.0.10) + activesupport (= 3.0.10) + activesupport (3.0.10) + arel (2.0.10) + builder (2.1.2) + diff-lcs (1.1.3) + erubis (2.6.6) + abstract (>= 1.0.0) + i18n (0.5.0) + mail (2.2.19) + activesupport (>= 2.3.6) + i18n (>= 0.4.0) + mime-types (~> 1.16) + treetop (~> 1.4.8) + mime-types (1.16) + polyglot (0.3.2) + rack (1.2.4) + rack-mount (0.6.14) + rack (>= 1.0.0) + rack-test (0.5.7) + rack (>= 1.0) + rails (3.0.10) + actionmailer (= 3.0.10) + actionpack (= 3.0.10) + activerecord (= 3.0.10) + activeresource (= 3.0.10) + activesupport (= 3.0.10) + bundler (~> 1.0) + railties (= 3.0.10) + railties (3.0.10) + actionpack (= 3.0.10) + activesupport (= 3.0.10) + rake (>= 0.8.7) + rdoc (~> 3.4) + thor (~> 0.14.4) + rake (0.9.2) + rdoc (3.6.1) + rspec (2.6.0) + rspec-core (~> 2.6.0) + rspec-expectations (~> 2.6.0) + rspec-mocks (~> 2.6.0) + rspec-core (2.6.4) + rspec-expectations (2.6.0) + diff-lcs (~> 1.1.2) + rspec-mocks (2.6.0) + sqlite3 (1.3.4) + sqlite3-ruby (1.3.3) + sqlite3 (>= 1.3.3) + thor (0.14.6) + treetop (1.4.10) + polyglot + polyglot (>= 0.3.1) + tzinfo (0.3.30) + +PLATFORMS + ruby + +DEPENDENCIES + rails (~> 3.0.0) + rspec (~> 2.6.0) + sqlite3-ruby + validates_xml! diff --git a/gemfiles/Gemfile-shared b/gemfiles/Gemfile-shared new file mode 100644 index 0000000..b0d36a8 --- /dev/null +++ b/gemfiles/Gemfile-shared @@ -0,0 +1,2 @@ +gem "sqlite3-ruby" +gem "rspec", "~> 2.6.0" diff --git a/lib/validates_xml.rb b/lib/validates_xml.rb index 3198a66..6978f41 100644 --- a/lib/validates_xml.rb +++ b/lib/validates_xml.rb @@ -1,3 +1,5 @@ +require 'active_record' + module ValidatesXML def self.validates_xml(xml, options={}) begin diff --git a/runspecs b/runspecs new file mode 100644 index 0000000..0746427 --- /dev/null +++ b/runspecs @@ -0,0 +1,7 @@ +#!/usr/bin/env ruby + +%w[rails2 rails3].each do |rails| + ENV["BUNDLE_GEMFILE"] = "gemfiles/Gemfile-#{rails}" + system("bundle exec rake spec") +end + diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..f3f67d8 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,36 @@ +$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) +require 'bundler' +Bundler.require +require 'rexml/document' + +Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f} + +ActiveRecord::Migration.verbose = false +ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:") + +RSpec.configure do |config| + config.before(:all) do + create_database + end + + config.after(:all) do + drop_database + end + + config.extend Rails3::Execute +end + +def create_database + ActiveRecord::Schema.define(:version => 1) do + create_table :models do |t| + t.string :xml + t.string :xml_helper + end + end +end + +def drop_database + ActiveRecord::Base.connection.tables.each do |table| + ActiveRecord::Base.connection.drop_table(table) + end +end diff --git a/spec/support/model.rb b/spec/support/model.rb new file mode 100644 index 0000000..ea07bcf --- /dev/null +++ b/spec/support/model.rb @@ -0,0 +1,4 @@ +class Model < ActiveRecord::Base + validates_xml :xml + validates :xml_helper, :xml_format => true if ActiveRecord::VERSION::MAJOR > 2 +end diff --git a/spec/support/rails3.rb b/spec/support/rails3.rb new file mode 100644 index 0000000..7b82915 --- /dev/null +++ b/spec/support/rails3.rb @@ -0,0 +1,9 @@ +module Rails3 + module Execute + def rails3(&block) + if ActiveRecord::VERSION::MAJOR > 2 + block.call + end + end + end +end diff --git a/spec/validates_xml_spec.rb b/spec/validates_xml_spec.rb new file mode 100644 index 0000000..e53e892 --- /dev/null +++ b/spec/validates_xml_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper' + +describe "validates_xml" do + it "validation should pass the correct XML (with helper)" do + @model = Model.new(:xml => "hello") + @model.xml_helper = "hello" + @model.should be_valid + end + + it "validation should not pass the incorrect XML (with helper)" do + @model = Model.new(:xml => "hello<.xml>") + @model.xml_helper = "hello<.xml>" + @model.should_not be_valid + end + + rails3 do + it "validation should pass correct XML" do + @model = Model.new(:xml_helper => "hello") + @model.should be_valid + end + + it "validation should not pass incorrect XML" do + @model = Model.new(:xml_helper => "hello<.xml>") + @model.should_not be_valid + end + end +end