Permalink
Browse files

Using fixtures for class variables specs.

  • Loading branch information...
1 parent 51ea0cd commit 945bab5add41b469432b5a827e2a09ba18872fc7 @febuiles febuiles committed Jan 5, 2009
Showing with 64 additions and 59 deletions.
  1. +42 −0 fixtures/class_variables.rb
  2. +22 −59 language/class_variable_spec.rb
@@ -0,0 +1,42 @@
+module ClassVariablesSpec
+
+ class ClassA
+ @@cvar_a = :cvar_a
+
+ def cvar_a
+ @@cvar_a
+ end
+
+ def cvar_a=(val)
+ @@cvar_a = val
+ end
+ end
+
+ class ClassB < ClassA; end
+
+ # Extended in ClassC
+ module ModuleM
+ @@cvar_m = :value
+
+ def cvar_m
+ @@cvar_m
+ end
+
+ def cvar_m=(val)
+ @@cvar_m = val
+ end
+ end
+
+
+ class ClassC
+ extend ModuleM
+
+ def self.cvar_defined?
+ self.class_variable_defined?(:@@cvar)
+ end
+
+ def self.cvar_c=(val)
+ @@cvar = val
+ end
+ end
+end
@@ -1,81 +1,44 @@
require File.dirname(__FILE__) + '/../spec_helper'
-
-module ClassVariableSpec
- class A
- @@a_cvar = :a_cvar
- def a_cvar() @@a_cvar end
- def a_cvar=(val) @@a_cvar = val end
- end
-
- class B < A; end
-
- module M
- @@cvar = :value
-
- def cvar
- @@cvar
- end
-
- def cvar=(val)
- @@cvar = val
- end
- end
-
- class C
- extend M
-
- def self.cvar?
- self.class_variable_defined?(:@@cvar)
- end
-
- def self.cvar2=(val)
- @@cvar = val
- end
- end
-end
+require File.dirname(__FILE__) + '/../fixtures/class_variables'
describe "A Class Variable" do
it "can be accessed from a subclass" do
- ClassVariableSpec::B.new.a_cvar.should == :a_cvar
+ ClassVariablesSpec::ClassB.new.cvar_a.should == :cvar_a
end
- it "sets the value in the superclass from the subclass" do
- a = ClassVariableSpec::A.new
- b = ClassVariableSpec::B.new
- b.a_cvar = :new_val
-
- [a.a_cvar].should == [:new_val]
- end
+ it "is set in the superclass" do
+ a = ClassVariablesSpec::ClassA.new
+ b = ClassVariablesSpec::ClassB.new
+ b.cvar_a = :new_val
- it "retrieves the value from the place it is defined" do
- [ClassVariableSpec::C.cvar.should] == [:value]
+ a.cvar_a.should == :new_val
end
end
describe "A Class Variable defined in a Module" do
- it "can be accessed from Classes that are extended by the Module" do
- ClassVariableSpec::C.cvar.should == :value
+ it "can be accessed from Classes that extend the Module" do
+ ClassVariablesSpec::ClassC.cvar_m.should == :value
end
it "is not defined in these Classes" do
- ClassVariableSpec::C.cvar?.should be_false
+ ClassVariablesSpec::ClassC.cvar_defined?.should be_false
end
- it "only updates the Class Variable in the Module when using a Method defined in the Module" do
- ClassVariableSpec::C.cvar = "new value"
- ClassVariableSpec::C.cvar.should == "new value"
+ it "is only updated in the Module a Method defined in the Module is used" do
+ ClassVariablesSpec::ClassC.cvar_m = "new value"
+ ClassVariablesSpec::ClassC.cvar_m.should == "new value"
- ClassVariableSpec::C.cvar?.should be_false
+ ClassVariablesSpec::ClassC.cvar_defined?.should be_false
end
- it "does updates the Class Variable in the Module but in the Class when using a Method defined in the Class" do
- ClassVariableSpec::C.cvar2 = "new value"
- ClassVariableSpec::C.cvar?.should be_true
+ it "is updated in the Class when a Method defined in the Class is used" do
+ ClassVariablesSpec::ClassC.cvar_c = "new value"
+ ClassVariablesSpec::ClassC.cvar_defined?.should be_true
end
-
- it "allows to retrieve the Class Variable in the Class with methods from the Module" do
- ClassVariableSpec::C.cvar2 = "new value"
-
- ClassVariableSpec::C.cvar.should == "new value"
+
+ it "can be accessed inside the Class using the Module methods" do
+ ClassVariablesSpec::ClassC.cvar_c = "new value"
+
+ ClassVariablesSpec::ClassC.cvar_m.should == "new value"
end
end

0 comments on commit 945bab5

Please sign in to comment.