require 'spec_helper' RSpec.describe Asciidoctor::Standoc::Yaml2TextPreprocessor do describe '#process' do let(:example_file) { 'example.yml' } before do if defined?(example_yaml_content) File.open(example_file, 'w') { |n| n.puts(example_yaml_content) } end end after do FileUtils.rm_rf(example_file) end context 'Array of hashes' do let(:example_yaml_content) do <<~TEXT --- - name: spaghetti desc: wheat noodles of 9mm diameter symbol: SPAG symbol_def: the situation is message like spaghetti at a kid's meal TEXT end let(:input) do <<~TEXT = Document title Author :docfile: test.adoc :nodoc: :novalid: :no-isobib: :imagesdir: spec/assets [yaml2text,#{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 yaml' 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_yaml_content) do <<~TEXT --- - lorem - ipsum - dolor TEXT end let(:input) do <<~TEXT = Document title Author :docfile: test.adoc :nodoc: :novalid: :no-isobib: :imagesdir: spec/assets [yaml2text,#{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 yaml' 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_yaml_content) do <<~TEXT --- name: Lorem ipsum desc: dolor sit amet TEXT end let(:input) do <<~TEXT = Document title Author :docfile: test.adoc :nodoc: :novalid: :no-isobib: :imagesdir: spec/assets [yaml2text,#{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 yaml' 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_yaml_content) do <<~TEXT --- name: Lorem ipsum desc: dolor sit amet TEXT end let(:input) do <<~TEXT = Document title Author :docfile: test.adoc :nodoc: :novalid: :no-isobib: :imagesdir: spec/assets [yaml2text,#{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 yaml' 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_yaml_content) do <<~TEXT --- - name: Lorem desc: ipsum nums: [2] - name: dolor desc: sit nums: [] - name: amet desc: lorem nums: [2, 4, 6] TEXT end let(:input) do <<~TEXT = Document title Author :docfile: test.adoc :nodoc: :novalid: :no-isobib: :imagesdir: spec/assets [yaml2text,#{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 yaml' 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_yaml_content) do <<~TEXT --- prefix: doc- items: - lorem - ipsum - dolor TEXT end let(:input) do <<~TEXT = Document title Author :docfile: test.adoc :nodoc: :novalid: :no-isobib: :imagesdir: spec/assets [yaml2text,#{example_file},yaml] ------ First item is {yaml.items[0]}. Last item is {yaml.items[-1]}. {yaml.items.*,s,EOS} === {s.#} -> {s.# + 1} {s} == {yaml.items[s.#]} [source,ruby] ---- include::{yaml.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 # TODO: fix frozen string error xit 'correctly renders input yaml' 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(:input) do <<~TEXT = Document title Author :docfile: test.adoc :nodoc: :novalid: :no-isobib: :imagesdir: spec/assets [yaml2text,#{File.expand_path('../assets/codes.yml', __dir__)},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 yaml' 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_yaml_content) do <<~TEXT 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`' TEXT end let(:input) do <<~TEXT = Document title Author :docfile: test.adoc :nodoc: :novalid: :no-isobib: :imagesdir: spec/assets [yaml2text,#{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 yaml' do expect( xmlpp( strip_guid( Asciidoctor.convert(input, backend: :standoc, header_footer: true), ), ), ).to(be_equivalent_to(xmlpp(output))) end end end end