Sha256: 902edf500a4adc15d9a8d6980892ee1add24b152fb899019cf0c1fe7de24c62e

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

require 'spec_helper'
module Gisele::Language
  describe ElsifFlattener do

    def ast(source)
      Syntax.ast(source.strip, :root => :if_st)
    end

    def rewrite(ast)
      ElsifFlattener.new.call(ast)
    end

    it 'rewrites a single if correctly' do
      source = ast("if goodCond Task1 end")
      rewrite(source).should eq(source)
    end

    it 'rewrites a if/else correctly' do
      source = ast("if goodCond Task1 else Task2 end")
      rewrite(source).should eq(source)
    end

    it 'rewrites a if/elsif correctly' do
      source   = ast("if good Task1 elsif bad Task2 end")
      expected = ast("if good Task1 else if bad Task2 end end")
      rewrite(source).should eq(expected)
    end

    it 'rewrites a if/elsif/else correctly' do
      source   = ast("if good Task1 elsif bad Task2 else Task3 end")
      expected = ast("if good Task1 else if bad Task2 else Task3 end end")
      rewrite(source).should eq(expected)
    end

    it 'rewrites a if/elsif/elsif/else correctly' do
      source = ast(<<-EOF.strip)
        if good Task1
        elsif bad Task2
        elsif none Task3
        else Task4
        end
      EOF
      expected = ast(<<-EOF.strip)
        if good
          Task1
        else
          if bad
            Task2
          else
            if none
              Task3
            else
              Task4
            end
          end
        end
      EOF
      rewrite(source).should eq(expected)
    end

    it 'recurses by default' do
      source = ast(<<-EOF.strip)
        if goodCond
          Task1
        else
          if bad
            Task2
          elsif middle
            Task3
          end
        end
      EOF
      expected = ast(<<-EOF.strip)
        if goodCond
          Task1
        else
          if bad
            Task2
          else
            if middle
              Task3
            end
          end
        end
      EOF
      rewrite(source).should eq(expected)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gisele-0.3.0 spec/unit/language/test_elsif_flattener.rb