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

Avoid be_falsey and be_truthy #2736

Merged
merged 9 commits into from Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 9 additions & 13 deletions features/configuration/custom_settings.feature
Expand Up @@ -14,18 +14,14 @@ Feature: custom settings
expect(RSpec.configuration.custom_setting).to be_nil
end
it "acts false by default" do
expect(RSpec.configuration.custom_setting).to be_falsey
end
Copy link
Member

Choose a reason for hiding this comment

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

This is part of the documentation, not a copy of another spec so should be restored.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed it thinking that:

  1. be_falsey should not be encouraged
  2. it is completely redundant with the previous spec (impossible to pass this and not pass the previous spec)
  3. it does not document in any way custom_setting, only that we can always expect(nil).to be_falsey

it "is exposed as a predicate" do
expect(RSpec.configuration.custom_setting?).to be_falsey
expect(RSpec.configuration.custom_setting?).to be(false)
end
it "can be overridden" do
RSpec.configuration.custom_setting = true
expect(RSpec.configuration.custom_setting).to be_truthy
expect(RSpec.configuration.custom_setting?).to be_truthy
expect(RSpec.configuration.custom_setting).to be(true)
expect(RSpec.configuration.custom_setting?).to be(true)
end
end
"""
Expand All @@ -41,17 +37,17 @@ Feature: custom settings
RSpec.describe "custom setting" do
it "is true by default" do
expect(RSpec.configuration.custom_setting).to be_truthy
expect(RSpec.configuration.custom_setting).to be(true)
end
it "is exposed as a predicate" do
expect(RSpec.configuration.custom_setting?).to be_truthy
expect(RSpec.configuration.custom_setting?).to be(true)
end
it "can be overridden" do
RSpec.configuration.custom_setting = false
expect(RSpec.configuration.custom_setting).to be_falsey
expect(RSpec.configuration.custom_setting?).to be_falsey
expect(RSpec.configuration.custom_setting).to be(false)
expect(RSpec.configuration.custom_setting?).to be(false)
end
end
"""
Expand All @@ -71,11 +67,11 @@ Feature: custom settings
RSpec.describe "custom setting" do
it "returns the value set in the last cofigure block to get eval'd" do
expect(RSpec.configuration.custom_setting).to be_truthy
expect(RSpec.configuration.custom_setting).to be(true)
end
it "is exposed as a predicate" do
expect(RSpec.configuration.custom_setting?).to be_truthy
expect(RSpec.configuration.custom_setting?).to be(true)
end
end
"""
Expand Down
4 changes: 2 additions & 2 deletions features/example_groups/shared_examples.feature
Expand Up @@ -106,13 +106,13 @@ Feature: shared examples
describe "#include?" do
context "with an item that is in the collection" do
it "returns true" do
expect(collection.include?(7)).to be_truthy
expect(collection.include?(7)).to be(true)
end
end
context "with an item that is not in the collection" do
it "returns false" do
expect(collection.include?(9)).to be_falsey
expect(collection.include?(9)).to be(false)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion features/hooks/around_hooks.feature
Expand Up @@ -219,7 +219,7 @@ Feature: `around` hooks
end
it "runs the example in the correct context" do
expect(included_in_configure_block).to be_truthy
expect(included_in_configure_block).to be(true)
end
end
"""
Expand Down
17 changes: 10 additions & 7 deletions lib/rspec/core/configuration.rb
Expand Up @@ -67,15 +67,17 @@ def self.define_reader(name)
end

# @private
def self.define_aliases(name, alias_name)
def self.define_alias(name, alias_name)
alias_method alias_name, name
alias_method "#{alias_name}=", "#{name}="
define_predicate_for alias_name
define_predicate alias_name
end

# @private
def self.define_predicate_for(*names)
names.each { |name| alias_method "#{name}?", name }
def self.define_predicate(name)
define_method "#{name}?" do
!!send(name)
marcandre marked this conversation as resolved.
Show resolved Hide resolved
end
end

# @private
Expand All @@ -88,7 +90,7 @@ def self.add_setting(name, opts={})
add_read_only_setting name

Array(opts[:alias_with]).each do |alias_name|
define_aliases(name, alias_name)
define_alias(name, alias_name)
end
end

Expand All @@ -98,7 +100,7 @@ def self.add_setting(name, opts={})
def self.add_read_only_setting(name, opts={})
raise "Use the instance add_setting method if you want to set a default" if opts.key?(:default)
define_reader name
define_predicate_for name
define_predicate name
end

# @macro [attach] add_setting
Expand Down Expand Up @@ -312,7 +314,8 @@ def exclude_pattern=(value)
# Report the times for the slowest examples (default: `false`).
# Use this to specify the number of examples to include in the profile.
# @return [Boolean]
add_setting :profile_examples
attr_writer :profile_examples
define_predicate :profile_examples

# @macro add_setting
# Run all examples if none match the configured filters
Expand Down
1 change: 1 addition & 0 deletions lib/rspec/core/world.rb
Expand Up @@ -17,6 +17,7 @@ class World
attr_accessor :non_example_failure

def initialize(configuration=RSpec.configuration)
@wants_to_quit = false
marcandre marked this conversation as resolved.
Show resolved Hide resolved
@configuration = configuration
configuration.world = self
@example_groups = []
Expand Down
20 changes: 10 additions & 10 deletions spec/rspec/core/configuration_options_spec.rb
Expand Up @@ -296,16 +296,16 @@
end

describe "--fail-fast" do
it "defaults to false" do
expect(parse_options[:fail_fast]).to be_falsey
it "defaults to nil" do
Copy link
Member

Choose a reason for hiding this comment

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

These concern me as they represent a change in how we document them, if they are wrong I almost want to fix them rather than the specs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't check the implementation, but I find it perfect that the setting is nil if there's no CLI option, and 1 or false if there's an explicit CLI option. For example it makes it possible to override a nil setting with a local setting file, but false should not be overriden.
Or are you talking about 1 vs true?

expect(parse_options[:fail_fast]).to be(nil)
end

it "sets fail_fast on config" do
expect(parse_options("--fail-fast")[:fail_fast]).to be_truthy
it "sets fail_fast to 1 on config" do
expect(parse_options("--fail-fast")[:fail_fast]).to be(1)
end

it "sets fail_fast on config" do
expect(parse_options("--no-fail-fast")[:fail_fast]).to be_falsey
it "sets fail_fast to false on config" do
expect(parse_options("--no-fail-fast")[:fail_fast]).to be(false)
end
end

Expand All @@ -322,12 +322,12 @@
end

describe "--dry-run" do
it "defaults to false" do
expect(parse_options[:dry_run]).to be_falsey
it "defaults to nil" do
expect(parse_options[:dry_run]).to be(nil)
end

it "sets dry_run on config" do
expect(parse_options("--dry-run")[:dry_run]).to be_truthy
expect(parse_options("--dry-run")[:dry_run]).to be(true)
end
end

Expand Down Expand Up @@ -464,7 +464,7 @@ def expect_parsing_to_fail_mentioning_source(source, options=[])

expect(options[:requires]).to eq(["some_file"])
expect(options[:full_description]).to eq([/foo\ bar/])
expect(options[:drb]).to be_truthy
expect(options[:drb]).to be(true)
expect(options[:formatters]).to eq([['global']])
end
end
Expand Down
24 changes: 12 additions & 12 deletions spec/rspec/core/configuration_spec.rb
Expand Up @@ -293,7 +293,7 @@ def mod.configuration; @config ||= Struct.new(:custom_setting).new; end
mod_config.custom_setting = true
end

expect(mod.configuration.custom_setting).to be_truthy
expect(mod.configuration.custom_setting).to be(true)
end

it "raises if framework module doesn't support configuration" do
Expand Down Expand Up @@ -1302,12 +1302,12 @@ def metadata_hash(*args)
describe "#run_all_when_everything_filtered?" do

it "defaults to false" do
expect(config.run_all_when_everything_filtered?).to be_falsey
expect(config.run_all_when_everything_filtered?).to be(false)
end

it "can be queried with question method" do
config.run_all_when_everything_filtered = true
expect(config.run_all_when_everything_filtered?).to be_truthy
expect(config.run_all_when_everything_filtered?).to be(true)
end
end

Expand Down Expand Up @@ -1779,14 +1779,14 @@ def metadata_hash(*args)
config_2 = Configuration.new

config_1.full_backtrace = true
expect(config_2.full_backtrace?).to be_falsey
expect(config_2.full_backtrace?).to be(false)
end
end

describe "#backtrace_exclusion_patterns=" do
it "actually receives the new filter values" do
config.backtrace_exclusion_patterns = [/.*/]
expect(config.backtrace_formatter.exclude? "this").to be_truthy
expect(config.backtrace_formatter.exclude? "this").to be(true)
end
end

Expand All @@ -1805,7 +1805,7 @@ def metadata_hash(*args)
describe "#backtrace_exclusion_patterns" do
it "can be appended to" do
config.backtrace_exclusion_patterns << /.*/
expect(config.backtrace_formatter.exclude? "this").to be_truthy
expect(config.backtrace_formatter.exclude? "this").to be(true)
end
end

Expand Down Expand Up @@ -2213,7 +2213,7 @@ def exclude?(line)
end

it "adds a predicate" do
expect(config.custom_option?).to be_falsey
expect(config.custom_option?).to be(false)
end

it "can be overridden" do
Expand All @@ -2232,7 +2232,7 @@ def exclude?(line)
end

it "returns true for the predicate" do
expect(config.custom_option?).to be_truthy
expect(config.custom_option?).to be(true)
end

it "can be overridden with a truthy value" do
Expand Down Expand Up @@ -2269,7 +2269,7 @@ def exclude?(line)

it "delegates the predicate to the other option" do
config.custom_option = true
expect(config.another_custom_option?).to be_truthy
expect(config.another_custom_option?).to be(true)
end
end
end
Expand Down Expand Up @@ -2526,11 +2526,11 @@ def example_numbered(num)
it "forces 'false' value" do
config.add_setting :custom_option
config.custom_option = true
expect(config.custom_option?).to be_truthy
expect(config.custom_option?).to be(true)
config.force :custom_option => false
expect(config.custom_option?).to be_falsey
expect(config.custom_option?).to be(false)
config.custom_option = true
expect(config.custom_option?).to be_falsey
expect(config.custom_option?).to be(false)
end
end

Expand Down