Module: Artifact::Matcher::Helper

Included in:
RSpec::RailsApp::Artifact::Matchers::HaveArtifact, RSpec::RailsApp::Artifact::Matchers::HaveArtifacts
Defined in:
lib/rails_app_spec/matchers/artifact/shared/artifact_helper.rb

Constant Summary collapse

SUPERCLASS_MAP =
{
:observer   => 'ActiveRecord::Observer', 
:mailer     => 'ActionMailer::Base',
:migration  => 'ActiveRecord::Migration',
:permit     => 'Permit::Base'
}
POSTFIX =
[:helper, :observer, :controller, :mailer, :permit]
SPACES =
'\s+'
OPT_SPACES =
'\s*'
ANY_GROUP =
'(.*)'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



14
15
16
# File 'lib/rails_app_spec/matchers/artifact/shared/artifact_helper.rb', line 14

def action
  @action
end

#artifact_foundObject (readonly)

Returns the value of attribute artifact_found.



15
16
17
# File 'lib/rails_app_spec/matchers/artifact/shared/artifact_helper.rb', line 15

def artifact_found
  @artifact_found
end

#artifact_nameObject

Returns the value of attribute artifact_name.



8
9
10
# File 'lib/rails_app_spec/matchers/artifact/shared/artifact_helper.rb', line 8

def artifact_name
  @artifact_name
end

#artifact_typeObject

Returns the value of attribute artifact_type.



8
9
10
# File 'lib/rails_app_spec/matchers/artifact/shared/artifact_helper.rb', line 8

def artifact_type
  @artifact_type
end

#class_typeObject

Returns the value of attribute class_type.



8
9
10
# File 'lib/rails_app_spec/matchers/artifact/shared/artifact_helper.rb', line 8

def class_type
  @class_type
end

#contentObject

Returns the value of attribute content.



8
9
10
# File 'lib/rails_app_spec/matchers/artifact/shared/artifact_helper.rb', line 8

def content
  @content
end

#folderObject (readonly)

Returns the value of attribute folder.



14
15
16
# File 'lib/rails_app_spec/matchers/artifact/shared/artifact_helper.rb', line 14

def folder
  @folder
end

#nameObject

class



10
11
12
# File 'lib/rails_app_spec/matchers/artifact/shared/artifact_helper.rb', line 10

def name
  @name
end

#postfixObject

class



10
11
12
# File 'lib/rails_app_spec/matchers/artifact/shared/artifact_helper.rb', line 10

def postfix
  @postfix
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



15
16
17
# File 'lib/rails_app_spec/matchers/artifact/shared/artifact_helper.rb', line 15

def root_path
  @root_path
end

#superclassObject

subclass



12
13
14
# File 'lib/rails_app_spec/matchers/artifact/shared/artifact_helper.rb', line 12

def superclass
  @superclass
end

#typeObject

class



10
11
12
# File 'lib/rails_app_spec/matchers/artifact/shared/artifact_helper.rb', line 10

def type
  @type
end

#view_extObject (readonly)

Returns the value of attribute view_ext.



14
15
16
# File 'lib/rails_app_spec/matchers/artifact/shared/artifact_helper.rb', line 14

def view_ext
  @view_ext
end

Instance Method Details

#alt_endObject



107
108
109
# File 'lib/rails_app_spec/matchers/artifact/shared/artifact_helper.rb', line 107

def alt_end
  'class'
end

#failure_messageObject



122
123
124
125
126
# File 'lib/rails_app_spec/matchers/artifact/shared/artifact_helper.rb', line 122

def failure_message        
  return "Expected the #{artifact_type} #{artifact_name} to exist at #{artifact_found} (root = #{@root_path}), but it didn't.\nError: #{@error_msg}.\n\nTrace:\n #{@trace}" if !@file_found
  puts "Content: #{content}"
  "Expected the #{artifact_name} file at: #{artifact_found} to have a #{artifact_type} class. The class should #{should_be_msg}. RegExp: #{main_expr}"        
end

#find_artifactObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rails_app_spec/matchers/artifact/shared/artifact_helper.rb', line 60

def find_artifact
  artifact_found = case artifact_type
  when :view                                           
    find_view_method = "#{artifact_type}_file_name"
    File.expand_path(send find_view_method, folder, action, view_ext, :root_path => root_path)          
  else                                                     
    find_existing_artifact_method = "existing_#{artifact_type}_file"
    if respond_to? find_existing_artifact_method
      begin
        send find_existing_artifact_method, artifact_name, artifact_type, :root_path => root_path 
      rescue Exception => e 
        @error_msg = e.message
        @trace = e.backtrace.join "\n"
        nil
      end
    else
      raise "The method ##{find_existing_artifact_method} to find the artifact was not available"
    end
  end
  if !artifact_found
    @artifact_found = '[unknown]'
    return nil
  end
  
  @file_found = File.file?(artifact_found)
  return nil if !@file_found

  File.expand_path(artifact_found)                
end

#has_postfix?(key) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/rails_app_spec/matchers/artifact/shared/artifact_helper.rb', line 26

def has_postfix? key
  POSTFIX.include? key
end

#main_exprObject

TODO: Refactor, make DRY



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rails_app_spec/matchers/artifact/shared/artifact_helper.rb', line 95

def main_expr
  # determine which regexp to use: class or subclass
  case class_type
  when :subclass
    'class' + SPACES + "#{name}#{postfix}" + OPT_SPACES + '<' + OPT_SPACES + "#{superclass}" + ANY_GROUP      
  when :class
    "#{type}" + SPACES + "#{name}#{postfix}" + SPACES + ANY_GROUP
  else 
    raise "Class type must be either :class or :subclass, was #{class_type}" 
  end
end

#negative_failure_messageObject



128
129
130
131
132
# File 'lib/rails_app_spec/matchers/artifact/shared/artifact_helper.rb', line 128

def negative_failure_message
  return "Did not expect the #{artifact_type} #{artifact_name} to exist at #{artifact_found}, but it did" if !@file_found
  puts "Content: #{content}"        
  "Did not expected #{artifact_name} at file: #{artifact_found} to have a #{artifact_type} class. The class should not #{should_be_msg}. RegExp: #{main_expr}"                
end

#parse_name(name) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rails_app_spec/matchers/artifact/shared/artifact_helper.rb', line 47

def parse_name name
  if name.kind_of? Hash                  
    view_options  = name
    @folder   = view_options[:folder]
    @action   = view_options[:action] 
    @view_ext = view_options[:view_ext] 
    @artifact_type = :view
    return nil
  end
  @artifact_name = name.to_s.downcase
  @name = name.to_s.camelize
end

#parse_type(artifact_type) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rails_app_spec/matchers/artifact/shared/artifact_helper.rb', line 30

def parse_type artifact_type
  @artifact_type = artifact_type      
  @postfix = artifact_type.to_s.camelize if has_postfix? artifact_type  
  @type = :class
  case artifact_type       
  when :helper, :controller
    # artifact class check 
    @class_type = :class                                  
  when :observer, :migration, :mailer, :permit
    @class_type = :subclass
    # artifact subclass check
    @superclass = SUPERCLASS_MAP[artifact_type]        
  when :model
    @class_type = :class        
  end
end

#should_be_msgObject



111
112
113
114
115
116
117
118
119
120
# File 'lib/rails_app_spec/matchers/artifact/shared/artifact_helper.rb', line 111

def should_be_msg
  case class_type
  when :subclass
    "have the name: #{name}#{postfix} and be a subclass of: #{superclass}"
  when :class
    "have the name: #{name}#{postfix}"  
  else           
    raise "Class type must be either :class or :subclass, was #{class_type}" if artifact_type != :view
  end
end