Skip to content

Commit c7282e6

Browse files
committed
Move example text to the line after the example label.
Test Plan: Added test. Reviewed By: krieb
1 parent 292ff81 commit c7282e6

File tree

4 files changed

+60
-15
lines changed

4 files changed

+60
-15
lines changed

babelapi/babel/parser.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -843,11 +843,9 @@ def p_examples_add(self, p):
843843

844844
# It's possible for no example fields to be specified.
845845
def p_example(self, p):
846-
"""example : KEYWORD ID STRING NEWLINE INDENT example_fields DEDENT
847-
| KEYWORD ID empty NEWLINE INDENT example_fields DEDENT
848-
| KEYWORD ID STRING NEWLINE
849-
| KEYWORD ID empty NEWLINE"""
850-
if len(p) > 5:
846+
"""example : KEYWORD ID NEWLINE INDENT docsection example_fields DEDENT
847+
| KEYWORD ID NEWLINE"""
848+
if len(p) > 4:
851849
seen_fields = set()
852850
for example_field in p[6]:
853851
if example_field.name in seen_fields:
@@ -857,11 +855,11 @@ def p_example(self, p):
857855
p.lineno(1), self.path))
858856
seen_fields.add(example_field.name)
859857
p[0] = BabelExample(
860-
self.path, p.lineno(1), p.lexpos(1), p[2], p[3],
858+
self.path, p.lineno(1), p.lexpos(1), p[2], p[5],
861859
OrderedDict((f.name, f) for f in p[6]))
862860
else:
863861
p[0] = BabelExample(
864-
self.path, p.lineno(1), p.lexpos(1), p[2], p[3], OrderedDict())
862+
self.path, p.lineno(1), p.lexpos(1), p[2], None, OrderedDict())
865863

866864
def p_example_fields_create(self, p):
867865
'example_fields : example_field'

babelapi/data_type.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ def __init__(self, label, text, value, token=None):
780780
assert isinstance(label, six.text_type), type(label)
781781
self.label = label
782782
assert isinstance(text, (six.text_type, type(None))), type(text)
783-
self.text = text
783+
self.text = doc_unwrap(text) if text else text
784784
assert isinstance(value, (six.text_type, OrderedDict)), type(value)
785785
self.value = value
786786
self._token = token

doc/lang_ref.rst

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ The spec should live in a file called ``users.babel``::
3939
status Status
4040
"The status of the account."
4141

42-
example default "A regular user"
42+
example default
43+
"A regular user"
4344
account_id="id-48sa2f0"
4445
email="alex@example.org"
4546
name="Alexander the Great"
@@ -297,10 +298,9 @@ examples help demonstrate how distinct fields might interact with each other.
297298
Generators have access to examples, which is useful when automatically
298299
generating documentation.
299300

300-
An example is declared by using the ``example`` keyword followed by a label
301-
and optionally a descriptive string. By convention, "default" should
302-
be used as the label name for an example that can be considered a good
303-
representation of the general case for the type::
301+
An example is declared by using the ``example`` keyword followed by a label.
302+
By convention, "default" should be used as the label name for an example that
303+
can be considered a good representation of the general case for the type::
304304

305305
struct Account extends BasicAccount
306306
"Information about a user's account."
@@ -310,13 +310,15 @@ representation of the general case for the type::
310310
status Status
311311
"The status of the account."
312312

313-
example default "A regular user"
313+
example default
314+
"A regular user"
314315
account_id = "id-48sa2f0"
315316
email = "alex@example.org"
316317
name = "Alexander the Great"
317318
status = active
318319

319-
example unnamed "An anonymous user"
320+
example unnamed
321+
"An anonymous user"
320322
account_id = "id-29sk2p1"
321323
email = "anony@example.org"
322324
name = null
@@ -326,6 +328,9 @@ Every required field (not nullable and no default) must be specified, otherwise
326328
an error will be returned. ``null`` can be used to mark that a nullable type
327329
is not present.
328330

331+
An optional multi-line documentation string can be specified after the line
332+
declaring the example and before the example fields.
333+
329334
When you have a set of nested types, each type defines examples for its fields
330335
with primitive types. For fields with composite types, the value of the example
331336
must be a label of an example in the target composite type. Here's an example

test/test_babel.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2278,6 +2278,48 @@ def test_examples_union(self):
22782278
"Bad example for field 'a': example of void type must be null",
22792279
cm.exception.msg)
22802280

2281+
def test_examples_text(self):
2282+
# Test multi-line example text (verify it gets unwrapp-ed)
2283+
text = textwrap.dedent("""\
2284+
namespace test
2285+
2286+
struct S
2287+
a String
2288+
2289+
example default
2290+
"This is the text for the example.
2291+
And I guess it's kind of long."
2292+
a = "Hello, World."
2293+
""")
2294+
t = TowerOfBabel([('test.babel', text)])
2295+
t.parse()
2296+
s_dt = t.api.namespaces['test'].data_type_by_name['S']
2297+
example = s_dt.get_examples()['default']
2298+
self.assertEqual(
2299+
example.text,
2300+
"This is the text for the example. And I guess it's kind of long.")
2301+
2302+
# Test union example
2303+
text = textwrap.dedent("""\
2304+
namespace test
2305+
2306+
union U
2307+
a
2308+
b String
2309+
2310+
example default
2311+
"This is the text for the example.
2312+
And I guess it's kind of long."
2313+
b = "Hi, World."
2314+
""")
2315+
t = TowerOfBabel([('test.babel', text)])
2316+
t.parse()
2317+
u_dt = t.api.namespaces['test'].data_type_by_name['U']
2318+
example = u_dt.get_examples()['default']
2319+
self.assertEqual(
2320+
example.text,
2321+
"This is the text for the example. And I guess it's kind of long.")
2322+
22812323
def test_examples_enumerated_subtypes(self):
22822324
# Test missing custom example
22832325
text = textwrap.dedent("""\

0 commit comments

Comments
 (0)