From d8963e834cc1ef7605ced9915a6dd4feedf28b64 Mon Sep 17 00:00:00 2001 From: hmdne <54514036+hmdne@users.noreply.github.com> Date: Mon, 27 May 2024 18:43:05 +0200 Subject: [PATCH] Tests: Section splitting --- lib/coradoc/reverse_adoc/postprocessor.rb | 8 +-- spec/reverse_adoc/assets/sections.html | 35 ++++++++++ .../lib/reverse_adoc/split_sections_spec.rb | 68 +++++++++++++++++++ 3 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 spec/reverse_adoc/assets/sections.html create mode 100644 spec/reverse_adoc/lib/reverse_adoc/split_sections_spec.rb diff --git a/lib/coradoc/reverse_adoc/postprocessor.rb b/lib/coradoc/reverse_adoc/postprocessor.rb index 6336e95..8b9ec48 100644 --- a/lib/coradoc/reverse_adoc/postprocessor.rb +++ b/lib/coradoc/reverse_adoc/postprocessor.rb @@ -33,12 +33,10 @@ def collapse_meaningless_sections # creating meaningful sections def generate_meaningful_sections @tree = Coradoc::Element::Base.visit(@tree) do |elem, dir| - # We are searching for an array, that has more than 2 elements and - # one of those elements is a title. This will be a candidate for - # our section array. + # We are searching for an array, that has a title. This + # will be a candidate for our section array. if dir == :post && elem.is_a?(Array) && - elem.length >= 2 && !elem.grep(Coradoc::Element::Title).empty? new_array = [] @@ -123,7 +121,7 @@ def split_sections sections[section_file] = elem up = "../" * (title.level_int - 1) - "include::#{up}#{section_file}[]\n\n" + "\ninclude::#{up}#{section_file}[]\n" end else elem diff --git a/spec/reverse_adoc/assets/sections.html b/spec/reverse_adoc/assets/sections.html new file mode 100644 index 0000000..bf4c915 --- /dev/null +++ b/spec/reverse_adoc/assets/sections.html @@ -0,0 +1,35 @@ +
+Preface + +
+

Section 1

+ +Hello! +
+ +
+
+

Section 2

+
+
+This document describes something. +
+
+

Section 2.1

+ +Content + +

Section 2.2

+ +
+

Section 2.2.1

Section 2.2.1.1

Section 2.3

+
+ +Hey! +
+
+ +

Section 3

Section 3.1

+ +Goodbye! +
diff --git a/spec/reverse_adoc/lib/reverse_adoc/split_sections_spec.rb b/spec/reverse_adoc/lib/reverse_adoc/split_sections_spec.rb new file mode 100644 index 0000000..fbb2849 --- /dev/null +++ b/spec/reverse_adoc/lib/reverse_adoc/split_sections_spec.rb @@ -0,0 +1,68 @@ +require "spec_helper" + +describe Coradoc::ReverseAdoc do + let(:input) { File.read("spec/reverse_adoc/assets/sections.html") } + let(:document) { Nokogiri::HTML(input) } + let(:level) { 1 } + subject { Coradoc::ReverseAdoc.convert(input, split_sections: level) } + let(:l1sections) { + %w[sections/section-01.adoc + sections/section-02.adoc + sections/section-03.adoc + ] + [nil] } + let(:l2sections) { + %w[sections/section-01.adoc + sections/section-02/section-01.adoc + sections/section-02/section-02.adoc + sections/section-02/section-03.adoc + sections/section-02.adoc + sections/section-03/section-01.adoc + sections/section-03.adoc + ] + [nil] } + + context "splitting in level nil" do + let(:level) { nil } + + it { should_not be_a Hash } + end + + shared_examples "can split and generate correct index" do + it { should be_a Hash } + it "should have a correct keys" do + subject.keys.should be == expected_sections + end + + it "should have a correct index" do + section_content = l1sections.compact.map { |i| "include::#{i}[]\n\n" }.join + subject[nil].should be == "[[__brokendiv]]\nPreface\n#{section_content}" + end + end + + context "splitting in level 1" do + let(:level) { 1 } + let(:expected_sections) { l1sections } + + include_examples "can split and generate correct index" + end + + context "splitting in level 2" do + let(:level) { 2 } + let(:expected_sections) { l2sections } + + include_examples "can split and generate correct index" + + it "should have a correct level2 index" do + subject["sections/section-02.adoc"].should be == + "== Section 2\n" + + "\n" + + "This document describes something.\n" + + "\n" + + "include::../sections/section-02/section-01.adoc[]\n" + + "\n" + + "include::../sections/section-02/section-02.adoc[]\n" + + "\n" + + "include::../sections/section-02/section-03.adoc[]\n" + + "\n" + end + end +end