2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "active_support/core_ext/array"
|
2015-03-10 01:00:37 -04:00
|
|
|
|
|
|
|
class ArrayInquirerTest < ActiveSupport::TestCase
|
|
|
|
def setup
|
2016-08-06 12:03:25 -04:00
|
|
|
@array_inquirer = ActiveSupport::ArrayInquirer.new([:mobile, :tablet, "api"])
|
2015-03-10 01:00:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_individual
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @array_inquirer, :mobile?
|
|
|
|
assert_predicate @array_inquirer, :tablet?
|
|
|
|
assert_not_predicate @array_inquirer, :desktop?
|
2015-03-10 01:00:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_any
|
|
|
|
assert @array_inquirer.any?(:mobile, :desktop)
|
|
|
|
assert @array_inquirer.any?(:watch, :tablet)
|
|
|
|
assert_not @array_inquirer.any?(:desktop, :watch)
|
|
|
|
end
|
|
|
|
|
2015-08-28 16:00:48 -04:00
|
|
|
def test_any_string_symbol_mismatch
|
2016-08-06 12:03:25 -04:00
|
|
|
assert @array_inquirer.any?("mobile")
|
2015-08-28 16:00:48 -04:00
|
|
|
assert @array_inquirer.any?(:api)
|
|
|
|
end
|
|
|
|
|
2015-03-10 01:00:37 -04:00
|
|
|
def test_any_with_block
|
|
|
|
assert @array_inquirer.any? { |v| v == :mobile }
|
|
|
|
assert_not @array_inquirer.any? { |v| v == :desktop }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_respond_to
|
|
|
|
assert_respond_to @array_inquirer, :development?
|
|
|
|
end
|
2015-03-30 12:38:13 -04:00
|
|
|
|
|
|
|
def test_inquiry
|
2016-08-06 12:03:25 -04:00
|
|
|
result = [:mobile, :tablet, "api"].inquiry
|
2015-03-30 12:38:13 -04:00
|
|
|
|
|
|
|
assert_instance_of ActiveSupport::ArrayInquirer, result
|
|
|
|
assert_equal @array_inquirer, result
|
|
|
|
end
|
2017-01-14 12:38:21 -05:00
|
|
|
|
|
|
|
def test_respond_to_fallback_to_array_respond_to
|
2017-01-14 13:26:16 -05:00
|
|
|
Array.class_eval do
|
|
|
|
def respond_to_missing?(name, include_private = false)
|
2017-01-14 12:38:21 -05:00
|
|
|
(name == :foo) || super
|
|
|
|
end
|
|
|
|
end
|
2017-01-14 13:26:16 -05:00
|
|
|
arr = ActiveSupport::ArrayInquirer.new([:x])
|
2017-01-14 12:38:21 -05:00
|
|
|
|
|
|
|
assert_respond_to arr, :can_you_hear_me?
|
|
|
|
assert_respond_to arr, :foo
|
|
|
|
assert_not_respond_to arr, :nope
|
2017-01-14 13:26:16 -05:00
|
|
|
ensure
|
2017-01-14 14:29:31 -05:00
|
|
|
Array.class_eval do
|
|
|
|
undef_method :respond_to_missing?
|
|
|
|
def respond_to_missing?(name, include_private = false)
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
2017-01-14 12:38:21 -05:00
|
|
|
end
|
2015-03-10 01:00:37 -04:00
|
|
|
end
|