File: recursive_const_methods_spec.rb

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (56 lines) | stat: -rw-r--r-- 1,616 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
require 'rspec/support/recursive_const_methods'

module RSpec
  module Support
    RSpec.describe RecursiveConstMethods do
      include described_class

      module Foo
        class Parent
          UNDETECTED = 'Not seen when looking up constants in Bar'
        end

        class Bar < Parent
          VAL = 10
        end
      end

      describe '#recursive_const_defined?' do
        it 'finds constants' do
          const, _ = recursive_const_defined?('::RSpec::Support::Foo::Bar::VAL')

          expect(const).to eq(10)
        end

        it 'returns the fully qualified name of the constant' do
          _, name = recursive_const_defined?('::RSpec::Support::Foo::Bar::VAL')

          expect(name).to eq('RSpec::Support::Foo::Bar::VAL')
        end

        it 'does not find constants in ancestors' do
          expect(recursive_const_defined?('::RSpec::Support::Foo::Bar::UNDETECTED')).to be_falsy
        end

        it 'does not blow up on buggy classes that raise weird errors on `to_str`' do
          allow(Foo::Bar).to receive(:to_str).and_raise("boom!")
          const, _ = recursive_const_defined?('::RSpec::Support::Foo::Bar::VAL')

          expect(const).to eq(10)
        end
      end

      describe '#recursive_const_get' do
        it 'gets constants' do
          expect(recursive_const_get('::RSpec::Support::Foo::Bar::VAL')).to eq(10)
        end

        it 'does not get constants in ancestors' do
          expect do
            recursive_const_get('::RSpec::Support::Foo::Bar::UNDETECTED')
          end.to raise_error(NameError)
        end
      end
    end
  end
end