Sha256: 760b4094682bc7928765415090ecd28d0b57bbf20baa0e66d43b6ac6c97b8ebf

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

require 'lib/spec_helper'

describe Opal::Rewriters::OpalEngineCheck do
  def s(type, *children)
    ::Opal::AST::Node.new(type, children)
  end

  let(:rewriter) { Opal::Rewriters::OpalEngineCheck.new }

  def expect_rewritten(node)
    processed = rewriter.process(node)
    expect(processed)
  end

  def expect_no_rewriting_for(node)
    expect_rewritten(node).to eq(node)
  end

  let(:opal_str_sexp) { s(:str, 'opal') }
  let(:true_branch) { s(:int, 1) }
  let(:false_branch) { s(:int, 2) }

  [:RUBY_ENGINE, :RUBY_PLATFORM].each do |const_name|
    let(:ruby_const_sexp) { s(:const, nil, const_name) }

    context "#{const_name} == rhs" do
      context "when rhs == 'opal'" do
        let(:check) do
          s(:send, ruby_const_sexp, :==, opal_str_sexp)
        end

        it 'replaces true branch with s(:nil)' do
          expect_rewritten(
            s(:if, check, true_branch, false_branch)
          ).to eq(
            s(:if, check, true_branch, s(:nil))
          )
        end
      end

      context "when rhs != 'opal'" do
        let(:check) do
          s(:send, ruby_const_sexp, :==, s(:nil))
        end

        it 'does not modify sexp' do
          expect_no_rewriting_for(
            s(:if, check, true_branch, false_branch)
          )
        end
      end
    end

    context "#{const_name} != rhs" do
      context "when rhs == 'opal'" do
        let(:check) do
          s(:send, ruby_const_sexp, :!=, opal_str_sexp)
        end

        it 'replaces true branch with s(:nil)' do
          expect_rewritten(
            s(:if, check, true_branch, false_branch)
          ).to eq(
            s(:if, check, s(:nil), false_branch)
          )
        end
      end

      context "when rhs != 'opal'" do
        let(:check) do
          s(:send, ruby_const_sexp, :!=, s(:nil))
        end

        it 'does not modify sexp' do
          expect_no_rewriting_for(
            s(:if, check, true_branch, false_branch)
          )
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
opal-0.11.0.rc1 spec/lib/rewriters/opal_engine_check_spec.rb