Browse files

add initial test suite

  • Loading branch information...
1 parent 5fa5e83 commit 73e92fdbc609a0ddd86c59cb17d904b884d6245f Ryan Sonnek committed Jun 24, 2009
Showing with 137 additions and 2 deletions.
  1. +1 −0 .gitignore
  2. +28 −0 lib/acts_as_stripped.rb
  3. +23 −2 test/acts_as_stripped_test.rb
  4. +3 −0 test/database.yml
  5. +77 −0 test/debug.log
  6. +5 −0 test/test_helper.rb
View
1 .gitignore
@@ -3,3 +3,4 @@
coverage
rdoc
pkg
+*.sqlite3
View
28 lib/acts_as_stripped.rb
@@ -0,0 +1,28 @@
+module ActsAsStripped
+ def self.included(base)
+ base.extend(ClassMethods)
+ end
+
+ module ClassMethods
+ def acts_as_stripped
+ before_validation :strip_fields
+
+ include ActsAsStripped::InstanceMethods
+ extend ActsAsStripped::SingletonMethods
+ end
+ end
+
+ module SingletonMethods
+ end
+
+ module InstanceMethods
+ private
+ def strip_fields
+ self.attributes.each_key {|a| self[a].strip! if self[a].respond_to? :strip! }
+ end
+ end
+end
+
+if Object.const_defined?("ActiveRecord")
+ ActiveRecord::Base.send(:include, ActsAsStripped)
+end
View
25 test/acts_as_stripped_test.rb
@@ -1,7 +1,28 @@
require 'test_helper'
+ActiveRecord::Schema.define(:version => 1) do
+ create_table :users, :force => true do |t|
+ t.column :first_name, :string
+ t.column :last_name, :string
+ end
+end
+
+class User < ActiveRecord::Base
+ acts_as_stripped
+end
+
class ActsAsStrippedTest < Test::Unit::TestCase
- should "probably rename this file and start testing for real" do
- flunk "hey buddy, you should probably rename this file and start testing for real"
+ context 'a basic user instance' do
+ setup do
+ @user = User.new
+ end
+ should "strips all attributes of extra whitespace" do
+ @user.first_name = ' ryan '
+ @user.last_name = ' sonnek '
+
+ @user.save!
+ assert_equal 'ryan', @user.first_name
+ assert_equal 'sonnek', @user.last_name
+ end
end
end
View
3 test/database.yml
@@ -0,0 +1,3 @@
+sqlite:
+ :adapter: sqlite3
+ :dbfile: acts_as_stripped.sqlite3
View
77 test/debug.log
@@ -0,0 +1,77 @@
+# Logfile created on Wed Jun 24 17:11:52 -0500 2009 by /
+ SQL (0.6ms) select sqlite_version(*)
+ SQL (0.4ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (2.5ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "first_name" varchar(255), "last_name" varchar(255)) 
+ SQL (0.6ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (2.0ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
+ SQL (2.5ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
+ SQL (0.7ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (0.5ms) SELECT version FROM "schema_migrations"
+ SQL (2.4ms) INSERT INTO "schema_migrations" (version) VALUES ('1')
+ SQL (0.7ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (0.6ms) select sqlite_version(*)
+ SQL (0.8ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (3.3ms) DROP TABLE "users"
+ SQL (2.1ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "first_name" varchar(255), "last_name" varchar(255)) 
+ SQL (0.8ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (1.8ms) SELECT version FROM "schema_migrations"
+ User Create (0.6ms) INSERT INTO "users" ("last_name", "first_name") VALUES(' sonnek ', ' ryan ')
+ SQL (0.7ms) select sqlite_version(*)
+ SQL (0.9ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (3.7ms) DROP TABLE "users"
+ SQL (2.1ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "first_name" varchar(255), "last_name" varchar(255)) 
+ SQL (0.6ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (0.3ms) SELECT version FROM "schema_migrations"
+ SQL (0.6ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (0.6ms) select sqlite_version(*)
+ SQL (0.7ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (3.1ms) DROP TABLE "users"
+ SQL (2.2ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "first_name" varchar(255), "last_name" varchar(255)) 
+ SQL (0.8ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (0.5ms) SELECT version FROM "schema_migrations"
+ SQL (0.6ms) select sqlite_version(*)
+ SQL (0.8ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (3.1ms) DROP TABLE "users"
+ SQL (2.2ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "first_name" varchar(255), "last_name" varchar(255)) 
+ SQL (0.5ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (0.4ms) SELECT version FROM "schema_migrations"
+ User Create (0.6ms) INSERT INTO "users" ("last_name", "first_name") VALUES('sonnek', 'ryan')
View
5 test/test_helper.rb
@@ -1,10 +1,15 @@
require 'rubygems'
require 'test/unit'
require 'shoulda'
+require 'activerecord'
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'acts_as_stripped'
+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'] || 'sqlite'])
+
class Test::Unit::TestCase
end

0 comments on commit 73e92fd

Please sign in to comment.