test/surrounded_context_test.rb in surrounded-0.8.3 vs test/surrounded_context_test.rb in surrounded-0.8.4

- old
+ new

@@ -41,10 +41,24 @@ assert_raises(NoMethodError){ user.other_user } assert_equal "Guille", context.access_other_object end + + it 'preserves arguments and blocks' do + result = context.block_method('argument') do |*args, obj| + "Having an #{args.first} with #{obj}" + end + end + + it 'allows usage of regular methods for triggers' do + assert context.regular_method_trigger + end + + it 'ignores nil trigger names' do + assert context.class.send(:trigger) + end end describe Surrounded::Context, '#role?' do let(:user){ test_user = User.new("Jim") @@ -108,32 +122,61 @@ trigger :check_other_user_response do user.respond_to?(:a_method!) end - def regular_method_trigger - user.respond_to?(:a_method!) - end - - trigger :user_ancestors, :other_user_ancestors, :regular_method_trigger + trigger :user_ancestors, :other_user_ancestors module User def a_method!; end end module OtherUser def a_method!; end end end +class Special; end +class IgnoreExternalConstantsContext + extend Surrounded::Context + + initialize :user, :special, :other + + role :other do + def something_or_other; end + end + + trigger :check_special do + special.class + end +end + describe Surrounded::Context, '.initialize' do it 'defines an initialize method accepting the same arguments' do assert_equal 2, RoleAssignmentContext.instance_method(:initialize).arity end end +class ClassRoleAssignmentContext + extend Surrounded::Context + + initialize(:thing, :the_test) + + trigger :check_user_response do + the_test.assert_respond_to thing, :method_from_class + end + + class Thing + include Surrounded + def initialize(obj); end + def method_from_class; end + end + +end + describe Surrounded::Context, 'assigning roles' do - include Surrounded + include Surrounded # the test must be context-aware + let(:user){ User.new("Jim") } let(:other_user){ CastingUser.new("Guille") } let(:context){ RoleAssignmentContext.new(user, other_user) } it 'tries to use casting to add roles' do @@ -148,41 +191,23 @@ assert context.check_user_response assert context.check_other_user_response end it 'will use classes as roles' do - ClassRoleAssignmentContext = Class.new do - extend Surrounded::Context - - initialize(:thing, :the_test) - - trigger :check_user_response do - the_test.assert_respond_to thing, :method_from_class - end - - class Thing - include Surrounded - def initialize(obj); end - def method_from_class; end - end - - end - user = User.new('Jim') context = ClassRoleAssignmentContext.new(user, self) assert context.check_user_response end - it 'allows usage of regular methods for triggers' do - assert context.regular_method_trigger + it 'does not use constants defined outside the context class' do + special = User.new('Special') + other = User.new('Other') + context = IgnoreExternalConstantsContext.new(user, special, other) + assert_equal User, context.check_special end - - it 'ignores nil trigger names' do - assert context.class.send(:trigger) - end end class CollectionContext extend Surrounded::Context @@ -205,9 +230,12 @@ role :member do def show "member show" end end + + role :others do; end + role :other do; end end describe Surrounded::Context, 'auto-assigning roles for collections' do let(:member_one){ User.new('Jim') }