# -*- coding: utf-8 -*-
import pytest

from apispec import APISpec
from tornado.web import RequestHandler

@pytest.fixture()
def spec():
    return APISpec(
        title='Swagger Petstore',
        version='1.0.0',
        description='This is a sample Petstore server.  You can find out more '
        'about Swagger at <a href=\"http://swagger.wordnik.com\">'
        'http://swagger.wordnik.com</a> or on irc.freenode.net, #swagger.'
        'For this sample, you can use the api key \"special-key\" to test the'
        'authorization filters',
        plugins=[
            'apispec.ext.tornado'
        ]
    )


class TestPathHelpers:

    def test_path_from_urlspec(self, spec):
        class HelloHandler(RequestHandler):
            def get(self):
                self.write("hello")

        urlspec = (r'/hello', HelloHandler)
        operations = (
            {'get': {'parameters': [], 'responses': {'200': '..params..'}}})

        spec.add_path(urlspec=urlspec, operations=operations)
        assert '/hello' in spec._paths
        assert 'get' in spec._paths['/hello']
        expected = {'parameters': [], 'responses': {'200': '..params..'}}
        assert spec._paths['/hello']['get'] == expected

    def test_path_with_multiple_methods(self, spec):

        class HelloHandler(RequestHandler):
            def get(self):
                self.write("hello")

            def post(self):
                self.write("hello")

        urlspec = (r'/hello', HelloHandler)
        operations = {
            'get': {
                'description': 'get a greeting',
                'responses': {'200': '..params..'}
            },
            'post': {
                'description': 'post a greeting',
                'responses': {'200': '..params..'}
            },
        }
        spec.add_path(urlspec=urlspec, operations=operations)
        get_op = spec._paths['/hello']['get']
        post_op = spec._paths['/hello']['post']
        assert get_op['description'] == 'get a greeting'
        assert post_op['description'] == 'post a greeting'

    def test_integration_with_docstring_introspection(self, spec):

        class HelloHandler(RequestHandler):
            """
            ---
            x-extension: value
            """
            def get(self):
                """Get a greeting endpoint.
                ---
                description: get a greeting
                responses:
                    200:
                        description: a pet to be returned
                        schema:
                            $ref: #/definitions/Pet
                """
                self.write("hello")

            def post(self):
                """Post a greeting endpoint.
                ---
                description: post a greeting
                responses:
                    200:
                        description:some data
                """
                self.write("hello")

        urlspec = (r'/hello', HelloHandler)
        spec.add_path(urlspec=urlspec)
        get_op = spec._paths['/hello']['get']
        post_op = spec._paths['/hello']['post']
        extension = spec._paths['/hello']['x-extension']
        assert get_op['description'] == 'get a greeting'
        assert post_op['description'] == 'post a greeting'
        assert extension == 'value'

    def test_path_removing_trailing_or_optional_slash(self, spec):

        class HelloHandler(RequestHandler):
            def get(self):
                self.write("hello world")

        urlspec = (r'/hello/world/*', HelloHandler)
        operations = (
            {'get': {'parameters': [], 'responses': {'200': '..params..'}}})

        spec.add_path(urlspec=urlspec, operations=operations)
        assert '/hello/world' in spec._paths

    def test_path_with_params(self, spec):

        class HelloHandler(RequestHandler):
            def get(self, param1, param2):
                self.write("hello")

        urlspec = (r'/hello/([^/]+)/world/([^/]+)', HelloHandler)
        operations = (
            {'get': {'parameters': [], 'responses': {'200': '..params..'}}})

        spec.add_path(urlspec=urlspec, operations=operations)
        path = '/hello/{param1}/world/{param2}'
        assert path in spec._paths
        assert 'get' in spec._paths[path]
        expected = {'parameters': [], 'responses': {'200': '..params..'}}
        assert spec._paths[path]['get'] == expected

@@ -98,16 +98,35 @@
 tHandler
+%0Aimport tornado.gen
 %0A%0A@pytes
@@ -3790,52 +3790,121 @@
     
-def test_path_with_params(self, spec
+class HelloWorldHandler(RequestHandler):%0A        def get(self, param1, param2
 ):%0A
-%0A
     
+        self.write(%22hello%22)%0A%0A
     
@@ -3906,39 +3906,45 @@
 %0A    class Hello
+World
 Handler
+2
 (RequestHandler)
@@ -3941,32 +3941,59 @@
 stHandler):%0A    
+    @tornado.gen.coroutine%0A
         def get(
@@ -4019,36 +4019,32 @@
 2):%0A            
-    
 self.write(%22hell
@@ -4040,32 +4040,192 @@
 write(%22hello%22)%0A%0A
+    @pytest.mark.parametrize('Handler',%0A    %5B%0A        HelloWorldHandler,%0A        HelloWorldHandler2,%0A    %5D)%0A    def test_path_with_params(self, spec, Handler):%0A
         urlspec 
@@ -4260,21 +4260,16 @@
 %5E/%5D+)', 
-Hello
 Handler)

Add regression test for handling coroutine methods