setsumei 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ module Setsumei
4
+ describe Build do
5
+ describe ".a(klass,from: hash_data)" do
6
+ let(:klass) { mock "class" }
7
+ let(:hash_data) { Hash.new }
8
+
9
+ let(:defined_attributes) { { an_attribute: attribute } }
10
+ let(:attribute) { mock "attribute" }
11
+ let(:object) { mock "object" }
12
+
13
+ before do
14
+ klass.stub(:defined_attributes).and_return(defined_attributes)
15
+ klass.stub(:new).and_return(object)
16
+ attribute.stub(:set_value_on)
17
+ end
18
+
19
+ subject { Build.a klass, from: hash_data }
20
+
21
+ it "should instantiate klass" do
22
+ klass.should_receive(:new).and_return(object)
23
+ subject
24
+ end
25
+ it "should set value on object using attribute" do
26
+ attribute.should_receive(:set_value_on).with(object, from_value_in: hash_data )
27
+ subject
28
+ end
29
+ it { should == object }
30
+ end
31
+
32
+ describe ".a(klass,from: hash_data) error conditions" do
33
+ context "klass doesn't have defined attributes" do
34
+ let(:klass) { mock "class" }
35
+ specify { expect { Build.a klass, from: {} }.to raise_error ArgumentError }
36
+ end
37
+
38
+ context "hash data isnt provided" do
39
+ let(:klass) { mock "class", defined_attributes: {} }
40
+ specify { expect { Build.a klass }.to raise_error ArgumentError }
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ module Setsumei
4
+ module Describable
5
+ describe BooleanAttribute do
6
+ its(:name) { should == nil }
7
+
8
+ describe ".named(name)" do
9
+ let(:name) { :my_boolean_field }
10
+
11
+ subject { BooleanAttribute.named(name) }
12
+
13
+ it { should be_a BooleanAttribute }
14
+ its(:name) { should == name }
15
+ end
16
+
17
+ describe "#value_for(pre_type_cast_value)" do
18
+ let(:boolean_attribute) { BooleanAttribute.new }
19
+
20
+ context "where the value is a boolean" do
21
+ specify { boolean_attribute.value_for(true).should === true }
22
+ specify { boolean_attribute.value_for(false).should === false }
23
+ end
24
+ context "where the value isn't a boolean" do
25
+ specify { boolean_attribute.value_for("false").should === false }
26
+ specify { boolean_attribute.value_for("true").should === true }
27
+ specify { boolean_attribute.value_for("True").should === true }
28
+ specify { boolean_attribute.value_for("1").should === true }
29
+ end
30
+ end
31
+
32
+ describe "#is_an_attribute_of_type?" do
33
+ let(:boolean_attribute) { BooleanAttribute.new }
34
+
35
+ subject { boolean_attribute.is_an_attribute_of_type? type }
36
+
37
+ context "where type is :boolean" do
38
+ let(:type) { :boolean }
39
+ it { should be_true }
40
+ end
41
+
42
+ context "where type is BooleanAttribute" do
43
+ let(:type) { BooleanAttribute }
44
+ it { should be_true }
45
+ end
46
+
47
+ context "where type is unknown" do
48
+ let(:type) { mock "an unknown type" }
49
+ it { should be_false }
50
+ end
51
+ end
52
+
53
+ describe "set_value_on object, from_value_in: hash" do
54
+ let(:hash) { Hash.new }
55
+ let(:key) { "key" }
56
+ let(:hash_keys) { mock "hash_keys" }
57
+ let(:value_in_hash) { mock "value_in_hash" }
58
+
59
+ let(:object) { mock "object", :my_boolean_attribute= => nil }
60
+
61
+ let(:boolean_attribute) { BooleanAttribute.named :my_boolean_attribute }
62
+ let(:converted_value) { mock "converted_value" }
63
+
64
+ before do
65
+ Build::Key.stub(:for).and_return(key)
66
+ hash[key] = value_in_hash
67
+ boolean_attribute.stub(:value_for).and_return(converted_value)
68
+ end
69
+
70
+ subject { boolean_attribute.set_value_on object, from_value_in: hash }
71
+
72
+ it "should detect the key it should use to retrieve the value from the hash" do
73
+ hash.should_receive(:keys).and_return(hash_keys)
74
+ Build::Key.should_receive(:for).with(:my_boolean_attribute, given: hash_keys ).and_return(key)
75
+ subject
76
+ end
77
+ it "should convert the value" do
78
+ boolean_attribute.should_receive(:value_for).with(value_in_hash).and_return(converted_value)
79
+ subject
80
+ end
81
+ it "should pass object a value to the attribute described by this class" do
82
+ object.should_receive(:my_boolean_attribute=).with(converted_value)
83
+ subject
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,108 @@
1
+ require 'spec_helper'
2
+
3
+ module Setsumei
4
+ module Describable
5
+ describe Collection do
6
+ describe "initialization" do
7
+ its(:klass) { should be_nil }
8
+ its(:options) { should be_nil }
9
+ end
10
+
11
+ describe ".of(klass,options)" do
12
+ let(:klass) { mock "klass" }
13
+ let(:options) { mock "options" }
14
+
15
+ subject { Collection.of klass, options }
16
+
17
+ it { should be_a Collection }
18
+ its(:klass) { should == klass }
19
+ its(:options) { should == options }
20
+ end
21
+
22
+ describe "#set_value_on(object, from_value_in: hash)" do
23
+ let(:object) { mock "object" }
24
+ let(:hash) { Hash.new }
25
+ let(:hash_keys) { mock "hash_keys" }
26
+ let(:key) { "aHashKey" }
27
+
28
+ let(:klass) { mock "a klass" }
29
+ let(:options) { Hash.new }
30
+ let(:collection) { Collection.of klass, options }
31
+
32
+ before do
33
+ hash.stub(:keys).and_return(hash_keys)
34
+ Build::Key.stub(:for).and_return(key)
35
+ hash[key] = []
36
+ Build.stub(:a)
37
+ end
38
+
39
+ subject { collection.set_value_on object, from_value_in: hash }
40
+
41
+ it "should detect they key it should use to retreive the values from the hash" do
42
+ hash.should_receive(:keys).and_return(hash_keys)
43
+ Build::Key.should_receive(:for).with( klass, given: hash_keys).and_return(key)
44
+ subject
45
+ end
46
+
47
+ context "single value" do
48
+ let(:single_value) { mock "single_value" }
49
+ let(:single_instance) { mock "single_instance" }
50
+
51
+ before do
52
+ hash[key] = single_value
53
+ Build.stub(:a).and_return single_instance
54
+ object.stub(:<<)
55
+ end
56
+
57
+ it "should build a single instance of klass" do
58
+ Build.should_receive(:a).with(klass, from: single_value).and_return(single_instance)
59
+ subject
60
+ end
61
+ it "should append this to the object" do
62
+ object.should_receive(:<<).with(single_instance)
63
+ subject
64
+ end
65
+ end
66
+ context "multiple values" do
67
+ let(:a_value) { mock "a_value" }
68
+ let(:another_value) { mock "another_value" }
69
+ let(:more_values) { mock "more_values" }
70
+
71
+ let(:first_instance) { mock "first_instance" }
72
+ let(:second_instance) { mock "second_instance" }
73
+ let(:final_instance) { mock "final_instance" }
74
+
75
+ before do
76
+ hash[key] = [a_value,another_value,more_values]
77
+ Build.stub(:a).and_return( first_instance, second_instance, final_instance )
78
+ object.stub(:<<)
79
+ end
80
+
81
+ it "should build multiple instances of klass" do
82
+ Build.should_receive(:a).with(klass, from: a_value)#and_return(first_instance)
83
+ Build.should_receive(:a).with(klass, from: another_value)#and_return(second_instance)
84
+ Build.should_receive(:a).with(klass, from: more_values)#and_return(final_instance)
85
+ subject
86
+ end
87
+ it "should append all of these to the object" do
88
+ object.should_receive(:<<).with(first_instance)
89
+ object.should_receive(:<<).with(second_instance)
90
+ object.should_receive(:<<).with(final_instance)
91
+ subject
92
+ end
93
+ end
94
+
95
+ context "options for collection specifiy within" do
96
+ before do
97
+ options[:within] = "SpecialKeyLocation"
98
+ end
99
+
100
+ it "should detect they key it should use to retreive the values from the hash" do
101
+ Build::Key.should_receive(:for).with( "SpecialKeyLocation", given: hash_keys).and_return(key)
102
+ subject
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ module Setsumei
4
+ module Describable
5
+ describe FloatAttribute do
6
+ its(:name) { should == nil }
7
+
8
+ describe ".named(name)" do
9
+ let(:name) { :my_float_field }
10
+
11
+ subject { FloatAttribute.named(name) }
12
+
13
+ it { should be_a FloatAttribute }
14
+ its(:name) { should == name }
15
+ end
16
+
17
+ describe "#value_for(pre_type_cast_value)" do
18
+ let(:float_attribute) { FloatAttribute.new }
19
+
20
+ subject { float_attribute.value_for pre_type_cast_value }
21
+
22
+ context "where the value is a float" do
23
+ specify { float_attribute.value_for(10.2).should == 10.2 }
24
+ end
25
+ context "where the value isn't a float" do
26
+ specify { float_attribute.value_for("10.2").should == 10.2 }
27
+ end
28
+ end
29
+
30
+ describe "#is_an_attribute_of_type?" do
31
+ let(:float_attribute) { FloatAttribute.new }
32
+
33
+ subject { float_attribute.is_an_attribute_of_type? type }
34
+
35
+ context "where type is :float" do
36
+ let(:type) { :float }
37
+ it { should be_true }
38
+ end
39
+
40
+ context "where type is FloatAttribute" do
41
+ let(:type) { FloatAttribute }
42
+ it { should be_true }
43
+ end
44
+
45
+ context "where type is unknown" do
46
+ let(:type) { mock "an unknown type" }
47
+ it { should be_false }
48
+ end
49
+ end
50
+
51
+ describe "set_value_on object, from_value_in: hash" do
52
+ let(:hash) { Hash.new }
53
+ let(:key) { "key" }
54
+ let(:hash_keys) { mock "hash_keys" }
55
+ let(:value_in_hash) { mock "value_in_hash" }
56
+
57
+ let(:object) { mock "object", :my_float_attribute= => nil }
58
+
59
+ let(:float_attribute) { FloatAttribute.named :my_float_attribute }
60
+ let(:converted_value) { mock "converted_value" }
61
+
62
+ before do
63
+ Build::Key.stub(:for).and_return(key)
64
+ hash[key] = value_in_hash
65
+ float_attribute.stub(:value_for).and_return(converted_value)
66
+ end
67
+
68
+ subject { float_attribute.set_value_on object, from_value_in: hash }
69
+
70
+ it "should detect the key it should use to retrieve the value from the hash" do
71
+ hash.should_receive(:keys).and_return(hash_keys)
72
+ Build::Key.should_receive(:for).with(:my_float_attribute, given: hash_keys ).and_return(key)
73
+ subject
74
+ end
75
+ it "should convert the value" do
76
+ float_attribute.should_receive(:value_for).with(value_in_hash).and_return(converted_value)
77
+ subject
78
+ end
79
+ it "should pass object a value to the attribute described by this class" do
80
+ object.should_receive(:my_float_attribute=).with(converted_value)
81
+ subject
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+
3
+ module Setsumei
4
+ module Describable
5
+ describe IntAttribute do
6
+ its(:name) { should == nil }
7
+
8
+ describe ".named(name)" do
9
+ let(:name) { :my_int_field }
10
+
11
+ subject { IntAttribute.named(name) }
12
+
13
+ it { should be_a IntAttribute }
14
+ its(:name) { should == name }
15
+ end
16
+
17
+ describe "#value_for(pre_type_cast_value)" do
18
+ let(:int_attribute) { IntAttribute.new }
19
+
20
+ subject { int_attribute.value_for pre_type_cast_value }
21
+
22
+ context "where the value is a int" do
23
+ specify { int_attribute.value_for(42).should == 42 }
24
+ end
25
+ context "where the value isn't a int" do
26
+ specify { int_attribute.value_for("10.6").should == 11 }
27
+ specify { int_attribute.value_for("10.4").should == 10 }
28
+ end
29
+ end
30
+
31
+ describe "#is_an_attribute_of_type?" do
32
+ let(:int_attribute) { IntAttribute.new }
33
+
34
+ subject { int_attribute.is_an_attribute_of_type? type }
35
+
36
+ context "where type is :int" do
37
+ let(:type) { :int }
38
+ it { should be_true }
39
+ end
40
+
41
+ context "where type is IntAttribute" do
42
+ let(:type) { IntAttribute }
43
+ it { should be_true }
44
+ end
45
+
46
+ context "where type is unknown" do
47
+ let(:type) { mock "an unknown type" }
48
+ it { should be_false }
49
+ end
50
+ end
51
+
52
+ describe "set_value_on object, from_value_in: hash" do
53
+ let(:hash) { Hash.new }
54
+ let(:key) { "key" }
55
+ let(:hash_keys) { mock "hash_keys" }
56
+ let(:value_in_hash) { mock "value_in_hash" }
57
+
58
+ let(:object) { mock "object", :my_int_attribute= => nil }
59
+
60
+ let(:int_attribute) { IntAttribute.named :my_int_attribute }
61
+ let(:converted_value) { mock "converted_value" }
62
+
63
+ before do
64
+ Build::Key.stub(:for).and_return(key)
65
+ hash[key] = value_in_hash
66
+ int_attribute.stub(:value_for).and_return(converted_value)
67
+ end
68
+
69
+ subject { int_attribute.set_value_on object, from_value_in: hash }
70
+
71
+ it "should detect the key it should use to retrieve the value from the hash" do
72
+ hash.should_receive(:keys).and_return(hash_keys)
73
+ Build::Key.should_receive(:for).with(:my_int_attribute, given: hash_keys ).and_return(key)
74
+ subject
75
+ end
76
+ it "should convert the value" do
77
+ int_attribute.should_receive(:value_for).with(value_in_hash).and_return(converted_value)
78
+ subject
79
+ end
80
+ it "should pass object a value to the attribute described by this class" do
81
+ object.should_receive(:my_int_attribute=).with(converted_value)
82
+ subject
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,105 @@
1
+ require 'spec_helper'
2
+
3
+ module Setsumei
4
+ module Describable
5
+ describe ObjectAttribute do
6
+ its(:name) { should == nil }
7
+ its(:klass) { should == Object }
8
+
9
+ describe ".named(name)" do
10
+ let(:name) { :my_object_field }
11
+ let(:my_klass) { Class.new }
12
+
13
+ subject { ObjectAttribute.named name, as_a: my_klass }
14
+
15
+ it { should be_a ObjectAttribute }
16
+ its(:name) { should == name }
17
+ its(:klass) { should == my_klass }
18
+
19
+ context "klass is left off" do
20
+ subject { ObjectAttribute.named name }
21
+ specify { expect { subject }.to raise_error ArgumentError }
22
+ end
23
+ end
24
+
25
+ describe "#value_for(pre_type_cast_value)" do
26
+ let(:klass) { mock "klass" }
27
+ let(:pre_type_cast_value) { { hash: "with_values" } }
28
+ let(:object_attribute) { ObjectAttribute.named :name, as_a: klass }
29
+
30
+ subject { object_attribute.value_for pre_type_cast_value }
31
+
32
+ it "should use the Builder to produce a object with klass" do
33
+ Build.should_receive(:a).with(klass, from: pre_type_cast_value)
34
+ subject
35
+ end
36
+
37
+ context "there is no data for this object" do
38
+ context "empty hash" do
39
+ let(:pre_type_cast_value) { {} }
40
+ it { should be_nil }
41
+ end
42
+ context "nil" do
43
+ let(:pre_type_cast_value) { nil }
44
+ it { should be_nil }
45
+ end
46
+ end
47
+ end
48
+
49
+ describe "#is_an_attribute_of_type?" do
50
+ let(:object_attribute) { ObjectAttribute.new }
51
+
52
+ subject { object_attribute.is_an_attribute_of_type? type }
53
+
54
+ context "where type is :object" do
55
+ let(:type) { :object }
56
+ it { should be_true }
57
+ end
58
+
59
+ context "where type is ObjectAttribute" do
60
+ let(:type) { ObjectAttribute }
61
+ it { should be_true }
62
+ end
63
+
64
+ context "where type is unknown" do
65
+ let(:type) { mock "an unknown type" }
66
+ it { should be_false }
67
+ end
68
+ end
69
+
70
+ describe "set_value_on object, from_value_in: hash" do
71
+ let(:hash) { Hash.new }
72
+ let(:key) { "key" }
73
+ let(:hash_keys) { mock "hash_keys" }
74
+ let(:value_in_hash) { mock "value_in_hash" }
75
+
76
+ let(:object) { mock "object", :my_object_attribute= => nil }
77
+
78
+ let(:object_attribute) { ObjectAttribute.named :my_object_attribute, as_a: mock("object_klass") }
79
+ let(:converted_value) { mock "converted_value" }
80
+
81
+ before do
82
+ Build::Key.stub(:for).and_return(key)
83
+ hash[key] = value_in_hash
84
+ object_attribute.stub(:value_for).and_return(converted_value)
85
+ end
86
+
87
+ subject { object_attribute.set_value_on object, from_value_in: hash }
88
+
89
+ it "should detect the key it should use to retrieve the value from the hash" do
90
+ hash.should_receive(:keys).and_return(hash_keys)
91
+ Build::Key.should_receive(:for).with(:my_object_attribute, given: hash_keys ).and_return(key)
92
+ subject
93
+ end
94
+ it "should convert the value" do
95
+ object_attribute.should_receive(:value_for).with(value_in_hash).and_return(converted_value)
96
+ subject
97
+ end
98
+ it "should pass object a value to the attribute described by this class" do
99
+ object.should_receive(:my_object_attribute=).with(converted_value)
100
+ subject
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end