Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 93 lines (71 sloc) 2.25 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
require 'shotgun-tests/helper'

unless File.exists?("code-cache")
  Dir.mkdir "code-cache"
end

class TestSexp < Test::Unit::TestCase

  include RubiniusHelper
  
  def to_sexp(what)
    code = "'#{what}'.to_sexp('test',1,false)"
    out = run_code("p #{code}\n", 2).strip
    return out
  end
  
  def test_number
    assert_equal '[:lit, 834234]', to_sexp("834234")
  end
  
  def test_regex
    assert_equal '[:regex, "blah"]', to_sexp("/blah/")
  end
  
  def test_string
    assert_equal '[:str, "hello"]', to_sexp("\"hello\"")
  end
  
  def test_lvar
    exc = '[:block, [:lasgn, :a, 0, [:lit, 1]], [:lvar, :a, 0]]'

    assert_equal exc, to_sexp("a = 1; a")
  end
  
  def test_ivar
    assert_equal '[:ivar, :@blah]', to_sexp("@blah")
  end
  
  def test_iasgn
    assert_equal '[:iasgn, :@blah, [:lit, 1]]', to_sexp("@blah = 1")
  end
  
  def test_gvar
    assert_equal '[:gvar, :$blah]', to_sexp("$blah")
  end
  
  def test_gasgn
    assert_equal '[:gasgn, :$blah, [:lit, 1]]', to_sexp("$blah = 1")
  end
  
  def test_symbol
    assert_equal '[:lit, :blah]', to_sexp(":blah")
  end
  
  def test_expand
    exc = '[:block, [:lasgn, :a, 0, [:lit, 1]], [:dstr, "hello ", [:evstr, [:lvar, :a, 0]], [:str, ", you rock."]]]'

    assert_equal exc, to_sexp('a = 1; "hello #{a}, you rock."')
  end

  def test_pathological_expand
    exc = '[:block, [:iasgn, :@thing, [:lit, 5]], [:dstr, "hello ", [:evstr, [:ivar, :@thing]], [:str, ", you are crazy."]]]'
    assert_equal exc, to_sexp('@thing = 5; "hello #@thing, you are crazy."')
  end
  
  def test_def
    input = "def name; 1; end"
    exc = '[:defn, :name, [:scope, [:block, [:args], [:lit, 1]], []]]'
    assert_equal exc, to_sexp(input)
  end
  
  def test_def_args
    input = "def name(a, b); 1; end"
    exc = '[:defn, :name, [:scope, [:block, [:args, [:a, :b], [], nil, nil], [:lit, 1]], [:a, :b]]]'
    assert_equal exc, to_sexp(input)
  end
  
  def test_class
    input = "class Blah < A::B; end"
    exc = '[:class, [:colon2, :Blah], [:colon2, [:const, :A], :B], [:scope, []]]'
    assert_equal exc, to_sexp(input)
  end
  
  def test_heredoc
    input = 'a = <<BLAH
hello
BLAH
'
    exc = %Q{[:lasgn, :a, 0, [:str, "hello\n"]]}
    assert_equal exc, to_sexp(input)
  end
end
Something went wrong with that request. Please try again.