DocTest is a tool for end-to-end testing of Asciidoctor backends based on comparing of textual output.
It provides a collection of categorized input examples (documents in AsciiDoc syntax) to simplify and systematize writing tests for new backends. You just write or generate the expected output, i.e. what the backend should produce for the given input.
Each example should be focused on one use case, so when writing a new backend, you can incrementally implement new features following the reference input examples. However, they are not strictly isolated like unit tests. For example, if you change a format of a paragraph, it may affect a variety of other examples.
When test fails, DocTest prints a nicely formatted diff of the expected and actual output (see Run tests), so you see exactly what went wrong. Insignificant differences, such as attributes order in HTML, are ignored.
DocTest supports HTML-based backends and can be easily extended to support any other backend with textual output.
Let’s say that you’re developing a new shiny HTML template-based backend named “shiny” and assume that you have templates in the directory data/templates
.
-
Create a directory for your output examples:
mkdir -p test/examples/shiny
and optionally a directory for your extra input examples:
mkdir -p test/examples/asciidoc
-
Add development dependency on
asciidoctor-doctest
to your gemspec:s.add_development_dependency 'asciidoctor-doctest', '~> 1.5.2'
or Gemfile if you’re not distributing the backend as a gem:
gem 'asciidoctor-doctest', '~> 1.5.2'
and run
bundle install
. -
Create or edit
test/test_helper.rb
; require test dependencies and setupexamples_path
:require 'asciidoctor/doctest' require 'minitest/autorun' # used to colorize output require 'minitest/rg' # needed if you're testing templates-based backend require 'tilt' # extra input examples (optional) DocTest.examples_path.unshift 'test/examples/asciidoc' # output examples DocTest.examples_path.unshift 'test/examples/shiny'
-
Create test file
test/templates_test.rb
and extend class DocTest::Test. Specifyconverter_opts
and then callgenerate_tests!
macro with a specific subclass ofBaseExamplesSuite
:require 'test_helper' class TestTemplates < DocTest::Test converter_opts template_dirs: 'data/templates' generate_tests! DocTest::HTML::ExamplesSuite end
-
Create or edit
Rakefile
; add tasks to run tests and optionally a generator of output examples:require 'asciidoctor/doctest' require 'rake/testtask' require 'thread_safe' require 'tilt' Rake::TestTask.new(:test) do |task| task.description = 'Run tests for templates' task.pattern = 'test/templates_test.rb' task.libs << 'test' end DocTest::GeneratorTask.new(:generate) do |task| task.output_suite = DocTest::HTML::ExamplesSuite.new(examples_path: 'test/examples/shiny') task.converter_opts[:template_dirs] = 'data/templates' # # add extra input examples (optional) task.examples_path.unshift 'test/examples/asciidoc' end # When no task specified, run test. task :default => :test
Test example is just a document fragment in AsciiDoc syntax (a reference input), or the backend’s target syntax (an expected output). Example should consider one case of the generated output, i.e. it should reflect one code branch in a converter or template. Examples are grouped in example groups. Each group focuses on one block or inline element — more precisely Asciidoctor’s AST node (paragraph, table, anchor, footnote…).
Examples group is a text file named similar to Asciidoctor templates, i.e. the AST node name with an extension according to syntax, for example block_table.adoc
, block_table.html
. See below for a list of the AST nodes.
Individual examples in the group file are separated by a special header with the name of the example, an optional description and options.
Each example is identified by its name and the group name like this: {group_name}:{example_name}
(e.g. block_table:with_title
).
Input and output examples are paired — for every input example there should be an output example with the same identifier.
When you run tests, the input example is converted using the tested backend (or templates) and its actual output is compared with the expected output example.
document |
TODO |
embedded |
TODO |
section |
document sections, i.e. headings |
block_admonition | |
block_audio | |
block_colist |
a code callouts list |
block_dlist |
a labeled list (aka definition list) and a Q&A style list |
block_example | |
block_floating_title | |
block_image | |
block_listing | |
block_literal | |
block_olist |
an ordered list (i.e. numbered list) |
block_open |
open blocks, abstract, … |
block_outline |
an actual TOC content (i.e. list of links), usually recursively called |
block_page_break | |
block_paragraph | |
block_pass | |
block_preamble |
a preamble, optionally with a TOC |
block_quote | |
block_sidebar | |
block_stem |
a STEM block (Science, Technology, Engineering and Math) |
block_table | |
block_thematic_break |
a thematic break (i.e. horizontal rule) |
block_toc |
a TOC macro (i.e. manually placed TOC); This block is used for |
block_ulist |
an unordered list (aka bullet list) and a checklist (e.g. TODO list) |
block_verse | |
block_video | |
inline_anchor |
anchors (links, cross references and bibliography references) |
inline_break | |
inline_button | |
inline_callout |
code callout icon/mark inside a code block |
inline_footnote | |
inline_image | |
inline_kbd | |
inline_menu | |
inline_quoted |
text formatting; emphasis, strong, monospaced, superscript, subscript, curved quotes and inline STEM |
DocTest provides a collection of the reference input examples that should be suitable for most backends. You can find them in data/examples/asciidoc.[1] There are a lot of test examples and some of them may not be relevant to your backend — that’s okay, when some output example is not found, it’s marked as skipped in test.
You can also write your own input examples and use them together with those provided (or replace them).
Just add another directory to your examples_path (e.g. test/examples/asciidoc
) and create example group files with .adoc
suffix here (e.g. block_video.adoc
).
When DocTest is looking for examples to test, it indexes all examples found in files with .adoc
suffix on the examples_path.
If there are two files with the same name, it simply merges their content, and if they contain two examples with the same name, then the first wins (i.e. that from the file that is ahead on the examples_path).
// .first_example
// Each block must be preceded by a header (comment); the first line must
// contain the example’s name prefixed with a dot. This text is interpreted
// as a description.
The example’s content in *Asciidoc*.
NOTE: The trailing new line (below this) will be removed.
// .second_example
* List item level 1
** List item level 2
HtmlTest assumes that paragraphs are enclosed in <p></p>
tags and implicitly sets the include option to ./p/node()
for inline_*:*
examples (if include is not already set).
If it’s not your case, then you must overwrite it:
class TestShiny < DocTest::Test
converter_opts template_dirs: 'data/templates'
generate_tests! DocTest::HTML::ExamplesSuite.new(paragraph_xpath: './div/p/node()')
end
List of options that can be set in the header of HTML example.
- include
-
XPath expression that specifies a subsection of the document that should be compared (asserted). Default is
./p/node()
forinline_*:*
groups and empty (i.e..
) for others. - exclude
-
XPath expression that specifies parts of the document that should not be compared (asserted). Always start the expression with a dot (e.g.
.//h1
). This option may be used multiple times per example. - header_footer
-
Option for Asciidoctor to render a full document (instead of embedded). This is default for
document:*
group.
<!-- .first_example
Each example must be preceded by a header (comment); the first line must
contain the example’s name prefixed with a dot. This text is interpreted
as a description.
-->
<p>The example’s content in <strong>HTML</strong>.</p>
<div class="note">The trailing new line (below this) will be removed.</div>
<!-- .second_example
You may also specify options for comparing or Asciidoctor renderer. Option
line starts with a semicolon, then comes the option name ended by a
semicolon and after that the option’s value (may be omitted for boolean
options).
:option_1: value 1
:option_2: value 1
:option_2: value 2
:boolean_option:
-->
<div class="colist">
<ol>
<li>Method signature</li>
<li>Some stuff inside</li>
<li>Return statement</li>
</ol>
</div>
Writing examples of an expected output for all the input examples from scratch is quite a chore. Therefore DocTest provides a generator. When you have at least partially working Asciidoctor backend (converter or a set of templates), you can pass the input examples through it and generate your output examples. Then you should verify them and modify if needed.
Assume that you have defined the generator Rake task named :generator
(see Setup DocTest).
Now you can generate output examples from all the input examples (those with .adoc
extension) found on the examples_path that doesn’t already exist (i.e. it doesn’t rewrite existing):
bundle exec rake generate
Same as previous, but rewrite existing tested examples:
bundle exec rake generate FORCE=yes
Generate just examples for block_ulist
node (i.e. all examples in block_ulist.adoc
file(s) found on the examples_path) that doesn’t exist yet:
bundle exec rake generate PATTERN='block_ulist:*'
(Re)generate examples which name starts with basic
for all block nodes (i.e. files that starts with block_
):
bundle exec rake generate PATTERN='block_*:basic*' FORCE=yes
You can extend DocTest to support any textual format you want. All what you need is to subclass BaseExamplesSuite and usually also BaseExample.
-
Fork it
-
Create your feature branch (
git checkout -b my-new-feature
) -
Commit your changes (
git commit -am 'Add some feature'
) -
Push to the branch (
git push origin my-new-feature
) -
Create new Pull Request
This project is licensed under MIT License. For the full text of the license, see the LICENSE file.