Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 373
- Log:
Initial import of Radiant 0.9.1, which is now packaged as a gem. This is an
import of the tagged 0.9.1 source checked out from GitHub, which isn't quite
the same as the gem distribution - but it doesn't seem to be available in an
archived form and the installed gem already has modifications, so this is
the closest I can get.
- Author:
- rool
- Date:
- Mon Mar 21 13:40:05 +0000 2011
- Size:
- 8787 Bytes
1 | module Spec |
2 | module Matchers |
3 | module GeneratorMatchers |
4 | class DirectoryGenerated |
5 | def initialize(dir) @dir = dir end |
6 | def failure_message() "expected directory '#{@path}' should exist but doesn't" end |
7 | def negative_failure_message() "expected no directory, but directory '#{@path}' was found" end |
8 | |
9 | def matches?(base) |
10 | @base = base |
11 | @path = File.join(RADIANT_ROOT, @base, @dir) |
12 | File.exist?(@path) && File.directory?(@path) |
13 | end |
14 | end |
15 | |
16 | class FileGenerated |
17 | def initialize(file) @file = file end |
18 | def failure_message() "expected file '#{@path}' should exist but doesn't" end |
19 | def negative_failure_message() "expected no file, but file '#{@path}' was found" end |
20 | |
21 | def matches?(base) |
22 | @base = base |
23 | @path = File.join(RADIANT_ROOT, @base, @file) |
24 | if (exists = File.exist?(@path)) && block_given? |
25 | File.open(@path) { |f| yield(f.read) } |
26 | end |
27 | return exists |
28 | end |
29 | end |
30 | |
31 | class YamlGenerated |
32 | def initialize(path) @path = path end |
33 | def failure_message() "the file '#{@path}.yml' should be a YAML file" end |
34 | def negative_failure_message() "expected no file, but file '#{@path}.yml' was found" end |
35 | |
36 | def matches?(base) |
37 | FileGenerated.new("#{@path}.yml").matches?(base) do |body| |
38 | yield(YAML.load(body.to_s)) if block_given? |
39 | return true |
40 | end |
41 | false |
42 | end |
43 | end |
44 | |
45 | class ClassGenerated |
46 | def initialize(path, parent=nil) @path = path; @parent = parent; end |
47 | def failure_message() "the file '#{@path}.rb' should be a class" end |
48 | def negative_failure_message() "expected no file, but file '#{@path}.rb' was found" end |
49 | |
50 | def matches?(base) |
51 | if @path.split('/').size > 3 |
52 | @path =~ /\/?(\d+_)?(\w+)\/(\w+)$/ |
53 | class_name = "#{$2.camelize}::#{$3.camelize}" |
54 | else |
55 | @path =~ /\/?(\d+_)?(\w+)$/ |
56 | class_name = $2.camelize |
57 | end |
58 | |
59 | FileGenerated.new("#{@path}.rb").matches?(base) do |body| |
60 | match_data = body.match(/class #{class_name}#{@parent.nil? ? '':" < #{@parent}"}\n((\n|\s*.*\n)*)end/) |
61 | yield(match_data[1]) if block_given? && !!match_data |
62 | return !!match_data |
63 | end |
64 | false |
65 | end |
66 | end |
67 | |
68 | class ModuleGenerated |
69 | def initialize(path, parent=nil) @path = path; @parent = parent; end |
70 | def failure_message() "the file '#{@path}.rb' should be a module" end |
71 | def negative_failure_message() "expected no file, but file '#{@path}.rb' was found" end |
72 | |
73 | def matches?(base) |
74 | if @path.split('/').size > 3 |
75 | @path =~ /\/?(\d+_)?(\w+)\/(\w+)$/ |
76 | module_name = "#{$2.camelize}::#{$3.camelize}" |
77 | else |
78 | @path =~ /\/?(\d+_)?(\w+)$/ |
79 | module_name = $2.camelize |
80 | end |
81 | |
82 | FileGenerated.new("#{@path}.rb").matches?(base) do |body| |
83 | match_data = body.match(/module #{module_name}#{@parent.nil? ? '':" < #{@parent}"}\n((\n|\s*.*\n)*)end/) |
84 | yield(match_data[1]) if block_given? && !!match_data |
85 | return !!match_data |
86 | end |
87 | false |
88 | end |
89 | end |
90 | |
91 | class SpecGenerated |
92 | def initialize(path, class_name=true) @path = path; @class_name = class_name; end |
93 | def failure_message() "the file '#{@path}.rb' should be a spec" end |
94 | def negative_failure_message() "expected no file, but file '#{@path}.rb' was found" end |
95 | |
96 | def matches?(base) |
97 | unless @class_name == false |
98 | if @path.split('/').size > 3 |
99 | @path =~ /\/?(\d+_)?(\w+)\/(\w+)$/ |
100 | @class_name = "#{$2.camelize}::#{$3.camelize}" |
101 | else |
102 | @path =~ /\/?(\d+_)?(\w+)$/ |
103 | @class_name = $2.camelize |
104 | end |
105 | end |
106 | |
107 | FileGenerated.new("#{@path}_spec.rb").matches?(base) do |body| |
108 | if @class_name |
109 | match_data = body.match(/describe #{@class_name} do\n((\s*.*\n)+)\s*end/) |
110 | yield(match_data[1]) if block_given? && !!match_data |
111 | return !!match_data |
112 | else |
113 | yield(body) if block_given? |
114 | return true |
115 | end |
116 | end |
117 | false |
118 | end |
119 | end |
120 | |
121 | class MigrationGenerated |
122 | def initialize(name, parent="ActiveRecord::Migration") @name = name; @parent = parent; end |
123 | def failure_message() "the file '#{@path}' should be a spec" end |
124 | def negative_failure_message() "expected no file, but file '#{@path}.rb' was found" end |
125 | |
126 | def matches?(base) |
127 | root_path = File.expand_path(File.join(RADIANT_ROOT, base)) |
128 | @path = Dir.glob("#{root_path}/db/migrate/*_#{@name.to_s.underscore}.rb").first |
129 | return false if @path.nil? |
130 | @path = @path.match(/db\/migrate\/[0-9]+_\w+/).to_s |
131 | |
132 | ClassGenerated.new(@path, @parent).matches?(base) do |body| |
133 | yield(body) if block_given? |
134 | return true |
135 | end |
136 | false |
137 | end |
138 | end |
139 | |
140 | class MethodMatcher |
141 | def initialize(name) @name = name end |
142 | def failure_message() "the string should contain a method definition for #{@name}" end |
143 | def negative_failure_message() "expected no method definition for #{@name}, but found it" end |
144 | |
145 | def matches?(actual) |
146 | match_data = actual.match(/^\s*def #{@name}(\(.+\))?\n((\n|\s+.*\n)*)\s*end/) |
147 | if !!match_data && block_given? |
148 | yield(match_data[2]) |
149 | end |
150 | return !!match_data |
151 | end |
152 | end |
153 | |
154 | def have_generated_directory(dir) |
155 | DirectoryGenerated.new(dir) |
156 | end |
157 | |
158 | def have_generated_file(file) |
159 | FileGenerated.new(file) |
160 | end |
161 | |
162 | def have_generated_class(path, parent = nil) |
163 | ClassGenerated.new(path, parent) |
164 | end |
165 | |
166 | def have_generated_module(path, parent = nil) |
167 | ModuleGenerated.new(path, parent) |
168 | end |
169 | |
170 | def have_generated_spec(path, class_name = true) |
171 | SpecGenerated.new(path, class_name) |
172 | end |
173 | |
174 | def have_generated_controller_for(name, parent = "ApplicationController") |
175 | ClassGenerated.new("app/controllers/#{name.to_s.underscore}_controller", parent) |
176 | end |
177 | |
178 | def have_generated_model_for(name, parent = "ActiveRecord::Base") |
179 | ClassGenerated.new("app/models/#{name.to_s.underscore}", parent) |
180 | end |
181 | |
182 | def have_generated_model_spec_for(name) |
183 | SpecGenerated.new("spec/models/#{name.to_s.underscore}") |
184 | end |
185 | |
186 | def have_generated_controller_spec_for(name) |
187 | SpecGenerated.new("spec/controllers/#{name.to_s.underscore}_controller") |
188 | end |
189 | |
190 | def have_generated_helper_spec_for(name) |
191 | SpecGenerated.new("spec/helpers/#{name.to_s.underscore}_helper") |
192 | end |
193 | |
194 | def have_generated_view_spec_for(controller, action) |
195 | SpecGenerated.new("spec/views/#{controller.to_s.underscore}/#{action.to_s.underscore}_view", false) |
196 | end |
197 | |
198 | def have_generated_helper_for(name) |
199 | ModuleGenerated.new("app/helpers/#{name.to_s.underscore}_helper") |
200 | end |
201 | |
202 | def have_generated_functional_test_for(name, parent = "ActionController::TestCase") |
203 | ClassGenerated.new("test/functional/#{name.to_s.underscore}_controller_test", parent) |
204 | end |
205 | |
206 | def have_generated_unit_test_for(name, parent = "ActiveSupport::TestCase") |
207 | ClassGenerated.new("test/unit/#{name.to_s.underscore}_test", parent) |
208 | end |
209 | |
210 | def have_generated_migration(name, parent = "ActiveRecord::Migration") |
211 | MigrationGenerated.new(name, parent) |
212 | end |
213 | |
214 | def have_generated_yaml(path) |
215 | YamlGenerated.new(path) |
216 | end |
217 | |
218 | def have_generated_fixtures_for(name) |
219 | YamlGenerated.new("test/fixtures/#{name.to_s.underscore}") |
220 | end |
221 | |
222 | def have_generated_view_for(name, action, suffix = "html.erb") |
223 | FileGenerated.new("app/views/#{name.to_s.underscore}/#{action}.#{suffix}") |
224 | end |
225 | |
226 | def have_method(name) |
227 | MethodMatcher.new(name) |
228 | end |
229 | |
230 | def have_generated_column(name, type) |
231 | simple_matcher("migration defines column") { |given| given =~ /t\.#{type.to_s} :#{name.to_s}/ } |
232 | end |
233 | end |
234 | end |
235 | end |