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