forked from dchelimsky/rspec-tmbundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch_command_spec.rb
213 lines (172 loc) · 6.22 KB
/
switch_command_spec.rb
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
require File.dirname(__FILE__) + '/../../../lib/spec/mate/switch_command'
module Spec
module Mate
class Twin
def initialize(expected, webapp)
@expected = expected
@webapp = webapp
end
def matches?(actual)
@actual = actual
# Satisfy expectation here. Return false or raise an error if it's not met.
command = SwitchCommand.new
command.stub!(:webapp?).and_return(@webapp)
path = command.twin(@actual)
path.should == @expected
path = command.twin(@expected)
path.should == @actual
true
end
def failure_message
"expected #{@actual.inspect} to twin #{@expected.inspect}, but it didn't"
end
def negative_failure_message
"expected #{@actual.inspect} not to twin #{@expected.inspect}, but it did"
end
def description
"twin #{@actual} <=> #{@expected}"
end
end
class BeA
def initialize(expected)
@expected = expected
end
def matches?(actual)
@actual = actual
c = SwitchCommand.new
c.stub!(:webapp?).and_return(@webapp)
c.file_type(@actual).should == @expected
true
end
def failure_message
"expected #{@actual.inspect} to be_a #{@expected.inspect}, but it didn't"
end
def negative_failure_message
"expected #{@actual.inspect} not to be_a #{@expected.inspect}, but it did"
end
end
def be_a(expected)
BeA.new(expected)
end
describe SwitchCommand, "in a regular app" do
include Spec::Mate
def twin(expected)
Twin.new(expected, false)
end
specify do
"/Users/aslakhellesoy/scm/rspec/trunk/RSpec.tmbundle/Support/spec/spec/mate/switch_command_spec.rb".should twin(
"/Users/aslakhellesoy/scm/rspec/trunk/RSpec.tmbundle/Support/lib/spec/mate/switch_command.rb")
end
it "should suggest plain spec" do
"/a/full/path/spec/snoopy/mooky_spec.rb".should be_a("spec")
end
it "should suggest plain file" do
"/a/full/path/lib/snoopy/mooky.rb".should be_a("file")
end
it "should create spec for spec files" do
regular_spec = <<-SPEC
require File.dirname(__FILE__) + '/../spec_helper'
describe ${1:Type} do
it "should ${2:description}" do
$0
end
end
SPEC
SwitchCommand.new.content_for('spec', "spec/foo/zap_spec.rb").should == regular_spec
SwitchCommand.new.content_for('spec', "spec/controller/zap_spec.rb").should == regular_spec
end
it "should create class for regular file" do
file = <<-EOF
module Foo
class Zap
end
end
EOF
SwitchCommand.new.content_for('file', "lib/foo/zap.rb").should == file
SwitchCommand.new.content_for('file', "some/other/path/lib/foo/zap.rb").should == file
end
end
describe SwitchCommand, "in a Rails app" do
include Spec::Mate
def twin(expected)
Twin.new(expected, true)
end
specify do
"/a/full/path/app/controllers/mooky_controller.rb".should twin(
"/a/full/path/spec/controllers/mooky_controller_spec.rb")
end
specify do
"/a/full/path/app/controllers/application.rb".should twin(
"/a/full/path/spec/controllers/application_controller_spec.rb")
end
specify do
"/a/full/path/spec/controllers/application_controller_spec.rb".should twin(
"/a/full/path/app/controllers/application.rb")
end
specify do
"/a/full/path/app/controllers/job_applications_controller.rb".should twin(
"/a/full/path/spec/controllers/job_applications_controller_spec.rb")
end
specify do
"/a/full/path/spec/controllers/job_applications_controller_spec.rb".should twin(
"/a/full/path/app/controllers/job_applications_controller.rb")
end
specify do
"/a/full/path/app/helpers/application_helper.rb".should twin(
"/a/full/path/spec/helpers/application_helper_spec.rb")
end
specify do
"/a/full/path/spec/helpers/application_helper_spec.rb".should twin(
"/a/full/path/app/helpers/application_helper.rb")
end
specify do
"/a/full/path/app/models/mooky.rb".should twin(
"/a/full/path/spec/models/mooky_spec.rb")
end
specify do
"/a/full/path/app/helpers/mooky_helper.rb".should twin(
"/a/full/path/spec/helpers/mooky_helper_spec.rb")
end
specify do
"/a/full/path/app/views/mooky/show.html.erb".should twin(
"/a/full/path/spec/views/mooky/show.html.erb_spec.rb")
end
specify do
"/a/full/path/app/views/mooky/show.rhtml".should twin(
"/a/full/path/spec/views/mooky/show.rhtml_spec.rb")
end
it "should work with lib dir in rails or merb" do
"/a/full/path/lib/foo/mooky.rb".should twin(
"/a/full/path/spec/lib/foo/mooky_spec.rb")
end
it "should suggest controller spec" do
"/a/full/path/spec/controllers/mooky_controller_spec.rb".should be_a("controller spec")
end
it "should suggest model spec" do
"/a/full/path/spec/models/mooky_spec.rb".should be_a("model spec")
end
it "should suggest helper spec" do
"/a/full/path/spec/helpers/mooky_helper_spec.rb".should be_a("helper spec")
end
it "should suggest view spec" do
"/a/full/path/spec/views/mooky/show.html.erb_spec.rb".should be_a("view spec")
end
it "should suggest controller" do
"/a/full/path/app/controllers/mooky_controller.rb".should be_a("controller")
end
it "should suggest model" do
"/a/full/path/app/models/mooky.rb".should be_a("model")
end
it "should suggest helper" do
"/a/full/path/app/helpers/mooky_helper.rb".should be_a("helper")
end
it "should suggest view" do
"/a/full/path/app/views/mooky/show.html.erb".should be_a("view")
end
it "should create spec that requires a helper" do
SwitchCommand.new.content_for('controller spec', "spec/controllers/mooky_controller_spec.rb").split("\n")[0].should ==
"require File.dirname(__FILE__) + '/../spec_helper'"
end
end
end
end