reality-generators 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitattributes +6 -0
- data/.gitignore +8 -0
- data/.ruby-version +1 -0
- data/.travis.yml +11 -0
- data/CONTRIBUTING.md +26 -0
- data/Gemfile +5 -0
- data/LICENSE +201 -0
- data/README.md +61 -0
- data/Rakefile +18 -0
- data/lib/reality/generators/core.rb +19 -0
- data/lib/reality/generators/erb_template.rb +48 -0
- data/lib/reality/generators/render_context.rb +47 -0
- data/lib/reality/generators/ruby_template.rb +62 -0
- data/lib/reality/generators/target_manager.rb +100 -0
- data/lib/reality/generators/template.rb +152 -0
- data/lib/reality/generators/template_set.rb +64 -0
- data/lib/reality/generators/template_set_container.rb +65 -0
- data/lib/reality/generators.rb +28 -0
- data/reality-generators.gemspec +30 -0
- data/test/generators/templates/mytemplate.java.erb +6 -0
- data/test/generators/templates/rubytemplate.rb +3 -0
- data/test/generators/test_erb_template.rb +53 -0
- data/test/generators/test_render_context.rb +44 -0
- data/test/generators/test_ruby_template.rb +46 -0
- data/test/generators/test_target_manager.rb +89 -0
- data/test/generators/test_template.rb +155 -0
- data/test/generators/test_template_set.rb +53 -0
- data/test/generators/test_template_set_container.rb +35 -0
- data/test/helper.rb +45 -0
- metadata +145 -0
@@ -0,0 +1,155 @@
|
|
1
|
+
require File.expand_path('../../helper', __FILE__)
|
2
|
+
|
3
|
+
class Reality::Generators::TestTemplate < Reality::TestCase
|
4
|
+
module MyHelper
|
5
|
+
def a
|
6
|
+
'A'
|
7
|
+
end
|
8
|
+
|
9
|
+
def b
|
10
|
+
'B'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class SimpleModel
|
15
|
+
def name
|
16
|
+
'SimpleModel'
|
17
|
+
end
|
18
|
+
|
19
|
+
def facet_enabled?(facet)
|
20
|
+
true
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class SimpleModel2
|
25
|
+
def name
|
26
|
+
'SimpleModel'
|
27
|
+
end
|
28
|
+
|
29
|
+
def facet_enabled?(facet)
|
30
|
+
false
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class ComplexModel
|
35
|
+
def name
|
36
|
+
'ComplexModel'
|
37
|
+
end
|
38
|
+
|
39
|
+
def qualified_name
|
40
|
+
'Module.ComplexModel'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class TestTemplate < Reality::Generators::Template
|
45
|
+
def output_path
|
46
|
+
'main/java/com/biz/foo'
|
47
|
+
end
|
48
|
+
|
49
|
+
def generate!(target_basedir, element, unprocessed_files)
|
50
|
+
# Clear the unprocessed files so test can determine this method is reached
|
51
|
+
unprocessed_files.clear
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_template
|
56
|
+
template_set = Reality::Generators::TemplateSet.new(TestTemplateSetContainer, 'foo')
|
57
|
+
|
58
|
+
facets = [:jpa, :ee]
|
59
|
+
target = :component
|
60
|
+
template_key = 'someMagicKey'
|
61
|
+
helpers = [MyHelper]
|
62
|
+
guard = 'component.name == "SimpleModel"'
|
63
|
+
extra_data = {'x' => 'X'}
|
64
|
+
name = 'Foo'
|
65
|
+
options = {:guard => guard, :name => name, :extra_data => extra_data}
|
66
|
+
|
67
|
+
assert_raise_message('Unexpected facets: "X"') {
|
68
|
+
Reality::Generators::Template.new(template_set, 'X', target, template_key, helpers, options)
|
69
|
+
}
|
70
|
+
assert_raise_message("Unknown target 'component' for template 'someMagicKey'. Valid targets include: ") {
|
71
|
+
Reality::Generators::Template.new(template_set, facets, target, template_key, helpers, options)
|
72
|
+
}
|
73
|
+
|
74
|
+
TestTemplateSetContainer.target_manager.target(target)
|
75
|
+
|
76
|
+
template1 = Reality::Generators::Template.new(template_set, facets, target, template_key, helpers, options)
|
77
|
+
|
78
|
+
assert_equal template_set, template1.template_set
|
79
|
+
assert_equal facets, template1.facets
|
80
|
+
assert_equal target, template1.target
|
81
|
+
assert_equal helpers, template1.helpers
|
82
|
+
assert_equal template_key, template1.template_key
|
83
|
+
assert_equal guard, template1.guard
|
84
|
+
assert_equal extra_data, template1.extra_data
|
85
|
+
assert_equal name, template1.name
|
86
|
+
assert_equal name, template1.to_s
|
87
|
+
|
88
|
+
assert_raise_message('output_path unimplemented') { template1.output_path }
|
89
|
+
|
90
|
+
render_context = template1.send(:create_context, 'SomeValue')
|
91
|
+
|
92
|
+
assert_equal 'SomeValue', eval("component rescue 'Missing'", render_context.context_binding)
|
93
|
+
assert_equal 'A', eval("a rescue 'Missing'", render_context.context_binding)
|
94
|
+
assert_equal 'B', eval("b rescue 'Missing'", render_context.context_binding)
|
95
|
+
assert_equal 'X', eval("x rescue 'Missing'", render_context.context_binding)
|
96
|
+
|
97
|
+
assert_equal 'SimpleModel', template1.send(:name_for_element, SimpleModel.new)
|
98
|
+
assert_equal 'Module.ComplexModel', template1.send(:name_for_element, ComplexModel.new)
|
99
|
+
|
100
|
+
assert_equal 'Module.ComplexModel', template1.send(:name_for_element, ComplexModel.new)
|
101
|
+
|
102
|
+
unprocessed_files1 = %w(A B C)
|
103
|
+
|
104
|
+
template1.generate('Ignored', ComplexModel.new, unprocessed_files1)
|
105
|
+
# ComplexModel not processed as guard protects against it
|
106
|
+
assert_equal 3, unprocessed_files1.size
|
107
|
+
|
108
|
+
TestTemplate.new(template_set, facets, target, template_key + '1', helpers, {}).
|
109
|
+
generate('Ignored', SimpleModel.new, unprocessed_files1)
|
110
|
+
assert_equal 0, unprocessed_files1.size
|
111
|
+
|
112
|
+
assert_equal true, Reality::Generators::Template.new(template_set, [], target, template_key + '2', helpers, {}).applicable?(SimpleModel.new)
|
113
|
+
assert_equal true, Reality::Generators::Template.new(template_set, facets, target, template_key + '3', helpers, {}).applicable?(SimpleModel.new)
|
114
|
+
assert_equal false, Reality::Generators::Template.new(template_set, facets, target, template_key + '4', helpers, {}).applicable?(SimpleModel2.new)
|
115
|
+
assert_equal false, Reality::Generators::Template.new(template_set, facets, target, template_key + '5', helpers, {}).applicable?(ComplexModel.new)
|
116
|
+
end
|
117
|
+
|
118
|
+
class StringTemplate < Reality::Generators::SingleFileOutputTemplate
|
119
|
+
def render_to_string(context_binding)
|
120
|
+
'X'
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_single_file_template
|
125
|
+
template_set = Reality::Generators::TemplateSet.new(TestTemplateSetContainer, 'foo')
|
126
|
+
|
127
|
+
output_filename_pattern = 'main/java/#{component.name}.java'
|
128
|
+
template_key = 'MyFiles/templates/foo.erb.java'
|
129
|
+
TestTemplateSetContainer.target_manager.target(:component)
|
130
|
+
|
131
|
+
template1 = StringTemplate.new(template_set, [], :component, template_key, output_filename_pattern, [], {})
|
132
|
+
|
133
|
+
assert_equal output_filename_pattern, template1.output_filename_pattern
|
134
|
+
assert_equal output_filename_pattern, template1.output_path
|
135
|
+
assert_equal template_set, template1.template_set
|
136
|
+
assert_equal [], template1.facets
|
137
|
+
assert_equal :component, template1.target
|
138
|
+
assert_equal [], template1.helpers
|
139
|
+
assert_equal template_key, template1.template_key
|
140
|
+
assert_equal nil, template1.guard
|
141
|
+
assert_equal({}, template1.extra_data)
|
142
|
+
assert_equal 'foo:MyFiles/templates/foo.erb.java', template1.name
|
143
|
+
|
144
|
+
target_basedir = "#{temp_dir}/generated/single_file_template"
|
145
|
+
target_filename = "#{target_basedir}/main/java/SimpleModel.java"
|
146
|
+
other_filename = "#{target_basedir}/main/java/Other.java"
|
147
|
+
unprocessed_files = %W(#{target_filename} #{other_filename})
|
148
|
+
assert_equal false, File.exist?(target_filename)
|
149
|
+
template1.generate(target_basedir, SimpleModel.new, unprocessed_files)
|
150
|
+
assert_equal true, File.exist?(target_filename)
|
151
|
+
assert_equal 1, unprocessed_files.size
|
152
|
+
|
153
|
+
assert_equal 'X', IO.read(target_filename)
|
154
|
+
end
|
155
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require File.expand_path('../../helper', __FILE__)
|
2
|
+
|
3
|
+
class Reality::Generators::TestTemplateSet < Reality::TestCase
|
4
|
+
def test_template_set
|
5
|
+
TestTemplateSetContainer.target_manager.target(:component)
|
6
|
+
|
7
|
+
assert_raise_message("TemplateSet 'iris_entity' defined requirement on template set 'iris_shared' that does not exist.") {
|
8
|
+
Reality::Generators::TemplateSet.new(TestTemplateSetContainer,
|
9
|
+
:iris_entity,
|
10
|
+
:required_template_sets => [:iris_shared])
|
11
|
+
}
|
12
|
+
|
13
|
+
template_set1 = Reality::Generators::TemplateSet.new(TestTemplateSetContainer, :iris_shared)
|
14
|
+
|
15
|
+
template_set2 =
|
16
|
+
Reality::Generators::TemplateSet.new(TestTemplateSetContainer,
|
17
|
+
:iris_entity,
|
18
|
+
:required_template_sets => [:iris_shared],
|
19
|
+
:description => 'Templates that generate iris entities')
|
20
|
+
|
21
|
+
assert_equal :iris_shared, template_set1.name
|
22
|
+
assert_equal [], template_set1.required_template_sets
|
23
|
+
assert_equal 0, template_set1.templates.size
|
24
|
+
assert_equal nil, template_set1.description
|
25
|
+
|
26
|
+
assert_equal :iris_entity, template_set2.name
|
27
|
+
assert_equal [:iris_shared], template_set2.required_template_sets
|
28
|
+
assert_equal 0, template_set2.templates.size
|
29
|
+
assert_equal 'Templates that generate iris entities', template_set2.description
|
30
|
+
|
31
|
+
template = Reality::Generators::ErbTemplate.new(template_set2,
|
32
|
+
[],
|
33
|
+
:component,
|
34
|
+
'templates/mytemplate.java.erb',
|
35
|
+
'src/main/#{component.name}',
|
36
|
+
[],
|
37
|
+
{})
|
38
|
+
assert_equal 1, template_set2.templates.size
|
39
|
+
assert_equal true, template_set2.template_by_name?(template.name)
|
40
|
+
assert_equal template, template_set2.template_by_name(template.name)
|
41
|
+
|
42
|
+
assert_raise_message('Template already exists with specified name iris_entity:templates/mytemplate.java.erb') {
|
43
|
+
Reality::Generators::ErbTemplate.new(template_set2,
|
44
|
+
[],
|
45
|
+
:component,
|
46
|
+
'templates/mytemplate.java.erb',
|
47
|
+
'src/main/#{component.name}',
|
48
|
+
[],
|
49
|
+
{})
|
50
|
+
}
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require File.expand_path('../../helper', __FILE__)
|
2
|
+
|
3
|
+
class Reality::Generators::TestTemplateSetContainer < Reality::TestCase
|
4
|
+
def test_template_set_container
|
5
|
+
assert_equal 0, TestTemplateSetContainer.template_sets.size
|
6
|
+
assert_equal false, TestTemplateSetContainer.template_set_by_name?(:foo)
|
7
|
+
assert_raise_message('Unable to locate template_set foo') { TestTemplateSetContainer.template_set_by_name(:foo) }
|
8
|
+
|
9
|
+
assert_raise_message("TemplateSet 'iris_entity' defined requirement on template set 'iris_shared' that does not exist.") {
|
10
|
+
Reality::Generators::TemplateSet.new(TestTemplateSetContainer,
|
11
|
+
:iris_entity,
|
12
|
+
:required_template_sets => [:iris_shared])
|
13
|
+
}
|
14
|
+
|
15
|
+
template_set1 = TestTemplateSetContainer.template_set(:foo)
|
16
|
+
|
17
|
+
assert_equal 1, TestTemplateSetContainer.template_sets.size
|
18
|
+
assert_equal true, TestTemplateSetContainer.template_set_by_name?(:foo)
|
19
|
+
assert_equal template_set1, TestTemplateSetContainer.template_set_by_name(:foo)
|
20
|
+
|
21
|
+
template_set2 = TestTemplateSetContainer.template_set(:bar => :foo)
|
22
|
+
|
23
|
+
assert_equal 2, TestTemplateSetContainer.template_sets.size
|
24
|
+
assert_equal true, TestTemplateSetContainer.template_set_by_name?(:bar)
|
25
|
+
assert_equal template_set2, TestTemplateSetContainer.template_set_by_name(:bar)
|
26
|
+
assert_equal [:foo], template_set2.required_template_sets
|
27
|
+
|
28
|
+
template_set3 = TestTemplateSetContainer.template_set(:baz => [:foo, :bar])
|
29
|
+
|
30
|
+
assert_equal 3, TestTemplateSetContainer.template_sets.size
|
31
|
+
assert_equal true, TestTemplateSetContainer.template_set_by_name?(:baz)
|
32
|
+
assert_equal template_set3, TestTemplateSetContainer.template_set_by_name(:baz)
|
33
|
+
assert_equal [:foo, :bar], template_set3.required_template_sets
|
34
|
+
end
|
35
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
$:.unshift File.expand_path('../../lib', __FILE__)
|
2
|
+
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require 'test/unit/assertions'
|
5
|
+
require 'reality/generators'
|
6
|
+
|
7
|
+
class Reality::TestCase < Minitest::Test
|
8
|
+
include Test::Unit::Assertions
|
9
|
+
|
10
|
+
module TestTemplateSetContainer
|
11
|
+
class << self
|
12
|
+
include Reality::Generators::TemplateSetContainer
|
13
|
+
|
14
|
+
def new_template_set(name, options, &block)
|
15
|
+
Reality::Generators::TemplateSet.new(self, name, options, &block)
|
16
|
+
end
|
17
|
+
|
18
|
+
def reset
|
19
|
+
template_set_map.clear
|
20
|
+
target_manager.reset_targets
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def setup
|
26
|
+
TestTemplateSetContainer.reset
|
27
|
+
@temp_dir = nil
|
28
|
+
end
|
29
|
+
|
30
|
+
def teardown
|
31
|
+
unless @temp_dir.nil?
|
32
|
+
FileUtils.rm_rf @temp_dir unless ENV['NO_DELETE_DIR'] == 'true'
|
33
|
+
@temp_dir = nil
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def temp_dir
|
38
|
+
if @temp_dir.nil?
|
39
|
+
base_temp_dir = ENV['TEST_TMP_DIR'] || File.expand_path("#{File.dirname(__FILE__)}/../tmp")
|
40
|
+
@temp_dir = "#{base_temp_dir}/generators-#{Time.now.to_i}"
|
41
|
+
FileUtils.mkdir_p @temp_dir
|
42
|
+
end
|
43
|
+
@temp_dir
|
44
|
+
end
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: reality-generators
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Peter Donald
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: reality-core
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.4.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.4.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: reality-naming
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.4.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.4.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: reality-orderedhash
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.0.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.0.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 5.9.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 5.9.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: test-unit
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 3.1.5
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 3.1.5
|
83
|
+
description: A basic toolkit for abstracting the generation of files from model objects.
|
84
|
+
email: peter@realityforge.org
|
85
|
+
executables: []
|
86
|
+
extensions: []
|
87
|
+
extra_rdoc_files: []
|
88
|
+
files:
|
89
|
+
- ".gitattributes"
|
90
|
+
- ".gitignore"
|
91
|
+
- ".ruby-version"
|
92
|
+
- ".travis.yml"
|
93
|
+
- CONTRIBUTING.md
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- lib/reality/generators.rb
|
99
|
+
- lib/reality/generators/core.rb
|
100
|
+
- lib/reality/generators/erb_template.rb
|
101
|
+
- lib/reality/generators/render_context.rb
|
102
|
+
- lib/reality/generators/ruby_template.rb
|
103
|
+
- lib/reality/generators/target_manager.rb
|
104
|
+
- lib/reality/generators/template.rb
|
105
|
+
- lib/reality/generators/template_set.rb
|
106
|
+
- lib/reality/generators/template_set_container.rb
|
107
|
+
- reality-generators.gemspec
|
108
|
+
- test/generators/templates/mytemplate.java.erb
|
109
|
+
- test/generators/templates/rubytemplate.rb
|
110
|
+
- test/generators/test_erb_template.rb
|
111
|
+
- test/generators/test_render_context.rb
|
112
|
+
- test/generators/test_ruby_template.rb
|
113
|
+
- test/generators/test_target_manager.rb
|
114
|
+
- test/generators/test_template.rb
|
115
|
+
- test/generators/test_template_set.rb
|
116
|
+
- test/generators/test_template_set_container.rb
|
117
|
+
- test/helper.rb
|
118
|
+
homepage: https://github.com/realityforge/reality-generators
|
119
|
+
licenses: []
|
120
|
+
metadata: {}
|
121
|
+
post_install_message:
|
122
|
+
rdoc_options:
|
123
|
+
- "--line-numbers"
|
124
|
+
- "--inline-source"
|
125
|
+
- "--title"
|
126
|
+
- reality-generators
|
127
|
+
require_paths:
|
128
|
+
- lib
|
129
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
requirements: []
|
140
|
+
rubyforge_project:
|
141
|
+
rubygems_version: 2.5.1
|
142
|
+
signing_key:
|
143
|
+
specification_version: 4
|
144
|
+
summary: A basic toolkit for abstracting the generation of files from model objects.
|
145
|
+
test_files: []
|