Skip to content

Commit eb88060

Browse files
committed
tests: Add doctests
1 parent de47bba commit eb88060

3 files changed

Lines changed: 53 additions & 48 deletions

File tree

docs/api.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,17 @@ Template
5050
Pipeline
5151
--------
5252

53-
.. autoclass:: BaseContextRole
54-
.. autoclass:: BaseContextDirective
55-
.. autoclass:: BaseDataDefineRole
56-
.. autoclass:: BaseDataDefineDirective
57-
.. autoclass:: StrictDataDefineDirective
53+
.. autoclass:: sphinxnotes.render.BaseContextRole
54+
.. autoclass:: sphinxnotes.render.BaseContextDirective
55+
.. autoclass:: sphinxnotes.render.BaseDataDefineRole
56+
.. autoclass:: sphinxnotes.render.BaseDataDefineDirective
57+
.. autoclass:: sphinxnotes.render.StrictDataDefineDirective
5858

5959
Registry
6060
========
6161

62+
.. autodata:: sphinxnotes.render.REGISTRY
63+
6264
.. autoclass:: sphinxnotes.render.Registry
6365

6466
.. autoproperty:: data

docs/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,5 @@
124124

125125
_ = extensions.pop() # no need to load extension
126126
primary_domain = 'py'
127+
128+
extensions.append('sphinx.ext.doctest')

docs/dsl.rst

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ The Field Declaration DSL is a Domain Specific Language (DSL) that used to
1212
define the type and structure of field values. A DSL declaration consists of
1313
one or more :term:`modifier`\ s separated by commas (``,``).
1414

15+
Python API
16+
==========
17+
18+
User can create a :class:`sphinxnotes.render.Field` from DSL and use it to parse
19+
string to :type:`sphinxnotes.render.Value`:
20+
21+
>>> from sphinxnotes.render import Field
22+
>>> Field.from_dsl('list of int').parse('1,2,3')
23+
[1, 2, 3]
24+
1525
Syntax
1626
======
1727

@@ -174,65 +184,56 @@ Extending the DSL
174184

175185
You can extend the DSL by registering custom types, flags, and by-options
176186
through the :attr:`~sphinxnotes.render.Registry.data` attribute of
177-
:class:`sphinxnotes.render.Registry`.
187+
:data:`sphinxnotes.render.REGISTRY`.
178188

179189
.. _add-custom-types:
180190

181191
Adding Custom Types
182192
-------------------
183193

184-
Use :meth:`sphinxnotes.render.data.Registry.add_type` to add a new type:
185-
186-
.. code-block:: python
187-
188-
from sphinxnotes.render import Registry
189-
190-
def parse_color(v: str):
191-
return tuple(int(x) for x in v.split(';'))
192-
193-
def color_to_str(v):
194-
return ';'.join(str(x) for x in v)
195-
196-
Registry.data.add_type('color', tuple, parse_color, color_to_str)
197-
val = Field.from_dsl('color').parse('255;0;0')
198-
assert val == (255, 0, 0)
194+
Use :meth:`~sphinxnotes.render.data.REGISTRY.add_type` method of
195+
:data:`sphinxnotes.render.REGISTRY` to add a new type:
196+
197+
>>> from sphinxnotes.render import REGISTRY
198+
>>>
199+
>>> def parse_color(v: str):
200+
... return tuple(int(x) for x in v.split(';'))
201+
...
202+
>>> def color_to_str(v):
203+
... return ';'.join(str(x) for x in v)
204+
...
205+
>>> REGISTRY.data.add_type('color', tuple, parse_color, color_to_str)
206+
>>> Field.from_dsl('color').parse('255;0;0')
207+
(255, 0, 0)
199208

200209
.. _add-custom-flags:
201210

202211
Adding Custom Flags
203212
-------------------
204213

205-
Use :meth:`sphinxnotes.render.data.Registry.add_flag` to add a new flag:
214+
Use :meth:`~sphinxnotes.render.data.Registry.add_flag` method of
215+
:data:`sphinxnotes.render.REGISTRY` to add a new type:
206216

207-
.. code-block:: python
208-
209-
from sphinxnotes.render import Registry
210-
211-
Registry.data.add_flag('unique', default=False)
212-
field = Field.from_dsl('int, unique')
213-
assert field.unique is True
217+
>>> from sphinxnotes.render import REGISTRY
218+
>>> REGISTRY.data.add_flag('unique', default=False)
219+
>>> field = Field.from_dsl('int, unique')
220+
>>> field.unique
221+
True
214222

215223
.. _add-custom-by-options:
216224

217225
Adding Custom By-Options
218226
------------------------
219227

220-
Use :meth:`sphinxnotes.render.data.Registry.add_by_option`: to add a new by-option:
221-
222-
.. code-block:: python
223-
224-
from sphinxnotes.render import Registry
225-
226-
Registry.data.add_by_option('group', str)
227-
field = Field.from_dsl('str, group by size')
228-
assert field.group == 'size'
229-
230-
# For options that can be specified multiple times:
231-
Registry.data.add_by_option('index', str, store='append')
232-
field = Field.from_dsl('str, index by month, index by year')
233-
assert field.index == ['month', 'year']
234-
235-
Usage::
236-
237-
int, group by foo
238-
int, index by year, index by month
228+
Use :meth:`~sphinxnotes.render.data.Registry.add_by_option` method of
229+
:data:`sphinxnotes.render.REGISTRY` to add a new by-option:
230+
231+
>>> from sphinxnotes.render import REGISTRY
232+
>>> REGISTRY.data.add_by_option('group', str)
233+
>>> field = Field.from_dsl('str, group by size')
234+
>>> field.group
235+
'size'
236+
>>> REGISTRY.data.add_by_option('index', str, store='append')
237+
>>> field = Field.from_dsl('str, index by month, index by year')
238+
>>> field.index
239+
['month', 'year']

0 commit comments

Comments
 (0)