5 def translate(from, to)
6 from = File.expand_path(from)
7 to = File.expand_path(to)
8 if File.directory?(from)
9 translate_dir(from, to)
10 elsif(from =~ /\.rb$/)
11 translate_file(from, to)
15 def translate_dir(from, to)
16 FileUtils.mkdir_p(to) unless File.directory?(to)
17 Dir["#{from}/*"].each do |sub_from|
18 path = sub_from[from.length+1..-1]
19 sub_to = File.join(to, path)
20 translate(sub_from, sub_to)
24 def translate_file(from, to)
26 File.open(from) do |io|
27 io.each_line do |line|
28 translation << translate_line(line)
31 File.open(to, "w") do |io|
36 def translate_line(line)
37 # Translate deprecated mock constraints
38 line.gsub!(/:any_args/, 'any_args')
39 line.gsub!(/:anything/, 'anything')
40 line.gsub!(/:boolean/, 'boolean')
41 line.gsub!(/:no_args/, 'no_args')
42 line.gsub!(/:numeric/, 'an_instance_of(Numeric)')
43 line.gsub!(/:string/, 'an_instance_of(String)')
45 return line if line =~ /(should_not|should)_receive/
47 line.gsub!(/(^\s*)context([\s*|\(]['|"|A-Z])/, '\1describe\2')
48 line.gsub!(/(^\s*)specify([\s*|\(]['|"|A-Z])/, '\1it\2')
49 line.gsub!(/(^\s*)context_setup(\s*[do|\{])/, '\1before(:all)\2')
50 line.gsub!(/(^\s*)context_teardown(\s*[do|\{])/, '\1after(:all)\2')
51 line.gsub!(/(^\s*)setup(\s*[do|\{])/, '\1before(:each)\2')
52 line.gsub!(/(^\s*)teardown(\s*[do|\{])/, '\1after(:each)\2')
54 if line =~ /(.*\.)(should_not|should)(?:_be)(?!_)(.*)/m
58 be_or_equal = post =~ /(<|>)/ ? "be" : "equal"
60 return "#{pre}#{should} #{be_or_equal}#{post}"
63 if line =~ /(.*\.)(should_not|should)_(?!not)\s*(.*)/m
68 post.gsub!(/^raise/, 'raise_error')
69 post.gsub!(/^throw/, 'throw_symbol')
71 unless standard_matcher?(post)
76 post.gsub!(/^(\w+)\s+([\w|\.|\,|\(.*\)|\'|\"|\:|@| ]+)(\})/, '\1(\2)\3') # inside a block
77 post.gsub!(/^(redirect_to)\s+(.*)/, '\1(\2)') # redirect_to, which often has http:
78 post.gsub!(/^(\w+)\s+([\w|\.|\,|\(.*\)|\{.*\}|\'|\"|\:|@| ]+)/, '\1(\2)')
79 post.gsub!(/(\s+\))/, ')')
80 post.gsub!(/\)\}/, ') }')
81 post.gsub!(/^(\w+)\s+(\/.*\/)/, '\1(\2)') #regexps
82 line = "#{pre}#{should} #{post}"
88 def standard_matcher?(matcher)
104 # Extra ones that we use in spec_helper
109 matched = patterns.detect{ |p| matcher =~ p }