Commit Graph

26 Commits

Author SHA1 Message Date
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 97722f67cb fix(rubocop): Satisfy Style/StringLiterals with single_quotes [ci skip] 2020-11-03 10:05:25 -07:00
Gustavo Cunha a11361125f
Add support to numericality: :other_than (#1282) 2020-07-22 23:23:51 -06:00
Elliot Winkler 476f0dbe53 Fix negative versions of numericality matcher
The numericality matcher works when used in the negative — i.e.
`should_not` vs `should` — as long as it is not being used with any
qualifiers. As soon as this happens, this breaks because the classes
that back the qualifiers do not respond to `does_not_match?`. This
commit fixes that.
2019-05-31 22:50:28 -06: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 fc010175c7 Small refactoring in NumericTypeMatcher 2015-12-31 02:51:42 -05:00
Elliot Winkler bffc9c9b73 Refactor numericality matcher
This is part of a collection of commits that aim to improve failure
messages across the board. The goal here is to make the matcher easier
to debug when something goes wrong.

* Have the failure message describe what the matcher was trying to do
  when it failed.
* Make the description of the matcher more readable.
* Change how the matcher works by stopping at the first failing
  submatcher instead of running all submatchers. Coincidentally, pending
  tests involving a strict validation but a non-strict expectation now
  pass.
* 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 18b2859d25 Fix numericality matcher w/ numeric columns
Fix the matcher so it still raises a CouldNotSetAttributeError against a
numeric column, but only if the matcher has not been qualified at all.
When the matcher is qualified with anything else, then it's okay to use
a numeric column, as long as the matcher no longer asserts that the
record disallows a non-numeric value.
2015-10-08 23:37:08 -06:00
Elliot Winkler 9d9dc4e6b9 Tighten CouldNotSetAttributeError restriction
Why:

* Previously, `allow_value` would raise a CouldNotSetAttributeError
  if the value being set didn't match the value the attribute had after
  being set, but only if the attribute was being changed from nil to
  non-nil or non-nil to nil.
* It turns out it doesn't matter which value you're trying to set the
  attribute to -- if the attribute rejects that change it's confusing
  either way. (In fact, I was recently bit by a case in which I was
  trying to validate numericality of an attribute, where the writer
  method for that attribute was overridden to ensure that the attribute
  always stored a number and never contained non-number characters.
  This ended up making the numericality validation useless, of
  course -- but it caused confusion because the test acted in a way
  I didn't expect.)

To satisfy the above:

* `allow_value` now raises a CouldNotSetAttributeError if the attribute
  rejects the value being set in *any* way.
* However, add a `ignoring_interference_by_writer` qualifier so that
  it is possible to manually override this behavior.
* Fix tests that are failing now because of this new change:
  * Fix tests for allow_value matcher
  * Fix tests for numericality matcher
  * Remove tests for numericality matcher + integer column
    * An integer column will typecast any non-integer value to an
      integer.
    * Because of the typecasting, our tests for the numericality matcher
      against an integer column don't quite work, because we can't
      really test what happens when the attribute is set to a
      non-integer value. Now that `allow_value` is more strict, we're
      getting a CouldNotSetAttributeError when attempting to do so.
    * The tests mentioned were originally added to ensure that we are
      handling RangeErrors that ActiveRecord used to emit. This doesn't
      happen anymore, so the tests aren't necessary anymore either.
  * Fix tests for acceptance matcher
  * Fix tests for absence matcher
2015-09-27 14:56:59 -06:00
Elliot Winkler ef80d2be42 Rewrite the tests for ComparisonMatcher
Why:

* The tests for ComparisonMatcher don't actually test
  ComparisonMatcher -- they test the comparison qualifiers for
  NumericalityMatcher. Not only is this misleading, but it also creates
  a problem as `validate_numericality_of` is no longer available in any
  example group, but only ones that have been specially tagged.

To satisfy the above:

* Ensure that the tests build an instance of ComparisonMatcher and test
  against that.
* Fix style issues with the tests.
* Additionally, add a #description method to ComparisonMatcher while
  we're at it.
2015-09-25 17:45:33 -06:00
Mauro George 32c0e62596 Drop support for RSpec 2 2015-09-22 13:12:46 -06:00
Elliot Winkler 9748869091 Add #on qualifier to numericality matcher
This is present in all other matchers through ValidationMatcher.
However, ValidateNumericalityOfMatcher does not inherit from
ValidationMatcher. (This happens to be okay, since
ValidateNumericalityOfMatcher contains submatchers and we have to make
sure to pass the context all the way through.)

Hat tip @sj26 for most of the content of this commit, as well as
@anujbiyani for another approach to this.
2015-04-03 23:28:29 -06:00
Adam89 4e4c546d0d Added strict option to Numericality matcher 2015-03-29 00:06:03 -06:00
Elliot Winkler 9ba21381d7 Handle RangeErrors emitted now in ActiveRecord 4.2
In Rails 4.2, ActiveRecord was changed such that if you attempt to set
an attribute to a value and that value is outside the range of the
column, then it will raise a RangeError. For instance, an integer column
with a limit of 2 (i.e. a smallint) only accepts values between -32768
and +32767.

This means that if you try to do any of these three things, a RangeError
could be raised:

* Use validate_numericality_of along with any of the comparison
  submatchers and a value that sits on either side of the boundary.
* Use allow_value with a value that sits outside the range.
* Use validates_inclusion_of against an integer column. (Here we attempt
  to set that column to a non-integer value to verify that the attribute
  does not allow said value. That value is really a string version of a
  large number, so if the column does not take large numbers then the
  matcher could blow up.)

Ancillary changes in this commit:

* Remove ValidationMessageFinder and ExceptionMessageFinder in favor of
  Validator, StrictValidator, and ValidatorWithCapturedRangeError.
* The allow_value matcher now uses an instance of Validator under the
  hood. StrictValidator and/or ValidatorWithCapturedRangeError may be
  mixed into the Validator object as needed.
2015-01-22 21:05:09 -07:00
Elliot Winkler 1766a09299 Refactor submatcher logic in numericality matcher
...and also ComparisonMatcher
2015-01-22 21:05:08 -07:00
Elliot Winkler 12cc7aaace Remove all Ruby-emitted warnings
Run RSpec tests with warnings enabled so we stay on top of this better
in the future.
2014-07-18 18:00:08 -06:00
Elliot Winkler 4d8faed1c4 Generate docs using YARD 2014-06-20 16:41:32 -06:00
Elliot Winkler c22d7c89e0 Extract examples in README to inline documentation 2014-06-20 16:41:27 -06:00
Elliot Winkler 91c6992912 Fix tests for ComparisonMatcher to use proper error messages 2014-04-26 11:27:28 -04:00
Elliot Winkler 23ff55ff5d v_n_o no longer treats numbers as floats
Essentially validate_numericality_of when used with a comparison
qualifier required you to use only_integer when the validation itself
had only_integer on it, because the boundaries of the comparison were
tested using small decimal numbers rather than whole numbers. This
change was introduced in bc110f7, but is obviously is a backward
incompatible change, so we are reverting it.
2014-04-26 11:27:24 -04:00
jmpage 36ed5ff7e5 Fix thoughtbot/shoulda-matchers#483 by using a less precise `diff_to_compare` value for large numbers in `ComparisonMatcher`. 2014-04-15 17:16:01 -04:00
Yukio Mizuta bc110f73b9 Update numericality comarison matcher more accurate 2014-02-26 19:48:29 -08:00
Yukio Mizuta 806716c79c Improve ValidateNumericalityOfMatcher document expression 2014-01-31 09:47:50 -07:00
Christopher Chow 1878e89a13 Update deprecated matcher protocol for RSpec 3.
The methods failure_message_for_should and failure_message_for_should_not
have been updated to failure_message and failure_message_negated respectively.
Alias to the old methods to remain backwards compatibility with RSpec 2.
2013-12-24 22:34:26 +11:00
Elliot Winkler 27c065ba55 Move ValidateNumericalityOf submatchers to a subfolder 2013-12-14 12:04:36 -07:00