Sha256: 92e648261ebc1ae5c11616ee76a1de7f7840ad6e86f2b149751b1d8a03bbf654

Contents?: true

Size: 1.1 KB

Versions: 9

Compression:

Stored size: 1.1 KB

Contents

require 'spec_helper'
require 'mspec/guards'
require 'mspec/helpers'

CONST = 2

module ConstLookupSpecs
  class A
    class B
      CONST = 1
    end

    class C; end

    class D
      def self.const_missing(const)
        A::B::CONST
      end
    end
  end
end

describe Kernel, "#const_lookup" do
  it "returns the constant specified by 'A::B'" do
    const_lookup("ConstLookupSpecs::A::B").should == ConstLookupSpecs::A::B
  end

  it "returns a regular constant specified without scoping" do
    const_lookup("ConstLookupSpecs").should == ConstLookupSpecs
  end

  it "returns an explicit toplevel constant" do
    const_lookup("::ConstLookupSpecs").should == ConstLookupSpecs
  end

  it "returns the constant from the proper scope" do
    const_lookup("ConstLookupSpecs::A::B::CONST").should == 1
  end

  it "raises NameError if the constant is not contained within the module's scope" do
    lambda {
      const_lookup("ConstLookupSpecs::A::C::CONST")
    }.should raise_error(NameError)
  end

  it "returns the value of #const_missing" do
    const_lookup("ConstLookupSpecs::A::D::CONST").should == 1
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mspec-1.9.1 spec/helpers/const_lookup_spec.rb
mspec-1.9.0 spec/helpers/const_lookup_spec.rb
mspec-1.8.0 spec/helpers/const_lookup_spec.rb
mspec-1.7.0 spec/helpers/const_lookup_spec.rb
mspec-1.6.0 spec/helpers/const_lookup_spec.rb
mspec-1.5.21 spec/helpers/const_lookup_spec.rb
mspec-1.5.20 spec/helpers/const_lookup_spec.rb
mspec-1.5.19 spec/helpers/const_lookup_spec.rb
mspec-1.5.18 spec/helpers/const_lookup_spec.rb