File: constants_spec.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (113 lines) | stat: -rw-r--r-- 4,350 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
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../../../fixtures/constants', __FILE__)

describe "Module.constants" do
  it "returns an array of the names of all toplevel constants" do
    count = Module.constants.size
    module ConstantSpecsAdded
      CS_CONST1 = 1
    end
    Module.constants.size.should == count + 1
  end

  ruby_version_is "" ... "1.9" do
    it "returns an array of String names" do
      # This in NOT an exhaustive list
      Module.constants.should include("Array", "Bignum", "Class", "Comparable", "Dir",
                                      "Enumerable", "ENV", "Exception", "FalseClass",
                                      "File", "Fixnum", "Float", "Hash", "Integer", "IO",
                                      "Kernel", "Math", "Method", "Module", "NilClass",
                                      "Numeric", "Object", "Range", "Regexp", "String",
                                      "Symbol", "Thread", "Time", "TrueClass")
    end
  end

  ruby_version_is "1.9" do
    it "returns an array of Symbol names" do
      # This in NOT an exhaustive list
      Module.constants.should include(:Array, :Bignum, :Class, :Comparable, :Dir,
                                      :Enumerable, :ENV, :Exception, :FalseClass,
                                      :File, :Fixnum, :Float, :Hash, :Integer, :IO,
                                      :Kernel, :Math, :Method, :Module, :NilClass,
                                      :Numeric, :Object, :Range, :Regexp, :String,
                                      :Symbol, :Thread, :Time, :TrueClass)
    end

    it "returns Module's constants when given a parameter" do
      direct = Module.constants(false)
      indirect = Module.constants(true)
      module ConstantSpecsIncludedModule
        MODULE_CONSTANTS_SPECS_INDIRECT = :foo
      end

      class Module
        MODULE_CONSTANTS_SPECS_DIRECT = :bar
        include ConstantSpecsIncludedModule
      end
      (Module.constants(false) - direct).should == [:MODULE_CONSTANTS_SPECS_DIRECT]
      (Module.constants(true) - indirect).sort.should == [:MODULE_CONSTANTS_SPECS_DIRECT, :MODULE_CONSTANTS_SPECS_INDIRECT]
    end
  end
end

describe "Module#constants" do
  ruby_version_is "" ... "1.9" do
    it "returns an array of String names of all constants defined in the module" \
       "and all included modules" do
      ConstantSpecs::ContainerA.constants.sort.should == [
        "CS_CONST10", "CS_CONST23", "CS_CONST24", "CS_CONST5", "ChildA"
      ]
    end

    it "includes names of constants defined after a module is included" do
      ConstantSpecs::ModuleM::CS_CONST250 = :const250
      ConstantSpecs::ContainerA.constants.should include("CS_CONST250")
    end
  end

  ruby_version_is "1.9" do
    it "returns an array of Symbol names of all constants defined in the module" \
       "and all included modules" do
      ConstantSpecs::ContainerA.constants.sort.should == [
        :CS_CONST10, :CS_CONST23, :CS_CONST24, :CS_CONST5, :ChildA
      ]
    end

    it "returns all constants including inherited when passed true" do
      ConstantSpecs::ContainerA.constants(true).sort.should == [
        :CS_CONST10, :CS_CONST23, :CS_CONST24, :CS_CONST5, :ChildA
      ]
    end

    it "returns all constants including inherited when passed some object" do
      ConstantSpecs::ContainerA.constants(Object.new).sort.should == [
        :CS_CONST10, :CS_CONST23, :CS_CONST24, :CS_CONST5, :ChildA
      ]
    end

    it "includes names of constants defined after a module is included" do
      ConstantSpecs::ModuleM::CS_CONST251 = :const251
      ConstantSpecs::ContainerA.constants.should include(:CS_CONST251)
    end

    it "doesn't returns inherited constants when passed false" do
      ConstantSpecs::ContainerA.constants(false).sort.should == [
        :CS_CONST10, :CS_CONST23, :CS_CONST5, :ChildA
      ]
    end

    it "doesn't returns inherited constants when passed nil" do
      ConstantSpecs::ContainerA.constants(nil).sort.should == [
        :CS_CONST10, :CS_CONST23, :CS_CONST5, :ChildA
      ]
    end
  end

  ruby_version_is "1.9.3" do
    require File.expand_path('../fixtures/classes19', __FILE__)

    it "returns only public constants" do
      ModuleSpecs::PrivConstModule.constants.should == [:PUBLIC_CONSTANT]
    end
  end
end