dslable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,175 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+ require "dslable_core"
4
+ require "fileutils"
5
+
6
+ describe Dslable::Core do
7
+ context :init do
8
+ cases = [
9
+ {
10
+ case_no: 1,
11
+ case_title: "output template",
12
+ expected: Dslable::Core::DSLDEFINE_TEMPLATE
13
+ },
14
+ ]
15
+
16
+ cases.each do |c|
17
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
18
+ begin
19
+ case_before c
20
+
21
+ # -- given --
22
+ dslable_core = Dslable::Core.new
23
+
24
+ # -- when --
25
+ dslable_core.init
26
+
27
+ # -- then --
28
+ actual = File.open(Dslable::Core::DSLDEFINE_FILE) {|f|f.read}
29
+ expect(actual).to eq(c[:expected])
30
+ ensure
31
+ case_after c
32
+ end
33
+ end
34
+
35
+ def case_before(c)
36
+ # implement each case before
37
+ end
38
+
39
+ def case_after(c)
40
+ return unless File.exists? Dslable::Core::DSLDEFINE_FILE
41
+ File.delete(Dslable::Core::DSLDEFINE_FILE)
42
+ end
43
+ end
44
+ end
45
+
46
+ GENERATE_DSLABLE_GEM_CASE =<<-EOS
47
+ # encoding: utf-8
48
+ gem_name "sample_gem"
49
+
50
+ field :sample_field1 do |f|
51
+ f.desc "sample_field1 description"
52
+ f.args :sample_args1 do |a|
53
+ a.desc "sample_args1 description"
54
+ a.klass String
55
+ a.required
56
+ a.default_value "sample_field1_default"
57
+ end
58
+ end
59
+
60
+ field :sample_field2 do |f|
61
+ f.desc "sample_field2 description"
62
+ f.args :sample_args2 do |a|
63
+ a.desc "sample_args2 description"
64
+ a.klass Array
65
+ end
66
+ end
67
+
68
+ field :sample_field3 do |f|
69
+ f.desc "sample_field3 description"
70
+ f.args :sample_args3 do |a|
71
+ a.desc "sample_args3 description"
72
+ a.klass Hash
73
+ a.default_value :hash_sample_key1 => "hash_sample_value1", :hash_sample_key2 => "hash_sample_value2"
74
+ end
75
+ end
76
+ EOS
77
+
78
+ context :generate do
79
+ OUTPUT_CORE_TMP_DIR = "core_tmp"
80
+ DSLDEFINE_TEMPLATE1 = <<-EOS
81
+ # encoding: utf-8
82
+
83
+ # set your gem name. this is use in rb-filename and class-name
84
+ gem_name "sample_gem"
85
+
86
+ # set your bin name
87
+ bin_name "bin_name"
88
+
89
+ # set your dsl filed
90
+ field :field_name1 do |f|
91
+ # set your field description
92
+ f.desc "field1 description"
93
+ f.args :args_name do |a|
94
+ # set your args description
95
+ a.desc "args description"
96
+ # you can use String, Array and Hash
97
+ a.klass String
98
+ # if you want not required, comment out following line
99
+ a.required
100
+ # if you comment out following line, default => nil
101
+ a.default_value "args_value"
102
+ end
103
+ end
104
+
105
+ field :field_name2 do |f|
106
+ f.desc "field2 description"
107
+ f.args :args_name do |a|
108
+ a.desc "args description"
109
+ a.klass Array
110
+ a.default_value ["args_value1", "args_value2"]
111
+ end
112
+ end
113
+ EOS
114
+
115
+ cases = [
116
+ {
117
+ case_no: 1,
118
+ case_title: "output some_core, some_dsl, some_dsl_model and their spec.",
119
+ define_src: DSLDEFINE_TEMPLATE1,
120
+ gem_name: "sample_gem",
121
+ expected_files: [
122
+ "./sample_gem/Gemfile",
123
+ "./sample_gem/sample_gem.gemspec",
124
+ "./sample_gem/.gitignore",
125
+ "./sample_gem/.rspec",
126
+ "./sample_gem/Rakefile",
127
+ "./sample_gem/README.md",
128
+ "./sample_gem/lib/sample_gem_core.rb",
129
+ "./sample_gem/lib/sample_gem_dsl.rb",
130
+ "./sample_gem/lib/sample_gem_dsl_model.rb",
131
+ "./sample_gem/bin/bin_name",
132
+ "./sample_gem/spec/spec_helper.rb",
133
+ "./sample_gem/spec/sample_gem_core_spec.rb",
134
+ ]
135
+ },
136
+ ]
137
+
138
+ cases.each do |c|
139
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
140
+ begin
141
+ case_before c
142
+
143
+ # -- given --
144
+ dslable_core = Dslable::Core.new
145
+
146
+ # -- when --
147
+ dslable_core.generate
148
+
149
+ # -- then --
150
+ Dir.chdir("../")
151
+ c[:expected_files].each do |expected_file|
152
+ actual = File.exists? expected_file
153
+ expect(actual).to be_true
154
+ end
155
+ ensure
156
+ case_after c
157
+ end
158
+ end
159
+
160
+ def case_before(c)
161
+ # implement each case before
162
+ Dir.mkdir(OUTPUT_CORE_TMP_DIR) unless Dir.exists? OUTPUT_CORE_TMP_DIR
163
+ Dir.chdir(OUTPUT_CORE_TMP_DIR)
164
+ File.open(Dslable::Core::DSLDEFINE_FILE, "w") {|f|f.puts c[:define_src]}
165
+ end
166
+
167
+ def case_after(c)
168
+ # implement each case after
169
+ Dir.chdir("../")
170
+ FileUtils.rm_rf(OUTPUT_CORE_TMP_DIR) if Dir.exists? OUTPUT_CORE_TMP_DIR
171
+ end
172
+ end
173
+ end
174
+
175
+ end
@@ -0,0 +1,235 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+ require "dslable_dsl"
4
+
5
+ describe Dslable::Dsl do
6
+ context :gem_name do
7
+ cases = [
8
+ {
9
+ case_no: 1,
10
+ case_title: "valid gem name",
11
+ input: "abcdefghijklmnopqrstuvwxyz01234567891_",
12
+ expected: "abcdefghijklmnopqrstuvwxyz01234567891_"
13
+ },
14
+ {
15
+ case_no: 2,
16
+ case_title: "empty gem name",
17
+ input: "",
18
+ expect_error: true
19
+ },
20
+ {
21
+ case_no: 3,
22
+ case_title: "nil gem name",
23
+ input: nil,
24
+ expect_error: true
25
+ },
26
+ {
27
+ case_no: 4,
28
+ case_title: "invalid gem name contain space",
29
+ input: "gem name",
30
+ expect_error: true
31
+ },
32
+ ]
33
+
34
+ cases.each do |c|
35
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
36
+ begin
37
+ case_before c
38
+
39
+ # -- given --
40
+ dslable_dsl = Dslable::Dsl.new
41
+
42
+ # -- when --
43
+ if c[:expect_error]
44
+ lambda {dslable_dsl.gem_name c[:input]}.should raise_error(Dslable::InvalidDslError)
45
+ next
46
+ end
47
+ dslable_dsl.gem_name c[:input]
48
+ actual = dslable_dsl._gem_name
49
+
50
+ # -- then --
51
+ expect(actual).to eq(c[:expected])
52
+ ensure
53
+ case_after c
54
+ end
55
+ end
56
+
57
+ def case_before(c)
58
+ # implement each case before
59
+ end
60
+
61
+ def case_after(c)
62
+ # implement each case after
63
+ end
64
+ end
65
+ end
66
+
67
+ context :gem_desc do
68
+ cases = [
69
+ {
70
+ case_no: 1,
71
+ case_title: "valid gem desc",
72
+ input: "abcdefghijklmnopqrstuvwxyz01234567891_",
73
+ expected: "abcdefghijklmnopqrstuvwxyz01234567891_"
74
+ },
75
+ {
76
+ case_no: 2,
77
+ case_title: "empty gem desc",
78
+ input: "",
79
+ expect_error: true
80
+ },
81
+ {
82
+ case_no: 3,
83
+ case_title: "nil gem desc",
84
+ input: nil,
85
+ expect_error: true
86
+ },
87
+ ]
88
+
89
+ cases.each do |c|
90
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
91
+ begin
92
+ case_before c
93
+
94
+ # -- given --
95
+ dslable_dsl = Dslable::Dsl.new
96
+
97
+ # -- when --
98
+ if c[:expect_error]
99
+ lambda {dslable_dsl.gem_desc c[:input]}.should raise_error(Dslable::InvalidDslError)
100
+ next
101
+ end
102
+ dslable_dsl.gem_desc c[:input]
103
+ actual = dslable_dsl._gem_desc
104
+
105
+ # -- then --
106
+ expect(actual).to eq(c[:expected])
107
+ ensure
108
+ case_after c
109
+ end
110
+ end
111
+
112
+ def case_before(c)
113
+ # implement each case before
114
+ end
115
+
116
+ def case_after(c)
117
+ # implement each case after
118
+ end
119
+ end
120
+ end
121
+
122
+ context :field do
123
+ cases = [
124
+ {
125
+ case_no: 1,
126
+ case_title: "case_title",
127
+ input_field_name: :field_name,
128
+ input_field_desc: "field_desc",
129
+ input_args_name: :args_name,
130
+ input_args_desc: "args_desc",
131
+ expected_field_name: :field_name,
132
+ expected_field_desc: "field_desc",
133
+ expected_args_name: :args_name,
134
+ expected_args_desc: "args_desc",
135
+ },
136
+ ]
137
+
138
+ cases.each do |c|
139
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
140
+ begin
141
+ case_before c
142
+
143
+ # -- given --
144
+ dslable_dsl = Dslable::Dsl.new
145
+
146
+ # -- when --
147
+ dslable_dsl.field c[:input_field_name] do |f|
148
+ f.desc c[:input_field_desc]
149
+ f.args c[:input_args_name] do |a|
150
+ a.desc c[:input_args_desc]
151
+ end
152
+ end
153
+
154
+ # -- then --
155
+ actual = dslable_dsl.fields.first
156
+ expect(actual._field_name).to eq(c[:expected_field_name])
157
+ expect(actual._desc).to eq(c[:expected_field_desc])
158
+ expect(actual._args._args_name).to eq(c[:expected_args_name])
159
+ expect(actual._args._desc).to eq(c[:expected_args_desc])
160
+ ensure
161
+ case_after c
162
+ end
163
+ end
164
+
165
+ def case_before(c)
166
+ # implement each case before
167
+ end
168
+
169
+ def case_after(c)
170
+ # implement each case after
171
+ end
172
+ end
173
+ end
174
+
175
+ context :bin_name do
176
+ cases = [
177
+ {
178
+ case_no: 1,
179
+ case_title: "valid bin name",
180
+ input: "abcdefghijklmnopqrstuvwxyz01234567891_",
181
+ expected: "abcdefghijklmnopqrstuvwxyz01234567891_"
182
+ },
183
+ {
184
+ case_no: 2,
185
+ case_title: "empty bin name",
186
+ input: "",
187
+ expect_error: true
188
+ },
189
+ {
190
+ case_no: 3,
191
+ case_title: "nil bin name",
192
+ input: nil,
193
+ expect_error: true
194
+ },
195
+ {
196
+ case_no: 4,
197
+ case_title: "invalid bin name contain space",
198
+ input: "bin name",
199
+ expect_error: true
200
+ },
201
+ ]
202
+
203
+ cases.each do |c|
204
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
205
+ begin
206
+ case_before c
207
+
208
+ # -- given --
209
+ dslable_dsl = Dslable::Dsl.new
210
+
211
+ # -- when --
212
+ if c[:expect_error]
213
+ lambda {dslable_dsl.bin_name c[:input]}.should raise_error(Dslable::InvalidDslError)
214
+ next
215
+ end
216
+ dslable_dsl.bin_name c[:input]
217
+ actual = dslable_dsl._bin_name
218
+
219
+ # -- then --
220
+ expect(actual).to eq(c[:expected])
221
+ ensure
222
+ case_after c
223
+ end
224
+ end
225
+
226
+ def case_before(c)
227
+ # implement each case before
228
+ end
229
+
230
+ def case_after(c)
231
+ # implement each case after
232
+ end
233
+ end
234
+ end
235
+ end
@@ -0,0 +1,116 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+ require "dslable_field"
4
+
5
+ describe Dslable::Field do
6
+ context :desc do
7
+ cases = [
8
+ {
9
+ case_no: 1,
10
+ case_title: "valid description",
11
+ input: "this is descripotion",
12
+ expected: "this is descripotion"
13
+ },
14
+ {
15
+ case_no: 2,
16
+ case_title: "empty description",
17
+ input: "",
18
+ expected: ""
19
+ },
20
+ {
21
+ case_no: 3,
22
+ case_title: "nil description",
23
+ input: nil,
24
+ expected: ""
25
+ },
26
+ ]
27
+
28
+ cases.each do |c|
29
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
30
+ begin
31
+ case_before c
32
+
33
+ # -- given --
34
+ dslable_field = Dslable::Field.new
35
+
36
+ # -- when --
37
+ dslable_field.desc c[:input]
38
+
39
+ # -- then --
40
+ actual = dslable_field._desc
41
+ expect(actual).to eq(c[:expected])
42
+ ensure
43
+ case_after c
44
+ end
45
+ end
46
+
47
+ def case_before(c)
48
+ # implement each case before
49
+ end
50
+
51
+ def case_after(c)
52
+ # implement each case after
53
+ end
54
+ end
55
+ end
56
+
57
+ context :args do
58
+ cases = [
59
+ {
60
+ case_no: 1,
61
+ case_title: "valid args",
62
+ input_key: :args_name,
63
+ input_desc: "desc",
64
+ input_klass: String,
65
+ input_required: true,
66
+ input_default_value: "default",
67
+ expected_key: :args_name,
68
+ expected_desc: "desc",
69
+ expected_klass: String,
70
+ expected_required: true,
71
+ expected_default_value: "default",
72
+ },
73
+ ]
74
+
75
+ cases.each do |c|
76
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
77
+ begin
78
+ case_before c
79
+
80
+ # -- given --
81
+ dslable_field = Dslable::Field.new
82
+
83
+ # -- when --
84
+ dslable_field.args :args_name do |a|
85
+ a.desc c[:input_desc]
86
+ a.klass c[:input_klass]
87
+ a.required
88
+ a.default_value c[:input_default_value]
89
+ end
90
+
91
+ # -- then --
92
+ actual_args = dslable_field._args
93
+ actual_desc = actual_args._desc
94
+ actual_klass = actual_args._klass
95
+ actual_required = actual_args._required
96
+ actual_default_value = actual_args._default_value
97
+ expect(actual_desc).to eq(c[:expected_desc])
98
+ expect(actual_klass).to eq(c[:expected_klass])
99
+ expect(actual_required).to eq(c[:expected_required])
100
+ expect(actual_default_value).to eq(c[:expected_default_value])
101
+ ensure
102
+ case_after c
103
+ end
104
+ end
105
+
106
+ def case_before(c)
107
+ # implement each case before
108
+ end
109
+
110
+ def case_after(c)
111
+ # implement each case after
112
+ end
113
+ end
114
+ end
115
+
116
+ end