File: class_variable_defined_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 (82 lines) | stat: -rw-r--r-- 2,997 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
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

describe "Module#class_variable_defined?" do
  it "returns true if a class variable with the given name is defined in self" do
    c = Class.new { class_variable_set :@@class_var, "test" }
    c.class_variable_defined?(:@@class_var).should == true
    c.class_variable_defined?("@@class_var").should == true
    c.class_variable_defined?(:@@no_class_var).should == false
    c.class_variable_defined?("@@no_class_var").should == false
    ModuleSpecs::CVars.class_variable_defined?("@@cls").should == true
  end

  it "returns true if a class variable with the given name is defined in the metaclass" do
    ModuleSpecs::CVars.class_variable_defined?("@@meta").should == true
  end

  it "returns true if the class variable is defined in a metaclass" do
    obj = mock("metaclass class variable")
    meta = obj.singleton_class
    meta.send :class_variable_set, :@@var, 1
    meta.send(:class_variable_defined?, :@@var).should be_true
  end

  it "returns false if the class variable is not defined in a metaclass" do
    obj = mock("metaclass class variable")
    meta = obj.singleton_class
    meta.class_variable_defined?(:@@var).should be_false
  end

  it "returns true if a class variables with the given name is defined in an included module" do
    c = Class.new { include ModuleSpecs::MVars }
    c.class_variable_defined?("@@mvar").should == true
  end

  it "returns false if a class variables with the given name is defined in an extended module" do
    c = Class.new
    c.extend ModuleSpecs::MVars
    c.class_variable_defined?("@@mvar").should == false
  end

  ruby_version_is ""..."1.9" do
    not_compliant_on :rubinius do
      it "accepts Fixnums for class variables" do
        c = Class.new { class_variable_set :@@class_var, "test" }
        c.class_variable_defined?(:@@class_var.to_i).should == true
        c.class_variable_defined?(:@@no_class_var.to_i).should == false
      end
    end
  end

  it "raises a NameError when the given name is not allowed" do
    c = Class.new

    lambda {
      c.class_variable_defined?(:invalid_name)
    }.should raise_error(NameError)

    lambda {
      c.class_variable_defined?("@invalid_name")
    }.should raise_error(NameError)
  end

  it "converts a non string/symbol/fixnum name to string using to_str" do
    c = Class.new { class_variable_set :@@class_var, "test" }
    (o = mock('@@class_var')).should_receive(:to_str).and_return("@@class_var")
    c.class_variable_defined?(o).should == true
  end

  it "raises a TypeError when the given names can't be converted to strings using to_str" do
    c = Class.new { class_variable_set :@@class_var, "test" }
    o = mock('123')
    lambda {
      c.class_variable_defined?(o)
    }.should raise_error(TypeError)

    o.should_receive(:to_str).and_return(123)
    lambda {
      c.class_variable_defined?(o)
    }.should raise_error(TypeError)
  end
end