= Asciidoctor::DocTest Jakub Jirutka https://github.com/jirutka[@jirutka] :source-language: ruby // custom :gem-name: asciidoctor-doctest :gem-version: 2.0.0.beta.7 :gh-name: asciidoctor/{gem-name} :gh-branch: master :codeclimate-id: fa264b72eeeacd2e86df :doctor-man-uri: http://asciidoctor.org/docs/user-manual :rawgit-base: https://cdn.rawgit.com/{gh-name}/master :src-base: lib/asciidoctor/doctest
image:https://github.com/{gh-name}/workflows/CI/badge.svg[CI Status, link=https://github.com/{gh-name}/actions?query=workflow%3A%22CI%22] image:https://api.codeclimate.com/v1/badges/{codeclimate-id}/test_coverage[Test Coverage, link="https://codeclimate.com/github/{gh-name}/test_coverage"] image:https://api.codeclimate.com/v1/badges/{codeclimate-id}/maintainability[Maintainability, link="https://codeclimate.com/github/{gh-name}/maintainability"] image:https://inch-ci.org/github/{gh-name}.svg?branch={gh-branch}[Inline docs, link="http://inch-ci.org/github/{gh-name}"] image:https://img.shields.io/badge/yard-docs-blue.svg[Yard Docs, link="http://www.rubydoc.info/github/{gh-name}/frames"]
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, input examples>> (documents in AsciiDoc syntax) to simplify and systematize writing tests for new backends. You just write or <<generate-examples, generate>> the expected output, i.e. what the backend should produce for the given input.
image::{rawgit-base}/doc/img/doctest-diag.svg[diagram, align="center"]
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 <
DocTest supports HTML-based backends and can be easily <<how-to-extend-it, extended>> to support any other backend with textual output.
== Setup DocTest
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: + [source, sh] mkdir -p test/examples/shiny + and optionally a directory for your extra input examples: + [source, sh] mkdir -p test/examples/asciidoc
. Add development dependency on asciidoctor-doctest
to your gemspec:
+
[source, ruby, subs="+attributes"]
s.add_development_dependency 'asciidoctor-doctest', '= {gem-version}'
+
or Gemfile if you’re not distributing the backend as a gem:
+
[source, ruby, subs="+attributes"]
gem 'asciidoctor-doctest', '= {gem-version}'
+
and run bundle install
.
Rakefile
and configure DocTest tasks:
+
[source, ruby]require 'asciidoctor/doctest' require 'thread_safe' require 'tilt'
DocTest::RakeTasks.new(:doctest) do |t|
. Check if rake loads the DocTest tasks doctest, doctest:test and doctest:generate. + [source, sh] bundle exec rake -D
== Run tests
Assume that you have defined the Rake tasks in the default namespace doctest (see above). Then you can simply run:
[source, sh] bundle exec rake doctest:test
To test only specific examples, use PATTERN
with glob-like expression:
[source, sh] bundle exec rake doctest:test PATTERN='inline_:'
For verbose output, use VERBOSE=yes
:
[source, sh] bundle exec rake doctest:test VERBOSE=yes
image::doc/img/failing-test-term.gif[Failing test in term, align="center"]
== Examples
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 table.adoc
, 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. 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, run tests>>, the input example is converted using the tested backend (or templates) and its actual output is compared with the expected output example.
[horizontal]
.List of Asciidoctor’s AST nodes
document:: TODO
embedded:: TODO
section:: {doctor-man-uri}/#sections[document sections], i.e. headings
admonition:: {doctor-man-uri}/#admonition[an admonition block]
audio:: {doctor-man-uri}/#audio[an audio block]
colist:: {doctor-man-uri}/#callouts[a code callouts] list
dlist:: {doctor-man-uri}/#description-list[a labeled list] (aka definition list) and {doctor-man-uri}/#question-and-answer-style-list[a Q&A style list]
example:: {doctor-man-uri}/#example[an example block]
floating_title:: {doctor-man-uri}/#discrete-or-floating-section-titles[a discrete or floating section title]
image:: {doctor-man-uri}/#images[an image block]
listing:: {doctor-man-uri}/#listing-blocks[a listing and source code block]
literal:: {doctor-man-uri}/#literal-text-and-blocks[a literal block]
olist:: {doctor-man-uri}/#ordered-lists[an ordered list] (i.e. numbered list)
open:: {doctor-man-uri}/#open-blocks[open blocks], {doctor-man-uri}/#user-abstractabstract[abstract], …
outline:: an actual {doctor-man-uri}/#user-toc[TOC] content (i.e. list of links), usually recursively called
page_break:: {doctor-man-uri}/#page-break[page break]
paragraph:: {doctor-man-uri}/#paragraph[a paragraph]
pass:: {doctor-man-uri}/#pass-bl[a passthrough block]
preamble:: {doctor-man-uri}/#doc-preamble[a preamble], optionally with a TOC
quote:: {doctor-man-uri}/#quote[a quote block]
sidebar:: {doctor-man-uri}/#sidebar[a sidebar]
stem:: {doctor-man-uri}/#stem[a STEM block] (Science, Technology, Engineering and Math)
table:: {doctor-man-uri}/#tables[a table]
thematic_break:: {doctor-man-uri}/#horizontal-rules[a thematic break] (i.e. horizontal rule)
toc:: {doctor-man-uri}/#manual-placement[a TOC macro] (i.e. manually placed TOC); This block is used for toc::[]
macro only and it’s responsible just for rendering of a the TOC “envelope,” not an actual TOC content.
ulist:: {doctor-man-uri}/#unordered-lists[an unordered list] (aka bullet list) and a {doctor-man-uri}/#checklist[checklist] (e.g. TODO list)
verse:: {doctor-man-uri}/#verse[a verse block]
video:: {doctor-man-uri}/#video[a video block]
inline_anchor:: {doctor-man-uri}/#url[anchors] (links, cross references and bibliography references)
inline_break:: {doctor-man-uri}/#line-breaks[line break]
inline_button:: {doctor-man-uri}/#ui-buttons[UI button]
inline_callout:: {doctor-man-uri}/#callouts[code callout] icon/mark inside a code block
inline_footnote:: {doctor-man-uri}/#user-footnotes[footnote]
inline_image:: {doctor-man-uri}/#images[inline image] and {doctor-man-uri}/#inline-icons[inline icon]
inline_kbd:: {doctor-man-uri}/#keyboard-shortcuts[keyboard shortcut]
inline_menu:: {doctor-man-uri}/#menu-selections[menu section]
inline_quoted:: {doctor-man-uri}/#quotes[text formatting]; emphasis, strong, monospaced, superscript, subscript, curved quotes and inline STEM
=== Input examples
DocTest provides a collection of the reference input examples that should be suitable for most backends. You can find them in link:data/examples/asciidoc[].footnote:[Since GitHub implicitly renders them as a plain AsciiDoc, you must view a Raw source if you want to see what’s going on there.] 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. 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).
==== Format
// .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
=== HTML-based examples
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:
==== Options
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()
for inline_*:*
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.
==== Format
The example’s content in HTML.
=== Generate examples
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 Rake tasks in the default namespace doctest (see <
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):
[source, sh] bundle exec rake doctest:generate
Same as previous, but rewrite existing tested examples:
[source, sh] bundle exec rake doctest:generate FORCE=yes
Generate just examples for ulist
node (i.e. all examples in ulist.adoc
file(s) found on the examples_path) that doesn’t exist yet:
[source, sh] bundle exec rake doctest:generate PATTERN='ulist:*'
(Re)generate examples which name starts with basic
for all inline nodes (i.e. files that starts with inline_
):
[source, sh] bundle exec rake doctest:generate PATTERN='inline_:basic' FORCE=yes
== How to extend it
You can extend DocTest to support any textual format you want. All what you need is to subclass link:{src-base}/io/base.rb[IO::Base] and link:{src-base}/asciidoc_converter.rb[AsciidocConverter] (see link:{src-base}/html/converter.rb[HTML::Converter] for example). Please note that this code is still in beta – refactoring is not finished, so some parts of the doctest code-base are kinda strange now (i.e. AsciidocConverter and HTML::Converter).
== Contributing
. 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
== License
This project is licensed under http://opensource.org/licenses/MIT/[MIT License]. For the full text of the license, see the link:LICENSE[LICENSE] file.