require 'setup' spec :layouts do context :compositing do it 'inherits layouts from superclass' do a = mock_controller { define_layout(:a) {'a'} } b = mock_controller(a) { }.initialize_controller assert(b.__send__ b.layouts[:a]) == 'a' end it 'complements layouts inherited from superclass' do a = mock_controller { define_layout(:a) {'a'} } b = mock_controller(a) { define_layout(:b) {'b'} }.initialize_controller assert(b.__send__ b.layouts[:a]) == 'a' assert(b.__send__ b.layouts[:b]) == 'b' end it 'overrides layouts inherited from superclass' do a = mock_controller { define_layout(:x) {'a'} } b = mock_controller(a) { define_layout(:x) {'b'} }.initialize_controller assert(b.__send__ b.layouts[:x]) == 'b' end test '`inherit` overrides layouts inherited from superclass' do a = mock_controller { define_layout(:x) {'a'} } b = mock_controller(a) { define_layout(:x) {'b'} } c = mock_controller(a) { import :layouts, from: b }.initialize_controller assert(c.__send__ c.layouts[:x]) == 'b' end test '`inherit` complements layouts inherited from superclass' do a = mock_controller { define_layout(:a) {'a'} } b = mock_controller(a) { define_layout(:b) {'b'} } c = mock_controller(a) { import :layouts, from: b }.initialize_controller assert(c.__send__ c.layouts[:a]) == 'a' assert(c.__send__ c.layouts[:b]) == 'b' end test '`inherit` overrides defined layouts' do a = mock_controller { define_layout(:x) {'a'} } b = mock_controller { define_layout(:x) {'b'} import :layouts, from: a }.initialize_controller assert(b.__send__ b.layouts[:x]) == 'a' end test '`inherit` complements defined layouts' do a = mock_controller { define_layout(:a) {'a'} } b = mock_controller { define_layout(:b) {'b'} import :layouts, from: a }.initialize_controller assert(b.__send__ b.layouts[:a]) == 'a' assert(b.__send__ b.layouts[:b]) == 'b' end it 'overrides layouts inherited via `inherit`' do a = mock_controller { define_layout(:x) {'a'} } b = mock_controller { import :layouts, from: a define_layout(:x) {'b'} }.initialize_controller assert(b.__send__ b.layouts[:x]) == 'b' end end context :rendering do it 'uses a file with same name if only name given' do c = mock_controller { define_layout :master }.initialize_controller assert(c.render_layout(:master) {'yo'}) == "master yo layout\n" end it 'raises a TemplateError if there is no file with same name' do layout = rand.to_s c = mock_controller { define_layout layout }.initialize_controller assert {c.render_layout(layout) {}}.raise RocketIO::TemplateError end it 'uses given file - file resides in controller dirname' do c = mock_controller { define_layout :master, file: :layout }.initialize_controller assert(c.render_layout(:master) {'yo'}) == "=yo=\n" end it 'uses given file - file resides outside controller dirname' do c = mock_controller { define_layout :master, file: './layouts/master' }.initialize_controller assert(c.render_layout(:master) {'yo'}) == "outside yo layout\n" end it 'accepts a block for given file' do c = mock_controller { define_layout :master, file: -> {@outside ? './layouts/master' : :master} }.initialize_controller assert(c.render_layout(:master) {'yo'}) == "master yo layout\n" c.instance_variable_set(:@outside, true) assert(c.render_layout(:master) {'yo'}) == "outside yo layout\n" end it 'accepts a block and uses returned value as layout without searching for file' do c = mock_controller { define_layout(:master) {@admin ? ':<%= yield %>:' : '|<%= yield %>|'} }.initialize_controller assert(c.render_layout(:master) {'yo'}) == "|yo|" c.instance_variable_set(:@admin, true) assert(c.render_layout(:master) {'yo'}) == ":yo:" end end end