metaruby 1.0.0.rc1 → 1.0.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,68 +0,0 @@
1
- require 'metaruby/test'
2
-
3
- class Base
4
- extend MetaRuby::ModelAsClass
5
- end
6
-
7
- module DefinitionContext
8
- class Klass < Base; end
9
- end
10
-
11
- module PermanentDefinitionContext
12
- def self.permanent_model?; true end
13
- class Klass < Base; end
14
- end
15
-
16
- module NonPermanentDefinitionContext
17
- def self.permanent_model?; false end
18
- class Klass < Base; end
19
- end
20
-
21
- describe MetaRuby::ModelAsClass do
22
- include MetaRuby::SelfTest
23
-
24
- before do
25
- # Code that is run before each test
26
- end
27
- after do
28
- # Code that is run after each test
29
- end
30
-
31
- describe "Using modules as metamodel" do
32
- it "should apply the Attribute module on sub-metamodels for classes" do
33
- mod = Module.new { include MetaRuby::ModelAsClass }
34
- sub = Module.new { include mod }
35
- assert mod.respond_to?(:inherited_attribute)
36
- assert sub.respond_to?(:inherited_attribute)
37
- end
38
- end
39
-
40
- describe "#new_submodel" do
41
- it "should call setup_submodel only once" do
42
- mod = Module.new { include MetaRuby::ModelAsClass }
43
- klass = Class.new { extend mod }
44
- flexmock(klass).should_receive(:setup_submodel).once
45
- klass.new_submodel
46
- end
47
- it "should set permanent_model to false on the class" do
48
- end
49
- end
50
-
51
- describe "creating subclasses" do
52
- it "should set permanent_model to false on the class if the enclosing context is not properly registered as a constant" do
53
- definition_context = Module.new do
54
- Klass = Class.new(Base)
55
- end
56
- assert !definition_context.const_get(:Klass).permanent_model?
57
- end
58
- it "should set permanent_model to true on the class if the enclosing context is properly registered as a constant but is not responding to permanent_model?" do
59
- assert DefinitionContext::Klass.permanent_model?
60
- end
61
- it "should set permanent_model to true on the class if the enclosing context is permanent" do
62
- assert PermanentDefinitionContext::Klass.permanent_model?
63
- end
64
- it "should set permanent_model to false on the class if the enclosing context is not permanent" do
65
- assert !NonPermanentDefinitionContext::Klass.permanent_model?
66
- end
67
- end
68
- end
@@ -1,49 +0,0 @@
1
- require 'metaruby/test'
2
- require 'metaruby/dsls/find_through_method_missing'
3
-
4
- describe MetaRuby::DSLs do
5
- include MetaRuby::SelfTest
6
-
7
- describe "#find_through_method_missing" do
8
- it "should call the corresponding find method when matching" do
9
- obj = flexmock
10
- obj.should_receive(:find_obj).with("test").once.and_return(found = flexmock)
11
- assert_equal found, MetaRuby::DSLs.find_through_method_missing(obj, :test_obj, [], "obj")
12
- end
13
- it "should allow specifying the find method name" do
14
- obj = flexmock
15
- obj.should_receive(:find_obj).with("test").once.and_return(found = flexmock)
16
- assert_equal found, MetaRuby::DSLs.find_through_method_missing(obj, :test_bla, [], "bla" => 'find_obj')
17
- end
18
- it "should raise NoMethodError if the requested object is not found" do
19
- obj = flexmock
20
- obj.should_receive(:find_obj).with("test").once.and_return(nil)
21
- assert_raises(NoMethodError) do
22
- MetaRuby::DSLs.find_through_method_missing(obj, :test_obj, [], "obj")
23
- end
24
- end
25
- it "should raise ArgumentError if some arguments are given regardless of whether the object exists" do
26
- obj = flexmock
27
- obj.should_receive(:find_obj).never
28
- assert_raises(ArgumentError) do
29
- MetaRuby::DSLs.find_through_method_missing(obj, :test_obj, [10], "obj")
30
- end
31
- end
32
- it "should ignore non-matching methods and return nil" do
33
- obj = flexmock
34
- assert !MetaRuby::DSLs.find_through_method_missing(obj, :test_bla, [10], "obj")
35
- end
36
- it "should raise if the missing method is one of the expected find methods" do
37
- # NOTE: do not use flexmock here, as specifying 'never' on a call
38
- # spec makes flexmock raise a NoMethodError !!!
39
- called = false
40
- obj = Class.new do
41
- define_method(:find_obj) { |name| called = true }
42
- end.new
43
- assert_raises(NoMethodError) do
44
- MetaRuby::DSLs.find_through_method_missing(obj, :find_obj, [], "obj")
45
- end
46
- assert !called
47
- end
48
- end
49
- end
@@ -1,105 +0,0 @@
1
- require 'metaruby/test'
2
-
3
- describe MetaRuby::ModelAsModule do
4
- include MetaRuby::SelfTest
5
-
6
- attr_reader :root_m
7
- before do
8
- @root_m = Module.new { extend MetaRuby::ModelAsModule }
9
- end
10
-
11
- describe "#provides" do
12
- it "sets the supermodel to the provided model if it is root" do
13
- root_m.root = true
14
- submodel = Module.new { extend MetaRuby::ModelAsModule }
15
- submodel.provides root_m
16
- assert_equal root_m, submodel.supermodel
17
- end
18
- it "sets root_model to the provided model's supermodel if the provided model is not root itself" do
19
- flexmock(root_m).should_receive(:supermodel).once.
20
- and_return(root = flexmock)
21
- submodel = Module.new { extend MetaRuby::ModelAsModule }
22
- root.should_receive(:register_submodel).with(submodel).once
23
- submodel.provides root_m
24
- assert_equal root, submodel.supermodel
25
- end
26
- end
27
-
28
- describe "Using modules as metamodel" do
29
- it "should apply the Attribute module on sub-metamodels for modules" do
30
- mod = Module.new { include MetaRuby::ModelAsModule }
31
- sub = Module.new { include mod }
32
- assert mod.respond_to?(:inherited_attribute)
33
- assert sub.respond_to?(:inherited_attribute)
34
- end
35
- end
36
-
37
- describe "#new_submodel" do
38
- it "should mark the model as non-permanent" do
39
- root = Module.new do
40
- extend MetaRuby::ModelAsModule
41
- self.root = true
42
- end
43
- sub = root.new_submodel
44
- assert !sub.new_submodel.permanent_model?
45
- end
46
- end
47
-
48
- describe "#create_and_register_submodel" do
49
- attr_reader :definition_context, :base_m
50
- before do
51
- @definition_context = Module.new
52
- @base_m = Module.new do
53
- extend MetaRuby::ModelAsModule
54
- def self.root?; true end
55
- end
56
- end
57
-
58
- it "should set permanent_model to true if the enclosing module is a Ruby module that is accessible by name" do
59
- flexmock(MetaRuby::Registration).should_receive(:accessible_by_name?).with(definition_context).and_return(true)
60
- result = MetaRuby::ModelAsModule.create_and_register_submodel(definition_context, 'Test', base_m)
61
- assert result.permanent_model?
62
- end
63
- it "should set permanent_model to false if the enclosing module is a Ruby module that is not accessible by name" do
64
- flexmock(definition_context).should_receive(:accessible_by_name?).and_return(false)
65
- result = MetaRuby::ModelAsModule.create_and_register_submodel(definition_context, 'Test', base_m)
66
- assert !result.permanent_model?
67
- end
68
- it "should set permanent_model to true if the enclosing module is permanent" do
69
- flexmock(definition_context).should_receive(:permanent_model?).and_return(true)
70
- result = MetaRuby::ModelAsModule.create_and_register_submodel(definition_context, 'Test', base_m)
71
- assert result.permanent_model?
72
- end
73
- it "should set permanent_model to false if the enclosing module is non-permanent" do
74
- flexmock(definition_context).should_receive(:permanent_model?).and_return(false)
75
- result = MetaRuby::ModelAsModule.create_and_register_submodel(definition_context, 'Test', base_m)
76
- assert !result.permanent_model?
77
- end
78
- end
79
-
80
- describe "#clear_model" do
81
- attr_reader :root_m, :model_m
82
- before do
83
- @root_m = Module.new do
84
- extend MetaRuby::ModelAsModule
85
- self.root = true
86
- self.permanent_model = true
87
- end
88
- @model_m = root_m.new_submodel
89
- model_m.permanent_model = true
90
- end
91
-
92
- it "deregisters the module regardless of the permanent_model flag" do
93
- flexmock(root_m).should_receive(:deregister_submodels).with([model_m]).once
94
- model_m.permanent_model = true
95
- model_m.clear_model
96
- end
97
- it "clears its parent model set" do
98
- flexmock(root_m).should_receive(:deregister_submodels).with([model_m]).once
99
- model_m.permanent_model = true
100
- model_m.clear_model
101
- assert model_m.parent_models.empty?
102
- end
103
- end
104
- end
105
-
@@ -1,182 +0,0 @@
1
- require 'metaruby/test'
2
-
3
- class Constant
4
- extend MetaRuby::Registration
5
- end
6
- module Mod
7
- class Constant
8
- extend MetaRuby::Registration
9
- end
10
- end
11
-
12
- describe MetaRuby::Registration do
13
- include MetaRuby::SelfTest
14
-
15
- class ModelStub
16
- extend MetaRuby::Registration
17
- end
18
-
19
- def model_stub(parent_model = nil)
20
- result = Class.new(ModelStub)
21
- result.permanent_model = false
22
- flexmock(result).should_receive(:supermodel).and_return(parent_model).by_default
23
- if parent_model
24
- parent_model.register_submodel(result)
25
- end
26
- result
27
- end
28
-
29
- describe "#register_submodel" do
30
- attr_reader :base_model
31
- before do
32
- @base_model = model_stub
33
- end
34
-
35
- it "registers the model on the receiver" do
36
- sub_model = Class.new(ModelStub)
37
- base_model.register_submodel(sub_model)
38
- assert(base_model.each_submodel.find { |m| m == sub_model })
39
- end
40
- it "registers the model on the receiver's parent model" do
41
- parent_model = Class.new(ModelStub)
42
- sub_model = Class.new(ModelStub)
43
- flexmock(base_model).should_receive(:supermodel).and_return(parent_model)
44
- flexmock(parent_model).should_receive(:register_submodel).with(sub_model).once
45
- base_model.register_submodel(sub_model)
46
- end
47
- end
48
-
49
- describe "#deregister_submodel" do
50
- attr_reader :base_model, :sub_model
51
- before do
52
- @base_model = model_stub
53
- @sub_model = model_stub(base_model)
54
- end
55
-
56
- it "deregisters the models on the receiver" do
57
- flexmock(base_model).should_receive(:supermodel).and_return(nil).once
58
- base_model.deregister_submodels([sub_model])
59
- assert(base_model.each_submodel.to_a.empty?)
60
- end
61
- it "deregisters the models on the receiver's parent model" do
62
- parent_model = flexmock
63
- flexmock(base_model).should_receive(:supermodel).and_return(parent_model)
64
- flexmock(parent_model).should_receive(:deregister_submodels).with([sub_model]).once
65
- base_model.deregister_submodels([sub_model])
66
- end
67
- it "always calls the parent model's deregister method" do
68
- parent_model = flexmock
69
- flexmock(base_model).should_receive(:supermodel).and_return(parent_model)
70
- flexmock(base_model).should_receive(:deregister_submodels).with([sub_model]).pass_thru
71
- flexmock(parent_model).should_receive(:deregister_submodels).with([sub_model]).once
72
- base_model.deregister_submodels([sub_model])
73
- end
74
- it "returns true if a model got deregistered" do
75
- flexmock(base_model).should_receive(:supermodel).and_return(nil).once
76
- assert base_model.deregister_submodels([sub_model])
77
- end
78
- it "returns false if no models got deregistered" do
79
- assert !base_model.deregister_submodels([flexmock])
80
- end
81
- end
82
-
83
- describe "#clear_models" do
84
- attr_reader :base_model, :sub_model
85
- before do
86
- @base_model = model_stub
87
- @sub_model = model_stub(base_model)
88
- end
89
- it "deregisters the non-permanent models, and calls #clear_submodels on them" do
90
- base_model.should_receive(:deregister_submodels).with([sub_model]).once.pass_thru
91
- sub_model.should_receive(:clear_submodels).once
92
- base_model.clear_submodels
93
- end
94
- it "does not deregister the permanent models, but still calls #clear_submodels on them" do
95
- base_model.should_receive(:deregister_submodels).with([]).never
96
- sub_model.should_receive(:permanent_model?).and_return(true).once
97
- sub_model.should_receive(:clear_submodels).once
98
- base_model.clear_submodels
99
- end
100
- it "calls #clear_submodels on non-permanent submodels" do
101
- flexmock(sub_model).should_receive(:permanent_model?).and_return(false).once
102
- flexmock(sub_model).should_receive(:clear_submodels).once
103
- base_model.clear_submodels
104
- end
105
- it "calls #clear_submodels on permanent submodels" do
106
- flexmock(sub_model).should_receive(:permanent_model?).and_return(true).once
107
- flexmock(sub_model).should_receive(:clear_submodels).once
108
- # Create another submodel so that there is something to clear
109
- model_stub(base_model)
110
- base_model.clear_submodels
111
- end
112
- it "does not deregister the permanent models" do
113
- flexmock(sub_model).should_receive(:permanent_model?).and_return(true).once
114
- flexmock(base_model).should_receive(:deregister_submodels).with([]).never
115
- base_model.clear_submodels
116
- end
117
- it "should deregister before it clears" do
118
- flexmock(sub_model).should_receive(:permanent_model?).and_return(false).once
119
- flexmock(base_model).should_receive(:deregister_submodels).once.ordered.pass_thru
120
- flexmock(sub_model).should_receive(:clear_submodels).once.ordered
121
- base_model.clear_submodels
122
- end
123
- end
124
-
125
- describe "#accessible_by_name?" do
126
- it "should be true for toplevel classes / modules" do
127
- assert Constant.accessible_by_name?
128
- end
129
-
130
- it "should be true for classes / modules defined in namespaces" do
131
- assert Mod::Constant.accessible_by_name?
132
- end
133
-
134
- it "should be false for anonymous classes / modules" do
135
- klass = Class.new { extend MetaRuby::Registration }
136
- assert !klass.accessible_by_name?
137
- end
138
- end
139
-
140
- describe "#clear_model" do
141
- attr_reader :obj, :supermodel
142
- before do
143
- @obj = Class.new do
144
- extend MetaRuby::Registration
145
- end
146
- @supermodel = flexmock
147
- supermodel.should_receive(:deregister_submodels).by_default
148
- flexmock(obj).should_receive(:supermodel).and_return(supermodel)
149
- end
150
-
151
- it "should deregister itself from its parent models if it is non-permanent and has supermodels" do
152
- supermodel.should_receive(:deregister_submodels).once.with([obj])
153
- obj.permanent_model = false
154
- obj.clear_model
155
- end
156
-
157
- it "should deregister itself from the constant hierarchy if non-permanent" do
158
- obj.permanent_model = false
159
- flexmock(MetaRuby::Registration).should_receive(:accessible_by_name?).once.with(obj).and_return(true)
160
- flexmock(MetaRuby::Registration).should_receive(:deregister_constant).once.with(obj)
161
- obj.clear_model
162
- end
163
-
164
- it "should not touch the receiver's registration if permanent" do
165
- obj.permanent_model = true
166
- flexmock(MetaRuby::Registration).should_receive(:deregister_constant).never
167
- supermodel.should_receive(:deregister_submodels).never
168
- obj.clear_model
169
- end
170
- end
171
-
172
- describe "#deregister_constant" do
173
- it "should deregister the object on the enclosing context" do
174
- obj = flexmock(:basename => "Name", :spacename => "Test")
175
- context = flexmock
176
- flexmock(MetaRuby::Registration).should_receive(:constant).with("::Test").and_return(context)
177
- context.should_receive(:remove_const).with('Name').once
178
- MetaRuby::Registration.deregister_constant(obj)
179
- end
180
- end
181
- end
182
-