Skip to content

Commit fea0bc6

Browse files
Merge pull request #47 from swrobel/does-not-contain
Add does_not_contain matcher
2 parents 750899c + c801de4 commit fea0bc6

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Spec (files in `spec/lib/generators` are recognized as generator type example gr
1616

1717
```ruby
1818
# spec/lib/generators/test/test_generator_spec.rb
19-
19+
2020
require "generator_spec"
2121

2222
describe TestGenerator, type: :generator do
@@ -33,13 +33,13 @@ describe TestGenerator, type: :generator do
3333
end
3434
end
3535
```
36-
36+
3737
An RSpec file matching DSL is also provided, taken with permission from [beard](https://github.com/carlhuda/beard/blob/master/spec/support/matcher.rb) by [carlhuda](https://github.com/carlhuda).
3838

3939
```ruby
4040
describe TestGenerator, "using custom matcher", type: :generator do
4141
destination File.expand_path("../../tmp", __FILE__)
42-
42+
4343
before do
4444
prepare_destination
4545
run_generator
@@ -52,6 +52,7 @@ describe TestGenerator, "using custom matcher", type: :generator do
5252
directory "initializers" do
5353
file "test.rb" do
5454
contains "# Initializer"
55+
does_not_contain "Something else"
5556
end
5657
end
5758
end
@@ -60,6 +61,7 @@ describe TestGenerator, "using custom matcher", type: :generator do
6061
file "123_create_tests.rb"
6162
migration "create_tests" do
6263
contains "class TestMigration"
64+
does_not_contain "Something else"
6365
end
6466
end
6567
end

lib/generator_spec/matcher.rb

+16-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def description
1212
def initialize(name, &block)
1313
@contents = []
1414
@name = name
15+
@negative_contents = []
1516

1617
if block_given?
1718
instance_eval(&block)
@@ -22,6 +23,10 @@ def contains(text)
2223
@contents << text
2324
end
2425

26+
def does_not_contain(text)
27+
@negative_contents << text
28+
end
29+
2530
def matches?(root)
2631
unless root.join(@name).exist?
2732
throw :failure, root.join(@name)
@@ -40,6 +45,12 @@ def check_contents(file)
4045
throw :failure, [file, string, contents]
4146
end
4247
end
48+
49+
@negative_contents.each do |string|
50+
if contents.include?(string)
51+
throw :failure, [:not, file, string, contents]
52+
end
53+
end
4354
end
4455
end
4556

@@ -129,7 +140,11 @@ def description
129140

130141
def failure_message
131142
if @failure.is_a?(Array) && @failure[0] == :not
132-
"Structure should not have had #{@failure[1]}, but it did"
143+
if @failure.length > 2
144+
"Structure should have #{@failure[1]} without #{@failure[2]}. It had:\n#{@failure[3]}"
145+
else
146+
"Structure should not have had #{@failure[1]}, but it did"
147+
end
133148
elsif @failure.is_a?(Array)
134149
"Structure should have #{@failure[0]} with #{@failure[1]}. It had:\n#{@failure[2]}"
135150
else

spec/generator_spec/matcher_spec.rb

+40
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,26 @@ module Matcher
107107
}.to throw_symbol(:failure)
108108
end
109109
end
110+
111+
context 'with does_not_contain' do
112+
before do
113+
write_file(location, 'class CreatePosts')
114+
end
115+
116+
it 'doesnt throw if the contents dont include the string' do
117+
file.does_not_contain 'PostsMigration'
118+
expect {
119+
file.matches?(TMP_ROOT)
120+
}.to_not throw_symbol
121+
end
122+
123+
it 'throws :failure if the contents include the string' do
124+
file.does_not_contain 'CreatePosts'
125+
expect {
126+
file.matches?(TMP_ROOT)
127+
}.to throw_symbol(:failure)
128+
end
129+
end
110130
end
111131
end
112132

@@ -153,6 +173,26 @@ module Matcher
153173
}.to throw_symbol(:failure)
154174
end
155175
end
176+
177+
context 'with does_not_contain' do
178+
before do
179+
write_file(location, 'class CreatePosts')
180+
end
181+
182+
it 'doesnt throw if the migration doesnt include the given content' do
183+
migration.does_not_contain('CreateNotes')
184+
expect {
185+
migration.matches?(TMP_ROOT)
186+
}.to_not throw_symbol
187+
end
188+
189+
it 'throws failure if the migration includes the given content' do
190+
migration.does_not_contain('CreatePosts')
191+
expect {
192+
migration.matches?(TMP_ROOT)
193+
}.to throw_symbol(:failure)
194+
end
195+
end
156196
end
157197
end
158198

0 commit comments

Comments
 (0)