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 prefix and suffix to define enum for #1077

Merged
merged 5 commits into from
Jan 28, 2018

Conversation

mcmire
Copy link
Collaborator

@mcmire mcmire commented Jan 26, 2018

In Rails 5, the enum macro received two new options: _prefix and
_suffix. These options change the names of the methods that are
generated from the given enum values. This commit adds qualifiers to the
define_enum_for matcher so that they can be tested.


Closes #961.

@mcmire mcmire added this to the v4.0 milestone Jan 26, 2018
end

def record_with_array_values(column_type: :integer)
def record_with_array_values(

Choose a reason for hiding this comment

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

Avoid parameter lists longer than 5 parameters. [6/5]

end
end

context 'if the attribute was defined with the same prefix and suffix' do

Choose a reason for hiding this comment

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

Line is too long. [83/80]

end

message = <<-MESSAGE
Expected Example to define :attr as an enum, backed by an integer, using

Choose a reason for hiding this comment

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

Use 2 spaces for indentation in a heredoc by using <<~ instead of <<-.

with_suffix(:whatever)
end

message = <<-MESSAGE

Choose a reason for hiding this comment

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

Inconsistent indentation detected.

end

message = <<-MESSAGE
Expected Example to define :attr as an enum, backed by an integer, using

Choose a reason for hiding this comment

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

Use 2 spaces for indentation in a heredoc by using <<~ instead of <<-.

values: ['open', 'closed'],
)
message = <<-MESSAGE
Expected Example to define :attr as an enum, backed by a string, with

Choose a reason for hiding this comment

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

Use 2 spaces for indentation in a heredoc by using <<~ instead of <<-.

column_type: :integer,
)
message = <<-MESSAGE
Expected Example to define :attr as an enum, backed by a string.

Choose a reason for hiding this comment

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

Use 2 spaces for indentation in a heredoc by using <<~ instead of <<-.

attribute_name: :attr,
)
message = <<-MESSAGE
Expected Example to define :non_existent as an enum, backed by an

Choose a reason for hiding this comment

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

Use 2 spaces for indentation in a heredoc by using <<~ instead of <<-.

values: { active: 0, archived: 1 },
)
message = <<-MESSAGE
Expected Example to define :attr as an enum, backed by an integer, with

Choose a reason for hiding this comment

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

Use 2 spaces for indentation in a heredoc by using <<~ instead of <<-.

end

it "accepts a record where the attribute is defined as an enum but the enum values do not match" do
message = %{Expected #{record_with_hash_values.class} to define :#{enum_attribute} as an enum with {:active=>5, :archived=>10} and store the value in a column of type integer}
it 'rejects a record where the attribute is defined as an enum but the enum values do not match' do

Choose a reason for hiding this comment

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

Line is too long. [107/80]

@mcmire mcmire force-pushed the add-prefix-and-suffix-to-define-enum-for branch from 4221597 to a6739e0 Compare January 26, 2018 07:36
end

def record_with_hash_values
def build_record(

Choose a reason for hiding this comment

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

Avoid parameter lists longer than 5 parameters. [6/5]

end

def non_enum_attribute
:condition
def build_record_with_array_values(

Choose a reason for hiding this comment

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

Avoid parameter lists longer than 5 parameters. [6/5]

@mcmire mcmire force-pushed the add-prefix-and-suffix-to-define-enum-for branch 2 times, most recently from c8b3c7a to 457d306 Compare January 26, 2018 19:56
)
model.enum(enum_attribute => ['published', 'unpublished', 'draft'])
model.new
def build_record_with_enum_attribute(

Choose a reason for hiding this comment

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

Avoid parameter lists longer than 5 parameters. [6/5]

def enum_attribute
:status
end
def build_record_with_array_values(

Choose a reason for hiding this comment

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

Avoid parameter lists longer than 5 parameters. [6/5]

end
end

context 'if the attribute was defined with the same prefix and suffix' do

Choose a reason for hiding this comment

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

Line is too long. [81/80]


message = "Expected #{model} to define :statuses as an enum and store the value in a column of type integer"
context 'if a method to hold enum values exists on the model but was not created via the enum macro' do

Choose a reason for hiding this comment

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

Line is too long. [105/80]

def self.inspect_hash(hash)
output = '‹{'

output << hash.map { |key, value|

Choose a reason for hiding this comment

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

Avoid using {...} for multi-line blocks.

def hashify(value)
if value.nil?
return {}
def enum_value_methods_exist?

Choose a reason for hiding this comment

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

Method has too many lines. [19/10]
Perceived complexity for enum_value_methods_exist? is too high. [8/7]


description << ' a suffix of '

description << "#{options[:expected_suffix].inspect}"

Choose a reason for hiding this comment

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

Prefer to_s over string interpolation.

end

if options[:expected_suffix]
if options[:expected_prefix]

Choose a reason for hiding this comment

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

Use the return of the conditional for variable assignment and comparison.


if options[:expected_prefix]
description << ', using a prefix of '
description << "#{options[:expected_prefix].inspect}"

Choose a reason for hiding this comment

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

Prefer to_s over string interpolation.

end

def with(expected_enum_values)
def description

Choose a reason for hiding this comment

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

Assignment Branch Condition size for description is too high. [28.3/15]
Method has too many lines. [22/10]

@mcmire mcmire force-pushed the add-prefix-and-suffix-to-define-enum-for branch 2 times, most recently from d7c36c5 to 9e1680d Compare January 27, 2018 06:19

expect(model.new).to allow_value(1).for(:status)
context 'given an ActiveRecord model' do
context 'where the attribute under test is an enum and the given value is a value in that enum' do

Choose a reason for hiding this comment

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

Line is too long. [102/80]

@mcmire mcmire force-pushed the add-prefix-and-suffix-to-define-enum-for branch from 9e1680d to 8669078 Compare January 28, 2018 06:11
it 'rejects with an appropriate failure message' do
record = build_record_validating_scoped_uniqueness_with_enum(
enum_scope: :scope1,
additional_scopes: [:scope2]

Choose a reason for hiding this comment

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

Put a comma after the last parameter of a multiline method call.

enum_scope: :scope
enum_scope: :scope1,
additional_scopes: [:scope2],
additional_attributes: [:other]

Choose a reason for hiding this comment

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

Put a comma after the last parameter of a multiline method call.

context 'when one of the scoped attributes is an enum' do
it 'accepts' do
record = build_record_validating_scoped_uniqueness_with_enum(
enum_scope: :scope

Choose a reason for hiding this comment

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

Put a comma after the last parameter of a multiline method call.

@mcmire mcmire force-pushed the add-prefix-and-suffix-to-define-enum-for branch from 8669078 to 8c0e13d Compare January 28, 2018 06:22
* Break up long test names into contexts
* Make tests more isolated by reducing "global" scope (always use
  keyword argument to specify attribute name instead of a "global"
  method)
* Simplify how failure messages are tested by removing interpolation in
  favor of explicitness
The `enum` macro was introduced in Rails 4.1, which shoulda-matchers no
longer supports, so we can always assume that enum is available.
We'll need to introduce some other `with_*` qualifiers and it won't look
right to have one qualifier called `with` and other qualifiers called
`with_something_else`.
@mcmire mcmire force-pushed the add-prefix-and-suffix-to-define-enum-for branch from 8c0e13d to 386a773 Compare January 28, 2018 06:29
If a subtest fails, explain why. We're going to be adding another
subtest later, and so without this extra information, users will most
definitely be confused.
In Rails 5, the `enum` macro received two new options: `_prefix` and
`_suffix`. These options change the names of the methods that are
generated from the given enum values. This commit adds qualifiers to the
`define_enum_for` matcher so that they can be tested.
@mcmire mcmire force-pushed the add-prefix-and-suffix-to-define-enum-for branch from 386a773 to 147ac48 Compare January 28, 2018 06:48
@mcmire mcmire merged commit 147ac48 into master Jan 28, 2018
@mcmire mcmire deleted the add-prefix-and-suffix-to-define-enum-for branch January 28, 2018 08:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enum matcher should check for _prefix and _suffix
2 participants