PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Languages/Ruby/Tests/mspec/rubyspec/language/metaclass_spec.rb

http://github.com/IronLanguages/main
Ruby | 123 lines | 103 code | 20 blank | 0 comment | 13 complexity | 795f17a7099168363b3e95061c2b908f MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. require File.dirname(__FILE__) + '/../spec_helper'
  2. require File.dirname(__FILE__) + '/../fixtures/class'
  3. describe "self in a metaclass body (class << obj)" do
  4. it "is TrueClass for true" do
  5. class << true; self; end.should == TrueClass
  6. end
  7. it "is FalseClass for false" do
  8. class << false; self; end.should == FalseClass
  9. end
  10. it "is NilClass for nil" do
  11. class << nil; self; end.should == NilClass
  12. end
  13. it "raises a TypeError for numbers" do
  14. lambda { class << 1; self; end }.should raise_error(TypeError)
  15. end
  16. it "raises a TypeError for symbols" do
  17. lambda { class << :symbol; self; end }.should raise_error(TypeError)
  18. end
  19. it "is a singleton Class instance" do
  20. cls = class << mock('x'); self; end
  21. cls.is_a?(Class).should == true
  22. cls.should_not equal(Object)
  23. end
  24. end
  25. describe "A constant on a metaclass" do
  26. before(:each) do
  27. @object = Object.new
  28. class << @object
  29. CONST = self
  30. end
  31. end
  32. it "can be accessed after the metaclass body is reopened" do
  33. class << @object
  34. CONST.should == self
  35. end
  36. end
  37. it "can be accessed via self::CONST" do
  38. class << @object
  39. self::CONST.should == self
  40. end
  41. end
  42. it "can be accessed via const_get" do
  43. class << @object
  44. const_get(:CONST).should == self
  45. end
  46. end
  47. it "is not defined on the object's class" do
  48. @object.class.const_defined?(:CONST).should be_false
  49. end
  50. it "is not defined in the metaclass opener's scope" do
  51. class << @object
  52. CONST
  53. end
  54. lambda { CONST }.should raise_error(NameError)
  55. end
  56. it "cannot be accessed via object::CONST" do
  57. lambda do
  58. @object::CONST
  59. end.should raise_error(TypeError)
  60. end
  61. it "raises a NameError for anonymous_module::CONST" do
  62. @object = Class.new
  63. class << @object
  64. CONST = 100
  65. end
  66. lambda do
  67. @object::CONST
  68. end.should raise_error(NameError)
  69. end
  70. ruby_version_is ""..."1.9" do
  71. it "appears in the metaclass constant list" do
  72. constants = class << @object; constants; end
  73. constants.should include("CONST")
  74. end
  75. it "does not appear in the object's class constant list" do
  76. @object.class.constants.should_not include("CONST")
  77. end
  78. end
  79. ruby_version_is "1.9" do
  80. it "appears in the metaclass constant list" do
  81. constants = class << @object; constants; end
  82. constants.should include(:CONST)
  83. end
  84. it "does not appear in the object's class constant list" do
  85. @object.class.constants.should_not include(:CONST)
  86. end
  87. end
  88. it "is not preserved when the object is duped" do
  89. @object = @object.dup
  90. lambda do
  91. class << @object; CONST; end
  92. end.should raise_error(NameError)
  93. end
  94. it "is preserved when the object is cloned" do
  95. @object = @object.clone
  96. class << @object
  97. CONST.should_not be_nil
  98. end
  99. end
  100. end