started Quick-E-Mart example.
[merb_mart.git] / examples / quick-E-mart / autotest / merb.rb
blobb5d8ccb70f3f0d4240a2fd0c40a9784795ec82e4
1 # Adapted from Autotest::Rails
2 require 'autotest'
4 class Autotest::Merb < Autotest
5   
6   # +model_tests_dir+::      the directory to find model-centric tests
7   # +controller_tests_dir+:: the directory to find controller-centric tests
8   # +view_tests_dir+::       the directory to find view-centric tests
9   # +fixtures_dir+::         the directory to find fixtures in
10   attr_accessor :model_tests_dir, :controller_tests_dir, :view_tests_dir, :fixtures_dir
11   
12   def initialize
13     super
14     
15     initialize_test_layout
16     
17     # Ignore any happenings in these directories
18     add_exception %r%^\./(?:doc|log|public|tmp)%
19     
20     # Ignore any mappings that Autotest may have already set up
21     clear_mappings
22     
23     # Any changes to a file in the root of the 'lib' directory will run any 
24     # model test with a corresponding name.
25     add_mapping %r%^lib\/.*\.rb% do |filename, _|
26       files_matching Regexp.new(["^#{model_test_for(filename)}$"])
27     end
28     
29     # Any changes to a fixture will run corresponding view, controller and 
30     # model tests
31     add_mapping %r%^#{fixtures_dir}/(.*)s.yml% do |_, m|
32       [
33         model_test_for(m[1]), 
34         controller_test_for(m[1]), 
35         view_test_for(m[1])
36       ]
37     end
38     
39     # Any change to a test or test will cause it to be run
40     add_mapping %r%^test/(unit|models|integration|controllers|views|functional)/.*rb$% do |filename, _|
41       filename
42     end
43     
44     # Any change to a model will cause it's corresponding test to be run
45     add_mapping %r%^app/models/(.*)\.rb$% do |_, m|
46       model_test_for(m[1])
47     end
48     
49     # Any change to the global helper will result in all view and controller 
50     # tests being run
51     add_mapping %r%^app/helpers/global_helpers.rb% do
52       files_matching %r%^test/(views|functional|controllers)/.*_test\.rb$%
53     end
54     
55     # Any change to a helper will run it's corresponding view and controller 
56     # tests, unless the helper is the global helper. Changes to the global 
57     # helper run all view and controller tests.
58     add_mapping %r%^app/helpers/(.*)_helper(s)?.rb% do |_, m|
59       if m[1] == "global" then
60         files_matching %r%^test/(views|functional|controllers)/.*_test\.rb$%
61       else
62         [
63           view_test_for(m[1]), 
64           controller_test_for(m[1])
65         ]
66       end
67     end
68     
69     # Changes to views result in their corresponding view and controller test 
70     # being run
71     add_mapping %r%^app/views/(.*)/% do |_, m|
72       [
73         view_test_for(m[1]), 
74         controller_test_for(m[1])
75       ]
76     end
77     
78     # Changes to a controller result in its corresponding test being run. If 
79     # the controller is the exception or application controller, all 
80     # controller tests are run.
81     add_mapping %r%^app/controllers/(.*)\.rb$% do |_, m|
82       if ["application", "exception"].include?(m[1])
83         files_matching %r%^test/(controllers|views|functional)/.*_test\.rb$%
84       else
85         controller_test_for(m[1])
86       end
87     end
89     # If a change is made to the router, run all controller and view tests
90     add_mapping %r%^config/router.rb$% do # FIX
91       files_matching %r%^test/(controllers|views|functional)/.*_test\.rb$%
92     end
94     # If any of the major files governing the environment are altered, run 
95     # everything
96     add_mapping %r%^test/test_helper.rb|config/(init|rack|environments/test.rb|database.yml)% do # FIX
97       files_matching %r%^test/(unit|models|controllers|views|functional)/.*_test\.rb$%
98     end
99   end
100   
101 private
103   # Determines the paths we can expect tests or specs to reside, as well as 
104   # corresponding fixtures.
105   def initialize_test_layout
106     self.model_tests_dir      = "test/unit"
107     self.controller_tests_dir = "test/functional"
108     self.view_tests_dir       = "test/views"
109     self.fixtures_dir         = "test/fixtures"
110   end
111   
112   # Given a filename and the test type, this method will return the 
113   # corresponding test's or spec's name.
114   # 
115   # ==== Arguments
116   # +filename+<String>:: the file name of the model, view, or controller
117   # +kind_of_test+<Symbol>:: the type of test we that we should run
118   # 
119   # ==== Returns
120   # String:: the name of the corresponding test or spec
121   # 
122   # ==== Example
123   # 
124   #   > test_for("user", :model)
125   #   => "user_test.rb"
126   #   > test_for("login", :controller)
127   #   => "login_controller_test.rb"
128   #   > test_for("form", :view)
129   #   => "form_view_spec.rb" # If you're running a RSpec-like suite
130   def test_for(filename, kind_of_test)
131     name  = [filename]
132     name << kind_of_test.to_s if kind_of_test == :view
133     name << "test"
134     return name.join("_") + ".rb"
135   end
136   
137   def model_test_for(filename)
138     [model_tests_dir, test_for(filename, :model)].join("/")
139   end
140   
141   def controller_test_for(filename)
142     [controller_tests_dir, test_for(filename, :controller)].join("/")
143   end
144   
145   def view_test_for(filename)
146     [view_tests_dir, test_for(filename, :view)].join("/")
147   end
148