1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
require 'spec_helper'
require 'mspec/commands/mkspec'
require 'fileutils'
RSpec.describe "The -c, --constant CONSTANT option" do
before :each do
@options = MSpecOptions.new
allow(MSpecOptions).to receive(:new).and_return(@options)
@script = MkSpec.new
@config = @script.config
end
it "is enabled by #options" do
allow(@options).to receive(:on)
expect(@options).to receive(:on).with("-c", "--constant", "CONSTANT",
an_instance_of(String))
@script.options []
end
it "adds CONSTANT to the list of constants" do
["-c", "--constant"].each do |opt|
@config[:constants] = []
@script.options [opt, "Object"]
expect(@config[:constants]).to include("Object")
end
end
end
RSpec.describe "The -b, --base DIR option" do
before :each do
@options = MSpecOptions.new
allow(MSpecOptions).to receive(:new).and_return(@options)
@script = MkSpec.new
@config = @script.config
end
it "is enabled by #options" do
allow(@options).to receive(:on)
expect(@options).to receive(:on).with("-b", "--base", "DIR",
an_instance_of(String))
@script.options []
end
it "sets the base directory relative to which the spec directories are created" do
["-b", "--base"].each do |opt|
@config[:base] = nil
@script.options [opt, "superspec"]
expect(@config[:base]).to eq(File.expand_path("superspec"))
end
end
end
RSpec.describe "The -r, --require LIBRARY option" do
before :each do
@options = MSpecOptions.new
allow(MSpecOptions).to receive(:new).and_return(@options)
@script = MkSpec.new
@config = @script.config
end
it "is enabled by #options" do
allow(@options).to receive(:on)
expect(@options).to receive(:on).with("-r", "--require", "LIBRARY",
an_instance_of(String))
@script.options []
end
it "adds CONSTANT to the list of constants" do
["-r", "--require"].each do |opt|
@config[:requires] = []
@script.options [opt, "libspec"]
expect(@config[:requires]).to include("libspec")
end
end
end
RSpec.describe "The -V, --version-guard VERSION option" do
before :each do
@options = MSpecOptions.new
allow(MSpecOptions).to receive(:new).and_return(@options)
@script = MkSpec.new
@config = @script.config
end
it "is enabled by #options" do
allow(@options).to receive(:on)
expect(@options).to receive(:on).with("-V", "--version-guard", "VERSION",
an_instance_of(String))
@script.options []
end
it "sets the version for the ruby_version_is guards to VERSION" do
["-r", "--require"].each do |opt|
@config[:requires] = []
@script.options [opt, "libspec"]
expect(@config[:requires]).to include("libspec")
end
end
end
RSpec.describe MkSpec, "#options" do
before :each do
@options = MSpecOptions.new
allow(MSpecOptions).to receive(:new).and_return(@options)
@script = MkSpec.new
end
it "parses the command line options" do
expect(@options).to receive(:parse).with(["--this", "and", "--that"])
@script.options ["--this", "and", "--that"]
end
it "parses ARGV unless passed other options" do
expect(@options).to receive(:parse).with(ARGV)
@script.options
end
it "prints help and exits if passed an unrecognized option" do
expect(@options).to receive(:raise).with(MSpecOptions::ParseError, an_instance_of(String))
allow(@options).to receive(:puts)
allow(@options).to receive(:exit)
@script.options ["--iunknown"]
end
end
RSpec.describe MkSpec, "#create_directory" do
before :each do
@script = MkSpec.new
@script.config[:base] = "spec"
end
it "prints a warning if a file with the directory name exists" do
expect(File).to receive(:exist?).and_return(true)
expect(File).to receive(:directory?).and_return(false)
expect(FileUtils).not_to receive(:mkdir_p)
expect(@script).to receive(:puts).with("spec/class already exists and is not a directory.")
expect(@script.create_directory("Class")).to eq(nil)
end
it "does nothing if the directory already exists" do
expect(File).to receive(:exist?).and_return(true)
expect(File).to receive(:directory?).and_return(true)
expect(FileUtils).not_to receive(:mkdir_p)
expect(@script.create_directory("Class")).to eq("spec/class")
end
it "creates the directory if it does not exist" do
expect(File).to receive(:exist?).and_return(false)
expect(@script).to receive(:mkdir_p).with("spec/class")
expect(@script.create_directory("Class")).to eq("spec/class")
end
it "creates the directory for a namespaced module if it does not exist" do
expect(File).to receive(:exist?).and_return(false)
expect(@script).to receive(:mkdir_p).with("spec/struct/tms")
expect(@script.create_directory("Struct::Tms")).to eq("spec/struct/tms")
end
end
RSpec.describe MkSpec, "#write_requires" do
before :each do
@script = MkSpec.new
@script.config[:base] = "spec"
@file = double("file")
allow(File).to receive(:open).and_yield(@file)
end
it "writes the spec_helper require line" do
expect(@file).to receive(:puts).with("require_relative '../../../spec_helper'")
@script.write_requires("spec/core/tcejbo", "spec/core/tcejbo/inspect_spec.rb")
end
it "writes require lines for each library specified on the command line" do
allow(@file).to receive(:puts)
expect(@file).to receive(:puts).with("require_relative '../../../spec_helper'")
expect(@file).to receive(:puts).with("require 'complex'")
@script.config[:requires] << 'complex'
@script.write_requires("spec/core/tcejbo", "spec/core/tcejbo/inspect_spec.rb")
end
end
RSpec.describe MkSpec, "#write_spec" do
before :each do
@file = IOStub.new
allow(File).to receive(:open).and_yield(@file)
@script = MkSpec.new
allow(@script).to receive(:puts)
@response = double("system command response")
allow(@response).to receive(:include?).and_return(false)
allow(@script).to receive(:`).and_return(@response)
end
it "checks if specs exist for the method if the spec file exists" do
name = Regexp.escape(@script.ruby)
expect(@script).to receive(:`).with(
%r"#{name} #{MSPEC_HOME}/bin/mspec-run --dry-run --unguarded -fs -e 'Object#inspect' spec/core/tcejbo/inspect_spec.rb")
@script.write_spec("spec/core/tcejbo/inspect_spec.rb", "Object#inspect", true)
end
it "checks for the method name in the spec file output" do
expect(@response).to receive(:include?).with("Array#[]=")
@script.write_spec("spec/core/yarra/element_set_spec.rb", "Array#[]=", true)
end
it "returns nil if the spec file exists and contains a spec for the method" do
allow(@response).to receive(:include?).and_return(true)
expect(@script.write_spec("spec/core/tcejbo/inspect_spec.rb", "Object#inspect", true)).to eq(nil)
end
it "does not print the spec file name if it exists and contains a spec for the method" do
allow(@response).to receive(:include?).and_return(true)
expect(@script).not_to receive(:puts)
@script.write_spec("spec/core/tcejbo/inspect_spec.rb", "Object#inspect", true)
end
it "prints the spec file name if a template spec is written" do
expect(@script).to receive(:puts).with("spec/core/tcejbo/inspect_spec.rb")
@script.write_spec("spec/core/tcejbo/inspect_spec.rb", "Object#inspect", true)
end
it "writes a template spec to the file if the spec file does not exist" do
expect(@file).to receive(:puts).twice
expect(@script).to receive(:puts).with("spec/core/tcejbo/inspect_spec.rb")
@script.write_spec("spec/core/tcejbo/inspect_spec.rb", "Object#inspect", false)
end
it "writes a template spec to the file if it exists but contains no spec for the method" do
expect(@response).to receive(:include?).and_return(false)
expect(@file).to receive(:puts).twice
expect(@script).to receive(:puts).with("spec/core/tcejbo/inspect_spec.rb")
@script.write_spec("spec/core/tcejbo/inspect_spec.rb", "Object#inspect", true)
end
it "writes a template spec" do
@script.write_spec("spec/core/tcejbo/inspect_spec.rb", "Object#inspect", true)
expect(@file).to eq <<EOS
describe "Object#inspect" do
it "needs to be reviewed for spec completeness"
end
EOS
end
it "writes a template spec with version guard" do
@script.config[:version] = '""..."1.9"'
@script.write_spec("spec/core/tcejbo/inspect_spec.rb", "Object#inspect", true)
expect(@file).to eq <<EOS
ruby_version_is ""..."1.9" do
describe "Object#inspect" do
it "needs to be reviewed for spec completeness"
end
end
EOS
end
end
RSpec.describe MkSpec, "#create_file" do
before :each do
@script = MkSpec.new
allow(@script).to receive(:write_requires)
allow(@script).to receive(:write_spec)
allow(File).to receive(:exist?).and_return(false)
end
it "generates a file name based on the directory, class/module, and method" do
expect(File).to receive(:join).with("spec/tcejbo", "inspect_spec.rb"
).and_return("spec/tcejbo/inspect_spec.rb")
@script.create_file("spec/tcejbo", "Object", "inspect", "Object#inspect")
end
it "does not call #write_requires if the spec file already exists" do
expect(File).to receive(:exist?).and_return(true)
expect(@script).not_to receive(:write_requires)
@script.create_file("spec/tcejbo", "Object", "inspect", "Object#inspect")
end
it "calls #write_requires if the spec file does not exist" do
expect(File).to receive(:exist?).and_return(false)
expect(@script).to receive(:write_requires).with(
"spec/tcejbo", "spec/tcejbo/inspect_spec.rb")
@script.create_file("spec/tcejbo", "Object", "inspect", "Object#inspect")
end
it "calls #write_spec with the file, method name" do
expect(@script).to receive(:write_spec).with(
"spec/tcejbo/inspect_spec.rb", "Object#inspect", false)
@script.create_file("spec/tcejbo", "Object", "inspect", "Object#inspect")
end
end
RSpec.describe MkSpec, "#run" do
before :each do
@options = MSpecOptions.new
allow(MSpecOptions).to receive(:new).and_return(@options)
@map = NameMap.new
allow(NameMap).to receive(:new).and_return(@map)
@script = MkSpec.new
allow(@script).to receive(:create_directory).and_return("spec/mkspec")
allow(@script).to receive(:create_file)
@script.config[:constants] = [MkSpec]
end
it "loads files in the requires list" do
allow(@script).to receive(:require)
expect(@script).to receive(:require).with("alib")
expect(@script).to receive(:require).with("blib")
@script.config[:requires] = ["alib", "blib"]
@script.run
end
it "creates a map of constants to methods" do
expect(@map).to receive(:map).with({}, @script.config[:constants]).and_return({})
@script.run
end
it "calls #create_directory for each class/module in the map" do
expect(@script).to receive(:create_directory).with("MkSpec").twice
@script.run
end
it "calls #create_file for each method on each class/module in the map" do
expect(@map).to receive(:map).with({}, @script.config[:constants]
).and_return({"MkSpec#" => ["run"]})
expect(@script).to receive(:create_file).with("spec/mkspec", "MkSpec", "run", "MkSpec#run")
@script.run
end
end
RSpec.describe MkSpec, ".main" do
before :each do
@script = double("MkSpec").as_null_object
allow(MkSpec).to receive(:new).and_return(@script)
end
it "sets MSPEC_RUNNER = '1' in the environment" do
ENV["MSPEC_RUNNER"] = "0"
MkSpec.main
expect(ENV["MSPEC_RUNNER"]).to eq("1")
end
it "creates an instance of MSpecScript" do
expect(MkSpec).to receive(:new).and_return(@script)
MkSpec.main
end
it "calls the #options method on the script" do
expect(@script).to receive(:options)
MkSpec.main
end
it "calls the #run method on the script" do
expect(@script).to receive(:run)
MkSpec.main
end
end
|