# -*- coding: utf-8 -*-
from __future__ import with_statement
try:
    from unittest2 import TestCase, skip
except ImportError, e:
    from unittest import TestCase, skip

from pelican.contents import Page
from pelican.settings import _DEFAULT_CONFIG

class TestPage(TestCase):

    def setUp(self):
        super(TestPage, self).setUp()
        self.page_kwargs = {
            'content': 'content',
            'metadata':{
                'title': 'foo bar',
                'author': 'Blogger',
            },
        }

    def test_use_args(self):
        """Creating a page with arguments passed to the constructor should use
        them to initialise object's attributes.

        """
        metadata = {'foo': 'bar', 'foobar': 'baz', 'title': 'foobar', }
        page = Page('content', metadata=metadata)
        for key, value in metadata.items():
            self.assertTrue(hasattr(page, key))
            self.assertEqual(value, getattr(page, key))
        self.assertEqual(page.content, 'content')

    def test_mandatory_properties(self):
        """If the title is not set, must throw an exception."""
        self.assertRaises(AttributeError, Page, 'content')
        page = Page(**self.page_kwargs)
        page.check_properties()

    def test_slug(self):
        """If a title is given, it should be used to generate the slug."""
        page = Page(**self.page_kwargs)
        self.assertEqual(page.slug, 'foo-bar')

    def test_defaultlang(self):
        """If no lang is given, default to the default one."""
        page = Page(**self.page_kwargs)
        self.assertEqual(page.lang, _DEFAULT_CONFIG['DEFAULT_LANG'])

        # it is possible to specify the lang in the metadata infos
        self.page_kwargs['metadata'].update({'lang': 'fr', })
        page = Page(**self.page_kwargs)
        self.assertEqual(page.lang, 'fr')

    def test_save_as(self):
        """If a lang is not the default lang, save_as should be set
        accordingly.

        """
        # if a title is defined, save_as should be set
        page = Page(**self.page_kwargs)
        self.assertEqual(page.save_as, "pages/foo-bar.html")

        # if a language is defined, save_as should include it accordingly
        self.page_kwargs['metadata'].update({'lang': 'fr', })
        page = Page(**self.page_kwargs)
        self.assertEqual(page.save_as, "pages/foo-bar-fr.html")

    def test_datetime(self):
        """If DATETIME is set to a tuple, it should be used to override LOCALE
        """
        from datetime import datetime
        from sys import platform
        dt = datetime(2015,9,13)
        # make a deep copy of page_kawgs
        page_kwargs = {key:self.page_kwargs[key] for key in self.page_kwargs}
        for key in page_kwargs:
            if not isinstance(page_kwargs[key], dict): break
            page_kwargs[key] = {subkey:page_kwargs[key][subkey] for subkey in page_kwargs[key]}
        # set its date to dt
        page_kwargs['metadata']['date'] = dt
        page = Page( **page_kwargs)

        self.assertEqual(page.locale_date,
            unicode(dt.strftime(_DEFAULT_CONFIG['DEFAULT_DATE_FORMAT']), 'utf-8'))


        page_kwargs['settings'] = {x:_DEFAULT_CONFIG[x] for x in _DEFAULT_CONFIG}
        # I doubt this can work on all platforms ...
        if platform == "win32":
            locale = 'jpn'
        else:
            locale = 'ja_JP.utf8'
        page_kwargs['settings']['DATE_FORMATS'] = {'jp':(locale,'%Y-%m-%d(%a)')}
        page_kwargs['metadata']['lang'] = 'jp'

        import locale as locale_module
        try:
            page = Page( **page_kwargs)
            self.assertEqual(page.locale_date, u'2015-09-13(\u65e5)')
            # above is unicode in Japanese: 2015-09-13()
        except locale_module.Error:
            # The constructor of ``Page`` will try to set the locale to
            # ``ja_JP.utf8``. But this attempt will failed when there is no
            # such locale in the system. You can see which locales there are
            # in your system with ``locale -a`` command.
            #
            # Until we find some other method to test this functionality, we
            # will simply skip this test.
            skip("There is no locale %s in this system." % locale)

@@ -163,16 +163,24 @@
 se, skip
+  # NOQA
 %0A%0Afrom p
@@ -253,16 +253,17 @@
 CONFIG%0A%0A
+%0A
 class Te
@@ -426,16 +426,17 @@
 tadata':
+ 
 %7B%0A      
@@ -2610,10 +2610,12 @@
 015,
+ 
 9,
+ 
 13)%0A
@@ -2681,13 +2681,20 @@
 s = 
-%7Bkey:
+dict(%5B(key, 
 self
@@ -2706,24 +2706,25 @@
 _kwargs%5Bkey%5D
+)
  for key in 
@@ -2722,16 +2722,44 @@
 r key in
+%0A                           
  self.pa
@@ -2767,17 +2767,18 @@
 e_kwargs
-%7D
+%5D)
 %0A       
@@ -2856,16 +2856,32 @@
 , dict):
+%0A               
  break%0A 
@@ -2914,16 +2914,23 @@
 %5D = 
-%7B
+dict(%5B(
 subkey
-:
+, 
 page
@@ -2949,16 +2949,54 @@
 %5Bsubkey%5D
+)%0A                                    
  for sub
@@ -3018,17 +3018,18 @@
 rgs%5Bkey%5D
-%7D
+%5D)
 %0A       
@@ -3107,33 +3107,32 @@
     page = Page(
- 
 **page_kwargs)%0A%0A
@@ -3246,16 +3246,48 @@
 RMAT'%5D),
+%0A                               
  'utf-8'
@@ -3290,17 +3290,16 @@
 f-8'))%0A%0A
-%0A
         
@@ -3328,11 +3328,18 @@
 %5D = 
-%7Bx:
+dict(%5B(x, 
 _DEF
@@ -3352,16 +3352,17 @@
 ONFIG%5Bx%5D
+)
  for x i
@@ -3362,16 +3362,56 @@
 for x in
+%0A                                       
  _DEFAUL
@@ -3418,17 +3418,19 @@
 T_CONFIG
-%7D
+%5D)%0A
 %0A       
@@ -3642,16 +3642,17 @@
 jp':
+ 
 (locale,
 '%25Y-
@@ -3647,16 +3647,75 @@
 (locale,
+%0A                                                          
 '%25Y-%25m-%25
@@ -3847,17 +3847,16 @@
  = Page(
- 
 **page_k

fix python 2.6 support