require 'setup' spec :template_vars do context :compositing do it 'inherits vars from superclass' do a = mock_controller { define_template_var(:a, 'a') } b = mock_controller(a) {}.initialize_controller assert(b.template_vars[:a]) == 'a' end it 'complements vars inherited from superclass' do a = mock_controller { define_template_var(:a, 'a') } b = mock_controller(a) { define_template_var(:b, 'b') }.initialize_controller assert(b.template_vars[:a]) == 'a' assert(b.template_vars[:b]) == 'b' end it 'overrides vars inherited from superclass' do a = mock_controller { define_template_var(:a, 'a') } b = mock_controller(a) { define_template_var(:a, 'b') }.initialize_controller assert(b.template_vars[:a]) == 'b' end test '`inherit` overrides vars inherited from superclass' do a = mock_controller { define_template_var(:a, 'a') } b = mock_controller(a) { define_template_var(:a, 'b') } c = mock_controller(a) { inherit :template_vars, from: b }.initialize_controller assert(c.template_vars[:a]) == 'b' end test '`inherit` complements vars inherited from superclass' do a = mock_controller { define_template_var(:a, 'a') } b = mock_controller(a) { define_template_var(:b, 'b') } c = mock_controller(a) { inherit :template_vars, from: b }.initialize_controller assert(c.template_vars[:a]) == 'a' assert(c.template_vars[:b]) == 'b' end test '`inherit` overrides defined vars' do a = mock_controller { define_template_var(:a, 'a') } b = mock_controller { define_template_var(:a, 'b') inherit :template_vars, from: a }.initialize_controller assert(b.template_vars[:a]) == 'a' end end context :rendering do it 'accepts plain value via second argument' do c = mock_controller { define_template(:get) {'<%= x %>'} define_template_var(:x, 'y') }.initialize_controller assert(c.render(:get)) == 'y' end it 'accepts a proc as second argument' do c = mock_controller { define_template(:get) {'<%= x %>'} define_template_var(:x, -> {'y'}) }.initialize_controller assert(c.render(:get)) == 'y' end it 'accepts a block without second argument' do c = mock_controller { define_template(:get) {'<%= x %>'} define_template_var(:x) {'y'} }.initialize_controller assert(c.render(:get)) == 'y' end it 'raises ArgumentError if no value nor block given' do assert { mock_controller { define_template_var(:x) }}.raise(ArgumentError) end end end