Skip to content

Commit

Permalink
Tests: Section splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
hmdne authored and ronaldtse committed May 29, 2024
1 parent 00c893d commit e85eaa8
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 5 deletions.
8 changes: 3 additions & 5 deletions lib/coradoc/reverse_adoc/postprocessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions spec/reverse_adoc/assets/sections.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<div id="__brokendiv">
Preface

<div>
<h1>Section 1</h1>

Hello!
</div>

<div>
<div>
<h1>Section 2</h1>
</div>
<div>
This document describes something.
</div>
<div>
<h2>Section 2.1</h2>

Content

<h2>Section 2.2</h2>

<div class="nastydiv">
<h3>Section 2.2.1</h3><h4>Section 2.2.1.1</h4><h2>Section 2.3</h2>
</div>

Hey!
</div>
</div>

<h1>Section 3</h1><h2>Section 3.1</h2>

Goodbye!
</div>
68 changes: 68 additions & 0 deletions spec/reverse_adoc/lib/reverse_adoc/split_sections_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e85eaa8

Please sign in to comment.