Class: Spec::Translator

Inherits:
Object show all
Defined in:
lib/spec/translator.rb

Instance Method Summary collapse

Instance Method Details

#standard_matcher?(matcher) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/spec/translator.rb', line 62

def standard_matcher?(matcher)
  patterns = [
    /^be/, 
    /^be_close/,
    /^eql/, 
    /^equal/, 
    /^has/, 
    /^have/, 
    /^change/, 
    /^include/,
    /^match/, 
    /^raise_error/, 
    /^respond_to/, 
    /^satisfy/, 
    /^throw_symbol/,
    # Extra ones that we use in spec_helper
    /^pass/,
    /^fail/,
    /^fail_with/,
  ]
  matched = patterns.detect{ |p| matcher =~ p }
  !matched.nil?
end

#translate(line) ⇒ Object



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
# File 'lib/spec/translator.rb', line 32

def translate(line)
  return line if line =~ /(should_not|should)_receive/
  
  if line =~ /(.*\.)(should_not|should)(?:_be)(?!_)(.*)/m
    pre = $1
    should = $2
    post = $3
    be_or_equal = post =~ /(<|>)/ ? "be" : "equal"
    
    return "#{pre}#{should} #{be_or_equal}#{post}"
  end
  
  if line =~ /(.*\.)(should_not|should)_(?!not)(.*)/m
    pre = $1
    should = $2
    post = $3
    
    post.gsub!(/^raise/, 'raise_error')
    post.gsub!(/^throw/, 'throw_symbol')
    
    unless standard_matcher?(post)
      post = "be_#{post}"
    end
    
    line = "#{pre}#{should} #{post}"
  end

  line
end

#translate_dir(from, to) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/spec/translator.rb', line 5

def translate_dir(from, to)
  from = File.expand_path(from)
  to = File.expand_path(to)
  if File.directory?(from)
    FileUtils.mkdir_p(to) unless File.directory?(to)
    Dir["#{from}/*"].each do |sub_from|
      path = sub_from[from.length+1..-1]
      sub_to = File.join(to, path)
      translate_dir(sub_from, sub_to)
    end
  else
    translate_file(from, to)
  end
end

#translate_file(from, to) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/spec/translator.rb', line 20

def translate_file(from, to)
  translation = ""
  File.open(from) do |io|
    io.each_line do |line|
      translation << translate(line)
    end
  end
  File.open(to, "w") do |io|
    io.write(translation)
  end
end