spec/lib/hotcell/node/block_spec.rb in hotcell-0.0.1 vs spec/lib/hotcell/node/block_spec.rb in hotcell-0.1.0

- old
+ new

@@ -19,48 +19,53 @@ instance || klass.new(method, *args) end let(:context) { Hotcell::Context.new } - describe 'complex parsing and rendering' do + context 'complex parsing and rendering' do def parse source Hotcell::Template.parse(source) end let(:if_tag) do Class.new(described_class) do - subcommands :else, :elsif + subcommand :else + subcommand :elsif def validate! - names = subcommands.map { |subcommand| subcommand[:name] } + names = subcommands.map { |subcommand| subcommand.name } valid = names.empty? || ( - names.any? && names.last.in?('elsif', 'else') && - names[0..-2].uniq.in?(['elsif'], []) + names.any? && %w(elsif else).include?(names.last) && + [['elsif'], []].include?(names[0..-2].uniq) ) - raise Hotcell::Errors::BlockError.new 'Invalid if syntax' unless valid + raise Hotcell::BlockError.new 'Invalid if syntax', *position_info unless valid end - def process context, subnodes, condition + def process context, condition conditions = [[condition]] subnodes.each do |subnode| - if subnode.is_a?(Hash) + if subnode.is_a?(Hotcell::Command) conditions.last[1] = '' if conditions.last[1] == nil - conditions << (subnode[:name] == 'elsif' ? [subnode[:args].first] : [true]) + conditions << (subnode.name == 'elsif' ? [subnode.render_children(context).first] : [true]) else - conditions.last[1] = subnode + conditions.last[1] = subnode.render(context) end end condition = conditions.detect { |condition| !!condition[0] } condition ? condition[1] : nil end end end before { Hotcell.stub(:commands) { {} } } before { Hotcell.stub(:blocks) { { 'if' => if_tag } } } - before { Hotcell.stub(:subcommands) { { 'elsif' => if_tag, 'else' => if_tag } } } + specify { parse('{{ if true }}Hello{{ end if }}').render.should == 'Hello' } + specify { parse('{{! if true }}Hello{{ end if }}').render.should == '' } + specify { parse('{{ res = if true }}Hello{{ end if }} {{ res }}').render.should == 'Hello Hello' } + specify { parse('{{! res = if true }}Hello{{ end if }} {{ res }}').render.should == ' Hello' } + context do subject(:template) { parse(<<-SOURCE {{ if value == 'hello' }} hello {{ elsif value == 'world' }} @@ -89,83 +94,50 @@ end context do specify { expect { parse("{{ if value == 'hello' }}{{ else }}world{{ elsif }}{{ end if }}").syntax - }.to raise_error Hotcell::Errors::BlockError } + }.to raise_error Hotcell::BlockError } end end describe '.subcommands' do subject { Class.new(described_class) } - before { subject.subcommands :elsif, :else } - its(:subcommands) { should == %w(elsif else) } + before { subject.subcommand elsif: Class.new(Hotcell::Command), else: Class.new(Hotcell::Command) } + its('subcommands.keys') { should == %w(elsif else) } context do - before { subject.subcommands :when } - its(:subcommands) { should == %w(elsif else when) } + before { subject.subcommand :when } + its('subcommands.keys') { should == %w(elsif else when) } end end describe '#subcommands' do specify { described_class.new('if', - subnodes: [{name: 'elsif'}, JOINER(), {name: 'else'}, JOINER()]).subcommands.should == [ - {name: 'elsif'}, {name: 'else'}] } + subnodes: [COMMAND('elsif'), JOINER(), COMMAND('else'), JOINER()]).subcommands.should == [ + COMMAND('elsif'), COMMAND('else') + ] } end - describe '#render_subnodes' do - specify { described_class.new('if', - subnodes: [{name: 'elsif', args: ARRAY(3, 5)}, JOINER(42), {name: 'else'}, JOINER(43)] - ).render_subnodes(context).should == [ - {name: 'elsif', args: [3, 5]}, '42', {name: 'else'}, '43' - ] } - end - describe '#render' do let(:block) do Class.new(described_class) do - def process context, subnodes, condition + def process context, condition "condition #{condition}" end end end specify { block.new('if').render(context).should =~ /ArgumentError/ } - specify { block.new('if', mode: :silence).render(context).should =~ /ArgumentError/ } specify { block.new('if', true).render(context).should == "condition true" } - specify { block.new('if', true, mode: :silence).render(context).should == '' } - - context 'assigning' do - before { subject.render(context) } - - context do - subject { block.new('if', assign: 'result') } - specify { context.key?('result').should be_false } - end - - context do - subject { block.new('if', mode: :silence, assign: 'result') } - specify { context.key?('result').should be_false } - end - - context do - subject { block.new('if', true, assign: 'result') } - specify { context['result'].should == "condition true" } - end - - context do - subject { block.new('if', 42, mode: :silence, assign: 'result') } - specify { context['result'].should == "condition 42" } - end - end end context '#validate!' do let(:block) do Class.new(described_class) do - subcommands :else + subcommand :else def process context, subnodes, condition "condition #{condition}" end end end @@ -174,15 +146,15 @@ subject { block.new('if', true, subnodes: [TEMPLATE('hello')] ) } specify { expect { subject.validate! }.not_to raise_error } end context do - subject { block.new('if', true, subnodes: [{ name: 'else' }] ) } + subject { block.new('if', true, subnodes: [COMMAND('else')] ) } specify { expect { subject.validate! }.not_to raise_error } end context do - subject { block.new('if', true, subnodes: [{ name: 'case' }] ) } + subject { block.new('if', true, subnodes: [COMMAND('case')] ) } specify { expect { subject.validate! }.to raise_error } end end end