| Class | Spec::Translator |
| In: |
lib/spec/translator.rb
|
| Parent: | Object |
# File lib/spec/translator.rb, line 80
80: def standard_matcher?(matcher)
81: patterns = [
82: /^be/,
83: /^be_close/,
84: /^eql/,
85: /^equal/,
86: /^has/,
87: /^have/,
88: /^change/,
89: /^include/,
90: /^match/,
91: /^raise_error/,
92: /^respond_to/,
93: /^redirect_to/,
94: /^satisfy/,
95: /^throw_symbol/,
96: # Extra ones that we use in spec_helper
97: /^pass/,
98: /^fail/,
99: /^fail_with/,
100: ]
101: matched = patterns.detect{ |p| matcher =~ p }
102: !matched.nil?
103: end
# File lib/spec/translator.rb, line 5
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)
12: end
13: end
# File lib/spec/translator.rb, line 15
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)
21: end
22: end
# File lib/spec/translator.rb, line 24
24: def translate_file(from, to)
25: translation = ""
26: File.open(from) do |io|
27: io.each_line do |line|
28: translation << translate_line(line)
29: end
30: end
31: File.open(to, "w") do |io|
32: io.write(translation)
33: end
34: end
# File lib/spec/translator.rb, line 36
36: def translate_line(line)
37: return line if line =~ /(should_not|should)_receive/
38:
39: line.gsub!(/(^\s*)context([\s*|\(]['|"|A-Z])/, '\1describe\2')
40: line.gsub!(/(^\s*)specify([\s*|\(]['|"|A-Z])/, '\1it\2')
41: line.gsub!(/(^\s*)context_setup(\s*[do|\{])/, '\1before(:all)\2')
42: line.gsub!(/(^\s*)context_teardown(\s*[do|\{])/, '\1after(:all)\2')
43: line.gsub!(/(^\s*)setup(\s*[do|\{])/, '\1before(:each)\2')
44: line.gsub!(/(^\s*)teardown(\s*[do|\{])/, '\1after(:each)\2')
45:
46: if line =~ /(.*\.)(should_not|should)(?:_be)(?!_)(.*)/m
47: pre = $1
48: should = $2
49: post = $3
50: be_or_equal = post =~ /(<|>)/ ? "be" : "equal"
51:
52: return "#{pre}#{should} #{be_or_equal}#{post}"
53: end
54:
55: if line =~ /(.*\.)(should_not|should)_(?!not)\s*(.*)/m
56: pre = $1
57: should = $2
58: post = $3
59:
60: post.gsub!(/^raise/, 'raise_error')
61: post.gsub!(/^throw/, 'throw_symbol')
62:
63: unless standard_matcher?(post)
64: post = "be_#{post}"
65: end
66:
67: # Add parenthesis
68: post.gsub!(/^(\w+)\s+([\w|\.|\,|\(.*\)|\'|\"|\:|@| ]+)(\})/, '\1(\2)\3') # inside a block
69: post.gsub!(/^(redirect_to)\s+(.*)/, '\1(\2)') # redirect_to, which often has http:
70: post.gsub!(/^(\w+)\s+([\w|\.|\,|\(.*\)|\{.*\}|\'|\"|\:|@| ]+)/, '\1(\2)')
71: post.gsub!(/(\s+\))/, ')')
72: post.gsub!(/\)\}/, ') }')
73: post.gsub!(/^(\w+)\s+(\/.*\/)/, '\1(\2)') #regexps
74: line = "#{pre}#{should} #{post}"
75: end
76:
77: line
78: end