nm 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,94 @@
1
+ require 'assert'
2
+ require 'nm/source'
3
+
4
+ class Nm::Source
5
+
6
+ class UnitTests < Assert::Context
7
+ desc "Nm::Source"
8
+ subject{ Nm::Source }
9
+
10
+ should "know its extension" do
11
+ assert_equal ".nm", subject::EXT
12
+ end
13
+
14
+ end
15
+
16
+ class InitTests < UnitTests
17
+ desc "when init"
18
+ setup do
19
+ @root = Factory.template_root
20
+ @source = Nm::Source.new(@root)
21
+ end
22
+ subject{ @source }
23
+
24
+ should have_readers :root
25
+ should have_imeths :data, :render, :partial
26
+
27
+ should "know its root" do
28
+ assert_equal @root, subject.root.to_s
29
+ end
30
+
31
+ end
32
+
33
+ class DataTests < InitTests
34
+ desc "`data` method"
35
+ setup do
36
+ @file_path = Factory.template_file('obj.nm')
37
+ end
38
+
39
+ should "read the contents of a given file path" do
40
+ exp = File.read(@file_path)
41
+ assert_equal exp, subject.data(@file_path)
42
+ end
43
+
44
+ end
45
+
46
+ class RenderTests < InitTests
47
+ desc "`render` method"
48
+ setup do
49
+ @file_name = "locals"
50
+ @file_locals = { 'key' => 'a-value' }
51
+ @file_path = Factory.template_file("#{@file_name}#{Nm::Source::EXT}")
52
+ end
53
+
54
+ should "render a template for the given file name and return its data" do
55
+ exp = Nm::Template.new(subject, @file_path, @file_locals).__data__
56
+ assert_equal exp, subject.render(@file_name, @file_locals)
57
+ end
58
+
59
+ end
60
+
61
+ class PartialTests < InitTests
62
+ desc "`partial` method"
63
+
64
+ setup do
65
+ @file_name = "locals"
66
+ @file_locals = { 'key' => 'a-value' }
67
+ @file_path = Factory.template_file("_#{@file_name}#{Nm::Source::EXT}")
68
+ end
69
+
70
+ should "render a template for the given file name and return its data" do
71
+ exp = Nm::Template.new(subject, @file_path, @file_locals).__data__
72
+ assert_equal exp, subject.partial(@file_name, @file_locals)
73
+ end
74
+
75
+ end
76
+
77
+ class DefaultSource < UnitTests
78
+ desc "DefaultSource"
79
+ setup do
80
+ @source = Nm::DefaultSource.new
81
+ end
82
+ subject{ @source }
83
+
84
+ should "be a Source" do
85
+ assert_kind_of Nm::Source, subject
86
+ end
87
+
88
+ should "use `/` as its root" do
89
+ assert_equal '/', subject.root.to_s
90
+ end
91
+
92
+ end
93
+
94
+ end
@@ -0,0 +1,355 @@
1
+ require 'assert'
2
+ require 'nm/template'
3
+
4
+ require 'nm/ext'
5
+ require 'nm/source'
6
+
7
+ class Nm::Template
8
+
9
+ class UnitTests < Assert::Context
10
+ desc "Nm::Template"
11
+ subject{ Nm::Template }
12
+
13
+ end
14
+
15
+ class InitTests < UnitTests
16
+ desc "when init"
17
+ setup do
18
+ @template = Nm::Template.new
19
+ end
20
+ subject{ @template }
21
+
22
+ should have_imeths :__data__, :__node__, :__map__, :__render__, :__partial__
23
+ should have_imeths :node, :_node, :n
24
+ should have_imeths :map, :_map, :m
25
+ should have_imeths :render, :_render, :r
26
+ should have_imeths :partial, :_partial, :p
27
+
28
+ should "have no data if no source file is given" do
29
+ assert_nil subject.__data__
30
+ end
31
+
32
+ should "return itself when its markup methods are called" do
33
+ t = Nm::Template.new
34
+ assert_equal t, t.__node__('key', 'value')
35
+
36
+ t = Nm::Template.new
37
+ assert_equal t, t.__map__([], &Proc.new{})
38
+
39
+ t = Nm::Template.new
40
+ assert_equal t, t.__render__(Factory.template_file('obj'))
41
+
42
+ t = Nm::Template.new
43
+ assert_equal t, t.__partial__(Factory.template_file('obj'))
44
+ end
45
+
46
+ end
47
+
48
+ class NodeMethodTests < UnitTests
49
+ desc "`__node__` method"
50
+
51
+ should "add key-value pairs at any level" do
52
+ exp = {'a' => 'Aye'}
53
+ assert_equal exp, Nm::Template.new.__node__('a', 'Aye').__data__
54
+
55
+ exp = {
56
+ 'nested' => {'a' => 'Aye'}
57
+ }
58
+ t = Nm::Template.new.__node__('nested'){ __node__('a', 'Aye') }
59
+ assert_equal exp, t.__data__
60
+ end
61
+
62
+ should "be aliased as `node`, `_node` and `n`" do
63
+ exp = {'a' => 'Aye'}
64
+ assert_equal exp, Nm::Template.new.__node__('a', 'Aye').__data__
65
+ assert_equal exp, Nm::Template.new.node('a', 'Aye').__data__
66
+ assert_equal exp, Nm::Template.new._node('a', 'Aye').__data__
67
+ assert_equal exp, Nm::Template.new.n('a', 'Aye').__data__
68
+ end
69
+
70
+ should "complain if called after a `__map__` call" do
71
+ t = Nm::Template.new.__map__([1,2,3])
72
+ assert_raises Nm::InvalidError do
73
+ t.__node__('a', 'Aye')
74
+ end
75
+ end
76
+
77
+ end
78
+
79
+ class MapMethodTests < UnitTests
80
+ desc "`map` method"
81
+ setup do
82
+ @list = [1,2,3]
83
+ end
84
+
85
+ should "map a given list to the data" do
86
+ exp = @list
87
+ assert_equal exp, Nm::Template.new.__map__(@list).__data__
88
+
89
+ exp = [
90
+ {'1' => 1},
91
+ {'2' => 2},
92
+ {'3' => 3},
93
+ ]
94
+ t = Nm::Template.new.__map__(@list){ |item| __node__(item.to_s, item) }
95
+ assert_equal exp, t.__data__
96
+
97
+ exp = {
98
+ 'list' => [
99
+ {'1' => 1},
100
+ {'2' => 2},
101
+ {'3' => 3},
102
+ ]
103
+ }
104
+ list = @list
105
+ t = Nm::Template.new.__node__('list') do
106
+ __map__(list){ |item| __node__(item.to_s, item) }
107
+ end
108
+ assert_equal exp, t.__data__
109
+ end
110
+
111
+ should "be aliased as `map`, `_map` and `m`" do
112
+ exp = @list
113
+ assert_equal exp, Nm::Template.new.__map__(@list).__data__
114
+ assert_equal exp, Nm::Template.new.map(@list).__data__
115
+ assert_equal exp, Nm::Template.new._map(@list).__data__
116
+ assert_equal exp, Nm::Template.new.m(@list).__data__
117
+ end
118
+
119
+ should "complain if given a list that doesn't respond to `.map`" do
120
+ val = 123
121
+ assert_not_responds_to 'map', val
122
+ assert_raises ArgumentError do
123
+ Nm::Template.new.__map__(val)
124
+ end
125
+ end
126
+
127
+ should "complain if called after a `__node__` call" do
128
+ t = Nm::Template.new.__node__('a', 'Aye')
129
+ assert_raises Nm::InvalidError do
130
+ t.__map__([1,2,3])
131
+ end
132
+ end
133
+
134
+ end
135
+
136
+ class RenderTests < InitTests
137
+ setup do
138
+ @source = Nm::Source.new(Factory.template_root)
139
+
140
+ @obj_file_name = "obj"
141
+ @obj = {
142
+ 'obj' => {
143
+ 'a' => 'Aye',
144
+ 'b' => 'Bee',
145
+ 'c' => 'See'
146
+ }
147
+ }
148
+
149
+ @list_file_name = "list"
150
+ @list = [
151
+ { '1' => 1 },
152
+ { '2' => 2 },
153
+ { '3' => 3 }
154
+ ]
155
+ end
156
+
157
+ end
158
+
159
+ class RenderMethodTests < RenderTests
160
+ desc "`render` method"
161
+
162
+ should "render a template for the given file name and add its data" do
163
+ t = Nm::Template.new(@source)
164
+ assert_equal @obj, t.__render__(@obj_file_name).__data__
165
+ end
166
+
167
+ should "be aliased as `render`, `_render` and `r`" do
168
+ t = Nm::Template.new(@source)
169
+ assert_equal @obj, t.__render__(@obj_file_name).__data__
170
+
171
+ t = Nm::Template.new(@source)
172
+ assert_equal @obj, t.render(@obj_file_name).__data__
173
+
174
+ t = Nm::Template.new(@source)
175
+ assert_equal @obj, t._render(@obj_file_name).__data__
176
+
177
+ t = Nm::Template.new(@source)
178
+ assert_equal @obj, t.r(@obj_file_name).__data__
179
+ end
180
+
181
+ should "merge if call returns an obj and called after a `__node__` call" do
182
+ t = Nm::Template.new(@source)
183
+ t.__node__('1', 'One')
184
+
185
+ exp = {'1' => 'One'}.merge(@obj)
186
+ assert_equal exp, t.__render__(@obj_file_name).__data__
187
+ end
188
+
189
+ should "complain if call returns an obj and called after a `__map__` call" do
190
+ t = Nm::Template.new(@source)
191
+ t.__map__([1,2,3])
192
+ assert_raises Nm::InvalidError do
193
+ t.__render__(@obj_file_name).__data__
194
+ end
195
+ end
196
+
197
+ should "concat if call returns a list and called after a `__map__` call" do
198
+ t = Nm::Template.new(@source)
199
+ t.__map__([1,2,3])
200
+
201
+ exp = [1,2,3].concat(@list)
202
+ assert_equal exp, t.__render__(@list_file_name).__data__
203
+ end
204
+
205
+ should "complain if call returns a list and called after a `__node__` call" do
206
+ t = Nm::Template.new(@source)
207
+ t.__node__('1', 'One')
208
+
209
+ assert_raises Nm::InvalidError do
210
+ t.__render__(@list_file_name).__data__
211
+ end
212
+ end
213
+
214
+ end
215
+
216
+ class PartialMethodTests < RenderTests
217
+ desc "`partial` method"
218
+ setup do
219
+ @partial_obj = {
220
+ 'a' => 'Aye',
221
+ 'b' => 'Bee',
222
+ 'c' => 'See'
223
+ }
224
+ @partial_list = @list
225
+ end
226
+
227
+ should "render a template for the given partial name and add its data" do
228
+ t = Nm::Template.new(@source)
229
+ assert_equal @partial_obj, t.__partial__(@obj_file_name).__data__
230
+ end
231
+
232
+ should "be aliased as `render`, `_render` and `r`" do
233
+ t = Nm::Template.new(@source)
234
+ assert_equal @partial_obj, t.__partial__(@obj_file_name).__data__
235
+
236
+ t = Nm::Template.new(@source)
237
+ assert_equal @partial_obj, t.partial(@obj_file_name).__data__
238
+
239
+ t = Nm::Template.new(@source)
240
+ assert_equal @partial_obj, t._partial(@obj_file_name).__data__
241
+
242
+ t = Nm::Template.new(@source)
243
+ assert_equal @partial_obj, t.p(@obj_file_name).__data__
244
+ end
245
+
246
+ should "merge if call returns an obj and called after a `__node__` call" do
247
+ t = Nm::Template.new(@source)
248
+ t.__node__('1', 'One')
249
+
250
+ exp = {'1' => 'One'}.merge(@partial_obj)
251
+ assert_equal exp, t.__partial__(@obj_file_name).__data__
252
+ end
253
+
254
+ should "complain if call returns an obj and called after a `__map__` call" do
255
+ t = Nm::Template.new(@source)
256
+ t.__map__([1,2,3])
257
+ assert_raises Nm::InvalidError do
258
+ t.__partial__(@obj_file_name).__data__
259
+ end
260
+ end
261
+
262
+ should "merge if call returns a list and called after a `__map__` call" do
263
+ t = Nm::Template.new(@source)
264
+ t.__map__([1,2,3])
265
+
266
+ exp = [1,2,3].concat(@partial_list)
267
+ assert_equal exp, t.__partial__(@list_file_name).__data__
268
+ end
269
+
270
+ should "complain if call returns a list and called after a `__node__` call" do
271
+ t = Nm::Template.new(@source)
272
+ t.__node__('1', 'One')
273
+
274
+ assert_raises Nm::InvalidError do
275
+ t.__partial__(@list_file_name).__data__
276
+ end
277
+ end
278
+
279
+ end
280
+
281
+ class SourceFileTests < UnitTests
282
+ desc "when init given a source file"
283
+ setup do
284
+ @obj_source_file = Factory.template_file('obj.nm')
285
+ @exp_obj = {
286
+ 'obj' => {
287
+ 'a' => 'Aye',
288
+ 'b' => 'Bee',
289
+ 'c' => 'See'
290
+ }
291
+ }
292
+
293
+ @list_source_file = Factory.template_file('list.nm')
294
+ @exp_list = [
295
+ { '1' => 1 },
296
+ { '2' => 2 },
297
+ { '3' => 3 }
298
+ ]
299
+ end
300
+
301
+ should "evaluate the source file" do
302
+ assert_equal @exp_obj, Nm::Template.new(@obj_source_file).__data__
303
+ assert_equal @exp_list, Nm::Template.new(@list_source_file).__data__
304
+ end
305
+
306
+ should "evaluate the source file with locals" do
307
+ assert_equal @exp_obj, Nm::Template.new(@obj_source_file, {}).__data__
308
+ assert_equal @exp_list, Nm::Template.new(@list_source_file, {}).__data__
309
+ end
310
+
311
+ end
312
+
313
+ class NoExistSourceFileTests < UnitTests
314
+ desc "when init given a source file that does not exist"
315
+ setup do
316
+ @no_exist_source_file = Factory.template_file('does-not-exist.nm')
317
+ end
318
+
319
+ should "complain that the source does not exist" do
320
+ assert_raises ArgumentError do
321
+ Nm::Template.new(@no_exist_source_file)
322
+ end
323
+ end
324
+
325
+ end
326
+
327
+ class LocalsTests < UnitTests
328
+ desc "when init with locals"
329
+ setup do
330
+ @locals = {
331
+ 'key' => 'value',
332
+ 'node' => 'A Node',
333
+ 'map' => 'A Map'
334
+ }
335
+ end
336
+
337
+ should "expose the local as a reader method on the template" do
338
+ t = Nm::Template.new
339
+ assert_not_responds_to 'key', t
340
+
341
+ t = Nm::Template.new(@locals)
342
+ assert_equal 'value', t.key
343
+ end
344
+
345
+ should "not interfere with method aliases" do
346
+ d = Nm::Template.new(Factory.template_file('aliases.nm'), @locals).__data__
347
+ assert_kind_of ::Array, d
348
+ assert_equal 1, d.size
349
+ assert_equal 'A Node', d.first['node local value']
350
+ assert_equal 'A Map', d.first['map local value']
351
+ end
352
+
353
+ end
354
+
355
+ end
metadata CHANGED
@@ -1,13 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nm
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
5
- prerelease:
6
- segments:
7
- - 0
8
- - 1
9
- - 0
10
- version: 0.1.0
4
+ version: 0.2.0
11
5
  platform: ruby
12
6
  authors:
13
7
  - Kelly Redding
@@ -16,24 +10,19 @@ autorequire:
16
10
  bindir: bin
17
11
  cert_chain: []
18
12
 
19
- date: 2014-04-12 00:00:00 Z
13
+ date: 2014-06-04 00:00:00 Z
20
14
  dependencies:
21
15
  - !ruby/object:Gem::Dependency
22
- type: :development
23
- name: assert
24
- version_requirements: &id001 !ruby/object:Gem::Requirement
25
- none: false
16
+ requirement: &id001 !ruby/object:Gem::Requirement
26
17
  requirements:
27
18
  - - ~>
28
19
  - !ruby/object:Gem::Version
29
- hash: 23
30
- segments:
31
- - 2
32
- - 10
33
20
  version: "2.10"
21
+ type: :development
22
+ version_requirements: *id001
23
+ name: assert
34
24
  prerelease: false
35
- requirement: *id001
36
- description: "JSON/BSON data structure template system "
25
+ description: Data templating system.
37
26
  email:
38
27
  - kelly@kellyredding.com
39
28
  - collin.redding@me.com
@@ -49,44 +38,80 @@ files:
49
38
  - LICENSE.txt
50
39
  - README.md
51
40
  - Rakefile
41
+ - bench/_slide.nm
42
+ - bench/logger.rb
43
+ - bench/results/nm.txt
44
+ - bench/results/nm_partials.txt
45
+ - bench/results/nm_re_source.txt
46
+ - bench/results/rabl.txt
47
+ - bench/runner.rb
48
+ - bench/slideshow.nm
49
+ - bench/slideshow.rabl
50
+ - bench/slideshow.rb
51
+ - bench/slideshow_partials.nm
52
+ - bench/slideshow_partials.rb
53
+ - bench/template.rb
52
54
  - lib/nm.rb
55
+ - lib/nm/ext.rb
56
+ - lib/nm/source.rb
57
+ - lib/nm/template.rb
53
58
  - lib/nm/version.rb
54
59
  - log/.gitkeep
55
60
  - nm.gemspec
61
+ - script/bench_all.rb
62
+ - script/bench_nm.rb
63
+ - script/bench_nm_partials.rb
64
+ - script/bench_nm_re_source.rb
65
+ - script/bench_rabl.rb
56
66
  - test/helper.rb
67
+ - test/support/factory.rb
68
+ - test/support/templates/_list.nm
69
+ - test/support/templates/_locals.nm
70
+ - test/support/templates/_obj.nm
71
+ - test/support/templates/aliases.nm
72
+ - test/support/templates/list.nm
73
+ - test/support/templates/locals.nm
74
+ - test/support/templates/obj.nm
75
+ - test/unit/ext_tests.rb
76
+ - test/unit/source_tests.rb
77
+ - test/unit/template_tests.rb
57
78
  - tmp/.gitkeep
58
79
  homepage: http://github.com/redding/nm
59
80
  licenses:
60
81
  - MIT
82
+ metadata: {}
83
+
61
84
  post_install_message:
62
85
  rdoc_options: []
63
86
 
64
87
  require_paths:
65
88
  - lib
66
89
  required_ruby_version: !ruby/object:Gem::Requirement
67
- none: false
68
90
  requirements:
69
- - - ">="
91
+ - &id002
92
+ - ">="
70
93
  - !ruby/object:Gem::Version
71
- hash: 3
72
- segments:
73
- - 0
74
94
  version: "0"
75
95
  required_rubygems_version: !ruby/object:Gem::Requirement
76
- none: false
77
96
  requirements:
78
- - - ">="
79
- - !ruby/object:Gem::Version
80
- hash: 3
81
- segments:
82
- - 0
83
- version: "0"
97
+ - *id002
84
98
  requirements: []
85
99
 
86
100
  rubyforge_project:
87
- rubygems_version: 1.8.15
101
+ rubygems_version: 2.2.2
88
102
  signing_key:
89
- specification_version: 3
90
- summary: JSON/BSON data structure template system
103
+ specification_version: 4
104
+ summary: Data templating system.
91
105
  test_files:
92
106
  - test/helper.rb
107
+ - test/support/factory.rb
108
+ - test/support/templates/_list.nm
109
+ - test/support/templates/_locals.nm
110
+ - test/support/templates/_obj.nm
111
+ - test/support/templates/aliases.nm
112
+ - test/support/templates/list.nm
113
+ - test/support/templates/locals.nm
114
+ - test/support/templates/obj.nm
115
+ - test/unit/ext_tests.rb
116
+ - test/unit/source_tests.rb
117
+ - test/unit/template_tests.rb