From a2d0c353fdb6fb2c27e352f1c5b555ed1f1fa322 Mon Sep 17 00:00:00 2001
From: "Matthias C. M. Troffaes" <matthias.troffaes@gmail.com>
Date: Wed, 20 Jul 2022 13:02:25 +0100
Subject: [PATCH] Fix tests for older docutils versions. (#23)

Closes #22.
---
 .github/workflows/python-package.yml | 13 ++++++-
 CHANGELOG.rst                        |  7 ++++
 requirements.txt                     |  2 +-
 test/test_backend.py                 | 33 ++++++++++++-----
 test/test_simplebibliography.py      | 55 ++++++++++++++++++++--------
 5 files changed, 82 insertions(+), 28 deletions(-)

diff --git a/test/test_backend.py b/test/test_backend.py
index f099d1b..060afe2 100644
--- a/test/test_backend.py
+++ b/test/test_backend.py
@@ -1,3 +1,4 @@
+import docutils
 import docutils.nodes
 import docutils.utils
 import pytest
@@ -127,11 +128,15 @@ def test_citation(entry, document):
 
 def test_citation_reference(entry, document):
     node = Backend().citation_reference(entry, document)
+    id_ = (
+        "id1" if docutils.__version_info__ < (0, 18) else
+        "citation-reference-1"
+    )
     assert str(node) == (
-        '<citation_reference ids="citation-reference-1" '
-        'refname="hongquin1997">'
-        'hongquin1997'
-        '</citation_reference>')
+        f'<citation_reference ids="{id_}" '
+        f'refname="hongquin1997">'
+        f'hongquin1997'
+        f'</citation_reference>')
 
 
 def test_citation_use_label(entry, document):
@@ -152,11 +157,15 @@ def test_citation_use_label(entry, document):
 def test_citation_reference_use_label(entry, document):
     node = Backend().citation_reference(
         entry, document, use_key_as_label=False)
+    id_ = (
+        "id1" if docutils.__version_info__ < (0, 18) else
+        "citation-reference-1"
+    )
     assert str(node) == (
-        '<citation_reference ids="citation-reference-1" '
-        'refname="hongquin1997">'
-        '1'
-        '</citation_reference>')
+        f'<citation_reference ids="{id_}" '
+        f'refname="hongquin1997">'
+        f'1'
+        f'</citation_reference>')
 
 
 def test_footnote(entry, document):
@@ -174,9 +183,13 @@ def test_footnote(entry, document):
 
 def test_footnote_reference(entry, document):
     node = Backend().footnote_reference(entry, document)
+    id_ = (
+        "id1" if docutils.__version_info__ < (0, 18) else
+        "footnote-reference-1"
+    )
     assert str(node) == (
-        '<footnote_reference auto="1" ids="[\'footnote-reference-1\']" '
-        'refname="hongquin1997"/>')
+        f'<footnote_reference auto="1" ids="[\'{id_}\']" '
+        f'refname="hongquin1997"/>')
 
 
 def test_write_entry():
diff --git a/test/test_simplebibliography.py b/test/test_simplebibliography.py
index 22906cf..5727a3a 100644
--- a/test/test_simplebibliography.py
+++ b/test/test_simplebibliography.py
@@ -1,4 +1,5 @@
 import re
+import docutils
 from docutils.core import publish_file
 import docutils.parsers.rst.directives as directives
 
@@ -6,24 +7,48 @@
 
 
 def html_citation_ref(key):
-    return re.compile(
-        fr'<a class="citation-reference" '
-        fr'href="#{key.lower()}" id="[a-zA-Z0-9_-]+" '
-        fr'role="doc-biblioref">'
-        fr'\[{key}]'
-        fr'</a>')
+    if docutils.__version_info__ < (0, 18):
+        return re.compile(
+            fr'<a class="citation-reference" '
+            fr'href="#{key.lower()}" id="\w+">'
+            fr'\[{key}]'
+            fr'</a>')
+    else:
+        return re.compile(
+            fr'<a class="citation-reference" '
+            fr'href="#{key.lower()}" id="[a-zA-Z0-9_-]+" '
+            fr'role="doc-biblioref">'
+            fr'\[{key}]'
+            fr'</a>')
 
 
 def html_citation(key):
-    return re.compile(
-        fr'<div class="citation" id="{key.lower()}" role="doc-biblioentry">\s*'
-        fr'<span class="label">'
-        fr'<span class="fn-bracket">\[</span>'
-        fr'(?:<a role="doc-backlink" href="#[a-zA-Z0-9_-]+">)?'
-        fr'{key}'
-        fr'(?:</a>)?'
-        fr'<span class="fn-bracket">]</span>'
-        fr'</span>')
+    if docutils.__version_info__ < (0, 18):
+        return re.compile(
+            fr'<dt class="label" id="{key.lower()}">'
+            fr'<span class="brackets">'
+            fr'(?:<a class="fn-backref" href="#\w+">)?'
+            fr'{key}'
+            fr'(?:</a>)?'
+            fr'</span>'
+            fr'(?:<span class="fn-backref">\('
+            fr'<a href="#\w+">1</a>'
+            fr',<a href="#\w+">2</a>'
+            fr'(,<a href="#\w+">3</a>)?'
+            fr'(,<a href="#\w+">\d+</a>)*'
+            fr'\)</span>)?'
+            fr'</dt>\n')
+    else:
+        return re.compile(
+            fr'<div class="citation" id="{key.lower()}" '
+            fr'role="doc-biblioentry">\s*'
+            fr'<span class="label">'
+            fr'<span class="fn-bracket">\[</span>'
+            fr'(?:<a role="doc-backlink" href="#[a-zA-Z0-9_-]+">)?'
+            fr'{key}'
+            fr'(?:</a>)?'
+            fr'<span class="fn-bracket">]</span>'
+            fr'</span>')
 
 
 def test_simplebibliography(test_roots):
