Commit Graph

32 Commits

Author SHA1 Message Date
Nitin Singh 3e9d69ba7b
Fix the undefined method error for non rails project due to use of many? method. (#1459)
* Fix the undefined method error for non rails project due to use of many? method

* Add tests for inclusion validation for non rails app.

* Remove unnecessary spaces.

* Rubocop offenses.

Co-authored-by: Nitin S <nitin.singh@edcast.com>
Co-authored-by: = <=>
2021-08-31 20:04:37 -03:00
Kapil Sachdev b7e02184d9 fix(rubocop): Fix Layout/LineLength
- bump rubocop to v1.0
- Fix Layout/MultilineAssignmentLayout and other remaining offences
- Exculde appraisal generated gemfiles in rubocop
- Replace NON_NUMERIC_VALUE constant with instance method against 
failing test case in  rails <= 5.1 and postgres adapter.
This is a defect in rails where a frozen string is modified in 
https://github.com/rails/rails/blob/v5.1.7/activerecord/lib/active_record/connection_adapters/postgresql/oid/money.rb#L25
2020-11-03 10:05:25 -07:00
Kapil Sachdev 3e88500318 fix(rubocop): Fix Style, Layout, Lint and other offenses [ci skip]
Updated Layout/LineLength cop to Max 120 lenght and ignore everything 
inside specs/**/*

Added rules for below cops in .rubocop.yml
Lint/AmbiguousBlockAssociation
Naming/HeredocDelimiterNaming
Rails/SkipsModelValidations
Style/FormatStringToken


Fixed below mentioned cops:
- Layout/CaseIndentation
- Layout/DotPosition
- Layout/ElseAlignment
- Layout/IndentationWidth
- Layout/LineLength
- Layout/MultilineBlockLayout
- Layout/MultilineOperationIndentation
- Lint/AmbiguousBlockAssociation
- Lint/MissingCopEnableDirective
- Lint/NestedMethodDefinition
- Lint/RedundantCopDisableDirective
- Lint/RedundantRequireStatement
- Lint/UnusedBlockArgument
- Lint/UnusedMethodArgument
- Metrics/ModuleLength
- Naming/MemoizedInstanceVariableName
- Naming/RescuedExceptionsVariableName
- Rails/Output
- Rails/Presence
- Security/Eval
- Security/Open
- Style/ClassCheck
- Style/CollectionMethods
- Style/ConditionalAssignment
- Style/EvalWithLocation
- Style/FormatStringToken
- Style/InverseMethods
- Style/MutableConstant
- Style/ParallelAssignment
- Style/RedundantBegin
- Style/RedundantCondition
- Style/RedundantInterpolation
- Style/RedundantSelf
- Style/RedundantSort
- Style/RescueStandardError
- Style/SafeNavigation
- Style/StringLiteralsInInterpolation
- Style/SymbolProc
2020-11-03 10:05:25 -07:00
Kapil Sachdev ee1008de7a fix(rubocop): Fix Layout related offenses [ci skip]
- Layout/EmptyLines
- Layout/EmptyLineAfterGuardClause
- Layout/TrailingEmptyLines
- Layout/EmptyLinesAroundBlockBody
- Layout/EmptyLinesAroundModuleBody
- Layout/EmptyLineAfterMagicComment
- Layout/SpaceInsidePercentLiteralDelimiters
- Layout/SpaceAroundEqualsInParameterDefault
- Layout/SpaceInsideArrayLiteralBrackets
- Layout/LeadingCommentSpace
- Layout/SpaceBeforeComment
- Layout/SpaceAroundOperators
- Layout/SpaceInsideRangeLiteral
- Layout/SpaceInsideReferenceBrackets
- Layout/SpaceInLambdaLiteral
- Layout/SpaceAfterComma
- Layout/SpaceInsideHashLiteralBraces
- Layout/TrailingWhitespace
- Layout/ArgumentAlignment
- Layout/HashAlignment
- Layout/DotPosition
- Layout/IndentationWidth
- Layout/EndAlignment
- Layout/MultilineOperationIndentation
- Layout/IndentationConsistency
- Layout/ClosingHeredocIndentation
- Layout/MultilineMethodCallBrace- Layout
- Layout/ClosingParenthesisIndentation
2020-11-03 10:05:25 -07:00
Kapil Sachdev cdcef128cf fix(rubocop): Add trailing comma in args, Hash and Arrays [ci skip]
- Satisfies Style/TrailingCommaInArguments, 
Style/TrailingCommaInArrayLiteral and Style/TrailingCommaInHashLiteral
2020-11-03 10:05:25 -07:00
Kapil Sachdev 97722f67cb fix(rubocop): Satisfy Style/StringLiterals with single_quotes [ci skip] 2020-11-03 10:05:25 -07:00
Elliot Winkler a4289e9651
Add the qualifier allow_blank to validate_length_of (#1318)
Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com>
2020-07-11 11:33:54 -06:00
Dan Frenette b4d8d3566a ValidateInclusionOfMatcher#allow_nil should return `self`
It was doing this until this change, now some tests are breaking. C
2019-02-14 03:31:32 -07:00
Elliot Winkler 4a4255772d Revert "Fix Zeus (this is addressed by other commits)"
This reverts commit c600c61c9b.
2018-09-15 15:34:09 -06:00
Elliot Winkler c600c61c9b Fix Zeus (this is addressed by other commits) 2018-09-15 13:43:30 -03:00
Elliot Winkler 507dc57004 Fix negative versions of validation matchers
When using a validation matcher in the negative, i.e.:

    should_not validate_*(...)

as opposed to:

    should validate_*(...)

...it's common to receive the following error:

    undefined method `attribute_setter' for nil:NilClass

This happens particularly when using a matcher that makes use of
AllowValueMatcher or DisallowValueMatcher internally (which all of the
validation matchers do).

Whenever you make an assertion by using a matcher in the negative as
opposed to the positive, RSpec still calls the `matches?` method for
that matcher; however, the assertion will pass if that returns *false*
as opposed to true. In other words, it just inverts the result.

However, whenever we are using AllowValueMatcher or
DisallowValueMatcher, it doesn't really work to invert the result. like
this. This is because AllowValueMatcher and DisallowValueMatcher,
despite their name, aren't truly opposites of each other.

AllowValueMatcher performs these steps:

1. Set the attribute on the record to some value
2. Run validations on the record
3. Ask whether validations pass or fail
4. If validations fail, store the value that caused the failure along
   with the validation errors and return false
5. Otherwise, return true

However, DisallowValueMatcher performs these steps:

1. Set the attribute on the record to some value
2. Run validations on the record
3. Ask whether validations pass or fail
4. If validations *pass*, store the value that caused the failure along
   with some metadata and return false
5. Otherwise, return true

This difference in logic is achieved by having AllowValueMatcher
implement `does_not_match?` and then having DisallowValueMatcher use
this for its positive case and use `matches?` for its negative case.
It's easy to see because of this that `does_not_match?` is not the same
as `!matches?` and vice versa.

So a matcher that makes use of these submatchers internally needs to use
their opposite versions whenever that matcher is used in the negative
case. In other words, all of the matchers need a `does_not_match?` which
is like `matches?`, except that all of the logic is inverted, and in all
the cases in which AllowValueMatcher is used, DisallowValueMatcher needs
to be used.

Doing this ensures that when `failure_message` is called on
AllowValueMatcher or DisallowValueMatcher, step 4 in the list of steps
above stores a proper value that can then be referenced in the failure
message for the validation matcher itself.
2018-09-15 13:43:30 -03:00
Elliot Winkler ebe60449ee Fix inclusion to correctly disallow outside values
The inclusion matcher, when qualified with `in_array`, was using
AllowValueMatcher to check that values outside the array were disallowed
by the model (and then inverting its result). However, it should have
been using DisallowValueMatcher all this time. This commit fixes that.

Without this fix, the following error is raised when using the inclusion
matcher against a model which does not have the proper inclusion
validation on it:

    undefined method `attribute_setter' for nil:NilClass

This happens because the inclusion matcher is a complex matcher, i.e.,
it runs a series of submatchers internally and the result of those
submatchers contributes to whether or not the matcher matches. If one of
those submatchers fails, the inclusion matcher immediately fails and
spits out the failure message associated with that submatcher.

However, there is a fundamental difference between AllowValueMatcher and
DisallowValueMatcher as it relates to how they function:

* AllowValueMatcher sets an attribute to a value on a record and expects
  the record not to fail validation.
* DisallowValueMatcher sets an attribute to a value on a record, but
  expects the record *to* fail validation.

The issue in this case is that, because AllowValueMatcher was used
instead of DisallowValueMatcher, the inclusion matcher thought that the
AllowValueMatcher failed, when in fact it passed (this result was just
inverted). So it tried to generate a failure message for a matcher that
didn't fail in the first place. By using DisallowValueMatcher, we set
the proper expectations.
2018-09-15 13:43:30 -03:00
Mauro George 97e2569253 Keep consistency on allow_blank and allow_nil qualifiers
After some discussion[1] was decided that these qualifiers do not accept
any parameter.

https://github.com/thoughtbot/shoulda-matchers/pull/722
2018-08-31 21:48:09 -03:00
Teo Ljungberg 0c07bfb039 Use `BigDecimal()` over `BigDecimal.new`
As the latter is deprecated since: ruby/bigdecimal@5337373
2018-01-02 14:35:41 -02:00
Dmitri Koulikoff 03a1d21380 Deprecation for ruby 2.4 removed
Secondary credit: camelmasa <camelmasa@gmail.com>
2017-10-03 00:11:13 -05:00
Ionut Dorobantu 61c365416a Replace Fixnum with Integer 2017-09-17 17:01:50 -05:00
Elliot Winkler afa6a7b666 Update RSpec test style across docs
Instead of using

    describe Foo do
      # ...
    end

use

    RSpec.describe Foo, type: :model do
      # ...
    end

instead. This is not exactly official, as the former style still works,
but the latter style is highly suggested in RSpec documentation and the
like, so this is what people are used to.

[ci skip]
2016-06-15 18:02:07 -06:00
Ben J Woodcroft a45f447bf2 require date for ValidateInclusionOfMatcher 2016-01-28 00:11:42 -07:00
Elliot Winkler 663c2f2c4d Remove most docs for ignoring_interference_by_writer
Since `ignoring_interference_by_writer` is on by default now, we don't
need to explicitly document it (unless someone wants to turn it off, but
that's unlikely).

[ci skip]
2016-01-10 23:19:47 -07:00
Elliot Winkler 1189934806 Add ignoring_interference_by_writer to all matchers
`allow_value` matcher is, of course, concerned with setting values on a
particular attribute on a particular record, and then checking that the
record is valid after doing so. That comes with a caveat: if the
attribute is overridden in such a way so that the same value going into
the attribute isn't the same value coming out of it, then `allow_value`
will balk -- it'll say, "I can't do that because that changes how I
work."

That's all well and good, but what the attribute intentionally changes
incoming values? ActiveRecord's typecasting behavior, for instance,
would trigger such an exception. What if the developer needs a way to
get around this? This is where `ignoring_interference_by_writer` comes
into play. You can tack it on to the end of the matcher, and you're free
to go on your way.

So, prior to this commit you could already apply it to `allow_value`,
but now in this commit it also works on any other matcher.

But, one little thing: sometimes using this qualifier isn't going to
work. Perhaps you or something else actually *is* overriding the
attribute to change incoming values in a specific way, and perhaps the
value that comes out makes the record fail validation, and there's
nothing you can do about it. So in this case, even if you're using
`ignoring_interference_by_writer`, we want to inform you about what the
attribute is doing -- what the input and output was. And so we do.
2016-01-05 00:58:16 -07:00
Elliot Winkler 6b3253147a allow_value: Inspect values more clearly
Modify descriptions and failure messages for all matchers by way of
allow_value and put small angle brackets around inspected values. This
is to visually distinguish an inspected value from the rest of the text,
and is especially noticeable for complex values such as an array that
contains an object, particularly if the inspected version of the value
wraps onto another line. It's a little easier to see:

    When attempting to set :attr on Example to ‹[#<Child id:
    nil>]›...

rather than:

    When attempting to set :attr on Example to [#<Child id:
    nil>]...
2015-12-30 21:51:54 -05:00
Elliot Winkler 2e35b3730e Refactor inclusion & exclusion matchers
This is part of a collection of commits that aim to improve failure
messages across the board, in order to make matchers easier to debug
when something goes wrong.

* Make the description of the matcher more readable.
* Add boolean methods to check whether `allow_nil` or `allow_blank` have
  been specified, to conform to the interface that ValidationMatcher
  introduces.
* Fix or fill in tests involving failure messages and descriptions to
  match these changes and recent changes to ValidationMatcher and
  allow_value.
2015-12-13 20:22:22 -07:00
Elliot Winkler 8fa97b4ff3 Fix inclusion matcher w/ date & datetime attrs
Why:

* The inclusion matcher (when used with the `in_array` qualifier) makes
  the assertion that when the attribute is set to a value that is
  outside the given array, the record in question is invalid. The issue
  is that when used with a date or datetime attribute, the arbitrary
  value the matcher chose was a string. This was getting typecast and so
  the matcher was throwing a CouldNotSetAttributeError.

To satisfy the above:

* If the column is a date, use a Date for the arbitrary value
* If the column is a datetime, use a DateTime for the arbitrary value
* If the column is a time, use a Time for the arbitrary value
2015-10-04 16:55:48 -06:00
Elliot Winkler 142366ef16 Refer to Minitest in docs over Test::Unit
Minitest has been used instead of Test::Unit for quite some time now,
let's get with the times.

[ci skip]
2015-09-30 13:18:23 -06:00
Mauro George 9a893f372d Add documentation to validate_inclusion_of on qualifier
[ci skip]
2015-03-28 22:33:44 -06:00
Elliot Winkler 55c8d09bf2 Remove deprecated matchers 2015-02-09 10:52:51 -07:00
Elliot Winkler 5f8c3235d0 Fix validate_inclusion_of + with_message
It did not fail if the given message did not match the message on the
validation itself.
2014-11-18 21:03:06 +08:00
Elliot Winkler c714193528 Tweak warning to be descriptive, not prescriptive
While we do advise against using the validates_inclusion_of or
validates_format_of validations with boolean columns, we won't judge
if you would like to do so.
2014-08-14 14:51:17 -04:00
Elliot Winkler c998907926 Document that validate_inclusion_of may produce a warning 2014-07-25 23:35:46 -06:00
Dave Schweisguth 8376dd2fbf Emit BOOLEAN_ALLOWS_BOOLEAN_MESSAGE regardless of the order of the array of all booleans
Documented ensure_inclusion_of change
2014-07-25 23:35:43 -06:00
Elliot Winkler 15d0cee93b Deprecate ensure_inclusion_of 2014-07-23 19:35:20 -06:00
Maxim Kaschenko 93c2336e98 ensure_inclusion_of => validate_inclusion_of 2014-07-21 23:01:40 -06:00