Permalink
Browse files

refactor tests for acts_as_textiled completely

  • Loading branch information...
1 parent da225b4 commit d5fd7eebebede89cb9ffc152d6e08d2364df9bc0 @defunkt committed Jun 3, 2007
Showing with 233 additions and 210 deletions.
  1. +7 −4 lib/acts_as_textiled.rb
  2. +0 −18 test/database.yml
  3. +87 −0 test/helper.rb
  4. +0 −12 test/memory_test_fix.rb
  5. +0 −13 test/schema.rb
  6. +0 −35 test/setup_test.rb
  7. +0 −128 test/test_textiled.rb
  8. +139 −0 test/textiled_test.rb
View
@@ -8,7 +8,6 @@ def self.included(klass)
module ClassMethods
def acts_as_textiled(*attrs)
@textiled_attributes = []
- def textiled_attributes; Array(@textiled_attributes) end
@textiled_unicode = "".respond_to? :chars
@@ -17,7 +16,7 @@ def textiled_attributes; Array(@textiled_attributes) end
attrs.each do |attr|
define_method(attr) do
- textiled[attr.to_s] ||= RedCloth.new(read_attribute(attr), Array(ruled[attr])).to_html if read_attribute(attr)
+ textiled[attr.to_s] ||= RedCloth.new(self[attr], Array(ruled[attr])).to_html if self[attr]
end
define_method("#{attr}_plain", proc { strip_redcloth_html(__send__(attr)) if __send__(attr) } )
define_method("#{attr}_source", proc { __send__("#{attr}_before_type_cast") } )
@@ -26,6 +25,10 @@ def textiled_attributes; Array(@textiled_attributes) end
include Err::Acts::Textiled::InstanceMethods
end
+
+ def textiled_attributes
+ Array(@textiled_attributes)
+ end
end
module InstanceMethods
@@ -34,7 +37,7 @@ def textiled
end
def textiled?
- @is_textiled || true
+ @is_textiled != false
end
def textiled=(bool)
@@ -52,7 +55,7 @@ def reload
def write_attribute(attr_name, value)
textiled[attr_name.to_s] = nil
- super(attr_name, value)
+ super
end
private
View
@@ -1,18 +0,0 @@
-sqlite:
- :adapter: sqlite
- :dbfile: ':memory:'
-sqlite3:
- :adapter: sqlite3
- :dbfile: ':memory:'
-postgresql:
- :adapter: postgresql
- :username: postgres
- :password: postgres
- :database: acts_as_textiled_plugin_test
- :min_messages: ERROR
-mysql:
- :adapter: mysql
- :host: localhost
- :username: root
- :password:
- :database: acts_as_textiled_plugin_test
View
@@ -0,0 +1,87 @@
+$:.unshift File.dirname(__FILE__) + '/../lib'
+
+begin
+ require 'rubygems'
+ require 'yaml'
+ require 'mocha'
+ require 'active_support'
+ require 'test/spec'
+ require 'RedCloth'
+rescue LoadError
+ puts "acts_as_textiled requires the mocha and test-spec gems to run its tests"
+ exit
+end
+
+class ActiveRecord
+ class Base
+ attr_reader :attributes
+
+ def initialize(attributes = {})
+ @attributes = attributes.dup
+ after_find if respond_to?(:after_find)
+ end
+
+ def method_missing(name, *args)
+ if name.to_s[/=/]
+ @attributes[key = name.to_s.sub('=','')] = value = args.first
+ write_attribute key, value
+ else
+ self[name.to_s]
+ end
+ end
+
+ def save
+ true
+ end
+
+ def reload
+ self
+ end
+
+ def [](value)
+ @attributes[value.to_s.sub('_before_type_cast', '')]
+ end
+
+ def self.global
+ eval("$#{name.downcase}")
+ end
+
+ def self.find(id)
+ item = global.detect { |key, hash| hash['id'] == id }.last
+ new(item)
+ end
+ end
+end unless defined? ActiveRecord
+
+require File.dirname(__FILE__) + '/../init'
+
+class Author < ActiveRecord::Base
+ acts_as_textiled :blog => :lite_mode
+end
+
+class Story < ActiveRecord::Base
+ acts_as_textiled :body, :description => :lite_mode
+
+ def author
+ @author ||= Author.find(author_id)
+ end
+end
+
+class StoryWithAfterFind < Story
+ acts_as_textiled :body, :description => :lite_mode
+
+ def after_find
+ textilize
+ end
+
+ def self.name
+ Story.name
+ end
+
+ def author
+ @author ||= Author.find(author_id)
+ end
+end
+
+$author = YAML.load_file(File.dirname(__FILE__) + '/fixtures/authors.yml')
+$story = YAML.load_file(File.dirname(__FILE__) + '/fixtures/stories.yml')
View
@@ -1,12 +0,0 @@
-# MemoryTestFix
-def in_memory_database?
- ENV["RAILS_ENV"] == "test" and
- ActiveRecord::Base.connection.class == ActiveRecord::ConnectionAdapters::SQLiteAdapter and
- Rails::Configuration.new.database_configuration['test']['database'] == ':memory:'
-end
-
-if in_memory_database?
- puts "Creating sqlite in memory database"
- load "#{RAILS_ROOT}/db/schema.rb" # use db agnostic schema by default
-# ActiveRecord::Migrator.up('db/migrate') # use migrations
-end
View
@@ -1,13 +0,0 @@
-ActiveRecord::Schema.define(:version => 0) do
- create_table :stories, :force => true do |t|
- t.column :title, :string
- t.column :description, :string
- t.column :body, :text
- t.column :author_id, :integer
- end
-
- create_table :authors, :force => true do |t|
- t.column :name, :string
- t.column :blog, :string
- end
-end
View
@@ -1,35 +0,0 @@
-# borrowed from topfunky and scott barron, thanks
-require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
-require 'active_record/fixtures'
-require File.join(File.dirname(__FILE__), 'fixtures/story')
-require File.join(File.dirname(__FILE__), 'fixtures/author')
-require File.join(File.dirname(__FILE__), 'memory_test_fix')
-require 'test/unit'
-
-config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
-ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
-ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite3'])
-
-load(File.dirname(__FILE__) + "/schema.rb")
-
-Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
-$LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
-
-
-class Test::Unit::TestCase #:nodoc:
- def create_fixtures(*table_names)
- if block_given?
- Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
- else
- Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
- end
- end
-
- # Turn off transactional fixtures if you're working with MyISAM tables in MySQL
- self.use_transactional_fixtures = true
-
- # Instantiated fixtures are slow, but give you @david where you otherwise would need people(:david)
- self.use_instantiated_fixtures = false
-
- # Add more helper methods to be used by all tests here...
-end
View
@@ -1,128 +0,0 @@
-require File.join(File.dirname(__FILE__), 'setup_test')
-
-class TestTextiled < Test::Unit::TestCase
- fixtures :stories, :authors
-
- def test_empty_fields
- story = Story.new
-
- assert_equal nil, story.description
- assert_equal nil, story.description_source
- assert_equal nil, story.description_plain
- end
-
- def test_desc_is_html
- story = Story.find(1)
-
- desc_html = '_why announces <i>Sandbox</i>'
- desc_textile = '_why announces __Sandbox__'
- desc_plain = '_why announces Sandbox'
-
- assert_equal desc_html, story.description
- assert_equal desc_textile, story.description_source
- assert_equal desc_plain, story.description_plain
-
- # make sure we don't overwrite anything - thanks James
- assert_equal desc_html, story.description
- assert_equal desc_textile, story.description_source
- assert_equal desc_plain, story.description_plain
- end
-
- def test_desc_after_save
- story = Story.find(2)
-
- start_html = '<i>Beautify</i> your <strong>IRb</strong> prompt'
- assert_equal start_html, story.description
-
- story.description = "**IRb** is simple"
- changed_html = "<b>IRb</b> is simple"
- assert_equal changed_html, story.description
- story.save
- assert_equal changed_html, story.description
- assert_equal 'IRb is simple', story.description_plain
- end
-
- def test_desc_toggle_textile
- story = Story.find(2)
-
- desc_html = '<i>Beautify</i> your <strong>IRb</strong> prompt'
- desc_textile = '__Beautify__ your *IRb* prompt'
-
- assert_equal desc_html, story.description
- story.textiled = false
- assert_equal desc_textile, story.description
- story.save
- assert_equal desc_textile, story.description
- story.textiled = true
- assert_equal desc_html, story.description
- end
-
- def test_assocation_textiled
- story = Story.find(2)
-
- blog_html = '<a href="http://ozmm.org">ones zeros majors and minors</a>'
- blog_textile = '"ones zeros majors and minors":http://ozmm.org'
- blog_plain = 'ones zeros majors and minors'
-
- assert_equal blog_html, story.author.blog
- assert_equal blog_textile, story.author.blog_source
- assert_equal blog_plain, story.author.blog_plain
- end
-
- def test_assocation_textile_toggle
- story = Story.find(1)
-
- blog_html = '<a href="http://redhanded.hobix.com">RedHanded</a>'
- blog_textile = '"RedHanded":http://redhanded.hobix.com'
- blog_plain = 'RedHanded'
-
- assert_equal blog_html, story.author.blog
- story.author.textiled = false
- assert_equal blog_textile, story.author.blog
- story.author.textiled = true
- assert_equal blog_html, story.author.blog
- end
-
- def test_body_is_html
- story = Story.find(3)
-
- body_html = %[<p><em>Textile</em> is useful because it makes text <em>slightly</em> easier to <strong>read</strong>.</p>\n\n\n\t<p>If only it were so <strong>easy</strong> to use in every programming language. In Rails,\nwith the help of <a href="http://google.com/search?q=acts_as_textiled">acts_as_textiled</a>,\nit&#8217;s way easy. Thanks in no small part to <span style="color:red;">RedCloth</span>, of course.</p>]
- body_textile = %[_Textile_ is useful because it makes text _slightly_ easier to *read*.\n\nIf only it were so *easy* to use in every programming language. In Rails,\nwith the help of "acts_as_textiled":http://google.com/search?q=acts_as_textiled,\nit's way easy. Thanks in no small part to %{color:red}RedCloth%, of course.\n]
- body_plain = %[Textile is useful because it makes text slightly easier to read.\n\n\n\tIf only it were so easy to use in every programming language. In Rails,\nwith the help of acts_as_textiled,\nit's way easy. Thanks in no small part to RedCloth, of course.]
-
- assert_equal body_html, story.body
- assert_equal body_textile, story.body_source
- assert_equal body_plain, story.body_plain
- end
-
- def test_character_conversions
- story = Story.find(4)
-
- body_html = "<p>Is Textile&#8482; the wave of the future? What about acts_as_textiled&#169;? It&#8217;s\ndoubtful. Why does Textile&#8482; smell like <em>Python</em>? Can we do anything to\nfix that? No? Well, I guess there are worse smells &#8211; like Ruby. jk.</p>\n\n\n\t<p>But seriously, ice &gt; water and water &lt; rain. But&#8230;nevermind. 1&#215;1? 1.</p>\n\n\n\t<p>&#8220;You&#8217;re a good kid,&#8221; he said. &#8220;Keep it up.&#8221;</p>"
- body_plain = %[Is Textile(TM) the wave of the future? What about acts_as_textiled(C)? It's\ndoubtful. Why does Textile(TM) smell like Python? Can we do anything to\nfix that? No? Well, I guess there are worse smells-like Ruby. jk.\n\n\n\tBut seriously, ice > water and water < rain. But...nevermind. 1x1? 1.\n\n\n\t"You're a good kid," he said. "Keep it up."]
-
- assert_equal body_html, story.body
- assert_equal body_plain, story.body_plain
- end
-
- def test_textilize
- story = Story.find(1)
- desc_html = '_why announces <i>Sandbox</i>'
-
- assert_equal 0, story.textiled.size
-
- story.textilize
-
- assert_equal 2, story.textiled.size
- assert_equal desc_html, story.description
- end
-
- def test_textilize_after_find
- Story.send(:define_method, :after_find, proc { textilize })
- story = Story.find(2)
- desc_html = '<i>Beautify</i> your <strong>IRb</strong> prompt'
-
- assert_equal 2, story.textiled.size
- assert_equal desc_html, story.description
- end
-end
Oops, something went wrong.

0 comments on commit d5fd7ee

Please sign in to comment.