require 'setup' spec :Filters do context :composition do it 'inherits filters from superclass' do before, around, after = [], [], [] a = mock_controller { before(:get) {before << :a} around(:get) {|a| around << :a} after(:get) {after << :a} def get; end } b = mock_controller(a) app(b) get assert([before, around, after]).all? {|a| a.include?(:a)} end it 'inherits filters explicitly' do before, around, after = [], [], [] a = mock_controller { before(:get) {before << :x} around(:get) {|a| around << :x} after(:get) {after << :x} def get; end } b = mock_controller { import :before, from: a import :around, from: a import :after, from: a } app(b) get assert([before, around, after]).all? {|a| a.include?(:x)} end test 'explicitly inherited filters overrides filters inherited from superclass' do before, around, after = [], [], [] a = mock_controller { before(:get) {before << :a} around(:get) {|a| around << :a} after(:get) {after << :a} def get; end } b = mock_controller { before(:get) {before << :b} around(:get) {|a| around << :b} after(:get) {after << :b} } c = mock_controller(a) { import :before, from: b import :around, from: b import :after, from: b } app(c) get assert([before, around, after]).none? {|a| a.include?(:a)} assert([before, around, after]).all? {|a| a.include?(:b)} end test 'explicitly inherited filters complements filters inherited from superclass' do before_get, around_get, after_get = [], [], [] before_post, around_post, after_post = [], [], [] a = mock_controller { before(:get) {before_get << :a} around(:get) {|a| around_get << :a} after(:get) {after_get << :a} } b = mock_controller { before(:post) {before_post << :b} around(:post) {|a| around_post << :b} after(:post) {after_post << :b} } c = mock_controller(a) { import :before, from: b import :around, from: b import :after, from: b } app(c) get assert([before_get, around_get, after_get]).all? {|a| a.include?(:a)} post assert([before_post, around_post, after_post]).all? {|a| a.include?(:b)} end it 'defined filters overrides filters inherited from superclass' do before, around, after = [], [], [] a = mock_controller { before(:get) {before << :a} around(:get) {|a| around << :a} after(:get) {after << :a} } b = mock_controller(a) { before(:get) {before << :b} after(:get) {after << :b} } app(b) get assert([before, after]).all? {|a| a == [:b]} assert(around) == [:a] end end it 'defines filters for all requested methods if called without arguments' do before, around, after = [], [], [] app mock_controller { before() {before << requested_method} around() {|a| around << requested_method} after() {after << requested_method} } filters = [before, around, after] get assert(filters).all? {|a| a.include?(:get)} post assert(filters).all? {|a| a.include?(:post)} put assert(filters).all? {|a| a.include?(:put)} head assert(filters).all? {|a| a.include?(:head)} delete assert(filters).all? {|a| a.include?(:delete)} end it 'defines filters only for given requested method(s)' do before, around, after = [], [], [] app mock_controller { before(:get) {before << requested_method} around(:put, :post) {|a| around << requested_method} after(:delete) {after << requested_method} def delete; end } get assert(before).include?(:get) assert([around, after]).none? {|a| a.include?(:get)} post assert(around).include?(:post) assert([before, after]).none? {|a| a.include?(:post)} put assert(around).include?(:put) assert([before, after]).none? {|a| a.include?(:put)} delete assert(after).include?(:delete) assert([before, around]).none? {|a| a.include?(:delete)} end it 'creates a void filter if called without a block' do ctrl = mock_controller { before(:get) {raise 'this should be overridden'} before(:get) def get; end }.initialize_controller app(ctrl) expect(ctrl).to_receive(:__before_get__) get end it 'it calls wildcard filters before specialized ones' do buffer = [] app mock_controller { # specialized filter, called only on GET requests before(:get) {buffer << requested_method} # wildcard filter, called on any requests before {buffer << :*} } get assert(buffer) == [:*, :get] end end