RSpec.shared_examples "structured data 2 text preprocessor" do describe "#process" do let(:example_file) { "example.#{extention}" } before do File.open(example_file, "w") do |n| n.puts(transform_to_type(example_content)) end end after do FileUtils.rm_rf(example_file) end context "Array of hashes" do let(:example_content) do [{ "name" => "spaghetti", "desc" => "wheat noodles of 9mm diameter", "symbol" => "SPAG", "symbol_def" => "the situation is message like spaghetti at a kid's meal" }] end let(:input) do <<~TEXT = Document title Author :docfile: test.adoc :nodoc: :novalid: :no-isobib: :imagesdir: spec/assets [#{extention}2text,#{example_file},my_context] ---- {my_context.*,item,EOF} {item.name}:: {item.desc} {EOF} ---- TEXT end let(:output) do <<~TEXT #{BLANK_HDR}
spaghetti

wheat noodles of 9mm diameter

TEXT end it "correctly renders input" do expect( xmlpp( strip_guid( Asciidoctor.convert(input, backend: :standoc, header_footer: true), ), ), ).to(be_equivalent_to(xmlpp(output))) end end context "An array of strings" do let(:example_content) do ["lorem", "ipsum", "dolor"] end let(:input) do <<~TEXT = Document title Author :docfile: test.adoc :nodoc: :novalid: :no-isobib: :imagesdir: spec/assets [#{extention}2text,#{example_file},ar] ---- {ar.*,s,EOS} === {s.#} {s} This section is about {s}. {EOS} ---- TEXT end let(:output) do <<~TEXT #{BLANK_HDR} 0 lorem

This section is about lorem.

1 ipsum

This section is about ipsum.

2 dolor

This section is about dolor.

TEXT end it "correctly renders input" do expect( xmlpp( strip_guid( Asciidoctor.convert(input, backend: :standoc, header_footer: true), ), ), ).to(be_equivalent_to(xmlpp(output))) end end context "A simple hash" do let(:example_content) do { "name" => "Lorem ipsum", "desc" => "dolor sit amet" } end let(:input) do <<~TEXT = Document title Author :docfile: test.adoc :nodoc: :novalid: :no-isobib: :imagesdir: spec/assets [#{extention}2text,#{example_file},my_item] ---- === {my_item.name} {my_item.desc} ---- TEXT end let(:output) do <<~TEXT #{BLANK_HDR} Lorem ipsum

dolor sit amet

TEXT end it "correctly renders input" do expect( xmlpp( strip_guid( Asciidoctor.convert(input, backend: :standoc, header_footer: true), ), ), ).to(be_equivalent_to(xmlpp(output))) end end context "A simple hash with free keys" do let(:example_content) do { "name" => "Lorem ipsum", "desc" => "dolor sit amet" } end let(:input) do <<~TEXT = Document title Author :docfile: test.adoc :nodoc: :novalid: :no-isobib: :imagesdir: spec/assets [#{extention}2text,#{example_file},my_item] ---- {my_item.*,key,EOI} === {key} {my_item[key]} {EOI} ---- TEXT end let(:output) do <<~TEXT #{BLANK_HDR} name

Lorem ipsum

desc

dolor sit amet

TEXT end it "correctly renders input" do expect( xmlpp( strip_guid( Asciidoctor.convert(input, backend: :standoc, header_footer: true), ), ), ).to(be_equivalent_to(xmlpp(output))) end end context "An array of hashes" do let(:example_content) do [{ "name" => "Lorem", "desc" => "ipsum", "nums" => [2] }, { "name" => "dolor", "desc" => "sit", "nums" => [] }, { "name" => "amet", "desc" => "lorem", "nums" => [2, 4, 6] }] end let(:input) do <<~TEXT = Document title Author :docfile: test.adoc :nodoc: :novalid: :no-isobib: :imagesdir: spec/assets [#{extention}2text,#{example_file},ar] ---- {ar.*,item,EOF} {item.name}:: {item.desc} {item.nums.*,num,EON} - {item.name}: {num} {EON} {EOF} ---- TEXT end let(:output) do <<~TEXT #{BLANK_HDR}
Lorem

ipsum

  • Lorem: 2

dolor

sit

amet

lorem

  • amet: 2

  • amet: 4

  • amet: 6

TEXT end it "correctly renders input" do expect( xmlpp( strip_guid( Asciidoctor.convert(input, backend: :standoc, header_footer: true), ), ), ).to(be_equivalent_to(xmlpp(output))) end end context "An array with interpolated file names, etc. \ for Asciidoc's consumption" do let(:example_content) do { "prefix" => "doc-", "items" => ["lorem", "ipsum", "dolor"] } end let(:input) do <<~TEXT = Document title Author :docfile: test.adoc :nodoc: :novalid: :no-isobib: :imagesdir: spec/assets [#{extention}2text,#{example_file},#{extention}] ------ First item is {#{extention}.items[0]}. Last item is {#{extention}.items[-1]}. {#{extention}.items.*,s,EOS} === {s.#} -> {s.# + 1} {s} == {#{extention}.items[s.#]} [source,ruby] ---- include::{#{extention}.prefix}{s.#}.rb[] ---- {EOS} ------ TEXT end let(:output) do <<~TEXT #{BLANK_HDR} Foreword

First item is lorem. Last item is dolor.

0 → 1 lorem == lorem link:doc-0.rb[] 1 → 2 ipsum == ipsum link:doc-1.rb[] 2 → 3 dolor == dolor link:doc-2.rb[] TEXT end it "correctly renders input" do expect( xmlpp( strip_guid( Asciidoctor.convert(input, backend: :standoc, header_footer: true), ), ), ).to(be_equivalent_to(xmlpp(output))) end end context "Array of language codes" do let(:example_content) do YAML.safe_load( File.read(File.expand_path("../../assets/codes.yml", __dir__)), ) end let(:input) do <<~TEXT = Document title Author :docfile: test.adoc :nodoc: :novalid: :no-isobib: :imagesdir: spec/assets [#{extention}2text,#{example_file},ar] ---- {ar.*,item,EOF} .{item.values[1]} [%noheader,cols="h,1"] |=== {item.*,key,EOK} | {key} | {item[key]} {EOK} |=== {EOF} ---- TEXT end let(:output) do <<~TEXT #{BLANK_HDR} #{File.read(File.expand_path('../../examples/codes_table.html', __dir__))} TEXT end it "correctly renders input" do expect( xmlpp( strip_guid( Asciidoctor.convert(input, backend: :standoc, header_footer: true), ), ), ).to(be_equivalent_to(xmlpp(output))) end end context "Nested hash dot notation" do let(:example_content) do { "data" => { "acadsin-zho-hani-latn-2002" => { "code" => "acadsin-zho-hani-latn-2002", "name" => { "en" => "Academica Sinica -- Chinese Tongyong Pinyin (2002)", }, "authority" => "acadsin", "lang" => { "system" => "iso-639-2", "code" => "zho" }, "source_script" => "Hani", "target_script" => "Latn", "system" => { "id" => "2002", "specification" => "Academica Sinica -- Chinese Tongyong Pinyin (2002)" }, "notes" => "NOTE: OGC 11-122r1 code `zho_Hani2Latn_AcadSin_2002`" } } } end let(:input) do <<~TEXT = Document title Author :docfile: test.adoc :nodoc: :novalid: :no-isobib: :imagesdir: spec/assets [#{extention}2text,#{example_file},authorities] ---- [cols="a,a,a,a",options="header"] |=== | Script conversion system authority code | Name in English | Notes | Name en {authorities.data.*,key,EOI} | {key} | {authorities.data[key]['code']} | {authorities.data[key]['notes']} | {authorities.data[key].name.en} {EOI} |=== ---- TEXT end let(:output) do <<~TEXT #{BLANK_HDR}
Script conversion system authority code Name in English Notes Name en

acadsin-zho-hani-latn-2002

acadsin-zho-hani-latn-2002

OGC 11-122r1 code zho_Hani2Latn_AcadSin_2002

Academica Sinica — Chinese Tongyong Pinyin (2002)

TEXT end it "correctly renders input" do expect( xmlpp( strip_guid( Asciidoctor.convert(input, backend: :standoc, header_footer: true), ), ), ).to(be_equivalent_to(xmlpp(output))) end end context "Liquid code snippets" do let(:example_content) do [{ "name" => "One", "show" => true }, { "name" => "Two", "show" => true }, { "name" => "Three", "show" => false }] end let(:input) do <<~TEXT = Document title Author :docfile: test.adoc :nodoc: :novalid: :no-isobib: :imagesdir: spec/assets [#{extention}2text,#{example_file},my_context] ---- {% for item in my_context %} {% if item.show %} {{ item.name | upcase }} {{ item.name | size }} {% endif %} {% endfor %} ---- TEXT end let(:output) do <<~TEXT #{BLANK_HDR}

ONE 3

TWO 3

TEXT end it "renders liquid markup" do expect( xmlpp( strip_guid( Asciidoctor.convert(input, backend: :standoc, header_footer: true), ), ), ).to(be_equivalent_to(xmlpp(output))) end end context "Date time objects support" do let(:example_content) do { "date" => Date.parse("1889-09-28"), "time" => Time.gm(2020, 10, 15, 5, 34), } end let(:input) do <<~TEXT = Document title Author :docfile: test.adoc :nodoc: :novalid: :no-isobib: :imagesdir: spec/assets [#{extention}2text,#{example_file},my_context] ---- {{my_context.time}} {{my_context.date}} ---- TEXT end let(:output) do <<~TEXT #{BLANK_HDR}

1889-09-28

2020-10-15 05:34:00 UTC

TEXT end it "renders liquid markup" do expect( xmlpp( strip_guid( Asciidoctor.convert(input, backend: :standoc, header_footer: true), ), ), ).to(be_equivalent_to(xmlpp(output))) end end context "Nested files support" do let(:example_content) do { "date" => Date.parse("1889-09-28"), "time" => Time.gm(2020, 10, 15, 5, 34), } end let(:parent_file) { "parent_file.#{extention}" } let(:parent_file_content) { [nested_file, nested_file_2] } let(:parent_file_2) { "parent_file_2.#{extention}" } let(:parent_file_2_content) { ["name", "description"] } let(:parent_file_3) { "parent_file_3.#{extention}" } let(:parent_file_3_content) { ["one", "two"] } let(:nested_file) { "nested_file.#{extention}" } let(:nested_file_content) do { "name" => "nested file-main", "description" => "nested description-main", "one" => "nested one-main", "two" => "nested two-main", } end let(:nested_file_2) { "nested_file_2.#{extention}" } let(:nested_file_2_content) do { "name" => "nested2 name-main", "description" => "nested2 description-main", "one" => "nested2 one-main", "two" => "nested2 two-main", } end let(:input) do <<~TEXT = Document title Author :docfile: test.adoc :nodoc: :novalid: :no-isobib: :imagesdir: spec/assets [#{extention}2text,#{parent_file},paths] ---- {% for path in paths %} [#{extention}2text,#{parent_file_2},attribute_names] --- {% for name in attribute_names %} [#{extention}2text,{{ path }},data] -- == {{ data[name] | split: "-" | last }}: {{ data[name] }} -- {% endfor %} --- [#{extention}2text,#{parent_file_3},attribute_names] --- {% for name in attribute_names %} [#{extention}2text,{{ path }},data] -- == {{ data[name] }} -- {% endfor %} --- {% endfor %} ---- TEXT end let(:output) do <<~TEXT #{BLANK_HDR} main: nested file-main main: nested description-main nested one-main nested two-main main: nested2 name-main main: nested2 description-main nested2 one-main nested2 two-main TEXT end let(:file_list) do { parent_file => parent_file_content, parent_file_2 => parent_file_2_content, parent_file_3 => parent_file_3_content, nested_file => nested_file_content, nested_file_2 => nested_file_2_content, } end before do file_list.each_pair do |file, content| File.open(file, "w") do |n| n.puts(transform_to_type(content)) end end end after do file_list.keys.each do |file| FileUtils.rm_rf(file) end end it "renders liquid markup" do expect( xmlpp( strip_guid( Asciidoctor.convert(input, backend: :standoc, header_footer: true), ), ), ).to(be_equivalent_to(xmlpp(output))) end end end end