dslable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,52 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+ require "generators/gem_template"
4
+ require "dslable_dsl"
5
+
6
+ describe Dslable::Generators::GemTemplate do
7
+ context :generate do
8
+ OUTPUT_TMP_DIR = "generate_gem_template"
9
+
10
+ cases = [
11
+ {
12
+ case_no: 1,
13
+ case_title: "valid gem template",
14
+ gem_name: "sample_gem"
15
+ },
16
+ ]
17
+
18
+ cases.each do |c|
19
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
20
+ begin
21
+ case_before c
22
+
23
+ # -- given --
24
+ dsl = Dslable::Dsl.new
25
+ dsl.gem_name c[:gem_name]
26
+ gem_template = Dslable::Generators::GemTemplate.new(dsl)
27
+
28
+ # -- when --
29
+ gem_template.generate
30
+
31
+ # -- then --
32
+ actual = Dir.exists?("../#{c[:gem_name]}")
33
+ expect(actual).to be_true
34
+ ensure
35
+
36
+ case_after c
37
+ end
38
+ end
39
+
40
+ def case_before(c)
41
+ Dir.mkdir(OUTPUT_TMP_DIR) unless Dir.exists? OUTPUT_TMP_DIR
42
+ Dir.chdir(OUTPUT_TMP_DIR)
43
+ end
44
+
45
+ def case_after(c)
46
+ Dir.chdir("../../")
47
+ FileUtils.rm_rf(OUTPUT_TMP_DIR) if Dir.exists? OUTPUT_TMP_DIR
48
+ end
49
+ end
50
+ end
51
+
52
+ end
@@ -0,0 +1,111 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+ require "generators/product_codes/cli"
4
+
5
+ describe Dslable::Generators::ProductCodes::CLI do
6
+
7
+ context :generate do
8
+ OUTPUT_CLI_TMP_DIR = "generate_cli"
9
+ OUTPUT_CLI_CASE1 =<<-EOF
10
+ #!/usr/bin/env ruby
11
+ # encoding: utf-8
12
+
13
+ require "sample_gem_core"
14
+ require "sample_gem/version"
15
+ require "thor"
16
+
17
+ module SampleGem
18
+ #= SampleGem CLI
19
+ class CLI < Thor
20
+ class_option :help, :type => :boolean, :aliases => '-h', :desc => 'help message.'
21
+ class_option :version, :type => :boolean, :desc => 'version'
22
+
23
+ desc "execute", "TODO: write your desc"
24
+ def execute
25
+ SampleGem::Core.new.execute
26
+ end
27
+
28
+ desc "init", "generate Samplegemfile"
29
+ def init
30
+ SampleGem::Core.new.init
31
+ end
32
+
33
+ desc "version", "version"
34
+ def version
35
+ p SampleGem::VERSION
36
+ end
37
+ end
38
+ end
39
+
40
+ SampleGem::CLI.start(ARGV)
41
+ EOF
42
+ cases = [
43
+ {
44
+ case_no: 1,
45
+ case_title: "generate",
46
+ gem_name: "sample_gem",
47
+ bin_name: "samplegem",
48
+ fields: [:field1, :field2, :field3, :field4, :field5, :field6],
49
+ fields_descs: ["field_desc1", "field_desc2", "field_desc3", "field_desc4", "field_desc5", "field_desc6"],
50
+ args: [:args1, :args2, :args3, :args4, :args5, :args6],
51
+ args_klass: [String, Array, Hash, String, Array, Hash],
52
+ args_required: [true, true, true, false, false, false],
53
+ args_default: ["default1", ["default1", "default2"], {default_key: "default_value"}, nil, nil, nil],
54
+ expected_file: "./bin/samplegem",
55
+ expected_contents: OUTPUT_CLI_CASE1
56
+ },
57
+ ]
58
+
59
+ cases.each do |c|
60
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
61
+ begin
62
+ case_before c
63
+
64
+ # -- given --
65
+ dsl = Dslable::Dsl.new
66
+ gen_cli = Dslable::Generators::ProductCodes::CLI.new(setup_dsl(c, dsl))
67
+
68
+ # -- when --
69
+ gen_cli.generate
70
+
71
+ # -- then --
72
+ actual = File.open(c[:expected_file]) {|f|f.read}
73
+ expect(actual).to eq(c[:expected_contents])
74
+ ensure
75
+ case_after c
76
+
77
+ end
78
+ end
79
+
80
+ def case_before(c)
81
+ # implement each case before
82
+ Dir.mkdir(OUTPUT_CLI_TMP_DIR) unless Dir.exists? OUTPUT_CLI_TMP_DIR
83
+ Dir.chdir(OUTPUT_CLI_TMP_DIR)
84
+ Dir.mkdir "bin"
85
+ end
86
+
87
+ def setup_dsl(c, dsl)
88
+ dsl.gem_name c[:gem_name]
89
+ dsl.bin_name c[:bin_name]
90
+ c[:fields].each_with_index do |field, i|
91
+ dsl.field c[:fields][i] do |f|
92
+ f.desc c[:fields_descs][i]
93
+ f.args c[:args][i] do |a|
94
+ a.klass c[:args_klass][i]
95
+ a.required if c[:args_required][i]
96
+ a.default_value c[:args_default][i]
97
+ end
98
+ end
99
+ end
100
+ dsl
101
+ end
102
+
103
+ def case_after(c)
104
+ # implement each case after
105
+ Dir.chdir("../")
106
+ FileUtils.rm_rf(OUTPUT_CLI_TMP_DIR) if Dir.exists? OUTPUT_CLI_TMP_DIR
107
+ end
108
+ end
109
+ end
110
+
111
+ end
@@ -0,0 +1,139 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+ require "generators/product_codes/core"
4
+ require "dslable_dsl"
5
+
6
+ describe Dslable::Generators::ProductCodes::Core do
7
+ context :generate do
8
+ OUTPUT_CORE_TMP_DIR = "generate_core"
9
+ OUTPUT_CORE_CASE1 =<<-EOF
10
+ # encoding: utf-8
11
+ require 'sample_gem_dsl'
12
+
13
+ module SampleGem
14
+ # SampleGem Core
15
+ class Core
16
+ SAMPLE_GEM_FILE = "Samplegemfile"
17
+ SAMPLE_GEM_TEMPLATE =<<-EOS
18
+ # encoding: utf-8
19
+
20
+ # field_desc1
21
+ # args1 is required
22
+ # args1 allow only String
23
+ # args1's default value => "default1"
24
+ field1 "default1"
25
+
26
+ # field_desc2
27
+ # args2 is required
28
+ # args2 allow only Array
29
+ # args2's default value => ["default1", "default2"]
30
+ field2 ["default1", "default2"]
31
+
32
+ # field_desc3
33
+ # args3 is required
34
+ # args3 allow only Hash
35
+ # args3's default value => {:default_key=>"default_value"}
36
+ field3 {:default_key=>"default_value"}
37
+
38
+ # field_desc4
39
+ # args4 allow only String
40
+ field4 "your value"
41
+
42
+ # field_desc5
43
+ # args5 allow only Array
44
+ field5 ["your value"]
45
+
46
+ # field_desc6
47
+ # args6 allow only Hash
48
+ field6 {"your key"=>"your value"}
49
+
50
+ EOS
51
+
52
+ #== generate Samplegemfile to current directory.
53
+ def init
54
+ File.open(SAMPLE_GEM_FILE, "w") {|f|f.puts SAMPLE_GEM_TEMPLATE}
55
+ end
56
+
57
+ #== TODO: write your gem's specific contents
58
+ def execute
59
+ src = read_dsl
60
+ dsl = SampleGem::Dsl.new
61
+ dsl.instance_eval src
62
+
63
+ # TODO: implement your gem's specific logic
64
+
65
+ end
66
+
67
+ private
68
+ def read_dsl
69
+ File.open(SAMPLE_GEM_FILE) {|f|f.read}
70
+ end
71
+ end
72
+ end
73
+ EOF
74
+ cases = [
75
+ {
76
+ case_no: 1,
77
+ case_title: "valid generate core class",
78
+ gem_name: "sample_gem",
79
+ fields: [:field1, :field2, :field3, :field4, :field5, :field6],
80
+ fields_descs: ["field_desc1", "field_desc2", "field_desc3", "field_desc4", "field_desc5", "field_desc6"],
81
+ args: [:args1, :args2, :args3, :args4, :args5, :args6],
82
+ args_klass: [String, Array, Hash, String, Array, Hash],
83
+ args_required: [true, true, true, false, false, false],
84
+ args_default: ["default1", ["default1", "default2"], {default_key: "default_value"}, nil, nil, nil],
85
+ expected_file: "./lib/sample_gem_core.rb",
86
+ expected_contents: OUTPUT_CORE_CASE1
87
+ },
88
+
89
+ ]
90
+
91
+ cases.each do |c|
92
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
93
+ begin
94
+ case_before c
95
+
96
+ # -- given --
97
+ dsl = Dslable::Dsl.new
98
+ core = Dslable::Generators::ProductCodes::Core.new(setup_dsl(c, dsl))
99
+
100
+ # -- when --
101
+ core.generate
102
+
103
+ # -- then --
104
+ actual = File.open(c[:expected_file]) {|f|f.read}
105
+ expect(actual).to eq(c[:expected_contents])
106
+ ensure
107
+ case_after c
108
+ end
109
+ end
110
+
111
+ def case_before(c)
112
+ Dir.mkdir(OUTPUT_CORE_TMP_DIR) unless Dir.exists? OUTPUT_CORE_TMP_DIR
113
+ Dir.chdir(OUTPUT_CORE_TMP_DIR)
114
+ Dir.mkdir "lib"
115
+ end
116
+
117
+ def setup_dsl(c, dsl)
118
+ dsl.gem_name c[:gem_name]
119
+ c[:fields].each_with_index do |field, i|
120
+ dsl.field c[:fields][i] do |f|
121
+ f.desc c[:fields_descs][i]
122
+ f.args c[:args][i] do |a|
123
+ a.klass c[:args_klass][i]
124
+ a.required if c[:args_required][i]
125
+ a.default_value c[:args_default][i]
126
+ end
127
+ end
128
+ end
129
+ dsl
130
+ end
131
+
132
+ def case_after(c)
133
+ Dir.chdir("../")
134
+ FileUtils.rm_rf(OUTPUT_CORE_TMP_DIR) if Dir.exists? OUTPUT_CORE_TMP_DIR
135
+ end
136
+ end
137
+ end
138
+
139
+ end
@@ -0,0 +1,109 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+ require "generators/product_codes/dsl_model"
4
+
5
+ describe Dslable::Generators::ProductCodes::DslModel do
6
+
7
+ context :generate do
8
+ OUTPUT_DSL_MODEL_TMP_DIR = "generate_dsl_model"
9
+ OUTPUT_DSL_MODEL_CASE1 =<<-EOF
10
+ # encoding: utf-8
11
+ require 'active_model'
12
+
13
+ module SampleGem
14
+ class DslModel
15
+ include ActiveModel::Model
16
+
17
+ # field_desc1
18
+ attr_accessor :field1
19
+ validates :field1, :presence => true
20
+
21
+ # field_desc2
22
+ attr_accessor :field2
23
+ validates :field2, :presence => true
24
+
25
+ # field_desc3
26
+ attr_accessor :field3
27
+ validates :field3, :presence => true
28
+
29
+ # field_desc4
30
+ attr_accessor :field4
31
+
32
+ # field_desc5
33
+ attr_accessor :field5
34
+
35
+ # field_desc6
36
+ attr_accessor :field6
37
+
38
+ end
39
+ end
40
+ EOF
41
+
42
+ cases = [
43
+ {
44
+ case_no: 1,
45
+ case_title: "generate",
46
+ gem_name: "sample_gem",
47
+ fields: [:field1, :field2, :field3, :field4, :field5, :field6],
48
+ fields_descs: ["field_desc1", "field_desc2", "field_desc3", "field_desc4", "field_desc5", "field_desc6"],
49
+ args: [:args1, :args2, :args3, :args4, :args5, :args6],
50
+ args_klass: [String, Array, Hash, String, Array, Hash],
51
+ args_required: [true, true, true, false, false, false],
52
+ args_default: ["default1", ["default1", "default2"], {default_key: "default_value"}, nil, nil, nil],
53
+ expected_file: "./lib/sample_gem_dsl_model.rb",
54
+ expected_contents: OUTPUT_DSL_MODEL_CASE1
55
+ },
56
+ ]
57
+
58
+ cases.each do |c|
59
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
60
+ begin
61
+ case_before c
62
+
63
+ # -- given --
64
+ dsl = Dslable::Dsl.new
65
+ gen_dsl_model = Dslable::Generators::ProductCodes::DslModel.new(setup_dsl(c, dsl))
66
+
67
+ # -- when --
68
+ gen_dsl_model.generate
69
+
70
+ # -- then --
71
+ actual = File.open(c[:expected_file]) {|f|f.read}
72
+ expect(actual).to eq(c[:expected_contents])
73
+ ensure
74
+ case_after c
75
+
76
+ end
77
+ end
78
+
79
+ def case_before(c)
80
+ # implement each case before
81
+ Dir.mkdir(OUTPUT_DSL_MODEL_TMP_DIR) unless Dir.exists? OUTPUT_DSL_MODEL_TMP_DIR
82
+ Dir.chdir(OUTPUT_DSL_MODEL_TMP_DIR)
83
+ Dir.mkdir "lib"
84
+ end
85
+
86
+ def setup_dsl(c, dsl)
87
+ dsl.gem_name c[:gem_name]
88
+ c[:fields].each_with_index do |field, i|
89
+ dsl.field c[:fields][i] do |f|
90
+ f.desc c[:fields_descs][i]
91
+ f.args c[:args][i] do |a|
92
+ a.klass c[:args_klass][i]
93
+ a.required if c[:args_required][i]
94
+ a.default_value c[:args_default][i]
95
+ end
96
+ end
97
+ end
98
+ dsl
99
+ end
100
+
101
+ def case_after(c)
102
+ # implement each case after
103
+ Dir.chdir("../")
104
+ FileUtils.rm_rf(OUTPUT_DSL_MODEL_TMP_DIR) if Dir.exists? OUTPUT_DSL_MODEL_TMP_DIR
105
+ end
106
+ end
107
+ end
108
+
109
+ end
@@ -0,0 +1,110 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+ require "generators/product_codes/dsl"
4
+ require "dslable_dsl"
5
+
6
+ describe Dslable::Generators::ProductCodes::Dsl do
7
+
8
+ context :generate do
9
+ OUTPUT_DSL_TMP_DIR = "generate_dsl"
10
+ OUTPUT_DSL_CASE1 =<<-EOF
11
+ # encoding: utf-8
12
+ require 'sample_gem_dsl_model'
13
+
14
+ module SampleGem
15
+ class Dsl
16
+ attr_accessor :sample_gem
17
+
18
+ # String Define
19
+ [:field1, :field4].each do |f|
20
+ define_method f do |value|
21
+ eval "@sample_gem.#\{f.to_s} = '#\{value}'", binding
22
+ end
23
+ end
24
+
25
+ # Array/Hash Define
26
+ [:field2, :field3, :field5, :field6].each do |f|
27
+ define_method f do |value|
28
+ eval "@sample_gem.#\{f.to_s} = #\{value}", binding
29
+ end
30
+ end
31
+
32
+ def initialize
33
+ @sample_gem = SampleGem::DslModel.new
34
+ @sample_gem.field1 = 'default1'
35
+ @sample_gem.field2 = ["default1", "default2"]
36
+ @sample_gem.field3 = {:default_key=>"default_value"}
37
+ end
38
+ end
39
+ end
40
+ EOF
41
+
42
+ cases = [
43
+ {
44
+ case_no: 1,
45
+ case_title: "generate",
46
+ gem_name: "sample_gem",
47
+ fields: [:field1, :field2, :field3, :field4, :field5, :field6],
48
+ fields_descs: ["field_desc1", "field_desc2", "field_desc3", "field_desc4", "field_desc5", "field_desc6"],
49
+ args: [:args1, :args2, :args3, :args4, :args5, :args6],
50
+ args_klass: [String, Array, Hash, String, Array, Hash],
51
+ args_required: [true, true, true, false, false, false],
52
+ args_default: ["default1", ["default1", "default2"], {default_key: "default_value"}, nil, nil, nil],
53
+ expected_file: "./lib/sample_gem_dsl.rb",
54
+ expected_contents: OUTPUT_DSL_CASE1
55
+
56
+ },
57
+ ]
58
+
59
+ cases.each do |c|
60
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
61
+ begin
62
+ case_before c
63
+
64
+ # -- given --
65
+ dsl = Dslable::Dsl.new
66
+ gen_dsl = Dslable::Generators::ProductCodes::Dsl.new(setup_dsl(c, dsl))
67
+
68
+ # -- when --
69
+ gen_dsl.generate
70
+
71
+ # -- then --
72
+ actual = File.open(c[:expected_file]) {|f|f.read}
73
+ expect(actual).to eq(c[:expected_contents])
74
+ ensure
75
+ case_after c
76
+
77
+ end
78
+ end
79
+
80
+ def case_before(c)
81
+ # implement each case before
82
+ Dir.mkdir(OUTPUT_DSL_TMP_DIR) unless Dir.exists? OUTPUT_DSL_TMP_DIR
83
+ Dir.chdir(OUTPUT_DSL_TMP_DIR)
84
+ Dir.mkdir "lib"
85
+ end
86
+
87
+ def setup_dsl(c, dsl)
88
+ dsl.gem_name c[:gem_name]
89
+ c[:fields].each_with_index do |field, i|
90
+ dsl.field c[:fields][i] do |f|
91
+ f.desc c[:fields_descs][i]
92
+ f.args c[:args][i] do |a|
93
+ a.klass c[:args_klass][i]
94
+ a.required if c[:args_required][i]
95
+ a.default_value c[:args_default][i]
96
+ end
97
+ end
98
+ end
99
+ dsl
100
+ end
101
+
102
+ def case_after(c)
103
+ # implement each case after
104
+ Dir.chdir("../")
105
+ FileUtils.rm_rf(OUTPUT_DSL_TMP_DIR) if Dir.exists? OUTPUT_DSL_TMP_DIR
106
+ end
107
+ end
108
+ end
109
+
110
+ end