Skip to content

Commit

Permalink
initial testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Rothstein committed Feb 4, 2010
1 parent 5e45012 commit bfb47be
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 6 deletions.
2 changes: 0 additions & 2 deletions Rakefile
Expand Up @@ -40,8 +40,6 @@ rescue LoadError
end
end

task :test => :check_dependencies

task :default => :test

require 'rake/rdoctask'
Expand Down
4 changes: 3 additions & 1 deletion lib/simply_stated.rb
@@ -1,3 +1,4 @@
require 'set'
module SimplyStated
def self.included(klass)
klass.instance_eval do
Expand All @@ -8,6 +9,7 @@ def self.included(klass)
named_scope :in_state, lambda {|*s| {:conditions => ['state in (?)', s.map(&:to_s)]}}
named_scope :not_in_state, lambda {|*s| {:conditions => ['state not in (?)', s.map(&:to_s)]}}
self.state_validation_hash = {}
self.states = Set.new
end
end

Expand All @@ -33,7 +35,7 @@ module ClassMethods
def state(state_name, &blk)
state_name = state_name.to_sym
State.new(state_name, self).instance_eval(&blk) if blk
(self.states ||= []) << state_name
self.states << state_name
(self.state_validation_hash[state_name] ||= []) << state_name

define_method(:"in_state_#{state_name}?") {state == state_name}
Expand Down
10 changes: 10 additions & 0 deletions test/models/two_state_rule.rb
@@ -0,0 +1,10 @@
require File.join(File.dirname(__FILE__), 'user')
class TwoStateRule < User
state :new do
transitions { self.state = :has_name if name.present? }
end

state :has_name do
transitions { self.state = :new if name.blank? }
end
end
3 changes: 3 additions & 0 deletions test/models/user.rb
@@ -0,0 +1,3 @@
class User < ActiveRecord::Base
include SimplyStated
end
9 changes: 9 additions & 0 deletions test/schema.rb
@@ -0,0 +1,9 @@
ActiveRecord::Schema.define :version => 0 do
create_table :users, :force => true do |t|
t.column :name, :string
t.column :email_confirmed, :boolean, :default => false, :null => false
t.column :approved, :boolean, :default => false, :null => false
t.column :state, :string, :default => "new"
t.column :type, :string
end
end
64 changes: 62 additions & 2 deletions test/simply_stated_test.rb
@@ -1,6 +1,66 @@
require File.instance_eval { expand_path join(dirname(__FILE__), 'test_helper') }
require 'active_record'

class TestSimplyStated < Test::Unit::TestCase
should 'write the tests'
context 'with a user' do
setup do
@user = User.new
end

should 'be in state new' do
assert_state :new, @user
end

should 'not accept an invalid state' do
exception = assert_raises(RuntimeError) {@user.state = :foo}
assert_message "invalid state foo", exception
end

context 'with a known state' do
setup { User.state :has_name }

should 'accept symbol wibble as a state' do
assert_nothing_raised {@user.state = :has_name}
end

should 'accept string wibble as a state' do
assert_nothing_raised {@user.state = "has_name"}
end
end

context 'with simple transition rules between new and has_name' do
setup { @user = TwoStateRule.new }

should 'list all the states' do
assert_equal Set.new([:new, :has_name]), TwoStateRule.states
end

context 'when saved, the user' do
setup {@user.save!}
should 'stay in state new' do
assert_state :new, @user
end
end

context 'when the user has a name, they' do
setup {@user.update_attributes! :name => "lorrie"}
should 'transition to state has_name' do
assert_state :has_name, @user
end
end

context 'if the user is somehow put into the has_name state but has no name' do
setup { @user.update_attributes! :state => :has_name }

should 'stay in new state' do
assert_state :new, @user
end
end
end

should 'test validations'
should 'test validation inheritence'

should 'test in_state named scope'
should 'test not_in_state named scope'
end
end
2 changes: 1 addition & 1 deletion test/test_helper.rb
Expand Up @@ -22,9 +22,9 @@
load(dirname + "/schema.rb")

require 'simply_stated'
require File.expand_path(dirname + '/../init.rb')

Dir[dirname + '/models/*'].each {|model| require model}
$:.unshift File.instance_eval { expand_path join(dirname, "models") }


class Test::Unit::TestCase
Expand Down

0 comments on commit bfb47be

Please sign in to comment.