dir = File.expand_path(File.dirname(__FILE__)) require "#{dir}/../micon_spec_helper" describe "Micon" do class CustomScope inherit Micon::Managed scope :custom attr_accessor :value end class CustomScopeScope inherit Micon::Managed scope :application def start; end scope_begin :start, :custom def stop; end scope_end :stop, :custom end it "Should raise error if not started" do lambda{Micon[CustomScope]}.should raise_error(/nil:NilClass/) end it "General usage case" do manager = Micon[CustomScopeScope] c = nil Micon.activate_thread :key do manager.start c = Micon[CustomScope] c.should_not be_nil end Micon.activate_thread :key do c.equal?(Micon[CustomScope]).should be_true manager.stop end Micon.activate_thread :key do lambda{Micon[CustomScope]}.should raise_error(/hasn't been started/) manager.start c.equal?(Micon[CustomScope]).should be_false manager.stop end end it "active?" do Micon.activate_thread :key do Micon.active?(:custom).should be_false Micon.begin :custom Micon.active?(:custom).should be_true Micon.end :custom Micon.active?(:custom).should be_false end end it "Should clear not closed scope when reopen" do manager = Micon[CustomScopeScope] c = nil Micon.activate_thread :key do manager.start c = Micon[CustomScope] end Micon.activate_thread :key do manager.start c.equal?(Micon[CustomScope]).should be_false manager.stop end end it "Should set custom values" do manager = Micon[CustomScopeScope] Micon.activate_thread :key do manager.start Micon[CustomScope] = 'new value' Micon[CustomScope].should == 'new value' manager.stop end end it "Continuation, general usage" do Micon.activate_thread :session_id do Micon.begin :custom Micon[CustomScope].value = 'value' Micon[CustomScope].value.should == 'value' Micon.continuation_begin :custom Micon[CustomScope].value.should == nil Micon[CustomScope].value = 'value2' Micon[CustomScope].value.should == 'value2' Micon.continuation_end :custom Micon[CustomScope].value.should == 'value' Micon.end :custom end end it "Continuation, if scope closes it should also close all continuations" do Micon.activate_thread :session_id do Micon.begin :custom Micon[CustomScope].value = 'value' Micon.continuation_begin :custom Micon[CustomScope].value = 'value2' Micon.end :custom lambda{Micon[CustomScope]}.should raise_error(/hasn't been started/) end end it "Before & After custom Scope" do mock = mock("Before & After") Micon.before(:custom3){mock.before} Micon.after(:custom3){mock.after} Micon.activate_thread :session_id do mock.should_receive(:before) Micon.begin :custom3 mock.should_receive(:after) Micon.end :custom3 end end it "Before & After custom Scope, shouldn't be called in Continuations" do mock = mock("Before & After") Micon.before(:custom2){mock.before} Micon.after(:custom2){mock.after} Micon.activate_thread :session_id do mock.should_receive(:before) Micon.begin :custom2 Micon.continuation_begin :custom2 mock.should_receive(:after) Micon.end :custom2 end Micon.after(:custom2){} Micon.before(:custom2){} end it "Save and Restore Scope, general usage" do Micon.activate_thread :session_id do Micon.begin :custom Micon[CustomScope].value = 'value' Micon[CustomScope].value.should == 'value' save = Micon.custom_scope_get :custom Micon.begin :custom Micon[CustomScope].value.should == nil Micon.custom_scope_set :custom, save Micon[CustomScope].value.should == 'value' Micon.end :custom end end it "Scope Groups (Defines Group and forwards all calls to Scope for_each Participant)" do Micon.register :object, :object Micon.register :view, :view Micon.group(:object_group) << :object Micon.group(:object_group) << :view Micon.activate_thread :session_id do lambda{Micon[:object]}.should raise_error(/hasn't been started/) Micon.group(:object_group).begin Micon[:object] end end end