~ubuntu-branches/ubuntu/oneiric/puppet/oneiric-security

« back to all changes in this revision

Viewing changes to vendor/gems/rspec/lib/spec/translator.rb

  • Committer: Bazaar Package Importer
  • Author(s): Micah Anderson
  • Date: 2008-07-26 15:43:45 UTC
  • mfrom: (1.1.8 upstream) (3.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080726154345-c03m49twzxewdwjn
Tags: 0.24.5-2
* Fix puppetlast to work with 0.24.5
* Adjust logcheck to match against new log messages in 0.24.5
* Update standards version to 3.8.0 (no changes)
* Update changelog to reduce length of line to make lintian happy

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'fileutils'
 
2
 
 
3
module Spec
 
4
  class Translator
 
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
 
14
    
 
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
 
23
 
 
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
 
35
 
 
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)')
 
44
 
 
45
      return line if line =~ /(should_not|should)_receive/
 
46
      
 
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')
 
53
      
 
54
      if line =~ /(.*\.)(should_not|should)(?:_be)(?!_)(.*)/m
 
55
        pre = $1
 
56
        should = $2
 
57
        post = $3
 
58
        be_or_equal = post =~ /(<|>)/ ? "be" : "equal"
 
59
        
 
60
        return "#{pre}#{should} #{be_or_equal}#{post}"
 
61
      end
 
62
      
 
63
      if line =~ /(.*\.)(should_not|should)_(?!not)\s*(.*)/m
 
64
        pre = $1
 
65
        should = $2
 
66
        post = $3
 
67
        
 
68
        post.gsub!(/^raise/, 'raise_error')
 
69
        post.gsub!(/^throw/, 'throw_symbol')
 
70
        
 
71
        unless standard_matcher?(post)
 
72
          post = "be_#{post}"
 
73
        end
 
74
        
 
75
        # Add parenthesis
 
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}"
 
83
      end
 
84
 
 
85
      line
 
86
    end
 
87
    
 
88
    def standard_matcher?(matcher)
 
89
      patterns = [
 
90
        /^be/, 
 
91
        /^be_close/,
 
92
        /^eql/, 
 
93
        /^equal/, 
 
94
        /^has/, 
 
95
        /^have/, 
 
96
        /^change/, 
 
97
        /^include/,
 
98
        /^match/, 
 
99
        /^raise_error/, 
 
100
        /^respond_to/, 
 
101
        /^redirect_to/, 
 
102
        /^satisfy/, 
 
103
        /^throw_symbol/,
 
104
        # Extra ones that we use in spec_helper
 
105
        /^pass/,
 
106
        /^fail/,
 
107
        /^fail_with/,
 
108
      ]
 
109
      matched = patterns.detect{ |p| matcher =~ p }
 
110
      !matched.nil?
 
111
    end
 
112
    
 
113
  end
 
114
end