spec/lib/hotcell/parser_spec.rb in hotcell-0.2.0 vs spec/lib/hotcell/parser_spec.rb in hotcell-0.3.0

- old
+ new

@@ -216,9 +216,57 @@ ), mode: :normal)) } end end + context 'ternary operator' do + specify { parse('{{ true ? 42 : 43 }}').should be_equal_node_to JOINER(TAG(42, mode: :normal)) } + specify { parse('{{ false ? 42 : 43 }}').should be_equal_node_to JOINER(TAG(43, mode: :normal)) } + specify { parse('{{ cond ? branch1 : branch2 }}').should be_equal_node_to JOINER( + TAG(TERNARY(METHOD('cond'), METHOD('branch1'), METHOD('branch2')), mode: :normal) + ) } + specify { parse('{{ cond1 ? branch1 : cond2 ? branch2 : branch3 }}').should be_equal_node_to JOINER( + TAG(TERNARY( + METHOD('cond1'), METHOD('branch1'), + TERNARY(METHOD('cond2'), METHOD('branch2'), METHOD('branch3')) + ), mode: :normal) + ) } + specify { parse('{{ (cond1 ? branch1 : cond2) ? branch2 : branch3 }}').should be_equal_node_to JOINER( + TAG(TERNARY( + TERNARY(METHOD('cond1'), METHOD('branch1'), METHOD('cond2')), + METHOD('branch2'), METHOD('branch3') + ), mode: :normal) + ) } + specify { parse('{{ cond1 ? cond2 ? branch1 : branch2 : branch3 }}').should be_equal_node_to JOINER( + TAG(TERNARY( + METHOD('cond1'), + TERNARY(METHOD('cond2'), METHOD('branch1'), METHOD('branch2')), + METHOD('branch3') + ), mode: :normal) + ) } + specify { parse('{{ res = cond ? branch1 + 3 : branch2 && true }}').should be_equal_node_to JOINER( + TAG(ASSIGN('res', + TERNARY(METHOD('cond'), PLUS(METHOD('branch1'), 3), AND(METHOD('branch2'), true) + )), mode: :normal) + ) } + specify { expect { parse('{{ (1 ? 2) : 3 }}') }.to raise_error Hotcell::UnexpectedLexem, "Unexpected PCLOSE `)` at 1:10"} + end + + context 'interpolation' do + specify { parse('{{ "hello" }}').should be_equal_node_to JOINER(TAG('hello', mode: :normal)) } + specify { parse('{{ \'#{}\' }}').should be_equal_node_to JOINER(TAG('#{}', mode: :normal)) } + specify { parse('{{ "#{}" }}').should be_equal_node_to JOINER(TAG('', mode: :normal)) } + specify { parse('{{ "#{ 3 }" }}').should be_equal_node_to JOINER(TAG('3', mode: :normal)) } + specify { parse('{{ "#{ var }" }}').should be_equal_node_to JOINER(TAG(DSTRING(METHOD('var')), mode: :normal)) } + specify { parse('{{ "#{ var }world" }}').should be_equal_node_to JOINER(TAG(DSTRING(METHOD('var'), 'world'), mode: :normal)) } + specify { parse('{{ "hello#{}" }}').should be_equal_node_to JOINER(TAG('hello', mode: :normal)) } + specify { parse('{{ "hello#{}world" }}').should be_equal_node_to JOINER(TAG('helloworld', mode: :normal)) } + specify { parse('{{ "hello#{ 6 * 7 }world" }}').should be_equal_node_to JOINER(TAG('hello42world', mode: :normal)) } + specify { parse('{{ "hello#{ var = 6 * 7; var + 1 }world" }}').should be_equal_node_to JOINER( + TAG(DSTRING('hello', SEQUENCE(ASSIGN('var', 42), PLUS(METHOD('var'), 1)), 'world'), mode: :normal) + ) } + end + context 'arrays' do specify { parse('{{ [] }}').should be_equal_node_to JOINER(TAG(ARRAY(), mode: :normal)) } specify { parse('{{ [ 2 ] }}').should be_equal_node_to JOINER(TAG(ARRAY(2), mode: :normal)) } specify { parse('{{ [ 2, 3 ] }}').should be_equal_node_to JOINER(TAG(ARRAY(2, 3), mode: :normal)) } specify { parse('{{ [2, 3][42] }}').should be_equal_node_to JOINER(TAG(METHOD('[]', ARRAY(2, 3), 42), mode: :normal)) }