erector-rails4 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +2 -0
- data/.gitignore +10 -0
- data/.rspec +1 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +143 -0
- data/README.md +4 -0
- data/Rakefile +24 -0
- data/erector-rails4.gemspec +36 -0
- data/erector-rails4.sublimeproject +20 -0
- data/lib/erector/abstract_widget.rb +231 -0
- data/lib/erector/after_initialize.rb +29 -0
- data/lib/erector/attributes.rb +29 -0
- data/lib/erector/cache.rb +37 -0
- data/lib/erector/caching.rb +65 -0
- data/lib/erector/convenience.rb +98 -0
- data/lib/erector/dependencies.rb +24 -0
- data/lib/erector/dependency.rb +31 -0
- data/lib/erector/element.rb +113 -0
- data/lib/erector/externals.rb +104 -0
- data/lib/erector/html.rb +12 -0
- data/lib/erector/html_widget.rb +220 -0
- data/lib/erector/inline.rb +37 -0
- data/lib/erector/jquery.rb +28 -0
- data/lib/erector/mixin.rb +12 -0
- data/lib/erector/needs.rb +95 -0
- data/lib/erector/output.rb +144 -0
- data/lib/erector/promise.rb +141 -0
- data/lib/erector/rails/form_builder.rb +44 -0
- data/lib/erector/rails/railtie.rb +13 -0
- data/lib/erector/rails/template_handler.rb +16 -0
- data/lib/erector/rails/widget_renderer.rb +6 -0
- data/lib/erector/rails.rb +221 -0
- data/lib/erector/raw_string.rb +12 -0
- data/lib/erector/sass.rb +32 -0
- data/lib/erector/tag.rb +66 -0
- data/lib/erector/text.rb +123 -0
- data/lib/erector/unicode.rb +18185 -0
- data/lib/erector/unicode_builder.rb +67 -0
- data/lib/erector/version.rb +5 -0
- data/lib/erector/widget.rb +94 -0
- data/lib/erector/widgets.rb +5 -0
- data/lib/erector/xml_widget.rb +131 -0
- data/lib/erector.rb +28 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/controllers/application.rb +6 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/views/layouts/erb_as_layout.html.erb +2 -0
- data/spec/dummy/app/views/layouts/widget_as_layout.rb +8 -0
- data/spec/dummy/app/views/test/_erb.erb +1 -0
- data/spec/dummy/app/views/test/_erector.rb +5 -0
- data/spec/dummy/app/views/test/_partial_with_locals.rb +7 -0
- data/spec/dummy/app/views/test/bare.rb +5 -0
- data/spec/dummy/app/views/test/erb_from_erector.html.rb +5 -0
- data/spec/dummy/app/views/test/erector_from_erb.html.erb +1 -0
- data/spec/dummy/app/views/test/erector_with_locals_from_erb.html.erb +6 -0
- data/spec/dummy/app/views/test/implicit_assigns.html.rb +5 -0
- data/spec/dummy/app/views/test/needs.html.rb +7 -0
- data/spec/dummy/app/views/test/needs_subclass.html.rb +5 -0
- data/spec/dummy/app/views/test/protected_instance_variable.html.rb +5 -0
- data/spec/dummy/app/views/test/render_default.html.rb +5 -0
- data/spec/dummy/app/views/test/render_default_erb_with_layout.html.erb +1 -0
- data/spec/dummy/app/views/test/render_default_widget_with_layout.html.rb +5 -0
- data/spec/dummy/app/views/test/render_partial.html.rb +5 -0
- data/spec/dummy/app/views/test/render_with_widget_as_layout.rb +5 -0
- data/spec/dummy/app/views/test/render_with_widget_as_layout_using_content_for.rb +8 -0
- data/spec/dummy/config/application.rb +44 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +22 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +22 -0
- data/spec/dummy/config/environments/production.rb +49 -0
- data/spec/dummy/config/environments/test.rb +36 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +10 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +12 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +3 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/db/seeds.rb +7 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/dummy/spec/form_builder_spec.rb +21 -0
- data/spec/dummy/spec/rails_helpers_spec.rb +236 -0
- data/spec/dummy/spec/rails_spec_helper.rb +10 -0
- data/spec/dummy/spec/rails_widget_spec.rb +83 -0
- data/spec/dummy/spec/render_spec.rb +369 -0
- data/spec/erector/after_initialize_spec.rb +45 -0
- data/spec/erector/cache_spec.rb +133 -0
- data/spec/erector/caching_spec.rb +184 -0
- data/spec/erector/convenience_spec.rb +250 -0
- data/spec/erector/dependency_spec.rb +67 -0
- data/spec/erector/hello_from_readme.rb +18 -0
- data/spec/erector/hello_from_readme_spec.rb +11 -0
- data/spec/erector/html_spec.rb +585 -0
- data/spec/erector/indentation_spec.rb +211 -0
- data/spec/erector/inline_spec.rb +94 -0
- data/spec/erector/jquery_spec.rb +35 -0
- data/spec/erector/mixin_spec.rb +65 -0
- data/spec/erector/needs_spec.rb +141 -0
- data/spec/erector/output_spec.rb +293 -0
- data/spec/erector/promise_spec.rb +173 -0
- data/spec/erector/sample-file.txt +1 -0
- data/spec/erector/sass_spec.rb +57 -0
- data/spec/erector/tag_spec.rb +67 -0
- data/spec/erector/unicode_builder_spec.rb +75 -0
- data/spec/erector/widget_spec.rb +310 -0
- data/spec/erector/xml_widget_spec.rb +73 -0
- data/spec/spec_helper.rb +31 -0
- metadata +368 -0
@@ -0,0 +1,211 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper")
|
2
|
+
|
3
|
+
describe "indentation" do
|
4
|
+
include Erector::Mixin
|
5
|
+
|
6
|
+
it "can detect newliney tags" do
|
7
|
+
widget = ::Erector.inline
|
8
|
+
widget.instance_eval do
|
9
|
+
@prettyprint = true
|
10
|
+
end
|
11
|
+
widget.send(:newliney?, "i").should == false
|
12
|
+
widget.send(:newliney?, "table").should == true
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should not add newline for non-newliney tags" do
|
16
|
+
Erector.inline do
|
17
|
+
text "Hello, "
|
18
|
+
b "World"
|
19
|
+
end.to_pretty.should == "Hello, <b>World</b>"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should add newlines before open newliney tags" do
|
23
|
+
Erector.inline do
|
24
|
+
p "foo"
|
25
|
+
p "bar"
|
26
|
+
end.to_pretty.should == "<p>foo</p>\n<p>bar</p>\n"
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should add newlines between text and open newliney tag" do
|
30
|
+
Erector.inline do
|
31
|
+
text "One"
|
32
|
+
p "Two"
|
33
|
+
end.to_pretty.should == "One\n<p>Two</p>\n"
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should add newlines after end newliney tags" do
|
37
|
+
Erector.inline do
|
38
|
+
tr do
|
39
|
+
td "cell"
|
40
|
+
end
|
41
|
+
end.to_pretty.should == "<tr>\n <td>cell</td>\n</tr>\n"
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should treat empty elements as start and end" do
|
45
|
+
Erector.inline do
|
46
|
+
p "before"
|
47
|
+
br
|
48
|
+
p "after"
|
49
|
+
end.to_pretty.should == "<p>before</p>\n<br />\n<p>after</p>\n"
|
50
|
+
end
|
51
|
+
|
52
|
+
it "empty elements sets at_start_of_line" do
|
53
|
+
Erector.inline do
|
54
|
+
text "before"
|
55
|
+
br
|
56
|
+
p "after"
|
57
|
+
end.to_pretty.should == "before<br />\n<p>after</p>\n"
|
58
|
+
end
|
59
|
+
|
60
|
+
it "will not insert extra space before/after input element" do
|
61
|
+
# If dim memory serves, the reason for not adding spaces here is
|
62
|
+
# because it affects/affected the rendering in browsers.
|
63
|
+
Erector.inline do
|
64
|
+
text 'Name'
|
65
|
+
input :type => 'text'
|
66
|
+
text 'after'
|
67
|
+
end.to_pretty.should == 'Name<input type="text" />after'
|
68
|
+
end
|
69
|
+
|
70
|
+
it "will indent" do
|
71
|
+
Erector.inline do
|
72
|
+
html do
|
73
|
+
head do
|
74
|
+
title "hi"
|
75
|
+
end
|
76
|
+
body do
|
77
|
+
div do
|
78
|
+
p "paragraph"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end.to_pretty.should == <<END
|
83
|
+
<html>
|
84
|
+
<head>
|
85
|
+
<title>hi</title>
|
86
|
+
</head>
|
87
|
+
<body>
|
88
|
+
<div>
|
89
|
+
<p>paragraph</p>
|
90
|
+
</div>
|
91
|
+
</body>
|
92
|
+
</html>
|
93
|
+
END
|
94
|
+
end
|
95
|
+
|
96
|
+
it "preserves indentation for sub-rendered widgets" do
|
97
|
+
tea = Erector.inline do
|
98
|
+
div do
|
99
|
+
p "oolong"
|
100
|
+
end
|
101
|
+
end
|
102
|
+
cup = Erector.inline do
|
103
|
+
div do
|
104
|
+
p "fine china"
|
105
|
+
widget tea
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
cup.to_pretty.should == <<END
|
110
|
+
<div>
|
111
|
+
<p>fine china</p>
|
112
|
+
<div>
|
113
|
+
<p>oolong</p>
|
114
|
+
</div>
|
115
|
+
</div>
|
116
|
+
END
|
117
|
+
end
|
118
|
+
|
119
|
+
# see http://github.com/erector/erector/issues/#issue/5
|
120
|
+
it "indents scripts properly" do
|
121
|
+
pending
|
122
|
+
Erector.inline do
|
123
|
+
html :xmlns => 'http://www.w3.org/1999/xhtml' do
|
124
|
+
head do
|
125
|
+
javascript "Cufon.replace('#content');"
|
126
|
+
javascript '$(document).ready(function(){ $(document).pngFix(); });'
|
127
|
+
end
|
128
|
+
body do
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end.to_pretty.should == <<-HTML
|
132
|
+
<html xmlns="http://www.w3.org/1999/xhtml">
|
133
|
+
<head>
|
134
|
+
<script type="text/javascript">
|
135
|
+
// <![CDATA[
|
136
|
+
Cufon.replace('#content');
|
137
|
+
// ]]>
|
138
|
+
</script>
|
139
|
+
<script type="text/javascript">
|
140
|
+
// <![CDATA[
|
141
|
+
$(document).ready(function(){ $(document).pngFix(); });
|
142
|
+
// ]]>
|
143
|
+
</script>
|
144
|
+
</head>
|
145
|
+
<body></body>
|
146
|
+
</html>
|
147
|
+
HTML
|
148
|
+
end
|
149
|
+
|
150
|
+
it "can turn off newlines" do
|
151
|
+
erector do
|
152
|
+
text "One"
|
153
|
+
p "Two"
|
154
|
+
end.should == "One<p>Two</p>"
|
155
|
+
end
|
156
|
+
|
157
|
+
it "can turn newlines on and off" do
|
158
|
+
widget = Erector.inline do
|
159
|
+
text "One"
|
160
|
+
p "Two"
|
161
|
+
end
|
162
|
+
widget.to_html.should == "One<p>Two</p>"
|
163
|
+
widget.to_pretty.should == "One\n<p>Two</p>\n"
|
164
|
+
widget.to_html.should == "One<p>Two</p>"
|
165
|
+
end
|
166
|
+
|
167
|
+
it "can turn on newlines via to_pretty" do
|
168
|
+
widget = Erector.inline do
|
169
|
+
text "One"
|
170
|
+
p "Two"
|
171
|
+
end.to_pretty.should == "One\n<p>Two</p>\n"
|
172
|
+
end
|
173
|
+
|
174
|
+
it "can turn newlines on/off via global variable" do
|
175
|
+
erector { br }.should == "<br />"
|
176
|
+
Erector::Widget.prettyprint_default = true
|
177
|
+
erector { br }.should == "<br />\n"
|
178
|
+
Erector::Widget.prettyprint_default = false
|
179
|
+
erector { br }.should == "<br />"
|
180
|
+
end
|
181
|
+
|
182
|
+
describe ":max_length" do
|
183
|
+
it "wraps after N characters" do
|
184
|
+
Erector.inline do
|
185
|
+
div "the quick brown fox jumps over the lazy dog"
|
186
|
+
end.to_html(:max_length => 20).should ==
|
187
|
+
"<div>the quick brown\n" +
|
188
|
+
"fox jumps over the\n" +
|
189
|
+
"lazy dog</div>"
|
190
|
+
end
|
191
|
+
|
192
|
+
it "preserves pretty indent" do
|
193
|
+
Erector.inline do
|
194
|
+
div "the quick brown fox jumps over the lazy dog"
|
195
|
+
end.to_pretty(:max_length => 20).should ==
|
196
|
+
"<div>the quick brown\n" +
|
197
|
+
" fox jumps over the\n" +
|
198
|
+
" lazy dog</div>\n"
|
199
|
+
end
|
200
|
+
|
201
|
+
it "preserves raw strings" do
|
202
|
+
Erector.inline do
|
203
|
+
div raw("the quick <brown> fox <jumps> over the lazy dog")
|
204
|
+
end.to_html(:max_length => 20).should ==
|
205
|
+
"<div>the quick\n" +
|
206
|
+
"<brown> fox <jumps>\n" +
|
207
|
+
"over the lazy dog\n" +
|
208
|
+
"</div>"
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper")
|
2
|
+
|
3
|
+
describe Erector::Widget do
|
4
|
+
it "provides access to instance variables from the calling context" do
|
5
|
+
@var = "yay"
|
6
|
+
Erector::Widget.new { @var.should == "yay"; @var = "yum" }.to_html
|
7
|
+
@var.should == "yum"
|
8
|
+
end
|
9
|
+
|
10
|
+
it "provides access to bound variables from the calling context" do
|
11
|
+
var = "yay"
|
12
|
+
Erector::Widget.new { var.should == "yay"; var = "yum" }.to_html
|
13
|
+
var.should == "yum"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "doesn't provide access to Erector methods" do
|
17
|
+
Erector::Widget.new { lambda { text "yay" }.should raise_error(NoMethodError) }.to_html
|
18
|
+
end
|
19
|
+
|
20
|
+
describe ".inline" do
|
21
|
+
it "returns an instance with Erector::Inline mixed in" do
|
22
|
+
widget_subclass = Class.new(Erector::Widget)
|
23
|
+
widget_subclass.ancestors.should_not include(Erector::Inline)
|
24
|
+
|
25
|
+
inlined_instance = widget_subclass.inline
|
26
|
+
inlined_instance.class.ancestors.should include(widget_subclass)
|
27
|
+
inlined_instance.class.ancestors.should include(Erector::Inline)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe Erector::Inline do
|
33
|
+
it "returns an InlineWidget" do
|
34
|
+
Erector.inline.should be_a_kind_of(Erector::InlineWidget)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "doesn't provide access to instance variables from the calling context" do
|
38
|
+
@var = "yay"
|
39
|
+
Erector.inline { text @var }.to_html.should == ""
|
40
|
+
end
|
41
|
+
|
42
|
+
it "provides access to bound variables from the calling context" do
|
43
|
+
var = "yay"
|
44
|
+
Erector.inline { text var }.to_html.should == "yay"
|
45
|
+
end
|
46
|
+
|
47
|
+
it "provides access to explicit assigns" do
|
48
|
+
Erector.inline(:var => "yay") { text @var }.to_html.should == "yay"
|
49
|
+
end
|
50
|
+
|
51
|
+
it "provides access to methods from the calling context" do
|
52
|
+
def helper
|
53
|
+
"yay"
|
54
|
+
end
|
55
|
+
|
56
|
+
Erector.inline { text helper }.to_html.should == "yay"
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "#call_block" do
|
60
|
+
it "calls the block with a pointer to self" do
|
61
|
+
x = Erector::InlineWidget.new do |y|
|
62
|
+
element "arg", y.object_id
|
63
|
+
element "self", self.object_id
|
64
|
+
end
|
65
|
+
|
66
|
+
# inside the block...
|
67
|
+
x.to_html.should ==
|
68
|
+
"<arg>#{x.object_id}</arg>" + # the argument is the child
|
69
|
+
"<self>#{x.object_id}</self>" # and self is also the child
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe Erector::Mixin do
|
75
|
+
include Erector::Mixin
|
76
|
+
|
77
|
+
it "doesn't provide access to instance variables from the calling context" do
|
78
|
+
@var = "yay"
|
79
|
+
erector { text @var }.should == ""
|
80
|
+
end
|
81
|
+
|
82
|
+
it "provides access to bound variables from the calling context" do
|
83
|
+
var = "yay"
|
84
|
+
erector { text var }.should == "yay"
|
85
|
+
end
|
86
|
+
|
87
|
+
it "provides access to methods from the calling context" do
|
88
|
+
def helper
|
89
|
+
"yay"
|
90
|
+
end
|
91
|
+
|
92
|
+
erector { text helper }.should == "yay"
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper")
|
2
|
+
|
3
|
+
describe Erector::JQuery do
|
4
|
+
include Erector::Mixin
|
5
|
+
|
6
|
+
describe "#jquery" do
|
7
|
+
def expected(event, *args)
|
8
|
+
|
9
|
+
"<script #{'id="foo" ' if args.include? :id}type=\"text/javascript\">\n" +
|
10
|
+
"// <![CDATA[\n\n" +
|
11
|
+
"jQuery(document).#{event}(function($){\n" +
|
12
|
+
"alert('hello');\n});\n// ]]>\n</script>\n"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "outputs a 'jquery ready' script block by default" do
|
16
|
+
erector { jquery "alert('hello');" }.should == expected("ready")
|
17
|
+
end
|
18
|
+
|
19
|
+
it "outputs attributes" do
|
20
|
+
erector { jquery "alert('hello');", :id => 'foo' }.should == expected("ready", :id)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "outputs a 'jquery ready' script block when passed a symbol for the first arg" do
|
24
|
+
erector { jquery :ready, "alert('hello');" }.should == expected("ready")
|
25
|
+
end
|
26
|
+
|
27
|
+
it "outputs a 'jquery load' script block" do
|
28
|
+
erector { jquery :load, "alert('hello');" }.should == expected("load")
|
29
|
+
end
|
30
|
+
|
31
|
+
it "combines event, text, and attributes" do
|
32
|
+
erector { jquery :load, "alert('hello');", :id => "foo" }.should == expected("load", :id)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper")
|
2
|
+
require 'benchmark'
|
3
|
+
|
4
|
+
module MixinSpec
|
5
|
+
|
6
|
+
describe Erector::Mixin do
|
7
|
+
describe "#erector" do
|
8
|
+
it "renders its block to a string" do
|
9
|
+
|
10
|
+
class Thing
|
11
|
+
include Erector::Mixin
|
12
|
+
def name
|
13
|
+
erector do
|
14
|
+
span :class => "name" do
|
15
|
+
text "Gabriel "
|
16
|
+
i "Garcia"
|
17
|
+
text " Marquez"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
Thing.new.name.should == "<span class=\"name\">Gabriel <i>Garcia</i> Marquez</span>"
|
24
|
+
end
|
25
|
+
|
26
|
+
it "passes its parameters to to_html" do
|
27
|
+
class Thing
|
28
|
+
include Erector::Mixin
|
29
|
+
def pretty_name
|
30
|
+
erector(:prettyprint => true) do
|
31
|
+
div :class => "name" do
|
32
|
+
ul do
|
33
|
+
li "Gabriel"
|
34
|
+
li "Garcia"
|
35
|
+
li "Marquez"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
Thing.new.pretty_name.should ==
|
43
|
+
"<div class=\"name\">\n" +
|
44
|
+
" <ul>\n" +
|
45
|
+
" <li>Gabriel</li>\n" +
|
46
|
+
" <li>Garcia</li>\n" +
|
47
|
+
" <li>Marquez</li>\n" +
|
48
|
+
" </ul>\n" +
|
49
|
+
"</div>\n"
|
50
|
+
end
|
51
|
+
|
52
|
+
it "passes its parameters to the widget too" do
|
53
|
+
class Thing
|
54
|
+
include Erector::Mixin
|
55
|
+
def foo
|
56
|
+
erector(:foo => "bar") do
|
57
|
+
div @foo
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
Thing.new.foo.should == "<div>bar</div>"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper")
|
2
|
+
|
3
|
+
describe Erector::Needs do
|
4
|
+
it "doesn't complain if there aren't any needs declared" do
|
5
|
+
class Thing1 < Erector::Widget
|
6
|
+
end
|
7
|
+
Thing1.new
|
8
|
+
end
|
9
|
+
|
10
|
+
it "allows you to say that you don't want any parameters" do
|
11
|
+
class Thing2 < Erector::Widget
|
12
|
+
needs nil
|
13
|
+
end
|
14
|
+
lambda { Thing2.new }.should_not raise_error(ArgumentError)
|
15
|
+
lambda { Thing2.new(:foo => 1) }.should raise_error(ArgumentError)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "doesn't complain if you pass it a declared parameter" do
|
19
|
+
class Thing2b < Erector::Widget
|
20
|
+
needs :foo
|
21
|
+
end
|
22
|
+
lambda { Thing2b.new(:foo => 1) }.should_not raise_error(ArgumentError)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "complains if you pass it an undeclared parameter" do
|
26
|
+
class Thing3 < Erector::Widget
|
27
|
+
needs :foo
|
28
|
+
end
|
29
|
+
lambda { Thing3.new(:bar => 1) }.should raise_error(ArgumentError)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "allows multiple declared parameters" do
|
33
|
+
class Thing4 < Erector::Widget
|
34
|
+
needs :foo, :bar
|
35
|
+
end
|
36
|
+
lambda { Thing4.new(:foo => 1, :bar => 2) }.should_not raise_error(ArgumentError)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "complains when passing in an extra parameter after declaring many parameters" do
|
40
|
+
class Thing5 < Erector::Widget
|
41
|
+
needs :foo, :bar
|
42
|
+
end
|
43
|
+
lambda { Thing5.new(:foo => 1, :bar => 2, :baz => 3) }.should raise_error(ArgumentError)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "complains when you forget to pass in a needed parameter" do
|
47
|
+
class Thing6 < Erector::Widget
|
48
|
+
needs :foo, :bar
|
49
|
+
end
|
50
|
+
lambda { Thing6.new(:foo => 1) }.should raise_error(ArgumentError)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "doesn't complain if you omit a parameter with a default value" do
|
54
|
+
class Thing7 < Erector::Widget
|
55
|
+
needs :foo
|
56
|
+
needs :bar => 7
|
57
|
+
needs :baz => 8
|
58
|
+
end
|
59
|
+
lambda {
|
60
|
+
thing = Thing7.new(:foo => 1, :baz => 3)
|
61
|
+
thing.instance_variable_get(:@bar).should equal(7)
|
62
|
+
thing.instance_variable_get(:@baz).should equal(3)
|
63
|
+
}.should_not raise_error(ArgumentError)
|
64
|
+
end
|
65
|
+
|
66
|
+
it "allows multiple values on a line, including default values at the end of the line" do
|
67
|
+
class Thing8 < Erector::Widget
|
68
|
+
needs :foo, :bar => 7, :baz => 8
|
69
|
+
end
|
70
|
+
lambda {
|
71
|
+
thing = Thing8.new(:foo => 1, :baz => 2)
|
72
|
+
thing.instance_variable_get(:@foo).should equal(1)
|
73
|
+
thing.instance_variable_get(:@bar).should equal(7)
|
74
|
+
thing.instance_variable_get(:@baz).should equal(2)
|
75
|
+
}.should_not raise_error(ArgumentError)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "duplicates default values, so they're not accidentally shared across all instances" do
|
79
|
+
class Thing8a < Erector::Widget
|
80
|
+
needs :foo => []
|
81
|
+
attr_reader :foo
|
82
|
+
end
|
83
|
+
t1 = Thing8a.new
|
84
|
+
t2 = Thing8a.new
|
85
|
+
|
86
|
+
t1.foo.object_id.should_not == t2.foo.object_id
|
87
|
+
t1.foo.should == []
|
88
|
+
t2.foo.should == []
|
89
|
+
end
|
90
|
+
|
91
|
+
it "allows nil to be a default value" do
|
92
|
+
class Thing9 < Erector::Widget
|
93
|
+
needs :foo => nil
|
94
|
+
end
|
95
|
+
lambda {
|
96
|
+
thing = Thing9.new
|
97
|
+
thing.instance_variable_get(:@foo).should be_nil
|
98
|
+
}.should_not raise_error(ArgumentError)
|
99
|
+
end
|
100
|
+
|
101
|
+
it "doesn't attempt to dup undupable value if there's another need passed in (bug)" do
|
102
|
+
class Section < Erector::Widget
|
103
|
+
needs :title, :href => nil, :stinky => false, :awesome => true, :answer => 42, :shoe_size => 12.5, :event_type => :jump
|
104
|
+
end
|
105
|
+
Section.new(:title => "Steal Underpants").instance_variable_get(:@awesome).should == true
|
106
|
+
Section.new(:title => "Steal Underpants").instance_variable_get(:@event_type).should == :jump
|
107
|
+
end
|
108
|
+
|
109
|
+
it "accumulates needs across the inheritance chain even with modules mixed in" do
|
110
|
+
module Something
|
111
|
+
end
|
112
|
+
|
113
|
+
class Vehicle < Erector::Widget
|
114
|
+
needs :wheels
|
115
|
+
end
|
116
|
+
|
117
|
+
class Car < Vehicle
|
118
|
+
include Something
|
119
|
+
needs :engine
|
120
|
+
end
|
121
|
+
|
122
|
+
lambda { Car.new(:engine => 'V-8', :wheels => 4) }.should_not raise_error(ArgumentError)
|
123
|
+
lambda { Car.new(:engine => 'V-8') }.should raise_error(ArgumentError)
|
124
|
+
lambda { Car.new(:wheels => 4) }.should raise_error(ArgumentError)
|
125
|
+
end
|
126
|
+
|
127
|
+
it "no longer defines accessors for each of the needed variables" do
|
128
|
+
class NeedfulThing < Erector::Widget
|
129
|
+
needs :love
|
130
|
+
end
|
131
|
+
thing = NeedfulThing.new(:love => "all we need")
|
132
|
+
lambda {thing.love}.should raise_error(NoMethodError)
|
133
|
+
end
|
134
|
+
|
135
|
+
it "no longer complains if you attempt to 'need' a variable whose name overlaps with an existing method" do
|
136
|
+
class ThingWithOverlap < Erector::Widget
|
137
|
+
needs :text
|
138
|
+
end
|
139
|
+
lambda { ThingWithOverlap.new(:text => "alas") }.should_not raise_error(ArgumentError)
|
140
|
+
end
|
141
|
+
end
|