import os

from django.test import TestCase
from django.test.utils import override_settings
from django.template import Template, Context

from achilles import blocks


@override_settings(
    TEMPLATE_DIRS=(os.path.join(os.path.dirname(__file__), 'templates/'),),
    TEMPLATE_LOADERS=('django.template.loaders.filesystem.Loader',),
)
class BlocksTests(TestCase):

    @classmethod
    def setUpClass(cls):
        cls.register = blocks.Library()

    def test_register_block1(self):
        @self.register.block()
        class MyBlock(blocks.Block):
            template_name = 'template'

        self.assertIsInstance(self.register.get('MyBlock'), MyBlock)

    def test_register_block2(self):
        @self.register.block
        class MyBlock(blocks.Block):
            template_name = 'template'

        self.assertIsInstance(self.register.get('MyBlock'), MyBlock)

    def test_register_block3(self):
        @self.register.block('new_name')
        class MyBlock(blocks.Block):
            template_name = 'template'

        self.assertIsInstance(self.register.get('new_name'), MyBlock)

    def test_register_simple_block(self):
        @self.register.simple_block('template')
        def foo(context):
            return {}

        self.assertIsInstance(self.register.get('foo'), blocks.Block)

    def test_block_get(self):
        @self.register.simple_block('block_template.html')
        def message(request):
            return {'message': 'foo'}

        self.assertIsInstance(blocks.get('message'), blocks.Block)

    def test_block_namespaces(self):
        register = blocks.Library('foo')
        @register.simple_block('block_template.html')
        def message(request):
            return {'message': 'foo'}

        self.assertIsInstance(blocks.get('foo:message'), blocks.Block)

    def test_render_block(self):
        @self.register.simple_block('block_template.html')
        def message(request):
            return {'message': 'foo'}

        out = Template(
            "{% load ablock %}"
            "{% ablock 'message' %}").render(Context())

        self.assertEqual(out, '<div data-ablock="name">foo\n</div>')

@@ -1366,38 +1366,32 @@
 mple_block('
-block_
 template
 .html')%0A    
@@ -1370,37 +1370,32 @@
 _block('template
-.html
 ')%0A        def m
@@ -1596,16 +1596,17 @@
 ('foo')%0A
+%0A
         
@@ -1625,38 +1625,32 @@
 mple_block('
-block_
 template
 .html')%0A    
@@ -1629,37 +1629,32 @@
 _block('template
-.html
 ')%0A        def m
@@ -1701,32 +1701,32 @@
 ssage': 'foo'%7D%0A%0A
-
         self.ass
@@ -1773,32 +1773,404 @@
  blocks.Block)%0A%0A
+    def test_block_repeated_namespaces(self):%0A        register1 = blocks.Library('repeated')%0A        self.assertRaises(ValueError, blocks.Library, 'repeated')%0A%0A    def test_unknown_namepsace(self):%0A        self.assertRaises(KeyError, blocks.get, 'non-existent:namespace')%0A%0A    def test_unknown_block(self):%0A        self.assertRaises(KeyError, blocks.get, 'non-existent')%0A%0A
     def test_ren

Test block namespaces