ad_hoc_template 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,33 +8,84 @@ require 'ad_hoc_template/command_line_interface'
8
8
 
9
9
  describe AdHocTemplate do
10
10
  describe AdHocTemplate::CommandLineInterface do
11
+ before do
12
+ @template_in_default_format = <<TEMPLATE
13
+ a test string with tags (<%= key1 %> and <%= key2 %>) in it
14
+
15
+ <%#iteration_block
16
+ the value of sub_key1 is <%= sub_key1 %>
17
+ the value of sub_key2 is <%= sub_key2 %>
18
+
19
+ #%>
20
+ <%= block %>
21
+ TEMPLATE
22
+
23
+ @record_in_default_format = <<CONFIG
24
+ key1: value1
25
+ key2: value2
26
+ key3: value3
27
+
28
+ //@#iteration_block
29
+
30
+ sub_key1: value1-1
31
+ sub_key2: value1-2
32
+
33
+ sub_key1: value2-1
34
+ sub_key2: value2-2
35
+
36
+ //@block
37
+
38
+ the first line of block
39
+ the second line of block
40
+
41
+ the second paragraph in block
42
+
43
+ CONFIG
44
+
45
+ @expected_result = <<RESULT
46
+ a test string with tags (value1 and value2) in it
47
+
48
+ the value of sub_key1 is value1-1
49
+ the value of sub_key2 is value1-2
50
+
51
+ the value of sub_key1 is value2-1
52
+ the value of sub_key2 is value2-2
53
+
54
+ the first line of block
55
+ the second line of block
56
+
57
+ the second paragraph in block
58
+
59
+ RESULT
60
+ end
61
+
11
62
  it "can set the input/output encoding" do
12
63
  command_line_interface = AdHocTemplate::CommandLineInterface.new
13
64
  command_line_interface.set_encoding("UTF-8:Shift_JIS")
14
- expect(command_line_interface.class::Encoding.default_external.names).to include("UTF-8")
15
- expect(command_line_interface.class::Encoding.default_internal.names).to include("Shift_JIS")
65
+ expect(Encoding.default_external.names).to include("UTF-8")
66
+ expect(Encoding.default_internal.names).to include("Shift_JIS")
16
67
  end
17
68
 
18
69
  it "accepts an internal only argument" do
19
70
  command_line_interface = AdHocTemplate::CommandLineInterface.new
20
71
  command_line_interface.set_encoding(":UTF-8")
21
- expect(command_line_interface.class::Encoding.default_internal.names).to include("UTF-8")
72
+ expect(Encoding.default_internal.names).to include("UTF-8")
22
73
  end
23
74
 
24
75
  it "accepts also an external only argument" do
25
76
  command_line_interface = AdHocTemplate::CommandLineInterface.new
26
77
  command_line_interface.set_encoding("Shift_JIS")
27
- expect(command_line_interface.class::Encoding.default_external.names).to include("Shift_JIS")
78
+ expect(Encoding.default_external.names).to include("Shift_JIS")
28
79
  command_line_interface.set_encoding("UTF-8:")
29
- expect(command_line_interface.class::Encoding.default_external.names).to include("UTF-8")
80
+ expect(Encoding.default_external.names).to include("UTF-8")
30
81
  end
31
82
 
32
83
  it "can set the internal/external encoding from the command line" do
33
84
  command_line_interface = AdHocTemplate::CommandLineInterface.new
34
85
  set_argv("-E UTF-8:Shift_JIS")
35
86
  command_line_interface.parse_command_line_options
36
- expect(command_line_interface.class::Encoding.default_external.names).to include("UTF-8")
37
- expect(command_line_interface.class::Encoding.default_internal.names).to include("Shift_JIS")
87
+ expect(Encoding.default_external.names).to include("UTF-8")
88
+ expect(Encoding.default_internal.names).to include("Shift_JIS")
38
89
  end
39
90
 
40
91
  it "can specify the output file from command line" do
@@ -46,6 +97,34 @@ describe AdHocTemplate do
46
97
  expect(command_line_interface.output_filename).to eq(File.join(pwd, output_filename))
47
98
  end
48
99
 
100
+ it "can specify tag type for template" do
101
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
102
+ set_argv("--tag-type=curly_brackets")
103
+ command_line_interface.parse_command_line_options
104
+ expect(command_line_interface.tag_type).to eq(:curly_brackets)
105
+ end
106
+
107
+ it "choose the default tag type when the given type is unkown" do
108
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
109
+ set_argv("--tag-type=unkown_tag_type")
110
+ command_line_interface.parse_command_line_options
111
+ expect(command_line_interface.tag_type).to eq(:default)
112
+ end
113
+
114
+ it "can specify data format" do
115
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
116
+ set_argv("--data-format=yaml")
117
+ command_line_interface.parse_command_line_options
118
+ expect(command_line_interface.data_format).to eq(:yaml)
119
+ end
120
+
121
+ it "choose the default data format when the given format is unkown" do
122
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
123
+ set_argv("--data-format=unknown")
124
+ command_line_interface.parse_command_line_options
125
+ expect(command_line_interface.data_format).to eq(:default)
126
+ end
127
+
49
128
  it "reads input data from command line" do
50
129
  template_filename = "template.txt"
51
130
  record_filename = "record.txt"
@@ -67,64 +146,344 @@ describe AdHocTemplate do
67
146
  template_filename = "template.txt"
68
147
  record_filename = "record.txt"
69
148
 
149
+ allow(File).to receive(:read).with(File.expand_path(template_filename)).and_return(@template_in_default_format)
150
+ allow(File).to receive(:read).with(File.expand_path(record_filename)).and_return(@record_in_default_format)
151
+ allow(STDOUT).to receive(:print).with(@expected_result)
152
+
153
+ set_argv("#{template_filename} #{record_filename}")
154
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
155
+ command_line_interface.execute
156
+ end
157
+
158
+ it "can change the tag type." do
159
+ template_filename = "template.txt"
160
+ record_filename = "record.txt"
161
+
70
162
  template = <<TEMPLATE
71
- a test string with tags (<%= key1 %> and <%= key2 %>) in it
163
+ a test string with tags ({{= key1 }} and {{= key2 }}) in it
72
164
 
73
- <%#iteration_block
74
- the value of sub_key1 is <%= sub_key1 %>
75
- the value of sub_key2 is <%= sub_key2 %>
165
+ {{#iteration_block
166
+ the value of sub_key1 is {{= sub_key1 }}
167
+ the value of sub_key2 is {{= sub_key2 }}
76
168
 
77
- #%>
78
- <%= block %>
169
+ #}}
170
+ {{= block }}
79
171
  TEMPLATE
80
172
 
81
- record = <<CONFIG
173
+ allow(File).to receive(:read).with(File.expand_path(template_filename)).and_return(template)
174
+ allow(File).to receive(:read).with(File.expand_path(record_filename)).and_return(@record_in_default_format)
175
+ allow(STDOUT).to receive(:print).with(@expected_result)
176
+
177
+ set_argv("--tag-type=curly_brackets #{template_filename} #{record_filename}")
178
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
179
+ command_line_interface.execute
180
+ end
181
+
182
+ it "can change the data format" do
183
+ record_in_yaml_format = <<YAML
82
184
  key1: value1
83
185
  key2: value2
84
186
  key3: value3
187
+ "#iteration_block":
188
+ - sub_key1: value1-1
189
+ sub_key2: value1-2
190
+ - sub_key1: value2-1
191
+ sub_key2: value2-2
192
+ block: |
193
+ the first line of block
194
+ the second line of block
195
+
196
+ the second paragraph in block
197
+ YAML
85
198
 
86
- //@#iteration_block
199
+ template_filename = "template.txt"
200
+ record_filename = "record.txt"
87
201
 
88
- sub_key1: value1-1
89
- sub_key2: value1-2
202
+ allow(File).to receive(:read).with(File.expand_path(template_filename)).and_return(@template_in_default_format)
203
+ allow(File).to receive(:read).with(File.expand_path(record_filename)).and_return(record_in_yaml_format)
204
+ allow(STDOUT).to receive(:print).with(@expected_result)
90
205
 
91
- sub_key1: value2-1
92
- sub_key2: value2-2
206
+ set_argv("--data-format=yaml #{template_filename} #{record_filename}")
207
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
208
+ command_line_interface.parse_command_line_options
209
+ expect(command_line_interface.data_format).to eq(:yaml)
210
+ command_line_interface.execute
211
+ end
93
212
 
94
- //@block
213
+ it "can register a user-defined tag type" do
214
+ template_filename = "template.txt"
215
+ record_filename = "record.txt"
216
+ tag_config_yaml = "tag_config.yaml"
95
217
 
96
- the first line of block
97
- the second line of block
218
+ template = <<TEMPLATE
219
+ a test string with tags (<!--%= key1 %--> and <!--%= key2 %-->) in it
98
220
 
99
- the second paragraph in block
221
+ <repeat>iteration_block
222
+ the value of sub_key1 is <!--%= sub_key1 %-->
223
+ the value of sub_key2 is <!--%= sub_key2 %-->
100
224
 
225
+ </repeat>
226
+ <!--%= block %-->
227
+ TEMPLATE
228
+
229
+ tag_config =<<CONFIG
230
+ tag_name: xml_like3
231
+ tag: ["<!--%", "%-->"]
232
+ iteration_tag: ["<repeat>", "</repeat>"]
233
+ remove_indent: true
101
234
  CONFIG
102
235
 
103
- expected_result = <<RESULT
104
- a test string with tags (value1 and value2) in it
236
+ allow(File).to receive(:read).with(File.expand_path(template_filename)).and_return(template)
237
+ allow(File).to receive(:read).with(File.expand_path(record_filename)).and_return(@record_in_default_format)
238
+ allow(File).to receive(:read).with(File.expand_path(tag_config_yaml)).and_return(tag_config)
239
+ allow(STDOUT).to receive(:print).with(@expected_result)
240
+
241
+ set_argv("--user-defined-tag=tag_config.yaml #{template_filename} #{record_filename}")
242
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
243
+ command_line_interface.parse_command_line_options
244
+ command_line_interface.execute
245
+ end
105
246
 
247
+ describe "--data-format=csv" do
248
+ before do
249
+ @record_in_csv_format = <<CSV
250
+ key1,key2,key3
251
+ value1-1,value1-2,value1-3
252
+ value2-1,value2-2,value2-3
253
+ value3-1,value3-2,value3-3
254
+ CSV
255
+
256
+ @expected_result = <<RESULT
106
257
  the value of sub_key1 is value1-1
107
258
  the value of sub_key2 is value1-2
259
+ the value of sub_key2 is value1-3
108
260
 
109
261
  the value of sub_key1 is value2-1
110
262
  the value of sub_key2 is value2-2
263
+ the value of sub_key2 is value2-3
111
264
 
265
+ the value of sub_key1 is value3-1
266
+ the value of sub_key2 is value3-2
267
+ the value of sub_key2 is value3-3
112
268
 
113
- the first line of block
114
- the second line of block
269
+ RESULT
115
270
 
116
- the second paragraph in block
271
+ @template_without_iteration_block = <<TEMPLATE
272
+ <%#iteration_block
273
+ the value of sub_key1 is <%= key1 %>
274
+ the value of sub_key2 is <%= key2 %>
275
+ the value of sub_key2 is <%= key3 %>
276
+
277
+ #%>
278
+ TEMPLATE
279
+ end
280
+
281
+ it "allows to specify a label when you choose CSV as data format" do
282
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
283
+ set_argv("--data-format=csv:sub_records")
284
+ command_line_interface.parse_command_line_options
285
+ expect(command_line_interface.data_format).to eq({ csv: "sub_records" })
286
+ end
287
+
288
+ it "allows to specify as data format CSV as other formats" do
289
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
290
+ set_argv("--data-format=csv")
291
+ command_line_interface.parse_command_line_options
292
+ expect(command_line_interface.data_format).to eq(:csv)
293
+ end
294
+
295
+ it "can read csv data with an iteration label" do
296
+ template = <<TEMPLATE
297
+ <%#iteration_block
298
+ the value of sub_key1 is <%= key1 %>
299
+ the value of sub_key2 is <%= key2 %>
300
+ the value of sub_key2 is <%= key3 %>
301
+
302
+ #%>
303
+ TEMPLATE
304
+
305
+ template_filename = "template.txt"
306
+ record_filename = "record.csv"
307
+
308
+ allow(File).to receive(:read).with(File.expand_path(template_filename)).and_return(template)
309
+ allow(File).to receive(:read).with(File.expand_path(record_filename)).and_return(@record_in_csv_format)
310
+ allow(STDOUT).to receive(:print).with(@expected_result)
311
+
312
+ set_argv("--data-format=csv:iteration_block #{template_filename} #{record_filename}")
313
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
314
+ command_line_interface.parse_command_line_options
315
+ expect(command_line_interface.data_format).to eq({ csv: "iteration_block" })
316
+ command_line_interface.execute
317
+ end
318
+
319
+ it "can read csv data without an iteration label" do
320
+ template_filename = "template.txt"
321
+ record_filename = "record.csv"
322
+
323
+ allow(File).to receive(:read).with(File.expand_path(template_filename)).and_return(@template_without_iteration_block)
324
+ allow(File).to receive(:read).with(File.expand_path(record_filename)).and_return(@record_in_csv_format)
325
+ allow(STDOUT).to receive(:print).with(@expected_result)
326
+
327
+ set_argv("--data-format=csv #{template_filename} #{record_filename}")
328
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
329
+ command_line_interface.parse_command_line_options
330
+ expect(command_line_interface.data_format).to eq(:csv)
331
+ command_line_interface.execute
332
+ end
333
+
334
+ it "can read csv data of only one record" do
335
+ record_in_csv_format = <<CSV
336
+ key1,key2,key3
337
+ value1-1,value1-2,value1-3
338
+ CSV
339
+
340
+ expected_result = <<RESULT
341
+ the value of sub_key1 is value1-1
342
+ the value of sub_key2 is value1-2
343
+ the value of sub_key2 is value1-3
117
344
 
118
345
  RESULT
119
346
 
347
+ template_filename = "template.txt"
348
+ record_filename = "record.csv"
120
349
 
121
- allow(File).to receive(:read).with(File.expand_path(template_filename)).and_return(template)
122
- allow(File).to receive(:read).with(File.expand_path(record_filename)).and_return(record)
123
- allow(STDOUT).to receive(:print).with(expected_result)
350
+ allow(File).to receive(:read).with(File.expand_path(template_filename)).and_return(@template_without_iteration_block)
351
+ allow(File).to receive(:read).with(File.expand_path(record_filename)).and_return(record_in_csv_format)
352
+ allow(STDOUT).to receive(:print).with(expected_result)
124
353
 
125
- set_argv("#{template_filename} #{record_filename}")
126
- command_line_interface = AdHocTemplate::CommandLineInterface.new
127
- command_line_interface.execute
354
+ set_argv("--data-format=csv #{template_filename} #{record_filename}")
355
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
356
+ command_line_interface.parse_command_line_options
357
+ expect(command_line_interface.data_format).to eq(:csv)
358
+ command_line_interface.execute
359
+ end
360
+ end
361
+
362
+ describe "--data-format=tsv" do
363
+ before do
364
+ @record_in_tsv_format = <<TSV
365
+ key1 key2 key3
366
+ value1-1 value1-2 value1-3
367
+ value2-1 value2-2 value2-3
368
+ value3-1 value3-2 value3-3
369
+ TSV
370
+
371
+ @expected_result = <<RESULT
372
+ the value of sub_key1 is value1-1
373
+ the value of sub_key2 is value1-2
374
+ the value of sub_key2 is value1-3
375
+
376
+ the value of sub_key1 is value2-1
377
+ the value of sub_key2 is value2-2
378
+ the value of sub_key2 is value2-3
379
+
380
+ the value of sub_key1 is value3-1
381
+ the value of sub_key2 is value3-2
382
+ the value of sub_key2 is value3-3
383
+
384
+ RESULT
385
+
386
+ @template_without_iteration_block = <<TEMPLATE
387
+ <%#iteration_block
388
+ the value of sub_key1 is <%= key1 %>
389
+ the value of sub_key2 is <%= key2 %>
390
+ the value of sub_key2 is <%= key3 %>
391
+
392
+ #%>
393
+ TEMPLATE
394
+ end
395
+
396
+ it "allows to specify a label when you choose TSV as data format" do
397
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
398
+ set_argv("--data-format=tsv:sub_records")
399
+ command_line_interface.parse_command_line_options
400
+ expect(command_line_interface.data_format).to eq({ tsv: "sub_records" })
401
+ end
402
+
403
+ it "allows to specify as data format TSV as other formats" do
404
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
405
+ set_argv("--data-format=tsv")
406
+ command_line_interface.parse_command_line_options
407
+ expect(command_line_interface.data_format).to eq(:tsv)
408
+ end
409
+
410
+ it "can read tsv data with an iteration label" do
411
+ template = <<TEMPLATE
412
+ <%#iteration_block
413
+ the value of sub_key1 is <%= key1 %>
414
+ the value of sub_key2 is <%= key2 %>
415
+ the value of sub_key2 is <%= key3 %>
416
+
417
+ #%>
418
+ TEMPLATE
419
+
420
+ template_filename = "template.txt"
421
+ record_filename = "record.tsv"
422
+
423
+ allow(File).to receive(:read).with(File.expand_path(template_filename)).and_return(template)
424
+ allow(File).to receive(:read).with(File.expand_path(record_filename)).and_return(@record_in_tsv_format)
425
+ allow(STDOUT).to receive(:print).with(@expected_result)
426
+
427
+ set_argv("--data-format=tsv:iteration_block #{template_filename} #{record_filename}")
428
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
429
+ command_line_interface.parse_command_line_options
430
+ expect(command_line_interface.data_format).to eq({ tsv: "iteration_block" })
431
+ command_line_interface.execute
432
+ end
433
+
434
+ it "can read tsv data without an iteration label" do
435
+ template_filename = "template.txt"
436
+ record_filename = "record.tsv"
437
+
438
+ allow(File).to receive(:read).with(File.expand_path(template_filename)).and_return(@template_without_iteration_block)
439
+ allow(File).to receive(:read).with(File.expand_path(record_filename)).and_return(@record_in_tsv_format)
440
+ allow(STDOUT).to receive(:print).with(@expected_result)
441
+
442
+ set_argv("--data-format=tsv #{template_filename} #{record_filename}")
443
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
444
+ command_line_interface.parse_command_line_options
445
+ expect(command_line_interface.data_format).to eq(:tsv)
446
+ command_line_interface.execute
447
+ end
448
+
449
+ it "can read tsv data of only one record" do
450
+ record_in_tsv_format = <<TSV
451
+ key1 key2 key3
452
+ value1-1 value1-2 value1-3
453
+ TSV
454
+
455
+ expected_result = <<RESULT
456
+ the value of sub_key1 is value1-1
457
+ the value of sub_key2 is value1-2
458
+ the value of sub_key2 is value1-3
459
+
460
+ RESULT
461
+
462
+ template_filename = "template.txt"
463
+ record_filename = "record.tsv"
464
+
465
+ allow(File).to receive(:read).with(File.expand_path(template_filename)).and_return(@template_without_iteration_block)
466
+ allow(File).to receive(:read).with(File.expand_path(record_filename)).and_return(record_in_tsv_format)
467
+ allow(STDOUT).to receive(:print).with(expected_result)
468
+
469
+ set_argv("--data-format=tsv #{template_filename} #{record_filename}")
470
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
471
+ command_line_interface.parse_command_line_options
472
+ expect(command_line_interface.data_format).to eq(:tsv)
473
+ command_line_interface.execute
474
+ end
475
+ end
476
+
477
+ describe "by file extentions" do
478
+ it "can guess the format of data" do
479
+ template_filename = "template.txt"
480
+ record_filename = "record.yaml"
481
+
482
+ set_argv("#{template_filename} #{record_filename}")
483
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
484
+ command_line_interface.parse_command_line_options
485
+ expect(command_line_interface.data_format).to eq(:yaml)
486
+ end
128
487
  end
129
488
  end
130
489
  end