Skip to content

Commit

Permalink
Extract formatting of options into common method format_options
Browse files Browse the repository at this point in the history
  • Loading branch information
jirutka committed Jan 23, 2015
1 parent 7ea261f commit c4d779c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 18 deletions.
30 changes: 30 additions & 0 deletions lib/asciidoctor/doctest/base_examples_suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,36 @@ def group_names
}.sort
end

protected

##
# Converts the given options into the format used in examples file.
#
# @example
# {
# option1: 'value 1',
# option2: ['value 1', 'value 2']
# option3: true
# }
# V---V---V---V---V---V---V---V---V
# [
# ':option1: value 1',
# ':option2: value 1',
# ':option2: value 2',
# ':option3:'
# ]
#
# @param opts [Hash] options
# @return [Array<String>] formatted options
#
def format_options(opts)
opts.each_with_object([]) do |(name, vals), ary|
Array.wrap(vals).each do |val|
ary << (val == true ? ":#{name}:" : ":#{name}: #{val}")
end
end
end

private

def read_files(file_name)
Expand Down
12 changes: 5 additions & 7 deletions lib/asciidoctor/doctest/html/examples_suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,13 @@ def parse(input, group_name)

def serialize(examples)
Array.wrap(examples).map { |exmpl|
header = [ ".#{exmpl.local_name}", exmpl.desc.presence ].compact
header = [
".#{exmpl.local_name}",
exmpl.desc.presence,
*format_options(exmpl.opts)
].compact

exmpl.opts.each do |name, vals|
Array.wrap(vals).each do |val|
header << (val == true ? ":#{name}:" : ":#{name}: #{val}")
end
end
header_str = header.one? ? (header.first + ' ') : (header.join("\n") + "\n")

[ "<!-- #{header_str}-->", exmpl.content.presence ].compact.join("\n") + "\n"
}.join("\n")
end
Expand Down
15 changes: 4 additions & 11 deletions spec/html/examples_suite_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
:exclude: .//code
:exclude: .//section
:include: ./p/node()
:header_footer:
-->
<p>dummy</p>
EOF
Expand All @@ -93,34 +94,26 @@
let :output do
create_example 's:basic', content: '<p>dummy</p>', opts: {
exclude: ['.//code', './/section'],
include: ['./p/node()']
include: ['./p/node()'],
header_footer: true
}
end
include_examples :example
end

context 'with boolean option' do
let(:input) { "<!-- .basic\n:header_footer:\n-->\n" }
let(:output) { create_example 's:basic', opts: { header_footer: true } }

include_examples :example
end

context 'with description and options' do
let :input do
<<-EOF.strip_heredoc
<!-- .basic
This is a description.
:exclude: .//code
:header_footer:
-->
EOF
end

let :output do
create_example 's:basic', desc: 'This is a description.', opts: {
exclude: ['.//code'],
header_footer: true
exclude: ['.//code']
}
end
include_examples :example
Expand Down
27 changes: 27 additions & 0 deletions spec/shared_examples/base_examples_suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,33 @@ def theirs_exmpl(suffix, group = 0)
end


describe '#format_options' do

shared_examples :format_options do |input, output|
it "returns #{output} for #{input}" do
expect(suite.send(:format_options, input)).to eq output
end
end

context 'empty' do
include_examples :format_options, {}, []
end

context 'options with one value' do
include_examples :format_options, {opt1: 'val1', opt2: 'val2'}, [':opt1: val1', ':opt2: val2']
end

context 'options with multiple values' do
include_examples :format_options, {opt1: %w[val11 val12], opt2: ['val2']},
[':opt1: val11', ':opt1: val12', ':opt2: val2']
end

context 'boolean options' do
include_examples :format_options, {opt1: true, opt2: false}, [':opt1:', ':opt2: false']
end
end


def create_and_write_group(path, group_name, file_ext, *examples)
content = [path, group_name + file_ext, *examples].join("\n")
File.write File.join(path, group_name + file_ext), content
Expand Down

0 comments on commit c4d779c

Please sign in to comment.