From 7fc628e3fcda6342a8cee0a6236987b57ac10270 Mon Sep 17 00:00:00 2001 From: rick Date: Fri, 27 Jun 2008 23:29:03 -0700 Subject: convert specs to tests --- activemodel/spec/observing_spec.rb | 120 ------------------------------------ activemodel/spec/spec_helper.rb | 17 ----- activemodel/test/observing_test.rb | 123 +++++++++++++++++++++++++++++++++++++ activemodel/test/test_helper.rb | 23 +++++++ 4 files changed, 146 insertions(+), 137 deletions(-) delete mode 100644 activemodel/spec/observing_spec.rb delete mode 100644 activemodel/spec/spec_helper.rb create mode 100644 activemodel/test/observing_test.rb create mode 100644 activemodel/test/test_helper.rb (limited to 'activemodel') diff --git a/activemodel/spec/observing_spec.rb b/activemodel/spec/observing_spec.rb deleted file mode 100644 index 1919bb5991..0000000000 --- a/activemodel/spec/observing_spec.rb +++ /dev/null @@ -1,120 +0,0 @@ -require File.join(File.dirname(__FILE__), 'spec_helper') - -class ObservedModel < ActiveModel::Base - class Observer - end -end - -class FooObserver < ActiveModel::Observer - class << self - public :new - end - - attr_accessor :stub - - def on_spec(record) - stub.event_with(record) if stub - end -end - -class Foo < ActiveModel::Base -end - -module ActiveModel - describe Observing do - before do - ObservedModel.observers.clear - end - - it "initializes model with no cached observers" do - ObservedModel.observers.should be_empty - end - - it "stores cached observers in an array" do - ObservedModel.observers << :foo - ObservedModel.observers.should include(:foo) - end - - it "flattens array of assigned cached observers" do - ObservedModel.observers = [[:foo], :bar] - ObservedModel.observers.should include(:foo) - ObservedModel.observers.should include(:bar) - end - - it "instantiates observer names passed as strings" do - ObservedModel.observers << 'foo_observer' - FooObserver.should_receive(:instance) - ObservedModel.instantiate_observers - end - - it "instantiates observer names passed as symbols" do - ObservedModel.observers << :foo_observer - FooObserver.should_receive(:instance) - ObservedModel.instantiate_observers - end - - it "instantiates observer classes" do - ObservedModel.observers << ObservedModel::Observer - ObservedModel::Observer.should_receive(:instance) - ObservedModel.instantiate_observers - end - - it "should pass observers to subclasses" do - FooObserver.instance - bar = Class.new(Foo) - bar.count_observers.should == 1 - end - end - - describe Observer do - before do - ObservedModel.observers = :foo_observer - FooObserver.models = nil - end - - it "guesses implicit observable model name" do - FooObserver.observed_class_name.should == 'Foo' - end - - it "tracks implicit observable models" do - instance = FooObserver.new - instance.send(:observed_classes).should include(Foo) - instance.send(:observed_classes).should_not include(ObservedModel) - end - - it "tracks explicit observed model class" do - FooObserver.new.send(:observed_classes).should_not include(ObservedModel) - FooObserver.observe ObservedModel - instance = FooObserver.new - instance.send(:observed_classes).should include(ObservedModel) - end - - it "tracks explicit observed model as string" do - FooObserver.new.send(:observed_classes).should_not include(ObservedModel) - FooObserver.observe 'observed_model' - instance = FooObserver.new - instance.send(:observed_classes).should include(ObservedModel) - end - - it "tracks explicit observed model as symbol" do - FooObserver.new.send(:observed_classes).should_not include(ObservedModel) - FooObserver.observe :observed_model - instance = FooObserver.new - instance.send(:observed_classes).should include(ObservedModel) - end - - it "calls existing observer event" do - foo = Foo.new - FooObserver.instance.stub = stub!(:stub) - FooObserver.instance.stub.should_receive(:event_with).with(foo) - Foo.send(:changed) - Foo.send(:notify_observers, :on_spec, foo) - end - - it "skips nonexistent observer event" do - foo = Foo.new - Foo.send(:changed) - Foo.send(:notify_observers, :whatever, foo) - end - end -end \ No newline at end of file diff --git a/activemodel/spec/spec_helper.rb b/activemodel/spec/spec_helper.rb deleted file mode 100644 index 004fdfca07..0000000000 --- a/activemodel/spec/spec_helper.rb +++ /dev/null @@ -1,17 +0,0 @@ -ENV['LOG_NAME'] = 'spec' -$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'vendor', 'rspec', 'lib') -$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib') -require 'active_model' -begin - require 'spec' -rescue LoadError - require 'rubygems' - require 'spec' -end - -begin - require 'ruby-debug' - Debugger.start -rescue LoadError - # you do not know the ways of ruby-debug yet, what a shame -end \ No newline at end of file diff --git a/activemodel/test/observing_test.rb b/activemodel/test/observing_test.rb new file mode 100644 index 0000000000..37291ae4c6 --- /dev/null +++ b/activemodel/test/observing_test.rb @@ -0,0 +1,123 @@ +require File.join(File.dirname(__FILE__), 'test_helper') + +class ObservedModel < ActiveModel::Base + class Observer + end +end + +class FooObserver < ActiveModel::Observer + class << self + public :new + end + + attr_accessor :stub + + def on_spec(record) + stub.event_with(record) if stub + end +end + +class Foo < ActiveModel::Base +end + +class ObservingTest < ActiveSupport::TestCase + def setup + ObservedModel.observers.clear + end + + test "initializes model with no cached observers" do + assert ObservedModel.observers.empty?, "Not empty: #{ObservedModel.observers.inspect}" + end + + test "stores cached observers in an array" do + ObservedModel.observers << :foo + assert ObservedModel.observers.include?(:foo), ":foo not in #{ObservedModel.observers.inspect}" + end + + test "flattens array of assigned cached observers" do + ObservedModel.observers = [[:foo], :bar] + assert ObservedModel.observers.include?(:foo), ":foo not in #{ObservedModel.observers.inspect}" + assert ObservedModel.observers.include?(:bar), ":bar not in #{ObservedModel.observers.inspect}" + end + + uses_mocha "observer instantiation" do + test "instantiates observer names passed as strings" do + ObservedModel.observers << 'foo_observer' + FooObserver.expects(:instance) + ObservedModel.instantiate_observers + end + + test "instantiates observer names passed as symbols" do + ObservedModel.observers << :foo_observer + FooObserver.expects(:instance) + ObservedModel.instantiate_observers + end + + test "instantiates observer classes" do + ObservedModel.observers << ObservedModel::Observer + ObservedModel::Observer.expects(:instance) + ObservedModel.instantiate_observers + end + end + + test "passes observers to subclasses" do + FooObserver.instance + bar = Class.new(Foo) + assert_equal Foo.count_observers, bar.count_observers + end +end + +class ObserverTest < ActiveSupport::TestCase + def setup + ObservedModel.observers = :foo_observer + FooObserver.models = nil + end + + test "guesses implicit observable model name" do + assert_equal 'Foo', FooObserver.observed_class_name + end + + test "tracks implicit observable models" do + instance = FooObserver.new + assert instance.send(:observed_classes).include?(Foo), "Foo not in #{instance.send(:observed_classes).inspect}" + assert !instance.send(:observed_classes).include?(ObservedModel), "ObservedModel in #{instance.send(:observed_classes).inspect}" + end + + test "tracks explicit observed model class" do + old_instance = FooObserver.new + assert !old_instance.send(:observed_classes).include?(ObservedModel), "ObservedModel in #{old_instance.send(:observed_classes).inspect}" + FooObserver.observe ObservedModel + instance = FooObserver.new + assert instance.send(:observed_classes).include?(ObservedModel), "ObservedModel not in #{instance.send(:observed_classes).inspect}" + end + + test "tracks explicit observed model as string" do + old_instance = FooObserver.new + assert !old_instance.send(:observed_classes).include?(ObservedModel), "ObservedModel in #{old_instance.send(:observed_classes).inspect}" + FooObserver.observe 'observed_model' + instance = FooObserver.new + assert instance.send(:observed_classes).include?(ObservedModel), "ObservedModel not in #{instance.send(:observed_classes).inspect}" + end + + test "tracks explicit observed model as symbol" do + old_instance = FooObserver.new + assert !old_instance.send(:observed_classes).include?(ObservedModel), "ObservedModel in #{old_instance.send(:observed_classes).inspect}" + FooObserver.observe :observed_model + instance = FooObserver.new + assert instance.send(:observed_classes).include?(ObservedModel), "ObservedModel not in #{instance.send(:observed_classes).inspect}" + end + + test "calls existing observer event" do + foo = Foo.new + FooObserver.instance.stub = stub + FooObserver.instance.stub.expects(:event_with).with(foo) + Foo.send(:changed) + Foo.send(:notify_observers, :on_spec, foo) + end + + test "skips nonexistent observer event" do + foo = Foo.new + Foo.send(:changed) + Foo.send(:notify_observers, :whatever, foo) + end +end \ No newline at end of file diff --git a/activemodel/test/test_helper.rb b/activemodel/test/test_helper.rb new file mode 100644 index 0000000000..8e608fa0bc --- /dev/null +++ b/activemodel/test/test_helper.rb @@ -0,0 +1,23 @@ +$:.unshift "#{File.dirname(__FILE__)}/../lib" +$:.unshift File.dirname(__FILE__) + +require 'test/unit' +require 'active_model' +require 'active_support/callbacks' # needed by ActiveSupport::TestCase +require 'active_support/test_case' + +def uses_gem(gem_name, test_name, version = '> 0') + require 'rubygems' + gem gem_name.to_s, version + require gem_name.to_s + yield +rescue LoadError + $stderr.puts "Skipping #{test_name} tests. `gem install #{gem_name}` and try again." +end + +# Wrap tests that use Mocha and skip if unavailable. +unless defined? uses_mocha + def uses_mocha(test_name, &block) + uses_gem('mocha', test_name, '>= 0.5.5', &block) + end +end -- cgit v1.2.3 From b7c6ceff9a31cc478c4bc89d57980900a775fbed Mon Sep 17 00:00:00 2001 From: rick Date: Fri, 27 Jun 2008 23:29:47 -0700 Subject: tweak activemodel load order a bit --- activemodel/Rakefile | 16 ++++++++++++---- activemodel/lib/active_model.rb | 20 ++++---------------- activemodel/lib/active_model/base.rb | 4 ++++ activemodel/lib/active_model/callbacks.rb | 2 ++ activemodel/lib/active_model/core.rb | 7 +++++++ activemodel/lib/active_model/observing.rb | 18 ++++++++---------- activemodel/lib/active_model/validations.rb | 2 ++ 7 files changed, 39 insertions(+), 30 deletions(-) create mode 100644 activemodel/lib/active_model/core.rb (limited to 'activemodel') diff --git a/activemodel/Rakefile b/activemodel/Rakefile index 87e9b547f3..034c2fbd01 100644 --- a/activemodel/Rakefile +++ b/activemodel/Rakefile @@ -1,11 +1,19 @@ #!/usr/bin/env ruby -$LOAD_PATH << File.join(File.dirname(__FILE__), 'vendor', 'rspec', 'lib') require 'rake' -require 'spec/rake/spectask' +require 'rake/testtask' require 'rake/rdoctask' +task :default => :test + +Rake::TestTask.new do |t| + t.libs << "test" + t.pattern = 'test/**/*_test.rb' + t.verbose = true + t.warning = true +end + # Generate the RDoc documentation -Rake::RDocTask.new { |rdoc| +Rake::RDocTask.new do |rdoc| rdoc.rdoc_dir = 'doc' rdoc.title = "Active Model" rdoc.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object' @@ -13,4 +21,4 @@ Rake::RDocTask.new { |rdoc| rdoc.template = "#{ENV['template']}.rb" if ENV['template'] rdoc.rdoc_files.include('README', 'CHANGES') rdoc.rdoc_files.include('lib/**/*.rb') -} \ No newline at end of file +end \ No newline at end of file diff --git a/activemodel/lib/active_model.rb b/activemodel/lib/active_model.rb index 369c7fed33..4ed7b0889d 100644 --- a/activemodel/lib/active_model.rb +++ b/activemodel/lib/active_model.rb @@ -1,17 +1,5 @@ -$LOAD_PATH << File.join(File.dirname(__FILE__), '..', '..', 'activesupport', 'lib') - -# premature optimization? -require 'active_support/inflector' -require 'active_support/core_ext/string/inflections' -String.send :include, ActiveSupport::CoreExtensions::String::Inflections - -require 'active_model/base' require 'active_model/observing' -require 'active_model/callbacks' -require 'active_model/validations' - -ActiveModel::Base.class_eval do - include ActiveModel::Observing - include ActiveModel::Callbacks - include ActiveModel::Validations -end \ No newline at end of file +# disabled until they're tested +# require 'active_model/callbacks' +# require 'active_model/validations' +require 'active_model/base' \ No newline at end of file diff --git a/activemodel/lib/active_model/base.rb b/activemodel/lib/active_model/base.rb index 1141156da4..a500adfdf1 100644 --- a/activemodel/lib/active_model/base.rb +++ b/activemodel/lib/active_model/base.rb @@ -1,4 +1,8 @@ module ActiveModel class Base + include Observing + # disabled, until they're tested + # include Callbacks + # include Validations end end \ No newline at end of file diff --git a/activemodel/lib/active_model/callbacks.rb b/activemodel/lib/active_model/callbacks.rb index 0114fc386b..1dd20156ec 100644 --- a/activemodel/lib/active_model/callbacks.rb +++ b/activemodel/lib/active_model/callbacks.rb @@ -1,3 +1,5 @@ +require 'active_model/observing' + module ActiveModel module Callbacks diff --git a/activemodel/lib/active_model/core.rb b/activemodel/lib/active_model/core.rb new file mode 100644 index 0000000000..47b968e121 --- /dev/null +++ b/activemodel/lib/active_model/core.rb @@ -0,0 +1,7 @@ +# This file is required by each major ActiveModel component for the core requirements. This allows you to +# load individual pieces of ActiveModel as needed. +$LOAD_PATH << File.join(File.dirname(__FILE__), '..', '..', '..', 'activesupport', 'lib') + +# premature optimization? +# So far, we only need the string inflections and not the rest of ActiveSupport. +require 'active_support/inflector' \ No newline at end of file diff --git a/activemodel/lib/active_model/observing.rb b/activemodel/lib/active_model/observing.rb index db758f5185..9e99d7472c 100644 --- a/activemodel/lib/active_model/observing.rb +++ b/activemodel/lib/active_model/observing.rb @@ -1,4 +1,6 @@ require 'observer' +require 'singleton' +require 'active_model/core' module ActiveModel module Observing @@ -73,7 +75,7 @@ module ActiveModel # Start observing the declared classes and their subclasses. def initialize self.observed_classes = self.class.models if self.class.models - observed_classes.each { |klass| add_observer! klass } + observed_classes.each { |klass| klass.add_observer(self) } end # Send observed_method(object) if the method exists. @@ -85,16 +87,12 @@ module ActiveModel # Passes the new subclass. def observed_class_inherited(subclass) #:nodoc: self.class.observe(observed_classes + [subclass]) - add_observer!(subclass) + subclass.add_observer(self) end - protected - def observed_classes - @observed_classes ||= [self.class.observed_class] - end - - def add_observer!(klass) - klass.add_observer(self) - end + protected + def observed_classes + @observed_classes ||= [self.class.observed_class] + end end end \ No newline at end of file diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 34ef3b8f6e..6b692e6a9a 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -1,3 +1,5 @@ +require 'active_model/observing' + module ActiveModel module Validations def self.included(base) # :nodoc: -- cgit v1.2.3 From b9528ad3c5379896b00772cb44faf1db0fd882d7 Mon Sep 17 00:00:00 2001 From: rick Date: Sat, 28 Jun 2008 00:55:02 -0700 Subject: initial statemachine machine and state classes --- activemodel/lib/active_model/callbacks.rb | 2 +- activemodel/lib/active_model/state_machine.rb | 43 +++ .../lib/active_model/state_machine/machine.rb | 30 ++ .../lib/active_model/state_machine/state.rb | 40 +++ activemodel/lib/active_model/validations.rb | 2 +- activemodel/test/observing_test.rb | 6 +- activemodel/test/state_machine/machine_test.rb | 28 ++ activemodel/test/state_machine/state_test.rb | 73 +++++ activemodel/test/state_machine_test.rb | 335 +++++++++++++++++++++ activemodel/test/test_helper.rb | 18 +- 10 files changed, 571 insertions(+), 6 deletions(-) create mode 100644 activemodel/lib/active_model/state_machine.rb create mode 100644 activemodel/lib/active_model/state_machine/machine.rb create mode 100644 activemodel/lib/active_model/state_machine/state.rb create mode 100644 activemodel/test/state_machine/machine_test.rb create mode 100644 activemodel/test/state_machine/state_test.rb create mode 100644 activemodel/test/state_machine_test.rb (limited to 'activemodel') diff --git a/activemodel/lib/active_model/callbacks.rb b/activemodel/lib/active_model/callbacks.rb index 1dd20156ec..c94f76109f 100644 --- a/activemodel/lib/active_model/callbacks.rb +++ b/activemodel/lib/active_model/callbacks.rb @@ -1,4 +1,4 @@ -require 'active_model/observing' +require 'active_model/core' module ActiveModel module Callbacks diff --git a/activemodel/lib/active_model/state_machine.rb b/activemodel/lib/active_model/state_machine.rb new file mode 100644 index 0000000000..bb038f6b7a --- /dev/null +++ b/activemodel/lib/active_model/state_machine.rb @@ -0,0 +1,43 @@ +Dir[File.dirname(__FILE__) + "/state_machine/*.rb"].sort.each do |path| + filename = File.basename(path) + require "active_model/state_machine/#{filename}" +end + +module ActiveModel + module StateMachine + def self.included(base) + base.extend ClassMethods + end + + module ClassMethods + def inherited(klass) + super + klass.state_machines = state_machines + end + + def state_machines + @state_machines ||= {} + end + + def state_machines=(value) + @state_machines = value ? value.dup : nil + end + + def state_machine(name = nil, options = {}, &block) + if name.is_a?(Hash) + options = name + name = nil + end + name ||= :default + state_machines[name] ||= Machine.new(self, name) + block ? state_machines[name].update(options, &block) : state_machines[name] + end + end + + def current_state(name = nil) + sm = self.class.state_machine(name) + ivar = "@#{sm.name}_current_state" + instance_variable_get(ivar) || instance_variable_set(ivar, sm.initial_state) + end + end +end \ No newline at end of file diff --git a/activemodel/lib/active_model/state_machine/machine.rb b/activemodel/lib/active_model/state_machine/machine.rb new file mode 100644 index 0000000000..75ed8f8b65 --- /dev/null +++ b/activemodel/lib/active_model/state_machine/machine.rb @@ -0,0 +1,30 @@ +module ActiveModel + module StateMachine + class Machine + attr_accessor :initial_state, :states, :event + attr_reader :klass, :name + + def initialize(klass, name) + @klass, @name, @states, @events = klass, name, [], {} + end + + def states_for_select + states.map { |st| [st.display_name, st.name.to_s] } + end + + def state(name, options = {}) + @states << State.new(self, name, options) + end + + def initial_state + @initial_state ||= (states.first ? states.first.name : nil) + end + + def update(options = {}, &block) + @initial_state = options[:initial] + instance_eval(&block) + self + end + end + end +end \ No newline at end of file diff --git a/activemodel/lib/active_model/state_machine/state.rb b/activemodel/lib/active_model/state_machine/state.rb new file mode 100644 index 0000000000..5851a6fb79 --- /dev/null +++ b/activemodel/lib/active_model/state_machine/state.rb @@ -0,0 +1,40 @@ +module ActiveModel + module StateMachine + class State + attr_reader :name, :options + + def initialize(machine, name, options={}) + @machine, @name, @options, @display_name = machine, name, options, options.delete(:display) + machine.klass.send(:define_method, "#{name}?") do + current_state.to_s == name.to_s + end + end + + def ==(state) + if state.is_a? Symbol + name == state + else + name == state.name + end + end + + def call_action(action, record) + action = @options[action] + case action + when Symbol, String + record.send(action) + when Proc + action.call(record) + end + end + + def display_name + @display_name ||= name.to_s.gsub(/_/, ' ').capitalize + end + + def for_select + [display_name, name.to_s] + end + end + end +end diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 6b692e6a9a..7efe9901ca 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -1,4 +1,4 @@ -require 'active_model/observing' +require 'active_model/core' module ActiveModel module Validations diff --git a/activemodel/test/observing_test.rb b/activemodel/test/observing_test.rb index 37291ae4c6..6e124de52f 100644 --- a/activemodel/test/observing_test.rb +++ b/activemodel/test/observing_test.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), 'test_helper') +require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper')) class ObservedModel < ActiveModel::Base class Observer @@ -20,7 +20,7 @@ end class Foo < ActiveModel::Base end -class ObservingTest < ActiveSupport::TestCase +class ObservingTest < ActiveModel::TestCase def setup ObservedModel.observers.clear end @@ -67,7 +67,7 @@ class ObservingTest < ActiveSupport::TestCase end end -class ObserverTest < ActiveSupport::TestCase +class ObserverTest < ActiveModel::TestCase def setup ObservedModel.observers = :foo_observer FooObserver.models = nil diff --git a/activemodel/test/state_machine/machine_test.rb b/activemodel/test/state_machine/machine_test.rb new file mode 100644 index 0000000000..34a4b384ce --- /dev/null +++ b/activemodel/test/state_machine/machine_test.rb @@ -0,0 +1,28 @@ +require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper')) + +class MachineTestSubject + include ActiveModel::StateMachine + + state_machine do + end + + state_machine :initial => :foo do + end + + state_machine :extra, :initial => :bar do + end +end + +class StateMachineMachineTest < ActiveModel::TestCase + test "allows reuse of existing machines" do + assert_equal 2, MachineTestSubject.state_machines.size + end + + test "sets #initial_state from :initial option" do + assert_equal :bar, MachineTestSubject.state_machine(:extra).initial_state + end + + test "accesses non-default state machine" do + assert_kind_of ActiveModel::StateMachine::Machine, MachineTestSubject.state_machine(:extra) + end +end \ No newline at end of file diff --git a/activemodel/test/state_machine/state_test.rb b/activemodel/test/state_machine/state_test.rb new file mode 100644 index 0000000000..444435d271 --- /dev/null +++ b/activemodel/test/state_machine/state_test.rb @@ -0,0 +1,73 @@ +require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper')) + +class StateTestSubject + include ActiveModel::StateMachine + + state_machine do + end +end + +class StateTest < ActiveModel::TestCase + def setup + @name = :astate + @options = { :crazy_custom_key => 'key' } + @machine = StateTestSubject.state_machine + end + + def new_state(options={}) + ActiveModel::StateMachine::State.new(options.delete(:machine) || @machine, @name, @options.merge(options)) + end + + test 'sets the name' do + assert_equal :astate, new_state.name + end + + test 'sets the display_name from name' do + assert_equal "Astate", new_state.display_name + end + + test 'sets the display_name from options' do + assert_equal "A State", new_state(:display => "A State").display_name + end + + test 'sets the options and expose them as options' do + assert_equal @options, new_state.options + end + + test 'equals a symbol of the same name' do + assert_equal new_state, :astate + end + + test 'equals a State of the same name' do + assert_equal new_state, new_state + end + + uses_mocha 'state actions' do + test 'should send a message to the record for an action if the action is present as a symbol' do + state = new_state(:entering => :foo) + + record = stub + record.expects(:foo) + + state.call_action(:entering, record) + end + + test 'should send a message to the record for an action if the action is present as a string' do + state = new_state(:entering => 'foo') + + record = stub + record.expects(:foo) + + state.call_action(:entering, record) + end + + test 'should call a proc, passing in the record for an action if the action is present' do + state = new_state(:entering => Proc.new {|r| r.foobar}) + + record = stub + record.expects(:foobar) + + state.call_action(:entering, record) + end + end +end \ No newline at end of file diff --git a/activemodel/test/state_machine_test.rb b/activemodel/test/state_machine_test.rb new file mode 100644 index 0000000000..e906744c77 --- /dev/null +++ b/activemodel/test/state_machine_test.rb @@ -0,0 +1,335 @@ +require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper')) + +class StateMachineSubject + include ActiveModel::StateMachine + + state_machine do + state :open, :exit => :exit + state :closed, :enter => :enter + + #event :close, :success => :success_callback do + # transitions :to => :closed, :from => [:open] + #end + # + #event :null do + # transitions :to => :closed, :from => [:open], :guard => :always_false + #end + end + + state_machine :bar do + state :read + state :ended + + #event :foo do + # transitions :to => :ended, :from => [:read] + #end + end + + def always_false + false + end + + def success_callback + end + + def enter + end + def exit + end +end + +class StateMachineSubjectSubclass < StateMachineSubject +end + +class StateMachineClassLevelTest < ActiveModel::TestCase + test 'defines a class level #state_machine method on its including class' do + assert StateMachineSubject.respond_to?(:state_machine) + end + + test 'defines a class level #state_machines method on its including class' do + assert StateMachineSubject.respond_to?(:state_machines) + end + + test 'class level #state_machine returns machine instance' do + assert_kind_of ActiveModel::StateMachine::Machine, StateMachineSubject.state_machine + end + + test 'class level #state_machine returns machine instance with given name' do + assert_kind_of ActiveModel::StateMachine::Machine, StateMachineSubject.state_machine(:default) + end + + test 'class level #state_machines returns hash of machine instances' do + assert_kind_of ActiveModel::StateMachine::Machine, StateMachineSubject.state_machines[:default] + end + + test "should return a select friendly array of states in the form of [['Friendly name', 'state_name']]" do + assert_equal [['Open', 'open'], ['Closed', 'closed']], StateMachineSubject.state_machine.states_for_select + end +end + +class StateMachineInstanceLevelTest < ActiveModel::TestCase + def setup + @foo = StateMachineSubject.new + end + + test 'defines an accessor for the current state' do + assert @foo.respond_to?(:current_state) + end + + test 'defines a state querying instance method on including class' do + assert @foo.respond_to?(:open?) + end + + #test 'should define an event! inance method' do + # assert @foo.respond_to?(:close!) + #end +end + +class StateMachineInitialStatesTest < ActiveModel::TestCase + def setup + @foo = StateMachineSubject.new + end + + test 'should set the initial state' do + assert_equal :open, @foo.current_state + end + + #test '#open? should be initially true' do + # @foo.open?.should be_true + #end + # + #test '#closed? should be initially false' do + # @foo.closed?.should be_false + #end + + test 'should use the first state defined if no initial state is given' do + assert_equal :read, @foo.current_state(:bar) + end +end +# +#describe AASM, '- event firing with persistence' do +# it 'should fire the Event' do +# foo = Foo.new +# +# Foo.aasm_events[:close].should_receive(:fire).with(foo) +# foo.close! +# end +# +# it 'should update the current state' do +# foo = Foo.new +# foo.close! +# +# foo.aasm_current_state.should == :closed +# end +# +# it 'should call the success callback if one was provided' do +# foo = Foo.new +# +# foo.should_receive(:success_callback) +# +# foo.close! +# end +# +# it 'should attempt to persist if aasm_write_state is defined' do +# foo = Foo.new +# +# def foo.aasm_write_state +# end +# +# foo.should_receive(:aasm_write_state) +# +# foo.close! +# end +#end +# +#describe AASM, '- event firing without persistence' do +# it 'should fire the Event' do +# foo = Foo.new +# +# Foo.aasm_events[:close].should_receive(:fire).with(foo) +# foo.close +# end +# +# it 'should update the current state' do +# foo = Foo.new +# foo.close +# +# foo.aasm_current_state.should == :closed +# end +# +# it 'should attempt to persist if aasm_write_state is defined' do +# foo = Foo.new +# +# def foo.aasm_write_state +# end +# +# foo.should_receive(:aasm_write_state_without_persistence) +# +# foo.close +# end +#end +# +#describe AASM, '- persistence' do +# it 'should read the state if it has not been set and aasm_read_state is defined' do +# foo = Foo.new +# def foo.aasm_read_state +# end +# +# foo.should_receive(:aasm_read_state) +# +# foo.aasm_current_state +# end +#end +# +#describe AASM, '- getting events for a state' do +# it '#aasm_events_for_current_state should use current state' do +# foo = Foo.new +# foo.should_receive(:aasm_current_state) +# foo.aasm_events_for_current_state +# end +# +# it '#aasm_events_for_current_state should use aasm_events_for_state' do +# foo = Foo.new +# foo.stub!(:aasm_current_state).and_return(:foo) +# foo.should_receive(:aasm_events_for_state).with(:foo) +# foo.aasm_events_for_current_state +# end +#end +# +#describe AASM, '- event callbacks' do +# it 'should call aasm_event_fired if defined and successful for bang fire' do +# foo = Foo.new +# def foo.aasm_event_fired(from, to) +# end +# +# foo.should_receive(:aasm_event_fired) +# +# foo.close! +# end +# +# it 'should call aasm_event_fired if defined and successful for non-bang fire' do +# foo = Foo.new +# def foo.aasm_event_fired(from, to) +# end +# +# foo.should_receive(:aasm_event_fired) +# +# foo.close +# end +# +# it 'should call aasm_event_failed if defined and transition failed for bang fire' do +# foo = Foo.new +# def foo.aasm_event_failed(event) +# end +# +# foo.should_receive(:aasm_event_failed) +# +# foo.null! +# end +# +# it 'should call aasm_event_failed if defined and transition failed for non-bang fire' do +# foo = Foo.new +# def foo.aasm_event_failed(event) +# end +# +# foo.should_receive(:aasm_event_failed) +# +# foo.null +# end +#end +# +#describe AASM, '- state actions' do +# it "should call enter when entering state" do +# foo = Foo.new +# foo.should_receive(:enter) +# +# foo.close +# end +# +# it "should call exit when exiting state" do +# foo = Foo.new +# foo.should_receive(:exit) +# +# foo.close +# end +#end +# +# +class StateMachineInheritanceTest < ActiveModel::TestCase + test "should have the same states as it's parent" do + assert_equal StateMachineSubject.state_machine.states, StateMachineSubjectSubclass.state_machine.states + end + + #test "should have the same events as it's parent" do + # StateMachineSubjectSubclass.aasm_events.should == Bar.aasm_events + #end +end +# +# +#class ChetanPatil +# include AASM +# aasm_initial_state :sleeping +# aasm_state :sleeping +# aasm_state :showering +# aasm_state :working +# aasm_state :dating +# +# aasm_event :wakeup do +# transitions :from => :sleeping, :to => [:showering, :working] +# end +# +# aasm_event :dress do +# transitions :from => :sleeping, :to => :working, :on_transition => :wear_clothes +# transitions :from => :showering, :to => [:working, :dating], :on_transition => Proc.new { |obj, *args| obj.wear_clothes(*args) } +# end +# +# def wear_clothes(shirt_color, trouser_type) +# end +#end +# +# +#describe ChetanPatil do +# it 'should transition to specified next state (sleeping to showering)' do +# cp = ChetanPatil.new +# cp.wakeup! :showering +# +# cp.aasm_current_state.should == :showering +# end +# +# it 'should transition to specified next state (sleeping to working)' do +# cp = ChetanPatil.new +# cp.wakeup! :working +# +# cp.aasm_current_state.should == :working +# end +# +# it 'should transition to default (first or showering) state' do +# cp = ChetanPatil.new +# cp.wakeup! +# +# cp.aasm_current_state.should == :showering +# end +# +# it 'should transition to default state when on_transition invoked' do +# cp = ChetanPatil.new +# cp.dress!(nil, 'purple', 'dressy') +# +# cp.aasm_current_state.should == :working +# end +# +# it 'should call on_transition method with args' do +# cp = ChetanPatil.new +# cp.wakeup! :showering +# +# cp.should_receive(:wear_clothes).with('blue', 'jeans') +# cp.dress! :working, 'blue', 'jeans' +# end +# +# it 'should call on_transition proc' do +# cp = ChetanPatil.new +# cp.wakeup! :showering +# +# cp.should_receive(:wear_clothes).with('purple', 'slacks') +# cp.dress!(:dating, 'purple', 'slacks') +# end +#end \ No newline at end of file diff --git a/activemodel/test/test_helper.rb b/activemodel/test/test_helper.rb index 8e608fa0bc..ccf93280ec 100644 --- a/activemodel/test/test_helper.rb +++ b/activemodel/test/test_helper.rb @@ -3,7 +3,8 @@ $:.unshift File.dirname(__FILE__) require 'test/unit' require 'active_model' -require 'active_support/callbacks' # needed by ActiveSupport::TestCase +require 'active_model/state_machine' +require 'active_support/callbacks' # needed by ActiveModel::TestCase require 'active_support/test_case' def uses_gem(gem_name, test_name, version = '> 0') @@ -21,3 +22,18 @@ unless defined? uses_mocha uses_gem('mocha', test_name, '>= 0.5.5', &block) end end + +begin + require 'rubygems' + require 'ruby-debug' + Debugger.start +rescue LoadError +end + +ActiveSupport::TestCase.send :include, ActiveSupport::Testing::Default + +module ActiveModel + class TestCase < ActiveSupport::TestCase + include ActiveSupport::Testing::Default + end +end \ No newline at end of file -- cgit v1.2.3 From 74cb05698684f237a7eb91afadec0020d8910c70 Mon Sep 17 00:00:00 2001 From: rick Date: Sat, 28 Jun 2008 09:19:44 -0700 Subject: add basic events and transitions. still more tests to convert --- activemodel/lib/active_model/state_machine.rb | 11 +- .../lib/active_model/state_machine/event.rb | 67 +++++++++ .../lib/active_model/state_machine/machine.rb | 44 ++++-- .../lib/active_model/state_machine/state.rb | 18 ++- .../active_model/state_machine/state_transition.rb | 40 +++++ activemodel/test/state_machine/event_test.rb | 51 +++++++ activemodel/test/state_machine/machine_test.rb | 13 ++ activemodel/test/state_machine/state_test.rb | 5 +- .../test/state_machine/state_transition_test.rb | 88 +++++++++++ activemodel/test/state_machine_test.rb | 166 ++++++++++----------- 10 files changed, 395 insertions(+), 108 deletions(-) create mode 100644 activemodel/lib/active_model/state_machine/event.rb create mode 100644 activemodel/lib/active_model/state_machine/state_transition.rb create mode 100644 activemodel/test/state_machine/event_test.rb create mode 100644 activemodel/test/state_machine/state_transition_test.rb (limited to 'activemodel') diff --git a/activemodel/lib/active_model/state_machine.rb b/activemodel/lib/active_model/state_machine.rb index bb038f6b7a..2a5ac95a3e 100644 --- a/activemodel/lib/active_model/state_machine.rb +++ b/activemodel/lib/active_model/state_machine.rb @@ -5,6 +5,9 @@ end module ActiveModel module StateMachine + class InvalidTransition < Exception + end + def self.included(base) base.extend ClassMethods end @@ -34,10 +37,14 @@ module ActiveModel end end - def current_state(name = nil) + def current_state(name = nil, new_state = nil) sm = self.class.state_machine(name) ivar = "@#{sm.name}_current_state" - instance_variable_get(ivar) || instance_variable_set(ivar, sm.initial_state) + if name && new_state + instance_variable_set(ivar, new_state) + else + instance_variable_get(ivar) || instance_variable_set(ivar, sm.initial_state) + end end end end \ No newline at end of file diff --git a/activemodel/lib/active_model/state_machine/event.rb b/activemodel/lib/active_model/state_machine/event.rb new file mode 100644 index 0000000000..cc7d563214 --- /dev/null +++ b/activemodel/lib/active_model/state_machine/event.rb @@ -0,0 +1,67 @@ +module ActiveModel + module StateMachine + class Event + attr_reader :name, :success + + def initialize(name, options = {}, &block) + @name, @transitions = name, [] + machine = options.delete(:machine) + if machine + machine.klass.send(:define_method, "#{name.to_s}!") do |*args| + machine.fire_event(name, self, true, *args) + end + + machine.klass.send(:define_method, "#{name.to_s}") do |*args| + machine.fire_event(name, self, false, *args) + end + end + update(options, &block) + end + + def fire(obj, to_state = nil, *args) + transitions = @transitions.select { |t| t.from == obj.current_state } + raise InvalidTransition if transitions.size == 0 + + next_state = nil + transitions.each do |transition| + next if to_state && !Array(transition.to).include?(to_state) + if transition.perform(obj) + next_state = to_state || Array(transition.to).first + transition.execute(obj, *args) + break + end + end + next_state + end + + def transitions_from_state?(state) + @transitions.any? { |t| t.from? state } + end + + def success? + !!@success + end + + def ==(event) + if event.is_a? Symbol + name == event + else + name == event.name + end + end + + def update(options = {}, &block) + if options.key?(:success) then @success = options[:success] end + if block then instance_eval(&block) end + self + end + + private + def transitions(trans_opts) + Array(trans_opts[:from]).each do |s| + @transitions << StateTransition.new(trans_opts.merge({:from => s.to_sym})) + end + end + end + end +end diff --git a/activemodel/lib/active_model/state_machine/machine.rb b/activemodel/lib/active_model/state_machine/machine.rb index 75ed8f8b65..1da48290c5 100644 --- a/activemodel/lib/active_model/state_machine/machine.rb +++ b/activemodel/lib/active_model/state_machine/machine.rb @@ -1,29 +1,49 @@ module ActiveModel module StateMachine class Machine - attr_accessor :initial_state, :states, :event + attr_accessor :initial_state, :states, :events, :state_index attr_reader :klass, :name - def initialize(klass, name) - @klass, @name, @states, @events = klass, name, [], {} + def initialize(klass, name, options = {}, &block) + @klass, @name, @states, @state_index, @events = klass, name, [], {}, {} + update(options, &block) + end + + def initial_state + @initial_state ||= (states.first ? states.first.name : nil) + end + + def update(options = {}, &block) + if options.key?(:initial) then @initial_state = options[:initial] end + if block then instance_eval(&block) end + self + end + + def fire_event(name, record, persist, *args) + state_index[record.current_state].call_action(:exit, record) + if new_state = @events[name].fire(record, *args) + state_index[new_state].call_action(:enter, record) + record.current_state(@name, new_state) + else + false + end end def states_for_select states.map { |st| [st.display_name, st.name.to_s] } end - def state(name, options = {}) - @states << State.new(self, name, options) + def events_for(state) + events = @events.values.select { |event| event.transitions_from_state?(state) } + events.map! { |event| event.name } end - - def initial_state - @initial_state ||= (states.first ? states.first.name : nil) + private + def state(name, options = {}) + @states << (state_index[name] ||= State.new(name, :machine => self)).update(options) end - def update(options = {}, &block) - @initial_state = options[:initial] - instance_eval(&block) - self + def event(name, options = {}, &block) + (@events[name] ||= Event.new(name, :machine => self)).update(options, &block) end end end diff --git a/activemodel/lib/active_model/state_machine/state.rb b/activemodel/lib/active_model/state_machine/state.rb index 5851a6fb79..68eb2aa34a 100644 --- a/activemodel/lib/active_model/state_machine/state.rb +++ b/activemodel/lib/active_model/state_machine/state.rb @@ -3,11 +3,15 @@ module ActiveModel class State attr_reader :name, :options - def initialize(machine, name, options={}) - @machine, @name, @options, @display_name = machine, name, options, options.delete(:display) - machine.klass.send(:define_method, "#{name}?") do - current_state.to_s == name.to_s + def initialize(name, options = {}) + @name = name + machine = options.delete(:machine) + if machine + machine.klass.send(:define_method, "#{name}?") do + current_state.to_s == name.to_s + end end + update(options) end def ==(state) @@ -35,6 +39,12 @@ module ActiveModel def for_select [display_name, name.to_s] end + + def update(options = {}) + if options.key?(:display) then @display_name = options.delete(:display) end + @options = options + self + end end end end diff --git a/activemodel/lib/active_model/state_machine/state_transition.rb b/activemodel/lib/active_model/state_machine/state_transition.rb new file mode 100644 index 0000000000..f9df998ea4 --- /dev/null +++ b/activemodel/lib/active_model/state_machine/state_transition.rb @@ -0,0 +1,40 @@ +module ActiveModel + module StateMachine + class StateTransition + attr_reader :from, :to, :options + + def initialize(opts) + @from, @to, @guard, @on_transition = opts[:from], opts[:to], opts[:guard], opts[:on_transition] + @options = opts + end + + def perform(obj) + case @guard + when Symbol, String + obj.send(@guard) + when Proc + @guard.call(obj) + else + true + end + end + + def execute(obj, *args) + case @on_transition + when Symbol, String + obj.send(@on_transition, *args) + when Proc + @on_transition.call(obj, *args) + end + end + + def ==(obj) + @from == obj.from && @to == obj.to + end + + def from?(value) + @from == value + end + end + end +end diff --git a/activemodel/test/state_machine/event_test.rb b/activemodel/test/state_machine/event_test.rb new file mode 100644 index 0000000000..01f3464cf2 --- /dev/null +++ b/activemodel/test/state_machine/event_test.rb @@ -0,0 +1,51 @@ +require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper')) + +class EventTest < ActiveModel::TestCase + def setup + @name = :close_order + @success = :success_callback + end + + def new_event + @event = ActiveModel::StateMachine::Event.new(@name, {:success => @success}) do + transitions :to => :closed, :from => [:open, :received] + end + end + + test 'should set the name' do + assert_equal @name, new_event.name + end + + test 'should set the success option' do + assert new_event.success? + end + + uses_mocha 'StateTransition creation' do + test 'should create StateTransitions' do + ActiveModel::StateMachine::StateTransition.expects(:new).with(:to => :closed, :from => :open) + ActiveModel::StateMachine::StateTransition.expects(:new).with(:to => :closed, :from => :received) + new_event + end + end +end + +class EventBeingFiredTest < ActiveModel::TestCase + test 'should raise an AASM::InvalidTransition error if the transitions are empty' do + event = ActiveModel::StateMachine::Event.new(:event) + + assert_raises ActiveModel::StateMachine::InvalidTransition do + event.fire(nil) + end + end + + test 'should return the state of the first matching transition it finds' do + event = ActiveModel::StateMachine::Event.new(:event) do + transitions :to => :closed, :from => [:open, :received] + end + + obj = stub + obj.stubs(:current_state).returns(:open) + + assert_equal :closed, event.fire(obj) + end +end diff --git a/activemodel/test/state_machine/machine_test.rb b/activemodel/test/state_machine/machine_test.rb index 34a4b384ce..64dea42b1f 100644 --- a/activemodel/test/state_machine/machine_test.rb +++ b/activemodel/test/state_machine/machine_test.rb @@ -4,9 +4,18 @@ class MachineTestSubject include ActiveModel::StateMachine state_machine do + state :open + state :closed end state_machine :initial => :foo do + event :shutdown do + transitions :from => :open, :to => :closed + end + + event :timeout do + transitions :from => :open, :to => :closed + end end state_machine :extra, :initial => :bar do @@ -25,4 +34,8 @@ class StateMachineMachineTest < ActiveModel::TestCase test "accesses non-default state machine" do assert_kind_of ActiveModel::StateMachine::Machine, MachineTestSubject.state_machine(:extra) end + + test "finds events for given state" do + assert_equal [:shutdown, :timeout], MachineTestSubject.state_machine.events_for(:open) + end end \ No newline at end of file diff --git a/activemodel/test/state_machine/state_test.rb b/activemodel/test/state_machine/state_test.rb index 444435d271..22d0d9eb93 100644 --- a/activemodel/test/state_machine/state_test.rb +++ b/activemodel/test/state_machine/state_test.rb @@ -10,12 +10,12 @@ end class StateTest < ActiveModel::TestCase def setup @name = :astate - @options = { :crazy_custom_key => 'key' } @machine = StateTestSubject.state_machine + @options = { :crazy_custom_key => 'key', :machine => @machine } end def new_state(options={}) - ActiveModel::StateMachine::State.new(options.delete(:machine) || @machine, @name, @options.merge(options)) + ActiveModel::StateMachine::State.new(@name, @options.merge(options)) end test 'sets the name' do @@ -31,6 +31,7 @@ class StateTest < ActiveModel::TestCase end test 'sets the options and expose them as options' do + @options.delete(:machine) assert_equal @options, new_state.options end diff --git a/activemodel/test/state_machine/state_transition_test.rb b/activemodel/test/state_machine/state_transition_test.rb new file mode 100644 index 0000000000..9a9e7f60c5 --- /dev/null +++ b/activemodel/test/state_machine/state_transition_test.rb @@ -0,0 +1,88 @@ +require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper')) + +class StateTransitionTest < ActiveModel::TestCase + test 'should set from, to, and opts attr readers' do + opts = {:from => 'foo', :to => 'bar', :guard => 'g'} + st = ActiveModel::StateMachine::StateTransition.new(opts) + + assert_equal opts[:from], st.from + assert_equal opts[:to], st.to + assert_equal opts, st.options + end + + uses_mocha 'checking ActiveModel StateMachine transitions' do + test 'should pass equality check if from and to are the same' do + opts = {:from => 'foo', :to => 'bar', :guard => 'g'} + st = ActiveModel::StateMachine::StateTransition.new(opts) + + obj = stub + obj.stubs(:from).returns(opts[:from]) + obj.stubs(:to).returns(opts[:to]) + + assert_equal st, obj + end + + test 'should fail equality check if from are not the same' do + opts = {:from => 'foo', :to => 'bar', :guard => 'g'} + st = ActiveModel::StateMachine::StateTransition.new(opts) + + obj = stub + obj.stubs(:from).returns('blah') + obj.stubs(:to).returns(opts[:to]) + + assert_not_equal st, obj + end + + test 'should fail equality check if to are not the same' do + opts = {:from => 'foo', :to => 'bar', :guard => 'g'} + st = ActiveModel::StateMachine::StateTransition.new(opts) + + obj = stub + obj.stubs(:from).returns(opts[:from]) + obj.stubs(:to).returns('blah') + + assert_not_equal st, obj + end + end +end + +class StateTransitionGuardCheckTest < ActiveModel::TestCase + test 'should return true of there is no guard' do + opts = {:from => 'foo', :to => 'bar'} + st = ActiveModel::StateMachine::StateTransition.new(opts) + + assert st.perform(nil) + end + + uses_mocha 'checking ActiveModel StateMachine transition guard checks' do + test 'should call the method on the object if guard is a symbol' do + opts = {:from => 'foo', :to => 'bar', :guard => :test_guard} + st = ActiveModel::StateMachine::StateTransition.new(opts) + + obj = stub + obj.expects(:test_guard) + + st.perform(obj) + end + + test 'should call the method on the object if guard is a string' do + opts = {:from => 'foo', :to => 'bar', :guard => 'test_guard'} + st = ActiveModel::StateMachine::StateTransition.new(opts) + + obj = stub + obj.expects(:test_guard) + + st.perform(obj) + end + + test 'should call the proc passing the object if the guard is a proc' do + opts = {:from => 'foo', :to => 'bar', :guard => Proc.new {|o| o.test_guard}} + st = ActiveModel::StateMachine::StateTransition.new(opts) + + obj = stub + obj.expects(:test_guard) + + st.perform(obj) + end + end +end diff --git a/activemodel/test/state_machine_test.rb b/activemodel/test/state_machine_test.rb index e906744c77..963ce84248 100644 --- a/activemodel/test/state_machine_test.rb +++ b/activemodel/test/state_machine_test.rb @@ -7,22 +7,22 @@ class StateMachineSubject state :open, :exit => :exit state :closed, :enter => :enter - #event :close, :success => :success_callback do - # transitions :to => :closed, :from => [:open] - #end - # - #event :null do - # transitions :to => :closed, :from => [:open], :guard => :always_false - #end + event :close, :success => :success_callback do + transitions :to => :closed, :from => [:open] + end + + event :null do + transitions :to => :closed, :from => [:open], :guard => :always_false + end end state_machine :bar do state :read state :ended - #event :foo do - # transitions :to => :ended, :from => [:read] - #end + event :foo do + transitions :to => :ended, :from => [:read] + end end def always_false @@ -80,9 +80,13 @@ class StateMachineInstanceLevelTest < ActiveModel::TestCase assert @foo.respond_to?(:open?) end - #test 'should define an event! inance method' do - # assert @foo.respond_to?(:close!) - #end + test 'defines an event! instance method' do + assert @foo.respond_to?(:close!) + end + + test 'defines an event instance method' do + assert @foo.respond_to?(:close) + end end class StateMachineInitialStatesTest < ActiveModel::TestCase @@ -90,19 +94,19 @@ class StateMachineInitialStatesTest < ActiveModel::TestCase @foo = StateMachineSubject.new end - test 'should set the initial state' do + test 'sets the initial state' do assert_equal :open, @foo.current_state end - #test '#open? should be initially true' do - # @foo.open?.should be_true - #end - # - #test '#closed? should be initially false' do - # @foo.closed?.should be_false - #end + test '#open? should be initially true' do + assert @foo.open? + end + + test '#closed? should be initially false' do + assert !@foo.closed? + end - test 'should use the first state defined if no initial state is given' do + test 'uses the first state defined if no initial state is given' do assert_equal :read, @foo.current_state(:bar) end end @@ -141,34 +145,36 @@ end # foo.close! # end #end -# -#describe AASM, '- event firing without persistence' do -# it 'should fire the Event' do -# foo = Foo.new -# -# Foo.aasm_events[:close].should_receive(:fire).with(foo) -# foo.close -# end -# -# it 'should update the current state' do -# foo = Foo.new -# foo.close -# -# foo.aasm_current_state.should == :closed -# end -# -# it 'should attempt to persist if aasm_write_state is defined' do -# foo = Foo.new -# -# def foo.aasm_write_state -# end -# -# foo.should_receive(:aasm_write_state_without_persistence) -# -# foo.close -# end -#end -# + +class StateMachineEventFiringWithoutPersistence < ActiveModel::TestCase + test 'updates the current state' do + subj = StateMachineSubject.new + assert_equal :open, subj.current_state + subj.close + assert_equal :closed, subj.current_state + end + + uses_mocha 'StateMachineEventFiringWithoutPersistence' do + test 'fires the Event' do + subj = StateMachineSubject.new + + StateMachineSubject.state_machine.events[:close].expects(:fire).with(subj) + subj.close + end + + test 'should attempt to persist if aasm_write_state is defined' do + subj = StateMachineSubject.new + + def subj.aasm_write_state + end + + subj.expects(:aasm_write_state_without_persistence) + + subj.close + end + end +end + #describe AASM, '- persistence' do # it 'should read the state if it has not been set and aasm_read_state is defined' do # foo = Foo.new @@ -180,22 +186,7 @@ end # foo.aasm_current_state # end #end -# -#describe AASM, '- getting events for a state' do -# it '#aasm_events_for_current_state should use current state' do -# foo = Foo.new -# foo.should_receive(:aasm_current_state) -# foo.aasm_events_for_current_state -# end -# -# it '#aasm_events_for_current_state should use aasm_events_for_state' do -# foo = Foo.new -# foo.stub!(:aasm_current_state).and_return(:foo) -# foo.should_receive(:aasm_events_for_state).with(:foo) -# foo.aasm_events_for_current_state -# end -#end -# + #describe AASM, '- event callbacks' do # it 'should call aasm_event_fired if defined and successful for bang fire' do # foo = Foo.new @@ -237,32 +228,31 @@ end # foo.null # end #end -# -#describe AASM, '- state actions' do -# it "should call enter when entering state" do -# foo = Foo.new -# foo.should_receive(:enter) -# -# foo.close -# end -# -# it "should call exit when exiting state" do -# foo = Foo.new -# foo.should_receive(:exit) -# -# foo.close -# end -#end -# -# + +uses_mocha 'StateMachineStateActionsTest' do + class StateMachineStateActionsTest < ActiveModel::TestCase + test "calls enter when entering state" do + subj = StateMachineSubject.new + subj.expects(:enter) + subj.close + end + + test "calls exit when exiting state" do + subj = StateMachineSubject.new + subj.expects(:exit) + subj.close + end + end +end + class StateMachineInheritanceTest < ActiveModel::TestCase - test "should have the same states as it's parent" do + test "has the same states as its parent" do assert_equal StateMachineSubject.state_machine.states, StateMachineSubjectSubclass.state_machine.states end - #test "should have the same events as it's parent" do - # StateMachineSubjectSubclass.aasm_events.should == Bar.aasm_events - #end + test "has the same events as its parent" do + assert_equal StateMachineSubject.state_machine.events, StateMachineSubjectSubclass.state_machine.events + end end # # -- cgit v1.2.3 From a9d9ca16c739ec39a192d29c62f760e51040fc6e Mon Sep 17 00:00:00 2001 From: rick Date: Sat, 28 Jun 2008 11:01:40 -0700 Subject: converted tests for more complex state transitions --- .../lib/active_model/state_machine/event.rb | 7 +- .../lib/active_model/state_machine/machine.rb | 26 ++- activemodel/test/state_machine/event_test.rb | 6 +- activemodel/test/state_machine/machine_test.rb | 4 +- activemodel/test/state_machine_test.rb | 222 ++++++++++----------- 5 files changed, 142 insertions(+), 123 deletions(-) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/state_machine/event.rb b/activemodel/lib/active_model/state_machine/event.rb index cc7d563214..ea4df343de 100644 --- a/activemodel/lib/active_model/state_machine/event.rb +++ b/activemodel/lib/active_model/state_machine/event.rb @@ -3,9 +3,8 @@ module ActiveModel class Event attr_reader :name, :success - def initialize(name, options = {}, &block) - @name, @transitions = name, [] - machine = options.delete(:machine) + def initialize(machine, name, options = {}, &block) + @machine, @name, @transitions = machine, name, [] if machine machine.klass.send(:define_method, "#{name.to_s}!") do |*args| machine.fire_event(name, self, true, *args) @@ -19,7 +18,7 @@ module ActiveModel end def fire(obj, to_state = nil, *args) - transitions = @transitions.select { |t| t.from == obj.current_state } + transitions = @transitions.select { |t| t.from == obj.current_state(@machine ? @machine.name : nil) } raise InvalidTransition if transitions.size == 0 next_state = nil diff --git a/activemodel/lib/active_model/state_machine/machine.rb b/activemodel/lib/active_model/state_machine/machine.rb index 1da48290c5..53ce71794f 100644 --- a/activemodel/lib/active_model/state_machine/machine.rb +++ b/activemodel/lib/active_model/state_machine/machine.rb @@ -19,12 +19,21 @@ module ActiveModel self end - def fire_event(name, record, persist, *args) - state_index[record.current_state].call_action(:exit, record) - if new_state = @events[name].fire(record, *args) + def fire_event(event, record, persist, *args) + state_index[record.current_state(@name)].call_action(:exit, record) + if new_state = @events[event].fire(record, *args) state_index[new_state].call_action(:enter, record) + + if record.respond_to?(event_fired_callback) + record.send(event_fired_callback, record.current_state, new_state) + end + record.current_state(@name, new_state) else + if record.respond_to?(event_failed_callback) + record.send(event_failed_callback, event) + end + false end end @@ -37,13 +46,22 @@ module ActiveModel events = @events.values.select { |event| event.transitions_from_state?(state) } events.map! { |event| event.name } end + private def state(name, options = {}) @states << (state_index[name] ||= State.new(name, :machine => self)).update(options) end def event(name, options = {}, &block) - (@events[name] ||= Event.new(name, :machine => self)).update(options, &block) + (@events[name] ||= Event.new(self, name)).update(options, &block) + end + + def event_fired_callback + @event_fired_callback ||= (@name == :default ? '' : "#{@name}_") + 'event_fired' + end + + def event_failed_callback + @event_failed_callback ||= (@name == :default ? '' : "#{@name}_") + 'event_failed' end end end diff --git a/activemodel/test/state_machine/event_test.rb b/activemodel/test/state_machine/event_test.rb index 01f3464cf2..7db4f8d887 100644 --- a/activemodel/test/state_machine/event_test.rb +++ b/activemodel/test/state_machine/event_test.rb @@ -7,7 +7,7 @@ class EventTest < ActiveModel::TestCase end def new_event - @event = ActiveModel::StateMachine::Event.new(@name, {:success => @success}) do + @event = ActiveModel::StateMachine::Event.new(nil, @name, {:success => @success}) do transitions :to => :closed, :from => [:open, :received] end end @@ -31,7 +31,7 @@ end class EventBeingFiredTest < ActiveModel::TestCase test 'should raise an AASM::InvalidTransition error if the transitions are empty' do - event = ActiveModel::StateMachine::Event.new(:event) + event = ActiveModel::StateMachine::Event.new(nil, :event) assert_raises ActiveModel::StateMachine::InvalidTransition do event.fire(nil) @@ -39,7 +39,7 @@ class EventBeingFiredTest < ActiveModel::TestCase end test 'should return the state of the first matching transition it finds' do - event = ActiveModel::StateMachine::Event.new(:event) do + event = ActiveModel::StateMachine::Event.new(nil, :event) do transitions :to => :closed, :from => [:open, :received] end diff --git a/activemodel/test/state_machine/machine_test.rb b/activemodel/test/state_machine/machine_test.rb index 64dea42b1f..2cdfcd9554 100644 --- a/activemodel/test/state_machine/machine_test.rb +++ b/activemodel/test/state_machine/machine_test.rb @@ -36,6 +36,8 @@ class StateMachineMachineTest < ActiveModel::TestCase end test "finds events for given state" do - assert_equal [:shutdown, :timeout], MachineTestSubject.state_machine.events_for(:open) + events = MachineTestSubject.state_machine.events_for(:open) + assert events.include?(:shutdown) + assert events.include?(:timeout) end end \ No newline at end of file diff --git a/activemodel/test/state_machine_test.rb b/activemodel/test/state_machine_test.rb index 963ce84248..2f08b522d9 100644 --- a/activemodel/test/state_machine_test.rb +++ b/activemodel/test/state_machine_test.rb @@ -186,48 +186,50 @@ end # foo.aasm_current_state # end #end - -#describe AASM, '- event callbacks' do -# it 'should call aasm_event_fired if defined and successful for bang fire' do -# foo = Foo.new -# def foo.aasm_event_fired(from, to) -# end -# -# foo.should_receive(:aasm_event_fired) -# -# foo.close! -# end -# -# it 'should call aasm_event_fired if defined and successful for non-bang fire' do -# foo = Foo.new -# def foo.aasm_event_fired(from, to) -# end -# -# foo.should_receive(:aasm_event_fired) -# -# foo.close -# end -# -# it 'should call aasm_event_failed if defined and transition failed for bang fire' do -# foo = Foo.new -# def foo.aasm_event_failed(event) -# end -# -# foo.should_receive(:aasm_event_failed) -# -# foo.null! -# end -# -# it 'should call aasm_event_failed if defined and transition failed for non-bang fire' do -# foo = Foo.new -# def foo.aasm_event_failed(event) -# end -# -# foo.should_receive(:aasm_event_failed) -# -# foo.null -# end -#end + +uses_mocha 'StateMachineEventCallbacksTest' do + class StateMachineEventCallbacksTest < ActiveModel::TestCase + test 'should call aasm_event_fired if defined and successful for bang fire' do + subj = StateMachineSubject.new + def subj.aasm_event_fired(from, to) + end + + subj.expects(:event_fired) + + subj.close! + end + + test 'should call aasm_event_fired if defined and successful for non-bang fire' do + subj = StateMachineSubject.new + def subj.aasm_event_fired(from, to) + end + + subj.expects(:event_fired) + + subj.close + end + + test 'should call aasm_event_failed if defined and transition failed for bang fire' do + subj = StateMachineSubject.new + def subj.event_failed(event) + end + + subj.expects(:event_failed) + + subj.null! + end + + test 'should call aasm_event_failed if defined and transition failed for non-bang fire' do + subj = StateMachineSubject.new + def subj.aasm_event_failed(event) + end + + subj.expects(:event_failed) + + subj.null + end + end +end uses_mocha 'StateMachineStateActionsTest' do class StateMachineStateActionsTest < ActiveModel::TestCase @@ -254,72 +256,70 @@ class StateMachineInheritanceTest < ActiveModel::TestCase assert_equal StateMachineSubject.state_machine.events, StateMachineSubjectSubclass.state_machine.events end end -# -# -#class ChetanPatil -# include AASM -# aasm_initial_state :sleeping -# aasm_state :sleeping -# aasm_state :showering -# aasm_state :working -# aasm_state :dating -# -# aasm_event :wakeup do -# transitions :from => :sleeping, :to => [:showering, :working] -# end -# -# aasm_event :dress do -# transitions :from => :sleeping, :to => :working, :on_transition => :wear_clothes -# transitions :from => :showering, :to => [:working, :dating], :on_transition => Proc.new { |obj, *args| obj.wear_clothes(*args) } -# end -# -# def wear_clothes(shirt_color, trouser_type) -# end -#end -# -# -#describe ChetanPatil do -# it 'should transition to specified next state (sleeping to showering)' do -# cp = ChetanPatil.new -# cp.wakeup! :showering -# -# cp.aasm_current_state.should == :showering -# end -# -# it 'should transition to specified next state (sleeping to working)' do -# cp = ChetanPatil.new -# cp.wakeup! :working -# -# cp.aasm_current_state.should == :working -# end -# -# it 'should transition to default (first or showering) state' do -# cp = ChetanPatil.new -# cp.wakeup! -# -# cp.aasm_current_state.should == :showering -# end -# -# it 'should transition to default state when on_transition invoked' do -# cp = ChetanPatil.new -# cp.dress!(nil, 'purple', 'dressy') -# -# cp.aasm_current_state.should == :working -# end -# -# it 'should call on_transition method with args' do -# cp = ChetanPatil.new -# cp.wakeup! :showering -# -# cp.should_receive(:wear_clothes).with('blue', 'jeans') -# cp.dress! :working, 'blue', 'jeans' -# end -# -# it 'should call on_transition proc' do -# cp = ChetanPatil.new -# cp.wakeup! :showering -# -# cp.should_receive(:wear_clothes).with('purple', 'slacks') -# cp.dress!(:dating, 'purple', 'slacks') -# end -#end \ No newline at end of file + +class StateMachineSubject + state_machine :chetan_patil, :initial => :sleeping do + state :sleeping + state :showering + state :working + state :dating + + event :wakeup do + transitions :from => :sleeping, :to => [:showering, :working] + end + + event :dress do + transitions :from => :sleeping, :to => :working, :on_transition => :wear_clothes + transitions :from => :showering, :to => [:working, :dating], :on_transition => Proc.new { |obj, *args| obj.wear_clothes(*args) } + end + end + + def wear_clothes(shirt_color, trouser_type) + end +end + +class StateMachineWithComplexTransitionsTest < ActiveModel::TestCase + def setup + @subj = StateMachineSubject.new + end + + test 'transitions to specified next state (sleeping to showering)' do + @subj.wakeup! :showering + + assert_equal :showering, @subj.current_state(:chetan_patil) + end + + test 'transitions to specified next state (sleeping to working)' do + @subj.wakeup! :working + + assert_equal :working, @subj.current_state(:chetan_patil) + end + + test 'transitions to default (first or showering) state' do + @subj.wakeup! + + assert_equal :showering, @subj.current_state(:chetan_patil) + end + + test 'transitions to default state when on_transition invoked' do + @subj.dress!(nil, 'purple', 'dressy') + + assert_equal :working, @subj.current_state(:chetan_patil) + end + + uses_mocha "StateMachineWithComplexTransitionsTest on_transition tests" do + test 'calls on_transition method with args' do + @subj.wakeup! :showering + + @subj.expects(:wear_clothes).with('blue', 'jeans') + @subj.dress! :working, 'blue', 'jeans' + end + + test 'calls on_transition proc' do + @subj.wakeup! :showering + + @subj.expects(:wear_clothes).with('purple', 'slacks') + @subj.dress!(:dating, 'purple', 'slacks') + end + end +end \ No newline at end of file -- cgit v1.2.3 From c9e366e997c6f3a383cfaa6351fa847e92de7fe4 Mon Sep 17 00:00:00 2001 From: rick Date: Sat, 28 Jun 2008 11:33:50 -0700 Subject: all aasm tests without activerecord moved over and passing --- activemodel/lib/active_model/state_machine.rb | 22 ++++- .../lib/active_model/state_machine/event.rb | 4 - .../lib/active_model/state_machine/machine.rb | 8 +- activemodel/test/state_machine/event_test.rb | 2 +- activemodel/test/state_machine_test.rb | 99 +++++++++++----------- 5 files changed, 76 insertions(+), 59 deletions(-) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/state_machine.rb b/activemodel/lib/active_model/state_machine.rb index 2a5ac95a3e..96df6539ae 100644 --- a/activemodel/lib/active_model/state_machine.rb +++ b/activemodel/lib/active_model/state_machine.rb @@ -37,13 +37,29 @@ module ActiveModel end end - def current_state(name = nil, new_state = nil) + def current_state(name = nil, new_state = nil, persist = false) sm = self.class.state_machine(name) - ivar = "@#{sm.name}_current_state" + ivar = sm.current_state_variable if name && new_state + if persist && respond_to?(:write_state) + write_state(sm, new_state) + end + + if respond_to?(:write_state_without_persistence) + write_state_without_persistence(sm, new_state) + end + instance_variable_set(ivar, new_state) else - instance_variable_get(ivar) || instance_variable_set(ivar, sm.initial_state) + instance_variable_set(ivar, nil) unless instance_variable_defined?(ivar) + value = instance_variable_get(ivar) + return value if value + + if respond_to?(:read_state) + value = instance_variable_set(ivar, read_state(sm)) + end + + value || sm.initial_state end end end diff --git a/activemodel/lib/active_model/state_machine/event.rb b/activemodel/lib/active_model/state_machine/event.rb index ea4df343de..e8bc8ebdb7 100644 --- a/activemodel/lib/active_model/state_machine/event.rb +++ b/activemodel/lib/active_model/state_machine/event.rb @@ -37,10 +37,6 @@ module ActiveModel @transitions.any? { |t| t.from? state } end - def success? - !!@success - end - def ==(event) if event.is_a? Symbol name == event diff --git a/activemodel/lib/active_model/state_machine/machine.rb b/activemodel/lib/active_model/state_machine/machine.rb index 53ce71794f..170505c0b2 100644 --- a/activemodel/lib/active_model/state_machine/machine.rb +++ b/activemodel/lib/active_model/state_machine/machine.rb @@ -28,7 +28,9 @@ module ActiveModel record.send(event_fired_callback, record.current_state, new_state) end - record.current_state(@name, new_state) + record.current_state(@name, new_state, persist) + record.send(@events[event].success) if @events[event].success + true else if record.respond_to?(event_failed_callback) record.send(event_failed_callback, event) @@ -47,6 +49,10 @@ module ActiveModel events.map! { |event| event.name } end + def current_state_variable + "@#{@name}_current_state" + end + private def state(name, options = {}) @states << (state_index[name] ||= State.new(name, :machine => self)).update(options) diff --git a/activemodel/test/state_machine/event_test.rb b/activemodel/test/state_machine/event_test.rb index 7db4f8d887..40b630da7c 100644 --- a/activemodel/test/state_machine/event_test.rb +++ b/activemodel/test/state_machine/event_test.rb @@ -17,7 +17,7 @@ class EventTest < ActiveModel::TestCase end test 'should set the success option' do - assert new_event.success? + assert_equal @success, new_event.success end uses_mocha 'StateTransition creation' do diff --git a/activemodel/test/state_machine_test.rb b/activemodel/test/state_machine_test.rb index 2f08b522d9..b2f0fc4ec0 100644 --- a/activemodel/test/state_machine_test.rb +++ b/activemodel/test/state_machine_test.rb @@ -110,41 +110,38 @@ class StateMachineInitialStatesTest < ActiveModel::TestCase assert_equal :read, @foo.current_state(:bar) end end -# -#describe AASM, '- event firing with persistence' do -# it 'should fire the Event' do -# foo = Foo.new -# -# Foo.aasm_events[:close].should_receive(:fire).with(foo) -# foo.close! -# end -# -# it 'should update the current state' do -# foo = Foo.new -# foo.close! -# -# foo.aasm_current_state.should == :closed -# end -# -# it 'should call the success callback if one was provided' do -# foo = Foo.new -# -# foo.should_receive(:success_callback) -# -# foo.close! -# end -# -# it 'should attempt to persist if aasm_write_state is defined' do -# foo = Foo.new -# -# def foo.aasm_write_state -# end -# -# foo.should_receive(:aasm_write_state) -# -# foo.close! -# end -#end + +class StateMachineEventFiringWithPersistenceTest < ActiveModel::TestCase + def setup + @subj = StateMachineSubject.new + end + + test 'updates the current state' do + @subj.close! + + assert_equal :closed, @subj.current_state + end + + uses_mocha "StateMachineEventFiringWithPersistenceTest with callbacks" do + test 'fires the Event' do + @subj.class.state_machine.events[:close].expects(:fire).with(@subj) + @subj.close! + end + + test 'calls the success callback if one was provided' do + @subj.expects(:success_callback) + @subj.close! + end + + test 'attempts to persist if write_state is defined' do + def @subj.write_state + end + + @subj.expects(:write_state) + @subj.close! + end + end +end class StateMachineEventFiringWithoutPersistence < ActiveModel::TestCase test 'updates the current state' do @@ -162,30 +159,32 @@ class StateMachineEventFiringWithoutPersistence < ActiveModel::TestCase subj.close end - test 'should attempt to persist if aasm_write_state is defined' do + test 'attempts to persist if write_state is defined' do subj = StateMachineSubject.new - def subj.aasm_write_state + def subj.write_state end - subj.expects(:aasm_write_state_without_persistence) + subj.expects(:write_state_without_persistence) subj.close end end end - -#describe AASM, '- persistence' do -# it 'should read the state if it has not been set and aasm_read_state is defined' do -# foo = Foo.new -# def foo.aasm_read_state -# end -# -# foo.should_receive(:aasm_read_state) -# -# foo.aasm_current_state -# end -#end + +uses_mocha 'StateMachinePersistenceTest' do + class StateMachinePersistenceTest < ActiveModel::TestCase + test 'reads the state if it has not been set and read_state is defined' do + subj = StateMachineSubject.new + def subj.read_state + end + + subj.expects(:read_state).with(StateMachineSubject.state_machine) + + subj.current_state + end + end +end uses_mocha 'StateMachineEventCallbacksTest' do class StateMachineEventCallbacksTest < ActiveModel::TestCase -- cgit v1.2.3