File: class_spec.rb

package info (click to toggle)
ruby-extlib 0.9.16-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 576 kB
  • sloc: ruby: 7,014; makefile: 5
file content (158 lines) | stat: -rw-r--r-- 4,022 bytes parent folder | download | duplicates (3)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
require 'spec_helper'
require 'extlib/class'

class Grandparent
end

class Parent < Grandparent
end

class Child < Parent
end

class Parent
end

class Grandparent
  class_inheritable_accessor :last_name, :_attribute

  self._attribute = 1900
end

class ClassWithInheritableSymbolAccessor
  class_inheritable_accessor :symbol
  self.symbol = :foo
end

class ClassInheritingSymbolAccessor < ClassWithInheritableSymbolAccessor
end

describe Class, "#inheritable_accessor" do

  after :each do
    Grandparent.send(:remove_instance_variable, "@last_name") rescue nil
    Parent.send(:remove_instance_variable, "@last_name") rescue nil
    Child.send(:remove_instance_variable, "@last_name") rescue nil
  end

  it 'inherits from parent unless overriden' do
    Parent._attribute.should == 1900
    Child._attribute.should == 1900
  end

  it 'inherits from grandparent unless overriden' do
    Child._attribute.should == 1900
  end

  it "inherits even if the accessor is made after the inheritance" do
    Grandparent.last_name = "Merb"
    Parent.last_name.should == "Merb"
    Child.last_name.should == "Merb"
  end

  it "supports ||= to change a child" do
    Parent.last_name ||= "Merb"
    Grandparent.last_name.should == nil
    Parent.last_name.should == "Merb"
    Child.last_name.should == "Merb"
  end

  it "supports << to change a child when the parent is an Array" do
    Grandparent.last_name = ["Merb"]
    Parent.last_name << "Core"
    Grandparent.last_name.should == ["Merb"]
    Parent.last_name.should == ["Merb", "Core"]
  end

  it "supports ! methods on an Array" do
    Grandparent.last_name = %w(Merb Core)
    Parent.last_name.reverse!
    Grandparent.last_name.should == %w(Merb Core)
    Parent.last_name.should == %w(Core Merb)
  end

  it "support modifying a parent Hash" do
    Grandparent.last_name = {"Merb" => "name"}
    Parent.last_name["Core"] = "name"
    Parent.last_name.should == {"Merb" => "name", "Core" => "name"}
    Grandparent.last_name.should == {"Merb" => "name"}
  end

  it "supports hard-merging a parent Hash" do
    Grandparent.last_name = {"Merb" => "name"}
    Parent.last_name.merge!("Core" => "name")
    Parent.last_name.should == {"Merb" => "name", "Core" => "name"}
    Grandparent.last_name.should == {"Merb" => "name"}
  end

  it "supports changes to the parent even if the child has already been read" do
    Child.last_name
    Grandparent.last_name = "Merb"
    Child.last_name.should == "Merb"
  end

  it "handles nil being set midstream" do
    Child.last_name
    Parent.last_name = nil
    Grandparent.last_name = "Merb"
    Child.last_name.should == nil
  end

  it "handles false being used in Parent" do
    Child.last_name
    Parent.last_name = false
    Grandparent.last_name = "Merb"
    Child.last_name.should == false
  end

  it "handles the grandparent changing the value (as long as the child isn't read first)" do
    Grandparent.last_name = "Merb"
    Grandparent.last_name = "Core"
    Child.last_name.should == "Core"
  end

end

describe Class, "#inheritable_accessor (of type Symbol)" do

  it "should not raise" do
    lambda { ClassInheritingSymbolAccessor.symbol }.should_not raise_error(TypeError)
  end

end

#
# The bug that prompted this estoric spec was found in
# the wild when using dm-is-versioned with c_i_w.
#

module Plugin
  def self.included(base)
    base.class_eval do
      class_inheritable_writer :plugin_options
      class_inheritable_reader :plugin_options
      self.plugin_options = :foo
    end
  end
end

class Model
  def self.new
    model = Class.new
    model.send(:include, Plugin)
    model
  end

  include Plugin
  self.const_set("Version", Model.new)
end

describe Class, "#inheritable_accessor" do
  it "uses object_id for comparison" do
    Model.methods.map { |m| m.to_sym }.should be_include(:plugin_options)
    Model.plugin_options.should == :foo

    Model::Version.methods.map { |m| m.to_sym }.should be_include(:plugin_options)
    Model::Version.plugin_options.should == :foo
  end
end