nm 0.5.4 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -7
- data/.gitignore +1 -0
- data/.l.yml +9 -0
- data/.rubocop.yml +3 -0
- data/.ruby-version +1 -0
- data/.t.yml +6 -0
- data/Gemfile +6 -2
- data/README.md +53 -51
- data/lib/nm.rb +12 -2
- data/lib/nm/context.rb +55 -0
- data/lib/nm/ext.rb +11 -18
- data/lib/nm/render.rb +12 -0
- data/lib/nm/source.rb +60 -47
- data/lib/nm/template_behaviors.rb +96 -0
- data/lib/nm/version.rb +3 -1
- data/nm.gemspec +12 -6
- data/test/helper.rb +6 -13
- data/test/support/factory.rb +3 -2
- data/test/support/templates/_list.nm +2 -0
- data/test/support/templates/_locals.nm +4 -2
- data/test/support/templates/_obj.nm +5 -3
- data/test/support/templates/aliases.nm +8 -3
- data/test/support/templates/list.nm +2 -0
- data/test/support/templates/locals.nm +4 -2
- data/test/support/templates/locals_alt.data.inem +2 -2
- data/test/support/templates/obj.nm +6 -4
- data/test/system/.keep +0 -0
- data/test/unit/context_tests.rb +51 -0
- data/test/unit/ext_tests.rb +27 -42
- data/test/unit/nm_tests.rb +20 -0
- data/test/unit/render_tests.rb +28 -0
- data/test/unit/source_tests.rb +89 -106
- data/test/unit/template_behaviors_tests.rb +259 -0
- metadata +82 -43
- data/lib/nm/template.rb +0 -103
- data/test/unit/template_tests.rb +0 -357
metadata
CHANGED
@@ -1,46 +1,80 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: nm
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
|
-
authors:
|
6
|
+
authors:
|
7
7
|
- Kelly Redding
|
8
8
|
- Collin Redding
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
12
|
+
date: 2021-06-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: much-style-guide
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 0.6.6
|
21
|
+
type: :development
|
17
22
|
prerelease: false
|
18
|
-
|
19
|
-
requirements:
|
20
|
-
- - ~>
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version:
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 0.6.6
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: assert
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 2.19.6
|
23
35
|
type: :development
|
24
|
-
|
25
|
-
|
26
|
-
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: 2.19.6
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: much-mixin
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 0.2.4
|
49
|
+
type: :runtime
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 0.2.4
|
56
|
+
description: 'Node-Map: a data templating DSL.'
|
57
|
+
email:
|
27
58
|
- kelly@kellyredding.com
|
28
59
|
- collin.redding@me.com
|
29
60
|
executables: []
|
30
|
-
|
31
61
|
extensions: []
|
32
|
-
|
33
62
|
extra_rdoc_files: []
|
34
|
-
|
35
|
-
|
36
|
-
- .
|
63
|
+
files:
|
64
|
+
- ".gitignore"
|
65
|
+
- ".l.yml"
|
66
|
+
- ".rubocop.yml"
|
67
|
+
- ".ruby-version"
|
68
|
+
- ".t.yml"
|
37
69
|
- Gemfile
|
38
70
|
- LICENSE
|
39
71
|
- README.md
|
40
72
|
- lib/nm.rb
|
73
|
+
- lib/nm/context.rb
|
41
74
|
- lib/nm/ext.rb
|
75
|
+
- lib/nm/render.rb
|
42
76
|
- lib/nm/source.rb
|
43
|
-
- lib/nm/
|
77
|
+
- lib/nm/template_behaviors.rb
|
44
78
|
- lib/nm/version.rb
|
45
79
|
- log/.gitkeep
|
46
80
|
- nm.gemspec
|
@@ -54,37 +88,38 @@ files:
|
|
54
88
|
- test/support/templates/locals.nm
|
55
89
|
- test/support/templates/locals_alt.data.inem
|
56
90
|
- test/support/templates/obj.nm
|
91
|
+
- test/system/.keep
|
92
|
+
- test/unit/context_tests.rb
|
57
93
|
- test/unit/ext_tests.rb
|
94
|
+
- test/unit/nm_tests.rb
|
95
|
+
- test/unit/render_tests.rb
|
58
96
|
- test/unit/source_tests.rb
|
59
|
-
- test/unit/
|
97
|
+
- test/unit/template_behaviors_tests.rb
|
60
98
|
- tmp/.gitkeep
|
61
99
|
homepage: http://github.com/redding/nm
|
62
|
-
licenses:
|
100
|
+
licenses:
|
63
101
|
- MIT
|
64
102
|
metadata: {}
|
65
|
-
|
66
103
|
post_install_message:
|
67
104
|
rdoc_options: []
|
68
|
-
|
69
|
-
require_paths:
|
105
|
+
require_paths:
|
70
106
|
- lib
|
71
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
-
|
74
|
-
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - "~>"
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '2.5'
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
80
117
|
requirements: []
|
81
|
-
|
82
|
-
rubyforge_project:
|
83
|
-
rubygems_version: 2.6.6
|
118
|
+
rubygems_version: 3.1.6
|
84
119
|
signing_key:
|
85
120
|
specification_version: 4
|
86
|
-
summary:
|
87
|
-
test_files:
|
121
|
+
summary: 'Node-Map: a data templating DSL.'
|
122
|
+
test_files:
|
88
123
|
- test/helper.rb
|
89
124
|
- test/support/factory.rb
|
90
125
|
- test/support/templates/_list.nm
|
@@ -95,6 +130,10 @@ test_files:
|
|
95
130
|
- test/support/templates/locals.nm
|
96
131
|
- test/support/templates/locals_alt.data.inem
|
97
132
|
- test/support/templates/obj.nm
|
133
|
+
- test/system/.keep
|
134
|
+
- test/unit/context_tests.rb
|
98
135
|
- test/unit/ext_tests.rb
|
136
|
+
- test/unit/nm_tests.rb
|
137
|
+
- test/unit/render_tests.rb
|
99
138
|
- test/unit/source_tests.rb
|
100
|
-
- test/unit/
|
139
|
+
- test/unit/template_behaviors_tests.rb
|
data/lib/nm/template.rb
DELETED
@@ -1,103 +0,0 @@
|
|
1
|
-
require 'nm/source'
|
2
|
-
require 'nm/ext'
|
3
|
-
|
4
|
-
module Nm
|
5
|
-
|
6
|
-
class Template
|
7
|
-
|
8
|
-
def initialize(*args)
|
9
|
-
@__dstack__ = [nil]
|
10
|
-
|
11
|
-
# apply any given locals to template scope as methods
|
12
|
-
metaclass = class << self; self; end
|
13
|
-
metaclass.class_eval do
|
14
|
-
(args.last.kind_of?(::Hash) ? args.pop : {}).each do |key, value|
|
15
|
-
define_method(key){ value }
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
source_file = args.last.kind_of?(::String) ? args.pop : ''
|
20
|
-
@__source__ = args.last.kind_of?(Source) ? args.pop : DefaultSource.new
|
21
|
-
|
22
|
-
return if source_file.empty?
|
23
|
-
if !File.exists?(source_file)
|
24
|
-
raise ArgumentError, "source file `#{source_file}` does not exist"
|
25
|
-
end
|
26
|
-
|
27
|
-
instance_eval(@__source__.data(source_file), source_file, 1)
|
28
|
-
end
|
29
|
-
|
30
|
-
def inspect
|
31
|
-
"#<Template:#{@__source__.class}:#{'0x0%x' % (@__source__.object_id << 1)}"\
|
32
|
-
" @__source__.root=#{@__source__.root.to_s.inspect}"\
|
33
|
-
" __data__=#{self.__data__.inspect}>"
|
34
|
-
end
|
35
|
-
|
36
|
-
def __data__
|
37
|
-
@__dstack__.last || ::Hash.new
|
38
|
-
end
|
39
|
-
|
40
|
-
def __node__(key, value = nil, &block)
|
41
|
-
unless @__dstack__[-1].nil? || @__dstack__[-1].is_a?(::Hash)
|
42
|
-
raise Nm::InvalidError, "invalid `node` call"
|
43
|
-
end
|
44
|
-
@__dstack__[-1] ||= ::Hash.new
|
45
|
-
|
46
|
-
@__dstack__.push(nil)
|
47
|
-
self.instance_exec(&(block || Proc.new {}))
|
48
|
-
@__dstack__.pop.tap{ |v| @__dstack__[-1][key] = (v || value) }
|
49
|
-
|
50
|
-
return self
|
51
|
-
end
|
52
|
-
|
53
|
-
alias_method :node, :__node__
|
54
|
-
alias_method :_node, :__node__
|
55
|
-
alias_method :n, :__node__
|
56
|
-
|
57
|
-
def __map__(list, &block)
|
58
|
-
unless list.respond_to?(:map)
|
59
|
-
raise ArgumentError, "given list (`#{list.class}`) doesn't respond to `.map`"
|
60
|
-
end
|
61
|
-
unless @__dstack__[-1].nil? || @__dstack__[-1].is_a?(::Array)
|
62
|
-
raise Nm::InvalidError, "invalid `map` call"
|
63
|
-
end
|
64
|
-
@__dstack__[-1] ||= ::Array.new
|
65
|
-
|
66
|
-
list.map do |item|
|
67
|
-
@__dstack__.push(nil)
|
68
|
-
self.instance_exec(item, &(block || Proc.new {}))
|
69
|
-
@__dstack__.pop.tap{ |v| @__dstack__[-1].push(v || item) }
|
70
|
-
end
|
71
|
-
|
72
|
-
return self
|
73
|
-
end
|
74
|
-
|
75
|
-
alias_method :map, :__map__
|
76
|
-
alias_method :_map, :__map__
|
77
|
-
alias_method :m, :__map__
|
78
|
-
|
79
|
-
def __render__(*args)
|
80
|
-
data = @__source__.render(*args)
|
81
|
-
@__dstack__[-1] = @__dstack__[-1].__nm_add_call_data__('render', data)
|
82
|
-
|
83
|
-
return self
|
84
|
-
end
|
85
|
-
|
86
|
-
alias_method :render, :__render__
|
87
|
-
alias_method :_render, :__render__
|
88
|
-
alias_method :r, :__render__
|
89
|
-
|
90
|
-
def __partial__(*args)
|
91
|
-
data = @__source__.partial(*args)
|
92
|
-
@__dstack__[-1] = @__dstack__[-1].__nm_add_call_data__('partial', data)
|
93
|
-
|
94
|
-
return self
|
95
|
-
end
|
96
|
-
|
97
|
-
alias_method :partial, :__partial__
|
98
|
-
alias_method :_partial, :__partial__
|
99
|
-
alias_method :p, :__partial__
|
100
|
-
|
101
|
-
end
|
102
|
-
|
103
|
-
end
|
data/test/unit/template_tests.rb
DELETED
@@ -1,357 +0,0 @@
|
|
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 empty data if no markup meths called or no source given" do
|
29
|
-
assert_equal ::Hash.new, 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_template_name = "obj"
|
141
|
-
@obj = {
|
142
|
-
'obj' => {
|
143
|
-
'a' => 'Aye',
|
144
|
-
'b' => 'Bee',
|
145
|
-
'c' => 'See'
|
146
|
-
}
|
147
|
-
}
|
148
|
-
|
149
|
-
@list_template_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 template name and add its data" do
|
163
|
-
t = Nm::Template.new(@source)
|
164
|
-
assert_equal @obj, t.__render__(@obj_template_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_template_name).__data__
|
170
|
-
|
171
|
-
t = Nm::Template.new(@source)
|
172
|
-
assert_equal @obj, t.render(@obj_template_name).__data__
|
173
|
-
|
174
|
-
t = Nm::Template.new(@source)
|
175
|
-
assert_equal @obj, t._render(@obj_template_name).__data__
|
176
|
-
|
177
|
-
t = Nm::Template.new(@source)
|
178
|
-
assert_equal @obj, t.r(@obj_template_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_template_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_template_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_template_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_template_name).__data__
|
211
|
-
end
|
212
|
-
end
|
213
|
-
|
214
|
-
end
|
215
|
-
|
216
|
-
class PartialMethodTests < RenderTests
|
217
|
-
desc "`partial` method"
|
218
|
-
setup do
|
219
|
-
@partial_obj_template_name = "_obj"
|
220
|
-
@partial_obj = {
|
221
|
-
'a' => 'Aye',
|
222
|
-
'b' => 'Bee',
|
223
|
-
'c' => 'See'
|
224
|
-
}
|
225
|
-
@partial_list_template_name = "_list"
|
226
|
-
@partial_list = @list
|
227
|
-
end
|
228
|
-
|
229
|
-
should "render a template for the given partial name and add its data" do
|
230
|
-
t = Nm::Template.new(@source)
|
231
|
-
assert_equal @partial_obj, t.__partial__(@partial_obj_template_name).__data__
|
232
|
-
end
|
233
|
-
|
234
|
-
should "be aliased as `render`, `_render` and `r`" do
|
235
|
-
t = Nm::Template.new(@source)
|
236
|
-
assert_equal @partial_obj, t.__partial__(@partial_obj_template_name).__data__
|
237
|
-
|
238
|
-
t = Nm::Template.new(@source)
|
239
|
-
assert_equal @partial_obj, t.partial(@partial_obj_template_name).__data__
|
240
|
-
|
241
|
-
t = Nm::Template.new(@source)
|
242
|
-
assert_equal @partial_obj, t._partial(@partial_obj_template_name).__data__
|
243
|
-
|
244
|
-
t = Nm::Template.new(@source)
|
245
|
-
assert_equal @partial_obj, t.p(@partial_obj_template_name).__data__
|
246
|
-
end
|
247
|
-
|
248
|
-
should "merge if call returns an obj and called after a `__node__` call" do
|
249
|
-
t = Nm::Template.new(@source)
|
250
|
-
t.__node__('1', 'One')
|
251
|
-
|
252
|
-
exp = {'1' => 'One'}.merge(@partial_obj)
|
253
|
-
assert_equal exp, t.__partial__(@partial_obj_template_name).__data__
|
254
|
-
end
|
255
|
-
|
256
|
-
should "complain if call returns an obj and called after a `__map__` call" do
|
257
|
-
t = Nm::Template.new(@source)
|
258
|
-
t.__map__([1,2,3])
|
259
|
-
assert_raises Nm::InvalidError do
|
260
|
-
t.__partial__(@partial_obj_template_name).__data__
|
261
|
-
end
|
262
|
-
end
|
263
|
-
|
264
|
-
should "merge if call returns a list and called after a `__map__` call" do
|
265
|
-
t = Nm::Template.new(@source)
|
266
|
-
t.__map__([1,2,3])
|
267
|
-
|
268
|
-
exp = [1,2,3].concat(@partial_list)
|
269
|
-
assert_equal exp, t.__partial__(@partial_list_template_name).__data__
|
270
|
-
end
|
271
|
-
|
272
|
-
should "complain if call returns a list and called after a `__node__` call" do
|
273
|
-
t = Nm::Template.new(@source)
|
274
|
-
t.__node__('1', 'One')
|
275
|
-
|
276
|
-
assert_raises Nm::InvalidError do
|
277
|
-
t.__partial__(@partial_list_template_name).__data__
|
278
|
-
end
|
279
|
-
end
|
280
|
-
|
281
|
-
end
|
282
|
-
|
283
|
-
class SourceFileTests < UnitTests
|
284
|
-
desc "when init given a source file"
|
285
|
-
setup do
|
286
|
-
@obj_source_file = Factory.template_file('obj.nm')
|
287
|
-
@exp_obj = {
|
288
|
-
'obj' => {
|
289
|
-
'a' => 'Aye',
|
290
|
-
'b' => 'Bee',
|
291
|
-
'c' => 'See'
|
292
|
-
}
|
293
|
-
}
|
294
|
-
|
295
|
-
@list_source_file = Factory.template_file('list.nm')
|
296
|
-
@exp_list = [
|
297
|
-
{ '1' => 1 },
|
298
|
-
{ '2' => 2 },
|
299
|
-
{ '3' => 3 }
|
300
|
-
]
|
301
|
-
end
|
302
|
-
|
303
|
-
should "evaluate the source file" do
|
304
|
-
assert_equal @exp_obj, Nm::Template.new(@obj_source_file).__data__
|
305
|
-
assert_equal @exp_list, Nm::Template.new(@list_source_file).__data__
|
306
|
-
end
|
307
|
-
|
308
|
-
should "evaluate the source file with locals" do
|
309
|
-
assert_equal @exp_obj, Nm::Template.new(@obj_source_file, {}).__data__
|
310
|
-
assert_equal @exp_list, Nm::Template.new(@list_source_file, {}).__data__
|
311
|
-
end
|
312
|
-
|
313
|
-
end
|
314
|
-
|
315
|
-
class NoExistSourceFileTests < UnitTests
|
316
|
-
desc "when init given a source file that does not exist"
|
317
|
-
setup do
|
318
|
-
@no_exist_source_file = Factory.template_file('does-not-exist.nm')
|
319
|
-
end
|
320
|
-
|
321
|
-
should "complain that the source does not exist" do
|
322
|
-
assert_raises ArgumentError do
|
323
|
-
Nm::Template.new(@no_exist_source_file)
|
324
|
-
end
|
325
|
-
end
|
326
|
-
|
327
|
-
end
|
328
|
-
|
329
|
-
class LocalsTests < UnitTests
|
330
|
-
desc "when init with locals"
|
331
|
-
setup do
|
332
|
-
@locals = {
|
333
|
-
'key' => 'value',
|
334
|
-
'node' => 'A Node',
|
335
|
-
'map' => 'A Map'
|
336
|
-
}
|
337
|
-
end
|
338
|
-
|
339
|
-
should "expose the local as a reader method on the template" do
|
340
|
-
t = Nm::Template.new
|
341
|
-
assert_not_responds_to 'key', t
|
342
|
-
|
343
|
-
t = Nm::Template.new(@locals)
|
344
|
-
assert_equal 'value', t.key
|
345
|
-
end
|
346
|
-
|
347
|
-
should "not interfere with method aliases" do
|
348
|
-
d = Nm::Template.new(Factory.template_file('aliases.nm'), @locals).__data__
|
349
|
-
assert_kind_of ::Array, d
|
350
|
-
assert_equal 1, d.size
|
351
|
-
assert_equal 'A Node', d.first['node local value']
|
352
|
-
assert_equal 'A Map', d.first['map local value']
|
353
|
-
end
|
354
|
-
|
355
|
-
end
|
356
|
-
|
357
|
-
end
|