Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add #fits and #xits methods #79

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ spec/reports
test/tmp
test/version_tmp
tmp
bundle
bin
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ Metadata arguments are supported.
its(:size, focus: true) { should eq(1) }
```

Alternatively, you can use the `f` and `x` prefix for `focus` and `skip` metadata, respectively.

```ruby
fits(:size) { should eq(1) }
xits(:not_implemented_method) { will_not raise_error }
```

## Contributing

1. Fork it
Expand Down
30 changes: 29 additions & 1 deletion lib/rspec/its.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,40 @@ def should_not(matcher=nil, message=nil)
end
end

# Calls the #its method adding the :focus tag to metadata to allowing
# to be run in focus.
#
# @example
#
# # This ...
# describe Array do
# fits(:size) { should eq(0) }
# end
def fits(attribute, *options, &block)
opts = (options << :focus).uniq
its(attribute, *opts, &block)
end

# Calls the #its method adding the :skip tag to metadata to allowing
# to be be skipped.
#
# @example
#
# # This ...
# describe Array do
# xits(:size) { should eq(0) }
# end
def xits(attribute, *options, &block)
opts = (options << :skip).uniq
its(attribute, *opts, &block)
end

end
end

RSpec.configure do |rspec|
rspec.extend RSpec::Its
rspec.backtrace_exclusion_patterns << %r(/lib/rspec/its)
rspec.backtrace_exclusion_patterns << %r{/lib/rspec/its}
end

RSpec::SharedContext.send(:include, RSpec::Its)
38 changes: 38 additions & 0 deletions spec/rspec/its_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module RSpec
its([]) { expect(described_class).to be Its }
end
end

context "with explicit subject" do
subject do
Class.new do
Expand Down Expand Up @@ -243,6 +244,7 @@ def false_if_first_time
end
end
end

context "with metadata" do
context "preserves access to metadata that doesn't end in hash" do
its([], :foo) do |example|
Expand Down Expand Up @@ -376,5 +378,41 @@ def terminator
end
end
end

describe "#fits" do
describe "focus with metadata" do
let(:group) do
RSpec.describe do
extend RSpec::Its

fits(:class) { true }
end
end
let(:reporter) { RSpec::Core::Reporter.new(RSpec::Core::Configuration.new) }

it "generates a focused example" do
group.run(reporter)
expect(group.descendants.last.examples.first.metadata[:focus]).to be_truthy
end
end
end

describe "#xits" do
describe "skip with metadata" do
let(:group) do
RSpec.describe do
extend RSpec::Its

xits(:class) { true }
end
end
let(:reporter) { RSpec::Core::Reporter.new(RSpec::Core::Configuration.new) }

it "generates a focused example" do
group.run(reporter)
expect(group.descendants.last.examples.first.metadata[:skip]).to be_truthy
end
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take a look at how rspec-core tests example groups, this isn't a solution we would want to merge.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @JonRowe I've updated the specs. Let me know if they're okay

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess Jon meant something more like this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't copy the simplicity of that example exactly, because we're working with it (example) vs fits (describe) so they need to be handled differently, reflected in the updated spec. Not sure if this is as clear as what I had before but it works I guess 🙁

end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'rspec/its'

Dir['./spec/support/**/*'].each {|f| require f}
Dir['./support/**/*'].each {|f| require f}

class NullFormatter
private
Expand Down