from django.core.urlresolvers import reverse
from django.core.urlresolvers import NoReverseMatch
from django.test import TestCase

from readthedocs.builds.constants import LATEST
import readthedocs.core.views


class SubdomainUrlTests(TestCase):

    def test_sub_index(self):
        url = reverse(readthedocs.core.views.redirect_project_slug,
                      urlconf='readthedocs.core.subdomain_urls')
        self.assertEqual(url, '/')

    def test_sub_lang_version(self):
        url = reverse('docs_detail', urlconf='readthedocs.core.subdomain_urls',
                      kwargs={'lang_slug': 'en', 'version_slug': LATEST})
        self.assertEqual(url, '/en/latest/')

    def test_sub_lang_version_filename(self):
        url = reverse('docs_detail', urlconf='readthedocs.core.subdomain_urls',
                      args=['en', 'latest', 'index.html'])
        self.assertEqual(url, '/en/latest/index.html')

    def test_sub_project_full_path(self):
        url = reverse('subproject_docs_detail',
                      urlconf='readthedocs.core.subdomain_urls',
                      kwargs={'project_slug': 'pyramid', 'lang_slug': 'en',
                              'version_slug': LATEST, 'filename': 'index.html'})
        self.assertEqual(url, '/projects/pyramid/en/latest/index.html')

    def test_sub_project_slug_only(self):
        url = reverse('subproject_docs_detail',
                      urlconf='readthedocs.core.subdomain_urls',
                      kwargs={'project_slug': 'pyramid'})
        self.assertEqual(url, '/projects/pyramid')

    def test_sub_page(self):
        url = reverse('docs_detail',
                      urlconf='readthedocs.core.subdomain_urls',
                      kwargs={'filename': 'install.html'})
        self.assertEqual(url, '/page/install.html')

    def test_sub_version(self):
        url = reverse('version_subdomain_handler',
                      urlconf='readthedocs.core.subdomain_urls',
                      kwargs={'version_slug': '1.4.1'})
        self.assertEqual(url, '/1.4.1/')

    def test_sub_lang(self):
        url = reverse('lang_subdomain_handler',
                      urlconf='readthedocs.core.subdomain_urls',
                      kwargs={'lang_slug': 'en'})
        self.assertEqual(url, '/en/')


class WipeUrlTests(TestCase):

    def test_wipe_no_params(self):
        try:
            reverse('wipe_version')
            self.fail('reverse with no parameters should fail')
        except NoReverseMatch:
            pass

    def test_wipe_alphabetic(self):
        project_slug = 'alphabetic'
        version = 'version'
        url = reverse('wipe_version', args=[project_slug, version])
        self.assertEqual(url, '/wipe/alphabetic/version/')

    def test_wipe_alphanumeric(self):
        project_slug = 'alpha123'
        version = '123alpha'
        url = reverse('wipe_version', args=[project_slug, version])
        self.assertEqual(url, '/wipe/alpha123/123alpha/')

    def test_wipe_underscore_hyphen(self):
        project_slug = 'alpha_123'
        version = '123-alpha'
        url = reverse('wipe_version', args=[project_slug, version])
        self.assertEqual(url, '/wipe/alpha_123/123-alpha/')

    def test_wipe_version_dot(self):
        project_slug = 'alpha-123'
        version = '1.2.3'
        url = reverse('wipe_version', args=[project_slug, version])
        self.assertEqual(url, '/wipe/alpha-123/1.2.3/')

    def test_wipe_version_start_dot(self):
        project_slug = 'alpha-123'
        version = '.2.3'
        try:
            reverse('wipe_version', args=[project_slug, version])
        except NoReverseMatch:
            pass


class TestVersionURLs(TestCase):

    def test_version_url_with_caps(self):
        url = reverse(
            'project_download_media',
            kwargs={'type_': 'pdf', 'version_slug': u'1.4.X', 'project_slug': u'django'}
        )
        self.assertTrue(url)

@@ -315,16 +315,22 @@
 e.views.
+serve.
 redirect
@@ -384,32 +384,37 @@
 eadthedocs.core.
+urls.
 subdomain_urls')
@@ -406,21 +406,16 @@
 ubdomain
-_urls
 ')%0A     
@@ -537,32 +537,37 @@
 eadthedocs.core.
+urls.
 subdomain_urls',
@@ -551,37 +551,32 @@
 e.urls.subdomain
-_urls
 ',%0A             
@@ -783,32 +783,37 @@
 eadthedocs.core.
+urls.
 subdomain_urls',
@@ -797,37 +797,32 @@
 e.urls.subdomain
-_urls
 ',%0A             
@@ -1053,32 +1053,37 @@
 eadthedocs.core.
+urls.
 subdomain_urls',
@@ -1067,37 +1067,32 @@
 e.urls.subdomain
-_urls
 ',%0A             
@@ -1438,32 +1438,37 @@
 eadthedocs.core.
+urls.
 subdomain_urls',
@@ -1452,37 +1452,32 @@
 e.urls.subdomain
-_urls
 ',%0A             
@@ -1679,32 +1679,37 @@
 eadthedocs.core.
+urls.
 subdomain_urls',
@@ -1693,37 +1693,32 @@
 e.urls.subdomain
-_urls
 ',%0A             
@@ -1939,32 +1939,37 @@
 eadthedocs.core.
+urls.
 subdomain_urls',
@@ -1953,37 +1953,32 @@
 e.urls.subdomain
-_urls
 ',%0A             
@@ -2187,16 +2187,21 @@
 cs.core.
+urls.
 subdomai
@@ -2205,13 +2205,8 @@
 main
-_urls
 ',%0A 

Fix url tests