Top Level Namespace

Defined Under Namespace

Modules: Ikra, Parsing, Sourcify Classes: Array, Binding, FalseClass, Fixnum, Float, Integer, NilClass, Object, STOP_DUMPING_OBJECT_SPACE_PROCS, Symbol, TrueClass

Constant Summary collapse

DUMPED_OBJECT_SPACE_PROCS =
{:max => nil, :done => 0}
DUMP_OBJECT_SPACE_PROCS_ERRORS =
[]

Instance Method Summary collapse

Instance Method Details

#capture(stdin_str = '') ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/sourcify/spec/spec_helper.rb', line 87

def capture(stdin_str = '')
  begin
    require 'stringio'
    $o_stdin, $o_stdout, $o_stderr = $stdin, $stdout, $stderr
    $stdin, $stdout, $stderr = StringIO.new(stdin_str), StringIO.new, StringIO.new
    yield
    {:stdout => $stdout.string, :stderr => $stderr.string}
  ensure
    $stdin, $stdout, $stderr = $o_stdin, $o_stdout, $o_stderr
  end
end

#code_to_sexp(code) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/sourcify/spec/spec_helper.rb', line 47

def code_to_sexp(code)
  if has_parsetree?
    require 'parse_tree'
    Unifier.new.process(ParseTree.translate(code))
  else
    require 'ruby_parser'
    RubyParser.new.parse(code)
  end
end

#dump_object_space_procs(debug = false) ⇒ Object



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
# File 'lib/sourcify/spec/dump_object_space_procs.rb', line 15

def dump_object_space_procs(debug = false)
  # Determine working dir
  name = [
    RUBY_DESCRIPTION =~ /enterprise/i ? 'ree' : (RUBY_PLATFORM =~ /java/i ? 'jruby' : 'mri'),
    RUBY_VERSION,
    Object.const_defined?(:ParseTree) ? 'parsetree' : nil
  ].compact.join('~')
  dump_dir = File.join(File.dirname(File.expand_path(__FILE__)), '..', 'tmp', name)
  Dir.mkdir(dump_dir) unless File.exists?(dump_dir)

  puts '',
    '== NOTE: dump files can be found at %s' % dump_dir

  # Core processing
  ObjectSpace.each_object(Proc).to_a.select{|o| o.source_location }.
    group_by{|o| o.source_location[0] }.each do |ofile, objs|
      nfile = File.join(dump_dir, ofile.gsub('/','~'))
      File.open(nfile,'w') do |f|
        objs.sort_by{|o| o.source_location[1] }.map do |o|
          DUMPED_OBJECT_SPACE_PROCS[:done] += 1
          begin
            data = {
              :location => o.source_location,
              :sexp => o.to_sexp,
              :source => o.to_source
            }
            f.puts(data.pretty_inspect)
            print '.'
          rescue Exception
            data = {
              :location => o.source_location,
              :error => $!.inspect
            }
            DUMP_OBJECT_SPACE_PROCS_ERRORS << data
            f.puts(data.pretty_inspect)
            pp(data) if debug
            print 'x'
          ensure
            raise STOP_DUMPING_OBJECT_SPACE_PROCS \
              if DUMPED_OBJECT_SPACE_PROCS.values.uniq.size == 1
          end
        end
      end
    end
end

#has_parsetree?Boolean

/////////////////////////////////////////////////////////// Spec helpers & matchers ///////////////////////////////////////////////////////////

Returns:

  • (Boolean)


39
40
41
# File 'lib/sourcify/spec/spec_helper.rb', line 39

def has_parsetree?
  Object.const_defined?(:ParseTree)
end

#having_raw_source(expected, opts = {}, &matcher) ⇒ Object



69
70
71
72
73
74
# File 'lib/sourcify/spec/spec_helper.rb', line 69

def having_raw_source(expected, opts={}, &matcher)
  lambda do |thing|
    found = block_given? ? thing.to_raw_source(&matcher) : thing.to_raw_source(opts)
    found == expected.strip
  end
end

#having_sexp(expected, opts = {}, &matcher) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/sourcify/spec/spec_helper.rb', line 76

def having_sexp(expected, opts={}, &matcher)
  lambda do |thing|
    expected = eval(expected) if expected.is_a?(String)
    if block_given?
      thing.to_sexp(&matcher).inspect == expected.inspect
    else
      thing.to_sexp(opts).inspect == expected.inspect
    end
  end
end

#having_source(expected, opts = {}, &matcher) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/sourcify/spec/spec_helper.rb', line 61

def having_source(expected, opts={}, &matcher)
  lambda do |thing|
    #normalize_code(expected) # added for bug fixing
    normalize_code(block_given? ? thing.to_source(&matcher) : thing.to_source(opts)) \
      == normalize_code(expected)
  end
end

#irb_exec(stdin_str) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/sourcify/spec/spec_helper.rb', line 99

def irb_exec(stdin_str)
  # See http://tyenglog.heroku.com/2010/9/how-to-test-irb-specific-support &
  # http://tyenglog.heroku.com/2010/9/how-to-test-irb-specific-support-2-
  sourcify_rb = File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib', 'sourcify.rb')
  irb_feedback = /^ ?=> /
  irb_opts = '--simple-prompt'
  values = %x(echo "#{stdin_str}" | irb #{irb_opts} -r #{sourcify_rb}).split("\n").
    grep(irb_feedback).map{|s| eval(s.sub(irb_feedback,'').strip) }

  # IRB behaves slightly differently in 1.9.2 for appending newline
  (values[-1].nil? && RUBY_VERSION =~ /1.9.(2|3)/) ? values[0 .. -2] : values
end

#normalize_code(code) ⇒ Object



57
58
59
# File 'lib/sourcify/spec/spec_helper.rb', line 57

def normalize_code(code)
  Ruby2Ruby.new.process(code_to_sexp(code))
end

#pry_exec(stdin_str) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/sourcify/spec/spec_helper.rb', line 112

def pry_exec(stdin_str)
  sourcify_rb = File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib', 'sourcify.rb')
  pry_opts = '--simple-prompt --no-color'
  results = []

  %x(echo "#{stdin_str}" | pry #{pry_opts} -r #{sourcify_rb}).
    split("\n").each do |line|
      case line
      when /^((?:>> |)=> )/
        results << [line.sub($1,'')]
      when /^( )/
        results[-1] << line
      end
    end

  results = results.map{|lines| eval(lines.join) }
  results[-1].nil? ? results[0..-2] : results
end

#watever(*args, &block) ⇒ Object



43
44
45
# File 'lib/sourcify/spec/spec_helper.rb', line 43

def watever(*args, &block)
  Proc.new(&block)
end