zafu 0.5.0 → 0.6.0

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.
@@ -0,0 +1,24 @@
1
+ module Foo
2
+ end
3
+
4
+ unless defined?(ActiveRecord)
5
+ module ActiveRecord
6
+ class Base
7
+ end
8
+ end
9
+ end
10
+
11
+ class Page < ActiveRecord::Base
12
+ include RubyLess
13
+ safe_context :root => Page
14
+ end
15
+
16
+ class SubPage < Page
17
+ include Foo
18
+ end
19
+
20
+ class SubSubPage < SubPage
21
+ end
22
+
23
+ class Comment
24
+ end
@@ -0,0 +1,9 @@
1
+ class String
2
+ alias blank? empty?
3
+ end
4
+
5
+ class NilClass
6
+ def blank?
7
+ true
8
+ end
9
+ end
data/test/mock/params.rb CHANGED
@@ -1,19 +1,13 @@
1
1
  module Mock
2
2
  module Params
3
3
  def self.included(base)
4
- base.before_process :filter_params
4
+ base.before_process :filter_post_string
5
5
  end
6
6
 
7
- def filter_params
8
- if klass = @params.delete(:class)
9
- if klass =~ /#\{/
10
- res = RubyLess.translate("\"#{klass}\"", self)
11
- @markup.append_dyn_param(:class, "<%= #{res} %>")
12
- else
13
- @markup.append_param(:class, klass)
14
- end
7
+ def filter_post_string
8
+ if str = @params.delete(:post_string)
9
+ out_post str
15
10
  end
16
- true
17
11
  end
18
12
  end
19
13
  end
@@ -0,0 +1,7 @@
1
+ module Mock
2
+ module Process
3
+ def r_link
4
+ out "<%= make_link(#{node}) %>"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ require 'mock/params'
2
+ require 'mock/process'
3
+ require 'mock/classes'
4
+
5
+ TestCompiler = Zafu.parser_with_rules(
6
+ Zafu::All,
7
+ Mock::Params,
8
+ Mock::Process
9
+ )
@@ -1,83 +1,145 @@
1
1
  require 'test_helper'
2
2
 
3
+ class String
4
+ def underscore
5
+ gsub(/(^|.)([A-Z])/) {$1 == '' ? $2.downcase : "#{$1}_#{$2.downcase}"}
6
+ end
7
+ end
8
+
3
9
  class NodeContextTest < Test::Unit::TestCase
4
- class Page;end
5
- class SubPage < Page; end
6
- class Comment;end
7
10
  NodeContext = Zafu::NodeContext
8
11
 
9
12
  context 'In a blank context' do
10
- setup do
11
- @context = NodeContext.new('@node', Page)
13
+ subject do
14
+ NodeContext.new('@node', Page)
12
15
  end
13
16
 
14
17
  should 'return the current name' do
15
- assert_equal '@node', @context.name
18
+ assert_equal '@node', subject.name
16
19
  end
17
20
 
18
21
  should 'return the current class' do
19
- assert_equal Page, @context.klass
22
+ assert_equal Page, subject.klass
20
23
  end
21
-
24
+
22
25
  should 'return true on will_be with the same class' do
23
- assert @context.will_be?(Page)
26
+ assert subject.will_be?(Page)
24
27
  end
25
-
28
+
26
29
  should 'return false on will_be with a sub class' do
27
- assert !@context.will_be?(SubPage)
30
+ assert !subject.will_be?(SubPage)
28
31
  end
29
-
32
+
30
33
  should 'return false on will_be with a different class' do
31
- assert !@context.will_be?(String)
34
+ assert !subject.will_be?(String)
32
35
  end
33
36
 
34
37
  should 'return self on a get for the same class' do
35
- assert_equal @context.object_id, @context.get(Page).object_id
38
+ assert_equal subject.object_id, subject.get(Page).object_id
36
39
  end
37
40
 
38
41
  should 'return nil on a get for another class' do
39
- assert_nil @context.get(Comment)
42
+ assert_nil subject.get(Comment)
43
+ end
44
+
45
+ context 'calling as_main' do
46
+ should 'build the name from the class' do
47
+ assert_equal '@page', subject.as_main.name
48
+ end
49
+
50
+ should 'return same class' do
51
+ assert_equal subject.klass, subject.as_main.klass
52
+ end
53
+ end
54
+
55
+ context 'calling form_name' do
56
+ should 'return underscore name of base class' do
57
+ assert_equal 'page', subject.form_name
58
+ end
40
59
  end
41
-
60
+
42
61
  context 'with a sub-class' do
43
- setup do
44
- @context = NodeContext.new('@node', SubPage)
62
+ subject do
63
+ NodeContext.new('@node', SubPage)
45
64
  end
46
-
65
+
47
66
  should 'return true on will_be with the same class' do
48
- assert @context.will_be?(SubPage)
67
+ assert subject.will_be?(SubPage)
49
68
  end
50
69
 
51
70
  should 'return true on will_be with a super class' do
52
- assert @context.will_be?(Page)
71
+ assert subject.will_be?(Page)
53
72
  end
54
73
 
55
74
  should 'return false on will_be with a different class' do
56
- assert !@context.will_be?(String)
75
+ assert !subject.will_be?(String)
57
76
  end
58
- end
77
+
78
+ context 'calling form_name' do
79
+ should 'return underscore name of base class' do
80
+ assert_equal 'page', subject.form_name
81
+ end
82
+ end
83
+
84
+ context 'calling as_main' do
85
+ should 'build the name from the class' do
86
+ assert_equal '@sub_page', subject.as_main.name
87
+ end
88
+
89
+ should 'return same class' do
90
+ assert_equal subject.klass, subject.as_main.klass
91
+ end
92
+
93
+ should 'return an ancestor when using after_class argument' do
94
+ subject = NodeContext.new('@node', SubSubPage)
95
+ assert_equal SubPage, subject.as_main(Page).klass
96
+ end
97
+
98
+ should 'rebuild name from ancestor when using after_class argument' do
99
+ subject = NodeContext.new('@node', SubSubPage)
100
+ assert_equal '@sub_page', subject.as_main(Page).name
101
+ end
102
+ end
103
+
104
+ should 'return subclass on class_name' do
105
+ assert_equal 'SubPage', subject.class_name
106
+ end
107
+ end # with a sub-class
108
+
109
+ context 'with an anonymoys sub-class' do
110
+ subject do
111
+ NodeContext.new('@node', Class.new(Page))
112
+ end
113
+
114
+ should 'return class on class_name' do
115
+ assert_equal 'Page', subject.class_name
116
+ end
117
+ end # with an anonymoys sub-class
59
118
  end
60
119
 
61
120
  context 'In a sub-context' do
62
121
  setup do
63
122
  @parent = NodeContext.new('@node', Page)
64
- @context = @parent.move_to('comment1', Comment)
123
+ end
124
+
125
+ subject do
126
+ @parent.move_to('comment1', Comment)
65
127
  end
66
128
 
67
129
  should 'return the current name' do
68
- assert_equal 'comment1', @context.name
130
+ assert_equal 'comment1', subject.name
69
131
  end
70
132
 
71
133
  should 'return the current class' do
72
- assert_equal Comment, @context.klass
134
+ assert_equal Comment, subject.klass
73
135
  end
74
136
 
75
137
  should 'return self on a get for the same class' do
76
- assert_equal @context.object_id, @context.get(Comment).object_id
138
+ assert_equal subject.object_id, subject.get(Comment).object_id
77
139
  end
78
140
 
79
141
  should 'return the parent on a get for the class of the parent' do
80
- assert_equal @parent.object_id, @context.get(Page).object_id
142
+ assert_equal @parent.object_id, subject.get(Page).object_id
81
143
  end
82
144
  end
83
145
 
@@ -86,56 +148,80 @@ class NodeContextTest < Test::Unit::TestCase
86
148
  @grandgrandma = NodeContext.new('@comment', Comment)
87
149
  @grandma = @grandgrandma.move_to('page', Page)
88
150
  @mother = @grandma.move_to('comment1', Comment)
89
- @context = @mother.move_to('var1', String)
151
+ end
152
+
153
+ subject do
154
+ @mother.move_to('var1', String)
90
155
  end
91
156
 
92
157
  should 'return the current name' do
93
- assert_equal 'var1', @context.name
158
+ assert_equal 'var1', subject.name
94
159
  end
95
160
 
96
161
  should 'return the current class' do
97
- assert_equal String, @context.klass
162
+ assert_equal String, subject.klass
98
163
  end
99
164
 
100
165
  should 'return the first ancestor matching class on get' do
101
- assert_equal @mother.object_id, @context.get(Comment).object_id
166
+ assert_equal @mother.object_id, subject.get(Comment).object_id
102
167
  end
103
168
 
104
169
  should 'return nil if no ancestor matches class on get' do
105
- assert_nil @context.get(Fixnum)
170
+ assert_nil subject.get(Fixnum)
171
+ end
172
+
173
+ should 'return the parent on up' do
174
+ assert_equal @mother, subject.up
106
175
  end
107
176
  end
108
177
 
109
178
  context 'In a sub-classes context' do
110
- setup do
111
- @context = NodeContext.new('super', SubPage)
179
+ subject do
180
+ NodeContext.new('super', SubPage)
112
181
  end
113
182
 
114
183
  should 'find the current context required class is an ancestor' do
115
- assert_equal @context.object_id, @context.get(Page).object_id
184
+ assert_equal subject.object_id, subject.get(Page).object_id
116
185
  end
117
186
  end
118
187
 
119
188
  context 'In a list context' do
120
189
  setup do
121
- @context = NodeContext.new('list', [Page])
190
+ @grandma = NodeContext.new('@page', Page)
191
+ @mother = @grandma.move_to('@comment', Comment)
192
+ end
193
+
194
+ subject do
195
+ @mother.move_to('list', [Page])
122
196
  end
123
197
 
124
198
  should 'find the context and resolve with first' do
125
- assert context = @context.get(Page)
199
+ assert context = subject.get(Page)
126
200
  assert_equal 'list.first', context.name
127
201
  assert_equal Page, context.klass
128
202
  end
203
+
204
+ should 'return parent on up with class' do
205
+ assert_equal @grandma, subject.up(Page)
206
+ end
207
+
208
+ should 'return true on will_be with the same class' do
209
+ assert subject.will_be?(Page)
210
+ end
211
+
212
+ should 'return class on master_class' do
213
+ assert_equal Page, subject.master_class(ActiveRecord::Base)
214
+ end
129
215
  end
130
216
 
131
217
  context 'Generating a dom id' do
132
218
  context 'in a blank context' do
133
- setup do
134
- @context = NodeContext.new('@foo', Page)
219
+ subject do
220
+ NodeContext.new('@foo', Page)
135
221
  end
136
222
 
137
223
  should 'return the node name in DOM id' do
138
- assert_equal '<%= @foo.zip %>', @context.dom_id
224
+ assert_equal '<%= @foo.zip %>', subject.dom_id
139
225
  end
140
226
  end
141
227
 
@@ -144,7 +230,10 @@ class NodeContextTest < Test::Unit::TestCase
144
230
  @a = NodeContext.new('@node', Page)
145
231
  @b = NodeContext.new('var1', [Page], @a)
146
232
  @c = NodeContext.new('var2', Page, @b)
147
- @context = NodeContext.new('var3', Page, @c)
233
+ end
234
+
235
+ subject do
236
+ NodeContext.new('var3', Page, @c)
148
237
  end
149
238
 
150
239
  context 'with parents as dom_scopes' do
@@ -154,18 +243,18 @@ class NodeContextTest < Test::Unit::TestCase
154
243
  end
155
244
 
156
245
  should 'use dom_scopes' do
157
- assert_equal '<%= var1.zip %>_<%= var2.zip %>_<%= var3.zip %>', @context.dom_id
246
+ assert_equal '<%= var1.zip %>_<%= var2.zip %>_<%= var3.zip %>', subject.dom_id
158
247
  end
159
248
  end
160
249
 
161
250
  context 'with ancestors and self as dom_scopes' do
162
251
  setup do
163
252
  @a.dom_scope!
164
- @context.dom_scope!
253
+ subject.dom_scope!
165
254
  end
166
255
 
167
256
  should 'not use self twice' do
168
- assert_equal '<%= @node.zip %>_<%= var3.zip %>', @context.dom_id
257
+ assert_equal '<%= @node.zip %>_<%= var3.zip %>', subject.dom_id
169
258
  end
170
259
  end
171
260
 
@@ -175,7 +264,7 @@ class NodeContextTest < Test::Unit::TestCase
175
264
  end
176
265
 
177
266
  should 'use dom_prefix' do
178
- assert_equal 'cart_<%= var3.zip %>', @context.dom_id
267
+ assert_equal 'cart_<%= var3.zip %>', subject.dom_id
179
268
  end
180
269
  end
181
270
 
@@ -0,0 +1,69 @@
1
+ require 'test_helper'
2
+
3
+ class OrderedHashTest < Test::Unit::TestCase
4
+
5
+ context 'An OrderedHash' do
6
+ setup do
7
+ @hash = Zafu::OrderedHash.new
8
+ @hash[:a] = 1
9
+ @hash[:c] = 2
10
+ @hash[:b] = 3
11
+ end
12
+
13
+ should 'keep keys in insertion order' do
14
+ assert_equal [:a, :c, :b], @hash.keys
15
+ end
16
+
17
+ should 'list each in insertion order' do
18
+ res = []
19
+ @hash.each do |k, v|
20
+ res << v
21
+ end
22
+ assert_equal [1, 2, 3], res
23
+ end
24
+
25
+ should 'remove entry on delete' do
26
+ @hash.delete(:c)
27
+ assert_equal [:a, :b], @hash.keys
28
+ end
29
+
30
+ context 'running through keys' do
31
+ should 'allow key alteration' do
32
+ @hash.keys.each do |k|
33
+ assert k != :d
34
+ @hash[:d] = 'x'
35
+ end
36
+ end
37
+ end
38
+
39
+ context 'running each' do
40
+ should 'allow key alteration' do
41
+ @hash.each do |k, v|
42
+ assert k != :d
43
+ @hash[:d] = 'x'
44
+ end
45
+ end
46
+ end
47
+
48
+ context 'with a duplicate' do
49
+ setup do
50
+ @dup = @hash.dup
51
+ end
52
+
53
+ should 'not alter duplicate on set' do
54
+ @hash[:d] = 4
55
+ assert_equal [:a, :c, :b], @dup.keys
56
+ end
57
+
58
+ should 'not alter duplicate on change' do
59
+ @hash[:a] = 10
60
+ assert_equal 1, @dup[:a]
61
+ end
62
+
63
+ should 'not alter duplicate on delete' do
64
+ @hash.delete(:a)
65
+ assert_equal [:a, :c, :b], @dup.keys
66
+ end
67
+ end
68
+ end
69
+ end