doodle 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,110 +1,98 @@
1
- require 'lib/spec_helper'
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
2
 
3
3
  describe Doodle::Attribute, 'basics' do
4
- after :each do
5
- undefine_const(:Bar)
6
- undefine_const(:Foo)
7
- end
8
- before(:each) do
9
- raise_if_defined(:Foo, :Bar)
10
-
11
- class Foo
12
- include Doodle::Helper
13
- has :name, :default => 'Hello'
14
- class << self
15
- has :metadata
4
+ temporary_constants :Foo, :Bar do
5
+ before(:each) do
6
+ class Foo
7
+ include Doodle::Helper
8
+ has :name, :default => 'Hello'
9
+ class << self
10
+ has :metadata
11
+ end
16
12
  end
17
- end
18
- class Bar < Foo
19
- has :info
20
- class << self
21
- has :doc
13
+ class Bar < Foo
14
+ has :info
15
+ class << self
16
+ has :doc
17
+ end
22
18
  end
23
- end
24
19
 
25
- @foo = Foo.new
26
- @bar = Bar.new :info => 'Hi'
27
- end
20
+ @foo = Foo.new
21
+ @bar = Bar.new :info => 'Hi'
22
+ end
28
23
 
29
- it 'should have attribute :name with default defined' do
30
- @foo.attributes[:name].default.should == 'Hello'
31
- end
24
+ it 'should have attribute :name with default defined' do
25
+ @foo.attributes[:name].default.should == 'Hello'
26
+ end
32
27
 
33
- it 'should have default name' do
34
- @foo.name.should == 'Hello'
35
- end
28
+ it 'should have default name' do
29
+ @foo.name.should == 'Hello'
30
+ end
36
31
 
37
- it 'should not have an instance variable for a default' do
38
- @foo.instance_variables.include?('@name').should == false
39
- end
32
+ it 'should not have an instance variable for a default' do
33
+ @foo.instance_variables.include?('@name').should == false
34
+ end
40
35
 
41
- it 'should have name required == false (because has default)' do
42
- @foo.attributes[:name].required?.should == false
43
- end
36
+ it 'should have name required == false (because has default)' do
37
+ @foo.attributes[:name].required?.should == false
38
+ end
44
39
 
45
- it 'should have info required == true' do
46
- @bar.attributes[:info].required?.should == true
47
- end
40
+ it 'should have info required == true' do
41
+ @bar.attributes[:info].required?.should == true
42
+ end
48
43
 
49
- it 'should have name.optional? == true (because has default)' do
50
- @foo.attributes[:name].optional?.should == true
51
- end
44
+ it 'should have name.optional? == true (because has default)' do
45
+ @foo.attributes[:name].optional?.should == true
46
+ end
52
47
 
53
- it 'should inherit attribute from parent' do
54
- @bar.attributes[:name].should == @foo.attributes[:name]
55
- end
48
+ it 'should inherit attribute from parent' do
49
+ @bar.attributes[:name].should == @foo.attributes[:name]
50
+ end
56
51
 
57
- it 'should have info.optional? == false' do
58
- @bar.attributes[:info].optional?.should == false
59
- end
52
+ it 'should have info.optional? == false' do
53
+ @bar.attributes[:info].optional?.should == false
54
+ end
60
55
 
61
- it "should have parents in correct order" do
62
- Bar.parents.should == [Foo, Object]
63
- end
56
+ it "should have parents in correct order" do
57
+ Bar.parents.should == [Foo, Object]
58
+ end
64
59
 
65
- it "should have Bar's singleton parents in reverse order of definition" do
66
- @bar.singleton_class.parents.should == [Bar.singleton_class.singleton_class, Bar.singleton_class, Foo.singleton_class]
67
- end
60
+ it "should have Bar's singleton parents in reverse order of definition" do
61
+ @bar.singleton_class.parents.should == [Bar.singleton_class.singleton_class, Bar.singleton_class, Foo.singleton_class]
62
+ end
68
63
 
69
- it 'should have inherited singleton local_attributes in order of definition' do
70
- @bar.singleton_class.class_eval { collect_inherited(:local_attributes).map { |x| x[0]} }.should == [:metadata, :doc]
71
- end
64
+ it 'should have inherited singleton local_attributes in order of definition' do
65
+ @bar.singleton_class.class_eval { collect_inherited(:local_attributes).map { |x| x[0]} }.should == [:metadata, :doc]
66
+ end
72
67
 
73
- it 'should have inherited singleton attributes in order of definition' do
74
- @bar.singleton_class.attributes.keys.should == [:metadata, :doc]
68
+ it 'should have inherited singleton attributes in order of definition' do
69
+ @bar.singleton_class.attributes.keys.should == [:metadata, :doc]
70
+ end
75
71
  end
76
72
  end
77
73
 
78
74
  describe Doodle::Attribute, 'attribute order' do
79
- before :each do
80
- raise_if_defined :A, :B, :C
81
-
82
- class A < Doodle::Base
83
- has :a
84
- end
75
+ temporary_constants :A, :B, :C do
76
+ before :each do
77
+ class A < Doodle::Base
78
+ has :a
79
+ end
85
80
 
86
- class B < A
87
- has :b
88
- end
81
+ class B < A
82
+ has :b
83
+ end
89
84
 
90
- class C < B
91
- has :c
85
+ class C < B
86
+ has :c
87
+ end
92
88
  end
93
- end
94
-
95
- after :each do
96
- undefine_const(:A)
97
- undefine_const(:B)
98
- undefine_const(:C)
99
- end
100
89
 
101
- it 'should keep order of inherited attributes' do
102
- C.parents.should == [B, A, Doodle::Base, Object]
103
- end
90
+ it 'should keep order of inherited attributes' do
91
+ C.parents.should == [B, A, Doodle::Base, Object]
92
+ end
104
93
 
105
- it 'should keep order of inherited attributes' do
106
- C.attributes.keys.should == [:a, :b, :c]
94
+ it 'should keep order of inherited attributes' do
95
+ C.attributes.keys.should == [:a, :b, :c]
96
+ end
107
97
  end
108
98
  end
109
-
110
- raise_if_defined(:Foo, :Bar, :A, :B, :C)
@@ -0,0 +1,21 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe 'Doodle', 'parents' do
4
+ temporary_constant :Foo do
5
+ before :each do
6
+ class Foo < Doodle::Base
7
+ has :var1, :kind => Integer
8
+ has :var2, :kind => Integer, :default => 1
9
+ must 'have var1 != var2' do
10
+ var1 != var2
11
+ end
12
+ end
13
+ end
14
+
15
+ it 'should not duplicate validations when accessing them!' do
16
+ foo = Foo 2
17
+ foo.validations.size.should == 1
18
+ foo.validations.size.should == 1
19
+ end
20
+ end
21
+ end
@@ -1,90 +1,83 @@
1
- $:.unshift(File.join(File.dirname(__FILE__), '../.'))
2
- require 'lib/spec_helper'
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
3
2
 
4
3
  describe Doodle, 'class attributes' do
5
- after :each do
6
- undefine_const(:Bar)
7
- undefine_const(:Foo)
8
- end
9
- before(:each) do
10
- raise_if_defined(:Foo, :Bar)
11
- class Foo
12
- include Doodle::Helper
13
- class << self
14
- has :metadata
4
+ temporary_constants :Bar, :Foo do
5
+ before(:each) do
6
+ class Foo
7
+ include Doodle::Helper
8
+ class << self
9
+ has :metadata
10
+ end
15
11
  end
16
- end
17
- @foo = Foo.new
18
- class Bar < Foo
19
- include Doodle::Helper
20
- class << self
21
- has :doc
12
+ @foo = Foo.new
13
+ class Bar < Foo
14
+ include Doodle::Helper
15
+ class << self
16
+ has :doc
17
+ end
22
18
  end
19
+ @foo = Foo.new
20
+ @bar = Bar.new
23
21
  end
24
- @foo = Foo.new
25
- @bar = Bar.new
26
- end
27
22
 
28
- it 'should create class attribute' do
29
- Foo.metadata = 'Foo metadata'
30
- Foo.metadata.should == 'Foo metadata'
31
- end
23
+ it 'should create class attribute' do
24
+ Foo.metadata = 'Foo metadata'
25
+ Foo.metadata.should == 'Foo metadata'
26
+ end
32
27
 
33
- it 'should access class attribute via self.class' do
34
- @foo.class.metadata = '@foo metadata'
35
- @foo.class.metadata.should == '@foo metadata'
36
- Foo.metadata.should == '@foo metadata'
28
+ it 'should access class attribute via self.class' do
29
+ @foo.class.metadata = '@foo metadata'
30
+ @foo.class.metadata.should == '@foo metadata'
31
+ Foo.metadata.should == '@foo metadata'
37
32
 
38
- Foo.metadata = 'Foo metadata'
39
- Foo.metadata.should == 'Foo metadata'
40
- @foo.class.metadata.should == 'Foo metadata'
41
- end
33
+ Foo.metadata = 'Foo metadata'
34
+ Foo.metadata.should == 'Foo metadata'
35
+ @foo.class.metadata.should == 'Foo metadata'
36
+ end
42
37
 
43
- it "should list all class's own attributes" do
44
- Foo.meta.attributes(false).keys.should == [:metadata]
45
- end
38
+ it "should list all class's own attributes" do
39
+ Foo.meta.attributes(false).keys.should == [:metadata]
40
+ end
46
41
 
47
- it "should list all class's own attributes" do
48
- Foo.meta.attributes.keys.should == [:metadata]
49
- end
42
+ it "should list all class's own attributes" do
43
+ Foo.meta.attributes.keys.should == [:metadata]
44
+ end
50
45
 
51
- it 'should create Bar class attribute' do
52
- Bar.metadata = 'Bar metadata'
53
- Bar.metadata.should == 'Bar metadata'
54
- end
46
+ it 'should create Bar class attribute' do
47
+ Bar.metadata = 'Bar metadata'
48
+ Bar.metadata.should == 'Bar metadata'
49
+ end
55
50
 
56
- it 'should access class attribute via self.class' do
57
- @bar.class.metadata = '@bar metadata'
58
- @bar.class.metadata.should == '@bar metadata'
59
- Bar.metadata.should == '@bar metadata'
51
+ it 'should access class attribute via self.class' do
52
+ @bar.class.metadata = '@bar metadata'
53
+ @bar.class.metadata.should == '@bar metadata'
54
+ Bar.metadata.should == '@bar metadata'
60
55
 
61
- Bar.metadata = 'Bar metadata'
62
- Bar.metadata.should == 'Bar metadata'
63
- @bar.class.metadata.should == 'Bar metadata'
64
- end
56
+ Bar.metadata = 'Bar metadata'
57
+ Bar.metadata.should == 'Bar metadata'
58
+ @bar.class.metadata.should == 'Bar metadata'
59
+ end
65
60
 
66
- it 'should not allow inherited class attributes to interfere with each other' do
67
- Foo.metadata = 'Foo metadata'
68
- @bar.class.metadata = '@bar metadata'
69
- @bar.class.metadata.should == '@bar metadata'
70
- Bar.metadata.should == '@bar metadata'
61
+ it 'should not allow inherited class attributes to interfere with each other' do
62
+ Foo.metadata = 'Foo metadata'
63
+ @bar.class.metadata = '@bar metadata'
64
+ @bar.class.metadata.should == '@bar metadata'
65
+ Bar.metadata.should == '@bar metadata'
71
66
 
72
- Bar.metadata = 'Bar metadata'
73
- Bar.metadata.should == 'Bar metadata'
74
- @bar.class.metadata.should == 'Bar metadata'
67
+ Bar.metadata = 'Bar metadata'
68
+ Bar.metadata.should == 'Bar metadata'
69
+ @bar.class.metadata.should == 'Bar metadata'
75
70
 
76
- Foo.metadata.should == 'Foo metadata'
77
- @foo.class.metadata.should == 'Foo metadata'
78
- end
71
+ Foo.metadata.should == 'Foo metadata'
72
+ @foo.class.metadata.should == 'Foo metadata'
73
+ end
79
74
 
80
- it "should list all class's own attributes" do
81
- Bar.meta.attributes(false).keys.should == [:doc]
82
- end
75
+ it "should list all class's own attributes" do
76
+ Bar.meta.attributes(false).keys.should == [:doc]
77
+ end
83
78
 
84
- it "should list all class's own attributes" do
85
- Bar.meta.attributes.keys.should == [:metadata, :doc]
79
+ it "should list all class's own attributes" do
80
+ Bar.meta.attributes.keys.should == [:metadata, :doc]
81
+ end
86
82
  end
87
-
88
83
  end
89
-
90
- raise_if_defined(:Foo, :Bar)
@@ -0,0 +1,97 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe Doodle, "Simple collector" do
4
+ temporary_constant :Foo do
5
+ before :each do
6
+ class Foo < Doodle::Base
7
+ has :list, :init => [], :collect => :item
8
+ end
9
+ @foo = Foo do
10
+ item "Hello"
11
+ item "World"
12
+ end
13
+ end
14
+ after :each do
15
+ remove_ivars :foo
16
+ end
17
+
18
+ it "should define a collector method :item" do
19
+ @foo.methods.include?('item').should == true
20
+ end
21
+
22
+ it "should collect items into attribute :list" do
23
+ @foo.list.should == ["Hello", "World"]
24
+ end
25
+
26
+ end
27
+ end
28
+
29
+ describe Doodle, "Typed collector with default collector name" do
30
+ temporary_constant :Event, :Location do
31
+ before :each do
32
+ class Location < Doodle::Base
33
+ has :name, :kind => String
34
+ end
35
+ class Event < Doodle::Base
36
+ has :locations, :init => [], :collect => Location
37
+ end
38
+ @event = Event do
39
+ location "Stage 1"
40
+ location "Stage 2"
41
+ end
42
+ end
43
+ after :each do
44
+ remove_ivars :event
45
+ end
46
+
47
+ it "should define a collector method :location" do
48
+ @event.methods.include?('location').should == true
49
+ end
50
+
51
+ it "should collect items into attribute :list" do
52
+ @event.locations.map{|loc| loc.name}.should == ["Stage 1", "Stage 2"]
53
+ end
54
+
55
+ end
56
+ end
57
+
58
+ describe Doodle, "Typed collector with specified collector name" do
59
+ temporary_constant :Location, :Event do
60
+ before :each do
61
+ class Location < Doodle::Base
62
+ has :name, :kind => String
63
+ end
64
+ class Event < Doodle::Base
65
+ has :locations, :init => [], :collect => { :place => :Location }
66
+ end
67
+ end
68
+ it "should define a collector method :place" do
69
+ Event.instance_methods.include?('place').should == true
70
+ end
71
+ end
72
+ end
73
+
74
+ describe Doodle, "Typed collector with specified collector name" do
75
+ temporary_constant :Location, :Event do
76
+ before :each do
77
+ class Location < Doodle::Base
78
+ has :name, :kind => String
79
+ end
80
+ class Event < Doodle::Base
81
+ has :locations, :init => [], :collect => { :place => Location }
82
+ end
83
+ end
84
+ it "should collect items into attribute :list" do
85
+ event = nil
86
+ proc {
87
+ event = Event do
88
+ place "Stage 1"
89
+ place "Stage 2"
90
+ end
91
+ }.should_not raise_error
92
+ event.locations.map{|loc| loc.name}.should == ["Stage 1", "Stage 2"]
93
+ event.locations.map{|loc| loc.class}.should == [Location, Location]
94
+ end
95
+ end
96
+ end
97
+