<commit_before># Copyright Hybrid Logic Ltd.  See LICENSE file for details.
"""
Tests for ``flocker.restapi.docs.publicapi``.
"""

from yaml import safe_dump

from twisted.python.filepath import FilePath
from twisted.trial.unittest import SynchronousTestCase
from twisted.python.reflect import namedModule

try:
    from sphinx.errors import SphinxError
    SPHINX_ERROR_AVAILABLE = True
except:
    SPHINX_ERROR_AVAILABLE = False

from klein import Klein

try:
    namedModule("sphinxcontrib")
    namedModule("sphinx")
    namedModule("docutils")
except ImportError:
    skip = "Sphinx not installed."
else:
    from ..publicapi import (
        Example, KleinRoute, getRoutes, _loadExamples, _formatExample, makeRst)

from ..._infrastructure import user_documentation, structured, private_api


class GetRoutesTests(SynchronousTestCase):
    """
    Tests for L{getRoutes}.
    """

    def test_routes(self):
        """
        L{getRoutes} returns all the defined routes and their attributes.
        """
        app = Klein()

        def f():
            pass
        f.attr = "attr"

        def g():
            pass
        g.attr = "G"
        app.route(b"/", methods=[b"GET"])(f)
        app.route(b"/g", methods=[b"PUT", b"OPTIONS"])(g)

        routes = sorted(getRoutes(app))
        self.assertEqual(routes, [
            KleinRoute(methods={b"GET"}, path=b"/", endpoint="f",
                       attributes={'attr': 'attr'}),
            KleinRoute(methods={b'PUT', b'OPTIONS'}, path=b'/g', endpoint='g',
                       attributes={'attr': 'G'})])


class MakeRstTests(SynchronousTestCase):
    """
    Tests for L{makeRst}.
    """
    @skipUnless(SPHINX_ERROR_AVAILABLE, "SphinxError must be available")
    def test_must_be_documented(self):
        """
        If any route doesn't have documentation, then ``SphinxError`` is
        raised.
        """
        app = Klein()

        @app.route(b"/", methods=[b"GET"])<commit_msg>Address review comment: Skip using existing skip logic<commit_after># Copyright Hybrid Logic Ltd.  See LICENSE file for details.
"""
Tests for ``flocker.restapi.docs.publicapi``.
"""

from yaml import safe_dump

from twisted.python.filepath import FilePath
from twisted.trial.unittest import SynchronousTestCase
from twisted.python.reflect import namedModule

from klein import Klein

try:
    namedModule("sphinxcontrib")
    namedModule("sphinx")
    namedModule("docutils")
except ImportError:
    skip = "Sphinx not installed."
else:
    from sphinx.errors import SphinxError

    from ..publicapi import (
        Example, KleinRoute, getRoutes, _loadExamples, _formatExample, makeRst)

from ..._infrastructure import user_documentation, structured, private_api


class GetRoutesTests(SynchronousTestCase):
    """
    Tests for L{getRoutes}.
    """

    def test_routes(self):
        """
        L{getRoutes} returns all the defined routes and their attributes.
        """
        app = Klein()

        def f():
            pass
        f.attr = "attr"

        def g():
            pass
        g.attr = "G"
        app.route(b"/", methods=[b"GET"])(f)
        app.route(b"/g", methods=[b"PUT", b"OPTIONS"])(g)

        routes = sorted(getRoutes(app))
        self.assertEqual(routes, [
            KleinRoute(methods={b"GET"}, path=b"/", endpoint="f",
                       attributes={'attr': 'attr'}),
            KleinRoute(methods={b'PUT', b'OPTIONS'}, path=b'/g', endpoint='g',
                       attributes={'attr': 'G'})])


class MakeRstTests(SynchronousTestCase):
    """
    Tests for L{makeRst}.
    """
    def test_must_be_documented(self):
        """
        If any route doesn't have documentation, then ``SphinxError`` is
        raised.
        """
        app = Klein()

        @app.route(b"/", methods=[b"GET"])
{'fraction_non_alphanumeric': 0.12932843651626444, 'fraction_numerical': 0.0, 'mean_word_length': 2.691190706679574, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3859573', 'n_tokens_mistral': 1166, 'n_tokens_neox': 1131, 'n_words': 279}