from unittest import TestCase
from kobin.routes import Route, Router
from kobin.environs import HTTPError, Response


class RouteTests(TestCase):
    def test_call(self):
        def dummy_func(num: int) -> Response:
            return Response(f'hello {num}')
        route = Route('/hoge/{num}', 'GET', 'hoge', dummy_func)
        actual_response = route.callback(num=1)
        self.assertEqual(actual_response.body, [b'hello 1'])

    def test_match_method(self):
        def dummy_func(num: int) -> Response:
            return Response(f'hello {num}')
        route = Route('/hoge/{num}', 'GET', 'hoge', dummy_func)
        self.assertTrue(route._match_method('GET'))

    def test_match_path(self):
        def dummy_func(num: int) -> Response:
            return Response(f'hello {num}')
        route = Route('/hoge/{num}', 'GET', 'hoge', dummy_func)
        self.assertTrue(route._match_path('/hoge/1'))

    def test_match_path_with_slash(self):
        def dummy_func(num: int) -> Response:
            return Response(f'hello {num}')
        route = Route('/hoge/{num}', 'GET', 'hoge', dummy_func)
        self.assertTrue(route._match_path('hoge/1/'))

    def test_match(self):
        def dummy_func(num: int) -> Response:
            return Response(f'hello {num}')
        route = Route('/hoge/{num}', 'GET', 'hoge', dummy_func)
        self.assertTrue(route.match('GET', 'hoge/1/'))


class RouterTests(TestCase):
    def setUp(self):
        self.router = Router()

    def test_match_dynamic_routes_with_casted_number(self):
        def dummy_func(year: int) -> Response:
            return Response(f'hello {year}')

        self.router.add('GET', '/tests/{year}', 'hoge', dummy_func)
        test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/tests/2015/'}
        actual_target, actual_args = self.router.match(test_env)
        self.assertEqual(actual_args, {'year': 2015})

    def test_match_dynamic_routes_with_string(self):
        def dummy_func(name):
            return Response(f'hello {name}')

        self.router.add('GET', '/tests/{name}', 'hoge', dummy_func)
        test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/tests/kobin/'}
        actual_target, actual_args = self.router.match(test_env)
        self.assertEqual(actual_args, {'name': 'kobin'})

    def test_404_not_found(self):
        def dummy_func(name):
            return Response(f'hello {name}')

        self.router.add('GET', '/tests/{name}', 'hoge', dummy_func)
        test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/this_is_not_found'}
        self.assertRaises(HTTPError, self.router.match, test_env)

@@ -2604,16 +2604,925 @@
 atch, test_env)%0A
+%0A%0Aclass ReverseRoutingTests(TestCase):%0A    def setUp(self):%0A        self.router = Router()%0A%0A        def index() -%3E Response:%0A            return Response('hello world')%0A%0A        def user_detail(user_id: int) -%3E Response:%0A            return Response(f'hello user%7Buser_id%7D')%0A%0A        self.router.add('GET', '/', 'top', index)%0A        self.router.add('GET', '/users/%7Buser_id%7D', 'user-detail', user_detail)%0A%0A    def test_reverse_route_without_url_vars(self):%0A        actual = self.router.reverse('top')%0A        expected = '/'%0A        self.assertEqual(actual, expected)%0A%0A    def test_reverse_route_with_url_vars(self):%0A        actual = self.router.reverse('user-detail', user_id=1)%0A        expected = '/users/1'%0A        self.assertEqual(actual, expected)%0A%0A    def test_reverse_not_match(self):%0A        actual = self.router.reverse('foobar', foo=1)%0A        expected = None%0A        self.assertEqual(actual, expected)%0A

Add reverse routing tests