Handle ensure_inclusion_of for boolean columns
Currently, using `ensure_inclusion_of` against a boolean column doesn't
work. We can assert that the column allows boolean values, but what
about values that should be rejected? Well, it depends on what you give
to `ensure_inclusion_of`. Here's how it works now:
* `ensure_inclusion_of(:attr).in_array([true])` asserts that false is
rejected
* `ensure_inclusion_of(:attr).in_array([false])` asserts that true is
rejected
* `ensure_inclusion_of(:attr).in_array([true, false])` does not assert
that anything is rejected, instead informing the developer how
this sort of expectation is not fully testable (anything other than
true, false, or nil will be converted to false).
* `ensure_inclusion_of(:attr).in_array([nil])`, when the column is
nullable, does not assert that anything is rejected, either, also
printing a warning
* `ensure_inclusion_of(:attr).in_array([nil])`, when the column is
non-nullable, raises an error because this expectation is not testable
in any way, as setting a boolean column to nil this way will get
converted to false.
2014-01-21 18:46:23 -05:00
|
|
|
module Shoulda
|
|
|
|
module Matchers
|
2019-02-16 05:01:35 -05:00
|
|
|
# @private
|
2014-07-18 20:58:52 -04:00
|
|
|
TERMINAL_MAX_WIDTH = 72
|
|
|
|
|
2014-01-23 13:07:36 -05:00
|
|
|
# @private
|
2014-07-18 20:58:52 -04:00
|
|
|
def self.warn(message)
|
2020-09-04 10:38:31 -04:00
|
|
|
header = 'Warning from shoulda-matchers:'
|
|
|
|
divider = '*' * TERMINAL_MAX_WIDTH
|
2015-10-08 01:28:45 -04:00
|
|
|
wrapped_message = word_wrap(message)
|
2014-07-18 20:58:52 -04:00
|
|
|
full_message = [
|
|
|
|
divider,
|
|
|
|
[header, wrapped_message.strip].join("\n\n"),
|
2020-09-04 11:13:05 -04:00
|
|
|
divider,
|
2014-07-18 20:58:52 -04:00
|
|
|
].join("\n")
|
|
|
|
|
|
|
|
Kernel.warn(full_message)
|
|
|
|
end
|
|
|
|
|
|
|
|
# @private
|
|
|
|
def self.warn_about_deprecated_method(old_method, new_method)
|
|
|
|
warn <<EOT
|
|
|
|
#{old_method} is deprecated and will be removed in the next major
|
|
|
|
release. Please use #{new_method} instead.
|
|
|
|
EOT
|
|
|
|
end
|
Handle ensure_inclusion_of for boolean columns
Currently, using `ensure_inclusion_of` against a boolean column doesn't
work. We can assert that the column allows boolean values, but what
about values that should be rejected? Well, it depends on what you give
to `ensure_inclusion_of`. Here's how it works now:
* `ensure_inclusion_of(:attr).in_array([true])` asserts that false is
rejected
* `ensure_inclusion_of(:attr).in_array([false])` asserts that true is
rejected
* `ensure_inclusion_of(:attr).in_array([true, false])` does not assert
that anything is rejected, instead informing the developer how
this sort of expectation is not fully testable (anything other than
true, false, or nil will be converted to false).
* `ensure_inclusion_of(:attr).in_array([nil])`, when the column is
nullable, does not assert that anything is rejected, either, also
printing a warning
* `ensure_inclusion_of(:attr).in_array([nil])`, when the column is
non-nullable, raises an error because this expectation is not testable
in any way, as setting a boolean column to nil this way will get
converted to false.
2014-01-21 18:46:23 -05:00
|
|
|
end
|
|
|
|
end
|