File: array_inquirer_test.rb

package info (click to toggle)
rails 2%3A6.0.3.7%2Bdfsg-2%2Bdeb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 70,976 kB
  • sloc: ruby: 271,623; javascript: 19,043; yacc: 46; sql: 43; makefile: 28; sh: 18
file content (63 lines) | stat: -rw-r--r-- 1,650 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# frozen_string_literal: true

require "abstract_unit"
require "active_support/core_ext/array"

class ArrayInquirerTest < ActiveSupport::TestCase
  def setup
    @array_inquirer = ActiveSupport::ArrayInquirer.new([:mobile, :tablet, "api"])
  end

  def test_individual
    assert_predicate @array_inquirer, :mobile?
    assert_predicate @array_inquirer, :tablet?
    assert_not_predicate @array_inquirer, :desktop?
  end

  def test_any
    assert @array_inquirer.any?(:mobile, :desktop)
    assert @array_inquirer.any?(:watch, :tablet)
    assert_not @array_inquirer.any?(:desktop, :watch)
  end

  def test_any_string_symbol_mismatch
    assert @array_inquirer.any?("mobile")
    assert @array_inquirer.any?(:api)
  end

  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

  def test_inquiry
    result = [:mobile, :tablet, "api"].inquiry

    assert_instance_of ActiveSupport::ArrayInquirer, result
    assert_equal @array_inquirer, result
  end

  def test_respond_to_fallback_to_array_respond_to
    Array.class_eval do
      def respond_to_missing?(name, include_private = false)
        (name == :foo) || super
      end
    end
    arr = ActiveSupport::ArrayInquirer.new([:x])

    assert_respond_to arr, :can_you_hear_me?
    assert_respond_to arr, :foo
    assert_not_respond_to arr, :nope
  ensure
    Array.class_eval do
      undef_method :respond_to_missing?
      def respond_to_missing?(name, include_private = false)
        super
      end
    end
  end
end