ad_hoc_template 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
1
  module AdHocTemplate
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -12,7 +12,7 @@ describe AdHocTemplate do
12
12
  @template_with_an_iteration_block = <<TEMPLATE
13
13
  a test string with tags (<%= key1 %> and <%= key2 %>) in it
14
14
 
15
- <%#iteration_block
15
+ <%#iteration_block:
16
16
  the value of sub_key1 is <%= sub_key1 %>
17
17
  the value of sub_key2 is <%= sub_key2 %>
18
18
 
@@ -145,7 +145,7 @@ RESULT
145
145
  it("should not add a newline at the head of IterationTagNode when the type of the node is not specified") do
146
146
  template = <<TEMPLATE
147
147
  a test string with tags
148
- <%#iteration_block
148
+ <%#iteration_block:
149
149
  the value of sub_key1 is <%= sub_key1 %>
150
150
  <%#
151
151
  the value of sub_key2 is <%= sub_key2 %>
@@ -192,7 +192,7 @@ The first line in the main part
192
192
  <%#
193
193
  The first line in the iteration part
194
194
 
195
- <%#iteration_block
195
+ <%#iteration_block:
196
196
  The value of key1 is <%= key1 %>
197
197
  #%>
198
198
 
@@ -209,7 +209,7 @@ Key value: <%= key %>
209
209
  Optinal values: <%# <%= optional1 %> and <%= optional2 %> are in the record.
210
210
  #%>
211
211
 
212
- <%#iteration_block
212
+ <%#iteration_block:
213
213
  The value of key1 is <%= key1 %>
214
214
  <%#
215
215
  The value of optional key2 is <%= key2 %>
@@ -406,10 +406,156 @@ RESULT
406
406
  end
407
407
  end
408
408
  end
409
+
410
+ describe 'fallback block' do
411
+ before do
412
+ @template = <<TEMPLATE
413
+ main start
414
+
415
+ <%#
416
+ <%* content in fallback tag <%= item_in_fallback %> fallback end *%>
417
+ optional content
418
+ <%#iterations:
419
+ in iteration tag <%= item %> #%> iteration part end
420
+ #%>
421
+
422
+ main end
423
+ TEMPLATE
424
+
425
+ @xml_comment_like_template = <<TEMPLATE
426
+ main start
427
+
428
+ <!--%iterate%-->
429
+ <!--%fallback%-->
430
+ content in fallback tag <!--%= item_in_fallback %--> fallback end
431
+ <!--%/fallback%-->
432
+ optional content
433
+ <!--%iterate%-->iterations:
434
+ in iteration tag <!--%= item %-->
435
+ <!--%/iterate%-->
436
+ iteration part end
437
+ <!--%/iterate%-->
438
+
439
+ main end
440
+ TEMPLATE
441
+
442
+ @data = <<DATA
443
+ item_in_fallback: ITEM_IN_FALLBACK
444
+
445
+ ///@#iterations
446
+
447
+ item: ITEM_1
448
+
449
+ item: ITEM_2
450
+ DATA
451
+ end
452
+
453
+ it 'should be ignored when tags can be filled with data' do
454
+ expected_result = <<RESULT
455
+ main start
456
+
457
+ optional content
458
+ in iteration tag ITEM_1 in iteration tag ITEM_2 iteration part end
459
+
460
+ main end
461
+ RESULT
462
+ tree = AdHocTemplate::Parser.parse(@template)
463
+ data = AdHocTemplate::RecordReader.read_record(@data)
464
+ tag_formatter = AdHocTemplate::DefaultTagFormatter.new
465
+
466
+ result = AdHocTemplate::DataLoader.format(tree, data, tag_formatter)
467
+
468
+ expect(result).to eq(expected_result)
469
+ end
470
+
471
+ it 'should be shown when tags out side of the node cannot be filled with data' do
472
+ expected_result = <<RESULT
473
+ main start
474
+
475
+ content in fallback tag ITEM_IN_FALLBACK fallback end
476
+ main end
477
+ RESULT
478
+
479
+ empty_data = <<DATA
480
+ item_in_fallback: ITEM_IN_FALLBACK
481
+
482
+ ///@#iterations
483
+
484
+ item:
485
+
486
+ item:
487
+ DATA
488
+
489
+ tree = AdHocTemplate::Parser.parse(@template)
490
+ data = AdHocTemplate::RecordReader.read_record(empty_data)
491
+ tag_formatter = AdHocTemplate::DefaultTagFormatter.new
492
+
493
+ result = AdHocTemplate::DataLoader.format(tree, data, tag_formatter)
494
+
495
+ expect(result).to eq(expected_result)
496
+ end
497
+
498
+ it 'may not contain any tag' do
499
+ template = <<TEMPLATE
500
+ main start
501
+
502
+ <%#
503
+ <%* content in fallback tag without inner tags fallback end
504
+ *%>
505
+ optional content
506
+ <%#iterations:
507
+ in iteration tag <%= item %> #%> iteration part end
508
+ #%>
509
+ main end
510
+ TEMPLATE
511
+ expected_result = <<RESULT
512
+ main start
513
+
514
+ content in fallback tag without inner tags fallback end
515
+ main end
516
+ RESULT
517
+
518
+ empty_data = <<DATA
519
+ ///@#iterations
520
+
521
+ item:
522
+
523
+ item:
524
+ DATA
525
+
526
+ tree = AdHocTemplate::Parser.parse(template)
527
+ data = AdHocTemplate::RecordReader.read_record(empty_data)
528
+ tag_formatter = AdHocTemplate::DefaultTagFormatter.new
529
+
530
+ result = AdHocTemplate::DataLoader.format(tree, data, tag_formatter)
531
+
532
+ expect(result).to eq(expected_result)
533
+ end
534
+
535
+ it 'should ignore indents preceding fallback tags when TagType[tag_name].remove_iteration_indent is set to true' do
536
+ expected_result = <<RESULT
537
+ main start
538
+
539
+ optional content
540
+ in iteration tag ITEM_1
541
+ in iteration tag ITEM_2
542
+ iteration part end
543
+
544
+ main end
545
+ RESULT
546
+ tree = AdHocTemplate::Parser.parse(@xml_comment_like_template, :xml_comment_like)
547
+ data = AdHocTemplate::RecordReader.read_record(@data)
548
+ tag_formatter = AdHocTemplate::DefaultTagFormatter.new
549
+
550
+ result = AdHocTemplate::DataLoader.format(tree, data, tag_formatter)
551
+
552
+ expect(result).to eq(expected_result)
553
+ end
554
+ end
409
555
  end
410
556
 
411
557
  it 'can convert &"<> into character entities' do
412
- result = AdHocTemplate.convert('characters: &, ", < and >',
558
+ result = AdHocTemplate.render('characters: &, ", < and >',
413
559
  'a string with characters (<%h characters %>) that should be represented as character entities.')
414
560
  expect(result).to eq('a string with characters (&amp;, &quot;, &lt; and &gt;) that should be represented as character entities.')
415
561
  end
@@ -12,7 +12,7 @@ describe AdHocTemplate do
12
12
  @template_in_default_format = <<TEMPLATE
13
13
  a test string with tags (<%= key1 %> and <%= key2 %>) in it
14
14
 
15
- <%#iteration_block
15
+ <%#iteration_block:
16
16
  the value of sub_key1 is <%= sub_key1 %>
17
17
  the value of sub_key2 is <%= sub_key2 %>
18
18
 
@@ -141,7 +141,7 @@ RESULT
141
141
  template = <<TEMPLATE
142
142
  a test string with tags ({{= key1 }} and {{= key2 }}) in it
143
143
 
144
- {{#iteration_block
144
+ {{#iteration_block:
145
145
  the value of sub_key1 is {{= sub_key1 }}
146
146
  the value of sub_key2 is {{= sub_key2 }}
147
147
 
@@ -197,7 +197,7 @@ YAML
197
197
  template = <<TEMPLATE
198
198
  a test string with tags (<!--%= key1 %--> and <!--%= key2 %-->) in it
199
199
 
200
- <repeat>iteration_block
200
+ <repeat>iteration_block:
201
201
  the value of sub_key1 is <!--%= sub_key1 %-->
202
202
  the value of sub_key2 is <!--%= sub_key2 %-->
203
203
 
@@ -209,6 +209,7 @@ TEMPLATE
209
209
  tag_name: xml_like3
210
210
  tag: ["<!--%", "%-->"]
211
211
  iteration_tag: ["<repeat>", "</repeat>"]
212
+ fallback_tag: ["<fallback>", "</fallback>"]
212
213
  remove_indent: true
213
214
  CONFIG
214
215
 
@@ -248,7 +249,7 @@ the value of sub_key2 is value3-3
248
249
  RESULT
249
250
 
250
251
  @template_without_iteration_block = <<TEMPLATE
251
- <%#iteration_block
252
+ <%#iteration_block:
252
253
  the value of sub_key1 is <%= key1 %>
253
254
  the value of sub_key2 is <%= key2 %>
254
255
  the value of sub_key2 is <%= key3 %>
@@ -273,7 +274,7 @@ TEMPLATE
273
274
 
274
275
  it "can read csv data with an iteration label" do
275
276
  template = <<TEMPLATE
276
- <%#iteration_block
277
+ <%#iteration_block:
277
278
  the value of sub_key1 is <%= key1 %>
278
279
  the value of sub_key2 is <%= key2 %>
279
280
  the value of sub_key2 is <%= key3 %>
@@ -363,7 +364,7 @@ the value of sub_key2 is value3-3
363
364
  RESULT
364
365
 
365
366
  @template_without_iteration_block = <<TEMPLATE
366
- <%#iteration_block
367
+ <%#iteration_block:
367
368
  the value of sub_key1 is <%= key1 %>
368
369
  the value of sub_key2 is <%= key2 %>
369
370
  the value of sub_key2 is <%= key3 %>
@@ -386,9 +387,31 @@ TEMPLATE
386
387
  expect(command_line_interface.data_format).to eq(:tsv)
387
388
  end
388
389
 
390
+ it "should propery parse given option values" do
391
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
392
+ set_argv("--data-format=t")
393
+ command_line_interface.parse_command_line_options
394
+ expect(command_line_interface.data_format).to eq(:tsv)
395
+
396
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
397
+ set_argv("--data-format=t:")
398
+ command_line_interface.parse_command_line_options
399
+ expect(command_line_interface.data_format).to eq(:tsv)
400
+
401
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
402
+ set_argv("--data-format=t:label")
403
+ command_line_interface.parse_command_line_options
404
+ expect(command_line_interface.data_format).to eq({ tsv: "label" })
405
+
406
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
407
+ set_argv("--data-format=:label")
408
+ command_line_interface.parse_command_line_options
409
+ expect(command_line_interface.data_format).to eq(:default)
410
+ end
411
+
389
412
  it "can read tsv data with an iteration label" do
390
413
  template = <<TEMPLATE
391
- <%#iteration_block
414
+ <%#iteration_block:
392
415
  the value of sub_key1 is <%= key1 %>
393
416
  the value of sub_key2 is <%= key2 %>
394
417
  the value of sub_key2 is <%= key3 %>
@@ -464,5 +487,82 @@ RESULT
464
487
  expect(command_line_interface.data_format).to eq(:yaml)
465
488
  end
466
489
  end
490
+
491
+ describe "--entry-format" do
492
+ before do
493
+ @template = <<TEMPLATE
494
+ The first line in the main part
495
+
496
+ <%#
497
+ The first line in the iteration part
498
+
499
+ Key value: <%= key %>
500
+ Optinal values: <%# <%= optional1 %> and <%= optional2 %> are in the record.
501
+ #%>
502
+
503
+ <%#iteration_block:
504
+ The value of key1 is <%= key1 %>
505
+ <%#
506
+ The value of optional key2 is <%= key2 %>
507
+ #%>
508
+ #%>
509
+
510
+ #%>
511
+
512
+ <%= block %>
513
+ TEMPLATE
514
+
515
+ @csv_compatible_template = <<TEMPLATE
516
+ <%= key1 %> <% key2 %>
517
+ <%= key3 %>
518
+ TEMPLATE
519
+ end
520
+
521
+ it 'can output an empty entry format in YAML' do
522
+ expected_format_in_yaml = <<YAML
523
+ ---
524
+ key:
525
+ optional1:
526
+ optional2:
527
+ "#iteration_block":
528
+ - key1:
529
+ key2:
530
+ block:
531
+ YAML
532
+
533
+ template_filename = "template.txt"
534
+
535
+ allow(File).to receive(:read).with(File.expand_path(template_filename)).and_return(@template)
536
+ allow(STDOUT).to receive(:print).with(expected_format_in_yaml)
537
+ allow(ARGF).to receive(:read).with(no_args)
538
+
539
+ set_argv("-d y --entry-format #{template_filename}")
540
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
541
+ command_line_interface.parse_command_line_options
542
+ expect(command_line_interface.data_format).to eq(:yaml)
543
+
544
+ command_line_interface.execute
545
+ end
546
+
547
+ it 'can output an empty entry format in TSV' do
548
+ expected_format_in_tsv = <<TSV
549
+ key1\tkey2\tkey3
550
+ \t\t
551
+ TSV
552
+
553
+ template_filename = "template.txt"
554
+
555
+ allow(File).to receive(:read).with(File.expand_path(template_filename)).and_return(@csv_compatible_template)
556
+ allow(STDOUT).to receive(:print).with(expected_format_in_tsv)
557
+ allow(ARGF).to receive(:read).with(no_args)
558
+
559
+ set_argv("-d tsv --entry-format #{template_filename}")
560
+ command_line_interface = AdHocTemplate::CommandLineInterface.new
561
+ command_line_interface.parse_command_line_options
562
+ expect(command_line_interface.data_format).to eq(:tsv)
563
+
564
+ command_line_interface.execute
565
+ end
566
+ end
467
567
  end
468
568
  end
@@ -0,0 +1,163 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'spec_helper'
4
+ require 'ad_hoc_template'
5
+
6
+ describe AdHocTemplate do
7
+ describe AdHocTemplate::EntryFormatGenerator::LabelChecker do
8
+ before do
9
+ @template = <<TEMPLATE
10
+ The first line in the main part
11
+
12
+ <%#
13
+ The first line in the iteration part
14
+
15
+ Key value: <%= key %>
16
+ Optinal values: <%# <%= optional1 %> and <%= optional2 %> are in the record.
17
+ #%>
18
+
19
+ <%#iteration_block:
20
+ The value of key1 is <%= key1 %>
21
+ <%#
22
+ The value of optional key2 is <%= key2 %>
23
+ #%>
24
+ #%>
25
+
26
+ #%>
27
+
28
+ <%= block %>
29
+ TEMPLATE
30
+
31
+ @expected_labels_as_ruby_objects = {
32
+ "key" => nil,
33
+ "optional1" => nil,
34
+ "optional2" => nil,
35
+ "#iteration_block" => [{
36
+ "key1" => nil,
37
+ "key2" => nil
38
+ }],
39
+ "block" => nil}
40
+ end
41
+
42
+ it 'collects tag labels from a parsed template' do
43
+ tree = AdHocTemplate::Parser.parse(@template)
44
+ label_checker = AdHocTemplate::EntryFormatGenerator::LabelChecker.new
45
+ tree.accept(label_checker)
46
+ labels = label_checker.labels
47
+ expect(labels).to eq(@expected_labels_as_ruby_objects)
48
+ end
49
+
50
+ it '.extract_labels collects tag labels from a parsed template' do
51
+ expected_labels_in_default_format = <<YAML
52
+ key:
53
+ optional1:
54
+ optional2:
55
+ block:
56
+
57
+ ///@#iteration_block
58
+
59
+ key1:
60
+ key2:
61
+ YAML
62
+
63
+ tree = AdHocTemplate::Parser.parse(@template)
64
+ labels = AdHocTemplate::EntryFormatGenerator.extract_labels(tree)
65
+
66
+ expect(labels).to eq(expected_labels_in_default_format)
67
+ end
68
+
69
+ it '.extract_labels should ignore fallback tags if they do not any contain value tag' do
70
+ template = <<TEMPLATE
71
+ main start
72
+ <%#
73
+ optional start <%* fallback part *%> <%= var1 %>
74
+ <%#iteration: <%= var2 %> #%>
75
+ #%>
76
+ <%= var3 %>
77
+
78
+ main end
79
+ TEMPLATE
80
+
81
+ expected = <<EXPECTED
82
+ var1:
83
+ var3:
84
+
85
+ ///@#iteration
86
+
87
+ var2:
88
+ EXPECTED
89
+
90
+ tree = AdHocTemplate::Parser.parse(template)
91
+ labels = AdHocTemplate::EntryFormatGenerator.extract_labels(tree)
92
+
93
+ expect(labels).to eq(expected)
94
+ end
95
+
96
+ it '.extract_labels should labels in fallback tags' do
97
+ template = <<TEMPLATE
98
+ main start
99
+ <%#
100
+ optional start <%* fallback <%= fallback_var1 %> and <%= fallback_var2 %> *%> <%= var1 %>
101
+ <%#iteration: <%= var2 %> #%>
102
+ #%>
103
+ <%= var3 %>
104
+
105
+ main end
106
+ TEMPLATE
107
+
108
+ expected = <<EXPECTED
109
+ fallback_var1:
110
+ fallback_var2:
111
+ var1:
112
+ var3:
113
+
114
+ ///@#iteration
115
+
116
+ var2:
117
+ EXPECTED
118
+
119
+ tree = AdHocTemplate::Parser.parse(template)
120
+ labels = AdHocTemplate::EntryFormatGenerator.extract_labels(tree)
121
+
122
+ expect(labels).to eq(expected)
123
+ end
124
+
125
+ it '.extract_labels accepts :yaml as its second argument' do
126
+ expected_labels_in_yaml = <<YAML
127
+ ---
128
+ key:
129
+ optional1:
130
+ optional2:
131
+ "#iteration_block":
132
+ - key1:
133
+ key2:
134
+ block:
135
+ YAML
136
+
137
+ tree = AdHocTemplate::Parser.parse(@template)
138
+ labels = AdHocTemplate::EntryFormatGenerator.extract_labels(tree, :yaml)
139
+
140
+ expect(labels).to eq(expected_labels_in_yaml)
141
+ end
142
+
143
+ it '.extract_labels accepts :json as its second argument' do
144
+ expected_labels_in_json = <<JSON
145
+ {
146
+ "key":null,
147
+ "optional1":null,
148
+ "optional2":null,
149
+ "#iteration_block":[{
150
+ "key1":null,
151
+ "key2":null
152
+ }],
153
+ "block":null
154
+ }
155
+ JSON
156
+
157
+ tree = AdHocTemplate::Parser.parse(@template)
158
+ labels = AdHocTemplate::EntryFormatGenerator.extract_labels(tree, :json)
159
+
160
+ expect(JSON.parse(labels)).to eq(JSON.parse(expected_labels_in_json))
161
+ end
162
+ end
163
+ end