spec/parameters_spec.rb in parameters-0.2.3 vs spec/parameters_spec.rb in parameters-0.3.0
- old
+ new
@@ -22,45 +22,51 @@
new_class.should have_param(:mixin_var)
end
end
context "in a Class" do
+ subject { TestParameters }
+
+ let(:inherited_class) { InheritedParameters }
+
it "should provide parameters" do
- TestParameters.params.should_not be_empty
+ subject.params.should_not be_empty
end
it "should have default values for parameters" do
- TestParameters.param_value(:var_with_default).should == 'thing'
+ subject.param_value(:var_with_default).should == 'thing'
end
it "should provide class methods for paremters" do
- TestParameters.var = 1
- TestParameters.var.should == 1
+ subject.var = 1
+ subject.var.should == 1
end
it "should inherite the super-classes parameters" do
- InheritedParameters.has_param?(:var).should == true
- InheritedParameters.has_param?(:child_var).should == true
+ inherited_class.has_param?(:var).should == true
+ inherited_class.has_param?(:child_var).should == true
end
it "should provide direct access to the parameter objects" do
- param = TestParameters.get_param(:var)
+ param = subject.get_param(:var)
param.should_not be_nil
param.name.should == :var
end
it "should raise a ParamNotFound exception when directly accessing non-existent parameter objects" do
- lambda { TestParameters.get_param(:unknown) }.should raise_error(Parameters::ParamNotFound)
+ lambda {
+ subject.get_param(:unknown)
+ }.should raise_error(Parameters::ParamNotFound)
end
it "should provide descriptions for parameters" do
- TestParameters.describe_param(:var).should_not be_empty
+ subject.describe_param(:var).should_not be_empty
end
it "should be able to initialize parameters" do
- obj = TestParameters.new
+ obj = subject.new
obj.params.should_not be_empty
end
it "should set initialize parameters with initial nil values" do
@@ -82,140 +88,157 @@
obj.var.should == false
obj.var_with_default.should == false
end
it "should be able to create an object with initial parameter values" do
- obj = TestParameters.new(:var => 2, :var_with_default => 'stuff')
+ obj = subject.new(:var => 2, :var_with_default => 'stuff')
obj.var.should == 2
obj.var_with_default.should == 'stuff'
end
end
context "in an Object" do
- before(:each) do
- @test = TestParameters.new
- @test_inherited = InheritedParameters.new
- end
+ subject { TestParameters.new }
+ let(:inherited_params) { InheritedParameters.new }
+
it "should provide direct access to all parameters" do
- @test.params[:var].should_not be_nil
- @test.params[:var_with_default].should_not be_nil
+ subject.params[:var].should_not be_nil
+ subject.params[:var_with_default].should_not be_nil
end
it "should have default values for parameters" do
- @test.param_value(:var_with_default).should == 'thing'
+ subject.param_value(:var_with_default).should == 'thing'
end
it "should provide instance methods for parameters" do
- @test.var = 2
- @test.var.should == 2
+ subject.var = 2
+ subject.var.should == 2
end
it "should set instance variables for paramters" do
- @test.instance_variable_get(:@var_with_default).should == 'thing'
+ subject.instance_variable_get(:@var_with_default).should == 'thing'
- @test.var = 3
- @test.instance_variable_get(:@var).should == 3
+ subject.var = 3
+ subject.instance_variable_get(:@var).should == 3
end
it "should allow using lambdas for the default values of parameters" do
- test2 = TestParameters.new
+ other_params = TestParameters.new
- @test.var_with_lambda.should_not == test2.var_with_lambda
+ subject.var_with_lambda.should_not == other_params.var_with_lambda
end
it "should contain the parameters from all ancestors" do
- @test_inherited.has_param?(:var).should == true
- @test_inherited.has_param?(:child_var).should == true
+ inherited_params.has_param?(:var).should == true
+ inherited_params.has_param?(:child_var).should == true
end
it "should provide direct access to the parameter objects" do
- @param = @test.get_param(:var)
+ @param = subject.get_param(:var)
@param.should_not be_nil
@param.name.should == :var
end
it "should raise a ParamNotFound exception when directly accessing non-existent parameter objects" do
- lambda { @test.get_param(:unknown) }.should raise_error(Parameters::ParamNotFound)
+ lambda {
+ subject.get_param(:unknown)
+ }.should raise_error(Parameters::ParamNotFound)
end
+ it "should allow setting arbitrary parameters" do
+ subject.set_param(:var,2)
+
+ subject.get_param(:var).value.should == 2
+ end
+
+ it "should raise a ParamNotFound exception when setting non-existent parameters" do
+ lambda {
+ subject.set_param(:unknown,2)
+ }.should raise_error(Parameters::ParamNotFound)
+ end
+
it "should allow for setting parameters en-mass" do
- @test.params = {:var => 3, :var_with_default => 7}
+ subject.params = {:var => 3, :var_with_default => 7}
- @test.param_value(:var).should == 3
- @test.param_value(:var_with_default).should == 7
+ subject.param_value(:var).should == 3
+ subject.param_value(:var_with_default).should == 7
end
it "should allow for setting parameters en-mass from another object" do
obj = TestParameters.new(:var => 5, :var_with_default => 'hello')
- @test.params = obj.params
+ subject.params = obj.params
- @test.var.should == 5
- @test.var_with_default.should == 'hello'
+ subject.var.should == 5
+ subject.var_with_default.should == 'hello'
end
it "should allow for setting parameters en-mass from another class" do
- @test.params = OtherParameters.params
+ subject.params = OtherParameters.params
- @test.var.should be_nil
- @test.var_with_default.should == 'other'
+ subject.var.should be_nil
+ subject.var_with_default.should == 'other'
end
it "should provide descriptions for parameters" do
- @test.describe_param(:var).should_not be_empty
+ subject.describe_param(:var).should_not be_empty
end
it "should require that certain parameters have non nil values" do
lambda {
- @test.instance_eval { require_params(:var_without_default) }
+ subject.instance_eval { require_params(:var_without_default) }
}.should raise_error(Parameters::MissingParam)
end
end
describe "runtime" do
+ subject { TestParameters.new }
+
before(:each) do
- @test = TestParameters.new
+ subject.parameter :new_param
- @test.parameter :new_param
- @test.parameter :new_param_with_default,
- :default => 5
- @test.parameter :new_param_with_lambda,
- :default => lambda { |obj| obj.new_param_with_default + 2 }
+ subject.parameter :new_param_with_default,
+ :default => 5
+
+ subject.parameter :new_param_with_lambda,
+ :default => lambda { |obj|
+ obj.new_param_with_default + 2
+ }
end
it "should allow for the addition of parameters" do
- @test.has_param?(:new_param).should == true
+ subject.has_param?(:new_param).should == true
end
it "should provide direct access to all parameters" do
- @test.params[:new_param].should_not be_nil
- @test.params[:new_param_with_default].should_not be_nil
- @test.params[:new_param_with_lambda].should_not be_nil
+ subject.params[:new_param].should_not be_nil
+ subject.params[:new_param_with_default].should_not be_nil
+ subject.params[:new_param_with_lambda].should_not be_nil
end
it "should add reader methods for parameters" do
- @test.new_param.should be_nil
+ subject.new_param.should be_nil
end
it "should add writer methods for parameters" do
- @test.new_param = 10
- @test.new_param.should == 10
+ subject.new_param = 10
+ subject.new_param.should == 10
end
it "should set the instance variables of parameters" do
- @test.instance_variable_get(:@new_param_with_default).should == 5
+ subject.instance_variable_get(:@new_param_with_default).should == 5
- @test.new_param_with_default = 10
- @test.instance_variable_get(:@new_param_with_default).should == 10
+ subject.new_param_with_default = 10
+ subject.instance_variable_get(:@new_param_with_default).should == 10
end
it "should initialize parameters with default values" do
- @test.new_param_with_default.should == 5
+ subject.new_param_with_default.should == 5
end
it "should initialize pamareters with default lambda values" do
- @test.new_param_with_lambda.should == 7
+ subject.new_param_with_lambda.should == 7
end
end
end