diff --git a/ZConfig/info.py b/ZConfig/info.py index 1364865..c663db1 100644 --- a/ZConfig/info.py +++ b/ZConfig/info.py @@ -317,6 +317,7 @@ def __init__(self, name, keytype, valuetype, datatype, registry, types): self.valuetype = valuetype self.handler = None self.description = None + self.example = None self.registry = registry self._children = [] # [(key, info), ...] self._attrmap = {} # {attribute: info, ...} @@ -504,6 +505,7 @@ def createDerivedSchema(base): base.handler, base.url, base.registry) new._components.update(base._components) new.description = base.description + new.example = base.example new._children[:] = base._children new._attrmap.update(base._attrmap) new._keymap.update(base._keymap) diff --git a/ZConfig/schema.py b/ZConfig/schema.py index 50af355..65e1c4f 100644 --- a/ZConfig/schema.py +++ b/ZConfig/schema.py @@ -56,7 +56,7 @@ class BaseParser(xml.sax.ContentHandler): "description": ["key", "section", "multikey", "multisection", "sectiontype", "abstracttype", "schema", "component"], - "example": ["key", "section", "multikey", "multisection"], + "example": ["schema", "sectiontype", "key", "multikey"], "metadefault": ["key", "section", "multikey", "multisection"], "default": ["key", "multikey"], "import": ["schema", "component"], @@ -280,6 +280,9 @@ def characters_description(self, data): self._stack[-1].description = data def characters_example(self, data): + if self._stack[-1].example is not None: + self.error( + "at most one may be used for each element") self._stack[-1].example = data def characters_metadefault(self, data): diff --git a/ZConfig/tests/test_schema.py b/ZConfig/tests/test_schema.py index f6bafa3..0ea32b8 100644 --- a/ZConfig/tests/test_schema.py +++ b/ZConfig/tests/test_schema.py @@ -1084,6 +1084,77 @@ def test_srepr(self): self.assertEqual(_srepr('foo'), "'foo'") self.assertEqual(_srepr(FOO), "'foo'") + def test_schema_example(self): + schema = self.load_schema_text("""\ + + This is an example + + """) + self.assertEqual(schema.example, 'This is an example') + + def test_key_example(self): + schema = self.load_schema_text("""\ + + + + This is an example + + + + """) + self.assertEqual(schema.gettype('abc').getinfo('def').example, 'This is an example') + + def test_multikey_example(self): + schema = self.load_schema_text("""\ + + + + This is an example + + + + """) + self.assertEqual(schema.gettype('abc').getinfo('def').example, 'This is an example') + + def test_sectiontype_example(self): + schema = self.load_schema_text("""\ + + + This is an example + +
+ + """) + self.assertEqual(schema.gettype('abc').example, 'This is an example') + + def test_multiple_examples_is_error(self): + self.assertRaises(ZConfig.SchemaError, self.load_schema_text, """\ + + This is an example + This is an example + + """) + + def test_section_example_is_error(self): + self.assertRaises(ZConfig.SchemaError, self.load_schema_text, """\ + + +
+ This is an example +
+
+ """) + + def test_multisection_example_is_error(self): + self.assertRaises(ZConfig.SchemaError, self.load_schema_text, """\ + + + + This is an example + + + """) + def test_suite(): return unittest.makeSuite(SchemaTestCase) diff --git a/doc/schema.dtd b/doc/schema.dtd index 52307a8..db1b071 100644 --- a/doc/schema.dtd +++ b/doc/schema.dtd @@ -46,7 +46,7 @@ -