summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorDan Allen <dan.j.allen@gmail.com>2012-12-17 03:20:30 -0700
committerDan Allen <dan.j.allen@gmail.com>2012-12-17 03:20:30 -0700
commit34e6ad2a2da41b759ccaa26c4b73d0ffecbdce42 (patch)
treee5143f46d76813387403ff0aa0d6614753cb649c /test
parente4102d5b8244e2c2573065701adabc9d196af3ed (diff)
support book doctype, streamline doc title accessors
- add doctype accessor to Document - support the book doctype (multiple level-0 headings) - polish and document logic for retrieving the document and header titles - subtitute attributes in section titles - tests for everything mentioned above
Diffstat (limited to 'test')
-rw-r--r--test/attributes_test.rb7
-rw-r--r--test/document_test.rb23
-rw-r--r--test/headers_test.rb107
-rw-r--r--test/preamble_test.rb24
4 files changed, 124 insertions, 37 deletions
diff --git a/test/attributes_test.rb b/test/attributes_test.rb
index 3fd2ba3b..ac2d6a5b 100644
--- a/test/attributes_test.rb
+++ b/test/attributes_test.rb
@@ -59,6 +59,13 @@ context "Attributes" do
assert_match /snort at the bar/, result.css("li").first.content.strip
end
+ test "substitutes inside heading" do
+ output = render_string(":prefix: Cool\n\n== {prefix} Title\n\ncontent")
+ result = Nokogiri::HTML(output)
+ assert_match /Cool Title/, result.css('h2').first.content
+ assert_match /_cool_title/, result.css('h2').first.attr('id')
+ end
+
test "renders attribute until it's deleted" do
pending "Not working yet (will require adding element-specific attributes or early attr substitution during parsing)"
# html = render_string(":foo: bar\nCrossing the {foo}\n\n:foo!:\nBelly up to the {foo}")
diff --git a/test/document_test.rb b/test/document_test.rb
index 1c5d5623..ea7eaafe 100644
--- a/test/document_test.rb
+++ b/test/document_test.rb
@@ -8,14 +8,35 @@ class DocumentTest < Test::Unit::TestCase
def test_title
assert_equal "AsciiDoc Home Page", @doc.doctitle
+ assert_equal "AsciiDoc Home Page", @doc.name
assert_equal 14, @doc.elements.size
assert_equal :preamble, @doc.elements[0].context
assert @doc.elements[1].is_a? ::Asciidoctor::Section
end
def test_with_no_title
- d = Asciidoctor::Document.new(["Snorf"])
+ d = document_from_string("Snorf")
assert_nil d.doctitle
+ assert_nil d.name
+ assert !d.has_header
+ assert_nil d.header
+ end
+
+ def test_with_explicit_title
+ d = document_from_string("= Title\n:title: Document Title\n\npreamble\n\n== Section")
+ assert_equal 'Document Title', d.doctitle
+ assert_equal 'Document Title', d.title
+ assert d.has_header
+ assert_equal 'Title', d.header.title
+ assert_equal 'Title', d.first_section.title
+ end
+
+ def test_empty_document
+ d = document_from_string('')
+ assert d.elements.empty?
+ assert_nil d.doctitle
+ assert !d.has_header
+ assert_nil d.header
end
def test_with_header_footer
diff --git a/test/headers_test.rb b/test/headers_test.rb
index fcf98ab4..a0c8d2c2 100644
--- a/test/headers_test.rb
+++ b/test/headers_test.rb
@@ -1,47 +1,49 @@
require 'test_helper'
context "Headers" do
- test "document title with multiline syntax" do
- title = "My Title"
- chars = "=" * title.length
- assert_xpath "//h1[not(@id)][text() = 'My Title']", render_string(title + "\n" + chars)
- assert_xpath "//h1[not(@id)][text() = 'My Title']", render_string(title + "\n" + chars + "\n")
- end
+ context "document title" do
+ test "document title with multiline syntax" do
+ title = "My Title"
+ chars = "=" * title.length
+ assert_xpath "//h1[not(@id)][text() = 'My Title']", render_string(title + "\n" + chars)
+ assert_xpath "//h1[not(@id)][text() = 'My Title']", render_string(title + "\n" + chars + "\n")
+ end
- test "document title with multiline syntax, give a char" do
- title = "My Title"
- chars = "=" * (title.length + 1)
- assert_xpath "//h1[not(@id)][text() = 'My Title']", render_string(title + "\n" + chars)
- assert_xpath "//h1[not(@id)][text() = 'My Title']", render_string(title + "\n" + chars + "\n")
- end
+ test "document title with multiline syntax, give a char" do
+ title = "My Title"
+ chars = "=" * (title.length + 1)
+ assert_xpath "//h1[not(@id)][text() = 'My Title']", render_string(title + "\n" + chars)
+ assert_xpath "//h1[not(@id)][text() = 'My Title']", render_string(title + "\n" + chars + "\n")
+ end
- test "document title with multiline syntax, take a char" do
- title = "My Title"
- chars = "=" * (title.length - 1)
- assert_xpath "//h1[not(@id)][text() = 'My Title']", render_string(title + "\n" + chars)
- assert_xpath "//h1[not(@id)][text() = 'My Title']", render_string(title + "\n" + chars + "\n")
- end
+ test "document title with multiline syntax, take a char" do
+ title = "My Title"
+ chars = "=" * (title.length - 1)
+ assert_xpath "//h1[not(@id)][text() = 'My Title']", render_string(title + "\n" + chars)
+ assert_xpath "//h1[not(@id)][text() = 'My Title']", render_string(title + "\n" + chars + "\n")
+ end
- test "not enough chars for a multiline document title" do
- title = "My Title"
- chars = "=" * (title.length - 2)
- assert_xpath '//h1', render_string(title + "\n" + chars), 0
- assert_xpath '//h1', render_string(title + "\n" + chars + "\n"), 0
- end
+ test "not enough chars for a multiline document title" do
+ title = "My Title"
+ chars = "=" * (title.length - 2)
+ assert_xpath '//h1', render_string(title + "\n" + chars), 0
+ assert_xpath '//h1', render_string(title + "\n" + chars + "\n"), 0
+ end
- test "too many chars for a multiline document title" do
- title = "My Title"
- chars = "=" * (title.length + 2)
- assert_xpath '//h1', render_string(title + "\n" + chars), 0
- assert_xpath '//h1', render_string(title + "\n" + chars + "\n"), 0
- end
+ test "too many chars for a multiline document title" do
+ title = "My Title"
+ chars = "=" * (title.length + 2)
+ assert_xpath '//h1', render_string(title + "\n" + chars), 0
+ assert_xpath '//h1', render_string(title + "\n" + chars + "\n"), 0
+ end
- test "document title with single-line syntax" do
- assert_xpath "//h1[not(@id)][text() = 'My Title']", render_string("= My Title")
- end
+ test "document title with single-line syntax" do
+ assert_xpath "//h1[not(@id)][text() = 'My Title']", render_string("= My Title")
+ end
- test "document title with symmetric syntax" do
- assert_xpath "//h1[not(@id)][text() = 'My Title']", render_string("= My Title =")
+ test "document title with symmetric syntax" do
+ assert_xpath "//h1[not(@id)][text() = 'My Title']", render_string("= My Title =")
+ end
end
context "level 1" do
@@ -110,5 +112,38 @@ context "Headers" do
test "with single line syntax" do
assert_xpath "//h5[@id='_my_title'][text() = 'My Title']", render_string("===== My Title")
end
- end
+ end
+
+ context "book doctype" do
+ test "document title with level 0 headings" do
+ input = <<-EOS
+Book
+====
+:doctype: book
+
+= Chapter One
+
+It was a dark and stormy night...
+
+= Chapter Two
+
+They couldn't believe their eyes when...
+
+== Interlude
+
+While they were waiting...
+
+= Chapter Three
+
+That's all she wrote!
+ EOS
+
+ output = render_string(input)
+ assert_xpath '//h1', output, 4
+ assert_xpath '//h2', output, 1
+ assert_xpath '//h1[@id="_chapter_one"][text() = "Chapter One"]', output, 1
+ assert_xpath '//h1[@id="_chapter_two"][text() = "Chapter Two"]', output, 1
+ assert_xpath '//h1[@id="_chapter_three"][text() = "Chapter Three"]', output, 1
+ end
+ end
end
diff --git a/test/preamble_test.rb b/test/preamble_test.rb
index 1b3b4fc6..5ae997c8 100644
--- a/test/preamble_test.rb
+++ b/test/preamble_test.rb
@@ -85,4 +85,28 @@ Section paragraph 1.
assert_xpath '//h2[@id="_first_section"]/preceding::p', result, 1
end
+ test 'preamble in book doctype' do
+ input = <<-EOS
+Book
+====
+:doctype: book
+
+Back then...
+
+= Chapter One
+
+It was a dark and stormy night...
+
+= Chapter Two
+
+They couldn't believe their eyes when...
+ EOS
+
+ d = document_from_string(input)
+ assert_equal 'book', d.doctype
+ output = d.render
+ assert_xpath '//h1', output, 3
+ assert_xpath '//*[@id="preamble"]//p[text() = "Back then..."]', output, 1
+ end
+
end