File: internal_api_spec.rb

package info (click to toggle)
ruby-memo-wise 1.5.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 384 kB
  • sloc: ruby: 2,663; makefile: 4; sh: 4
file content (120 lines) | stat: -rw-r--r-- 3,908 bytes parent folder | download
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# frozen_string_literal: true

RSpec.describe MemoWise::InternalAPI do
  describe ".original_class_from_singleton" do
    subject { described_class.original_class_from_singleton(klass) }

    context "when klass is not a singleton class" do
      let(:klass) { String }

      it { expect { subject }.to raise_error(ArgumentError) }
    end

    # These test cases would fail due to a JRuby bug
    # Skipping to make build pass until the bug is fixed
    # https://github.com/jruby/jruby/issues/6896
    unless RUBY_PLATFORM == "java"
      context "when klass is a singleton class of an original class" do
        let(:klass) { original_class.singleton_class }

        context "when assigned to a constant" do
          let(:original_class) { String }

          it { is_expected.to eq(original_class) }
        end

        context "when singleton class #to_s convention is not followed" do
          include_context "with context for instance methods"

          let(:original_class) { class_with_memo }
          let(:klass) do
            super().tap do |sc|
              sc.define_singleton_method(:to_s) { "not following convention" }
            end
          end

          it { is_expected.to eq(original_class) }
        end
      end
    end
  end

  describe ".method_arguments" do
    subject { described_class.method_arguments(method) }

    include_context "with context for instance methods"

    {
      no_args: described_class::NONE,
      with_one_positional_arg: described_class::ONE_REQUIRED_POSITIONAL,
      with_one_keyword_arg: described_class::ONE_REQUIRED_KEYWORD,
      with_positional_args: described_class::MULTIPLE_REQUIRED,
      with_keyword_args: described_class::MULTIPLE_REQUIRED,
      with_positional_and_keyword_args: described_class::MULTIPLE_REQUIRED,
      with_optional_positional_args: described_class::SPLAT,
      with_positional_and_splat_args: described_class::SPLAT,
      with_optional_keyword_args: described_class::DOUBLE_SPLAT,
      with_keyword_and_double_splat_args: described_class::DOUBLE_SPLAT,
      with_optional_positional_and_keyword_args: described_class::SPLAT_AND_DOUBLE_SPLAT,
      with_positional_splat_keyword_and_double_splat_args: described_class::SPLAT_AND_DOUBLE_SPLAT
    }.each do |method_name, expected_result|
      context "when given #{method_name} method" do
        let(:method) { class_with_memo.instance_method(method_name) }

        it { is_expected.to eq expected_result }
      end
    end
  end

  describe ".args_str" do
    subject { described_class.args_str(method) }

    include_context "with context for instance methods"

    context "when called on an unexpected method type" do
      let(:method) { class_with_memo.instance_method(:no_args) }

      it "raises an ArgumentError" do
        expect { subject }.to raise_error(ArgumentError)
      end
    end
  end

  describe ".call_str" do
    subject { described_class.call_str(method) }

    include_context "with context for instance methods"

    context "when called on an unexpected method type" do
      let(:method) { class_with_memo.instance_method(:no_args) }

      it "raises an ArgumentError" do
        expect { subject }.to raise_error(ArgumentError)
      end
    end
  end

  describe ".key_str" do
    subject { described_class.key_str(method) }

    include_context "with context for instance methods"

    context "when called on an unexpected method type" do
      let(:method) { class_with_memo.instance_method(:no_args) }

      it "raises an ArgumentError" do
        expect { subject }.to raise_error(ArgumentError)
      end
    end
  end

  describe ".method_visibility" do
    subject { described_class.method_visibility(String, method_name) }

    context "when method_name is not a method on klass" do
      let(:method_name) { :not_a_method }

      it { expect { subject }.to raise_error(ArgumentError) }
    end
  end
end